Arithmetic Operators and Expressions
( - ) : Subtraction
( / ) : Division
( * ) : Multiplication
( % ) : Modulus
(-x) : Changes the sign of the variable.
int number = 34;
printf("%d", -number);
Modulus is used to find remainders. For example, if you want to find the remainder of 15 divided by 6, you write:
15%6 = 3.
The order of operations is as follows:
- Parentheses
( )
are evaluated first. - Then, multiplication
*
, division/
, and modulus%
are evaluated from left to right. - Finally, addition
+
and subtraction-
are performed, also from left to right.
There are some additional details about division. For example, in standard arithmetic, 4/5
equals 0.8
. However, in C, 4/5
results in 0
because the program expects an integer result when dividing two integers.
Here are some examples:
8/4+2 => 2 + 2 => 4
8-4*2+-12 => 8 - 8 + -12 => -12
15*4/2%4*7 => 60/2%4*7 => 30%4*7 => 2*7 => 14
31+7/2-83%5*2-2 => 31+ 3 - 3 * 2 - 2 => 31 + 3 - 6 - 2 => 26
(31-7) * 2 + 83 / (5%2) => 24 * 2 + 83 / 1 => 48 + 83 => 131
Let's write a program that performs multiple operations on two user-input numbers:
#include<stdio.h>
int main( void ){
int num1, num2, product, quotient, sum, remainder, difference;
printf("Please enter two numbers > ");
scanf("%d%d", &num1, &num2);
product = num1 * num2;
quotient = num1 / num2;
sum = num1 + num2;
difference = num1 - num2;
remainder = num1 % num2;
printf("Sum = %d\nDifference = %d\nProduct = %d"
"\nQuotient = %d\nRemainder = %d", sum, difference, product, quotient, remainder);
}
Adding Comments
In the C programming language, there are two ways to add comments. One is for single-line comments, and the other is for multi-line comments. The compiler will ignore both types.
/*
This is a multi-line comment.
Everything between the asterisks
will be ignored by the compiler.
*/
// This is a single-line comment.
Increment and Decrement Operations
If you want to increase the value of a variable i
by 1, you usually write: i = i + 1
. Similarly, to decrease it by 1, you write: i = i - 1
. However, C provides a shorthand notation for these operations:
i++
(post-increment)++i
(pre-increment)i--
(post-decrement)--i
(pre-decrement)
Let's look at some examples:
/*
In this program, increment and decrement
operations will be executed first.
*/
#include<stdio.h>
int main( void )
{
int i = 10, j = 60;
printf("i = %d and j = %d\n", ++i, --j);
return 0;
}
Output:
i = 11 and j = 59
Since ++i
and --j
are pre-increment and pre-decrement, they are executed before printing.
Now, let's modify the program:
/*
In this program, increment and decrement
operations will be executed after printing.
*/
#include<stdio.h>
int main( void )
{
int i = 10, j = 60;
printf("i = %d and j = %d\n", i++, j--);
return 0;
}
Output:
i = 10 and j = 60
Even though the values change after execution, we don't see the updated values in the output because they are modified after printf
.
Here’s a summary:
i++
returnsi
first, then increments it.++i
incrementsi
first, then returns it.
Let's examine the following examples:
#include <stdio.h>
int main() {
int i = 5;
printf("%d %d", ++i, ++i);
}
Since there is no operator grouping here, the output is 7 7
.
In the next example, i++
is used, so the result is 5 + 6 = 11
:
#include <stdio.h>
int main() {
int i = 5;
printf("%d", i++ + i);
}
Using ++i
, the result is 6 + 6 = 12
:
#include <stdio.h>
int main() {
int i = 5;
printf("%d\n", ++i + i);
}
In this case, i++
is used, so the output is 5 + 6 = 11
, but i
becomes 7
afterward:
#include <stdio.h>
int main() {
int i = 5;
printf("%d\n", i++ + i++);
}
Advanced Assignment Methods
C provides shorthand assignment operators to simplify code. Instead of writing:
variable_1 = variable_1 (operator) variable_2
,
you can use:
variable_1 (operator) = variable_2
.
Common advanced assignment operators:
+= , -= , *= , /= , %=
Examples:
1-) j = j * ( 3 + x ) ==> j *= ( 3 + x )
2-) a = a / ( 5 - z ) ==> a /= ( 5 - z )
3-) x = x - 5 ==> x -= 5
Cast Operator (Type Conversions)
To convert a variable type (e.g., integer to float), we use the cast operator.
#include<stdio.h>
int main( void )
{
int dividend = 12, divisor = 8;
float quotient;
quotient = dividend / divisor;
printf("Result: %f\n", quotient);
return 0;
}
Output:
Result: 1.000000
The expected result is 1.5
, but we get 1.000000
because C performs integer division.
To fix this, we use casting:
#include<stdio.h>
int main( void )
{
int dividend = 12, divisor = 8;
float quotient;
quotient = (float)dividend / divisor;
printf("Result: %f\n", quotient);
return 0;
}
We can also force float division with 2.0/4
instead of (float)2/4
.
Another way to define a float is using 13.0
, 13.f
, or 13.
.
printf("Result: %.1f\n", (float) 2 / 4);
When using mixed data types, the larger type determines the result:
char <-> int <-> long <-> float <-> double
For example, 3.2/2
results in 1.6
because one operand is a float.
Finally, let's write a program that rounds a user-input floating-point number:
#include<stdio.h>
int main( void )
{
float input_number;
printf("Please enter a number> ");
scanf("%f", &input_number);
printf("Rounded number: %d\n", (int)(input_number + 0.5));
return 0;
}
Yorumlar
Yorum Gönder