id | title | sidebar_label | sidebar_position | tags | description | ||
---|---|---|---|---|---|---|---|
c-operators |
C operators |
C Operators |
5 |
|
In this tutorial, you will learn about the C Operators, what it is. |
Operators in C are special symbols used to perform operations on variables and values. They form the basis of computation and are categorized based on their functionality.
-
Arithmetic Operators:
- Used to perform mathematical operations.
int a = 10, b = 5; printf("Addition: %d\n", a + b); // Output: 15 printf("Subtraction: %d\n", a - b); // Output: 5 printf("Multiplication: %d\n", a * b); // Output: 50 printf("Division: %d\n", a / b); // Output: 2 printf("Modulus: %d\n", a % b); // Output: 0
-
Relational Operators:
- Used to compare two values.
int a = 10, b = 5; printf("Equal to: %d\n", a == b); // Output: 0 (false) printf("Not equal to: %d\n", a != b); // Output: 1 (true) printf("Greater than: %d\n", a > b); // Output: 1 (true) printf("Less than: %d\n", a < b); // Output: 0 (false) printf("Greater or equal: %d\n", a >= b); // Output: 1 (true) printf("Less or equal: %d\n", a <= b); // Output: 0 (false)
-
Logical Operators:
- Used to perform logical operations.
int a = 1, b = 0; printf("Logical AND: %d\n", a && b); // Output: 0 (false) printf("Logical OR: %d\n", a || b); // Output: 1 (true) printf("Logical NOT: %d\n", !a); // Output: 0 (false)
-
Bitwise Operators:
- Used to perform bit-level operations.
int a = 5, b = 3; // In binary: a = 0101, b = 0011 printf("Bitwise AND: %d\n", a & b); // Output: 1 (0001) printf("Bitwise OR: %d\n", a | b); // Output: 7 (0111) printf("Bitwise XOR: %d\n", a ^ b); // Output: 6 (0110) printf("Bitwise NOT: %d\n", ~a); // Output: -6 (two's complement) printf("Left shift: %d\n", a << 1); // Output: 10 (1010) printf("Right shift: %d\n", a >> 1); // Output: 2 (0010)
-
Assignment Operators:
- Used to assign values to variables.
int a = 10; a += 5; // Equivalent to a = a + 5 printf("a after += 5: %d\n", a); // Output: 15 a -= 3; // Equivalent to a = a - 3 printf("a after -= 3: %d\n", a); // Output: 12 a *= 2; // Equivalent to a = a * 2 printf("a after *= 2: %d\n", a); // Output: 24 a /= 4; // Equivalent to a = a / 4 printf("a after /= 4: %d\n", a); // Output: 6 a %= 3; // Equivalent to a = a % 3 printf("a after %= 3: %d\n", a); // Output: 0
-
Increment and Decrement Operators:
- Used to increase or decrease the value of a variable by one.
int a = 10; printf("a before increment: %d\n", a); // Output: 10 printf("Post-increment: %d\n", a++); // Output: 10 printf("a after post-increment: %d\n", a); // Output: 11 printf("Pre-increment: %d\n", ++a); // Output: 12 printf("Post-decrement: %d\n", a--); // Output: 12 printf("a after post-decrement: %d\n", a); // Output: 11 printf("Pre-decrement: %d\n", --a); // Output: 10
-
Conditional (Ternary) Operator:
- Used as a shorthand for
if-else
statements.
int a = 10, b = 5; int max = (a > b) ? a : b; printf("Maximum value: %d\n", max); // Output: 10
- Used as a shorthand for
-
Comma Operator:
- Used to separate expressions; the result of the entire expression is the value of the last expression.
int a, b, c; a = (b = 3, c = b + 2); // b is assigned 3, then c is assigned 5, and finally a is assigned 5 printf("a: %d, b: %d, c: %d\n", a, b, c); // Output: a: 5, b: 3, c: 5
A variable's data type can be changed from one data type to another by type conversion.Type conversion is mainly of two types:
- Implicit Type Conversion
- Explicit Type Conversion
-
Implicit Type Conversion:
- The type conversion that is done automatically by the compiler is known as implicit type conversion. This usually occurs when variables of different data types are used together in an expression in order to avoid loss of data. All the variables are upgraded to the data type of variable with largest data type.
- Example:
#include <stdio.h> int main() { int a = 5; float b = 4.5; float result; result = a + b; // Implicit conversion of 'a' to float before the addition printf("Result: %f\n", result); return 0; }
-
Explicit Type Conversion:
- Explicit type conversion is when the user explicitly specifies the type to which a variable should be converted. This is done using the cast operator
(type)
. - Example:
#include <stdio.h> int main() { int a = 5; float b = 4.5; int result; result = a + (int)b; // Explicit conversion of 'b' to int before the addition printf("Result: %d\n", result); return 0; }
- Explicit type conversion is when the user explicitly specifies the type to which a variable should be converted. This is done using the cast operator
-
Versatility:
- Operators in C cover a wide range of functionalities from basic arithmetic to advanced bitwise operations, providing a powerful toolset for developers.
-
Efficiency:
- C operators are designed to be efficient and are directly mapped to machine instructions, making them fast and suitable for performance-critical applications.
-
Combination and Chaining:
- Operators can be combined and chained to perform complex expressions in a concise manner. For instance,
a += b + c
combines addition and assignment.
- Operators can be combined and chained to perform complex expressions in a concise manner. For instance,
-
Precedence and Associativity:
- Operators in C follow specific precedence and associativity rules that determine the order in which expressions are evaluated. Understanding these rules is crucial for writing correct and efficient code.
-
Type Safety:
- C allows type conversions between different data types when using operators, but it's important to understand and handle these conversions to avoid unexpected results.
-
Pointer and Address Manipulation:
- C provides operators specifically for pointer manipulation (e.g.,
*
,&
,->
), enabling direct interaction with memory addresses.
- C provides operators specifically for pointer manipulation (e.g.,
Operators are fundamental components of the C programming language, enabling developers to perform a wide range of operations efficiently. From basic arithmetic to advanced bitwise manipulations, C operators provide the tools necessary for building robust and performant applications. Understanding the different types of operators, their functionalities, and their correct usage is essential for writing effective C code. Mastery of operators also contributes to better optimization and control over system resources, making C a powerful language for both low-level and high-level programming tasks.