Logo
Unit 2 – C Basics: Operators

C Basics: Operators

Duration: 5 minutes

Hello, budding C programmers!

After building on our understanding of variables and data types, let’s dive into another essential topic: operators. In C, operators are symbols that perform specific operations on one or more operands (variables and values) and produce a result. We’ll explore different types of operators in C.

Understanding C Operators

C provides a variety of operators to manipulate data. Here are some key types of operators:

‘+’: Addition‘-’: Subtraction‘*’: Multiplication
‘/’: Division‘%’: Modulus (remainder)‘++’: Increment
‘–’: Decrement

Comparison Operators: Used to compare two values.

‘==’: Equal to‘===’: Strictly equal to (value and type)
‘!=’: Not equal to‘!==’: Strictly not equal to (value and type)
‘>’: Greater than‘<’: Less than
‘>=’: Greater than or equal to‘<=’: Less than or equal to

Assignment Operators: Used to assign values to variables.

‘=’: Assign‘+=’: Assign after adding‘-=’: Assign after subtracting
‘*=’: Assign after multiplying‘/=’: Assign after dividing

Logical Operators: Used to determine the logic between variables or values.

‘&&’: Logical AND’: Logical OR‘!’: Logical NOT
‘&&’: Logical AND’: Logical OR‘!’: Logical NOT

Creating Expressions with Operators

Let’s construct some C expressions using these operators:

#include
int main() {
// Arithmetic Operators
int sum = 10 + 5;
printf("Sum: %d\n", sum);
int difference = 10 - 5;
printf("Difference: %d\n", difference);
int product = 10 * 2;
printf("Product: %d\n", product);
// Comparison Operators
int isEqual = (10 == 10); // True
printf("Is Equal: %d\n", isEqual);
int isNotEqual = (10 != 5); // True
printf("Is Not Equal: %d\n", isNotEqual);
// Assignment Operators
int x = 10;
x += 5; // equivalent to x = x + 5
printf("x after +=: %d\n", x);
// Logical Operators
int result = (x > 5) && (x < 20); // True because both conditions are True
printf("Logical AND Result: %d\n", result);
return 0;
}

Exercise

Now it’s your turn! Create different expressions in C by combining the operators discussed. Experiment with blending various types of operators, such as using the modulus operator in an arithmetic expression or the != comparison operator to observe its operation. Practice will enhance your proficiency with these operators.

Conclusion

Great job with advancing into the fundamentals of C! Operators are crucial for performing operations in C, providing you control and flexibility in your code. As we progress, we’ll encounter more complex tasks, but armed with the knowledge you’ve gained here, you’re well-prepared to tackle them. Keep up the fantastic work!

Next Tutorial: C Control Structures: Conditional Statements

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!