Operators in c language | operators program in c

Operators in c language with example

   

Operators in c programming with example

Operators in C language are symbols or characters that perform various operations on one or more operands. Here are some of the commonly used operators in C language with examples:


1. Arithmetic Operators:

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

Example:

int a = 10, b = 5;

int c = a + b; // Addition

int d = a - b; // Subtraction

int e = a * b; // Multiplication

int f = a / b; // Division

int g = a % b; // Modulus



2. Relational Operators:

Relational operators are used to compare the values of two operands.

Example:

int a = 10, b = 5;

if (a > b) {

  printf("a is greater than b\n");

}

if (a == b) {

  printf("a and b are equal\n");

}

if (a != b) {

  printf("a and b are not equal\n");

}



3. Logical Operators:

Logical operators are used to perform logical operations such as AND, OR, and NOT.

Example:

int a = 10, b = 5;

if (a > 0 && b > 0) {

  printf("Both a and b are positive\n");

}

if (a > 0 || b > 0) {

  printf("At least one of a or b is positive\n");

}

if (!(a == b)) {

  printf("a and b are not equal\n");

}


4. Assignment Operators:

Assignment operators are used to assign values to variables.

Example:

int a = 10, b = 5;

a += b; // a = a + b

b -= 2; // b = b - 2

a *= 2; // a = a * 2

b /= 2; // b = b / 2


5. Increment and Decrement Operators:

Increment and decrement operators are used to increase or decrease the value of a variable by one.

Example:

int a = 10;

a++; // a = a + 1

++a; // a = a + 1 (pre-increment)

a--; // a = a - 1

--a; // a = a - 1 (pre-decrement)


6. Bitwise Operators:

Bitwise operators are used to perform operations on the binary representation of numbers.

Example:

unsigned int a = 60; // binary: 0011 1100

unsigned int b = 13; // binary: 0000 1101

unsigned int c = 0;


c = a & b; // bitwise AND (binary: 0000 1100)

c = a | b; // bitwise OR (binary: 0011 1101)

c = a ^ b; // bitwise XOR (binary: 0011 0001)

c = ~a; // bitwise complement (binary: 1100 0011)

c = a << 2; // left shift (binary: 1111 0000)

c = a >> 2; // right shift (binary: 0000 1111)


7. Conditional Operator:

The conditional operator is also known as the ternary operator. It is used to evaluate a condition and return a value based on the result of the condition.

Syntax: condition ? value1 : value2


Example:

int a = 10, b = 5;

int c = (a > b) ? a : b;

// If a is greater than b, c will be assigned the value of a, otherwise c will be assigned the value of b.


8. Special Operators:

Special operators are the operators that do not fit into any of the other categories. Some of the special operators in C language are:

sizeof: 

It is used to determine the size of a data type or variable in bytes.

Example:

int a = 10;

printf("Size of int data type: %ld bytes\n", sizeof(int));

printf("Size of variable a: %ld bytes\n", sizeof(a));


& (address of) and * (value at address):

 & operator is used to get the memory address of a variable and * operator is used to access the value stored at that memory address.

Example:

int a = 10;

int *ptr = &a; // Assigns the address of variable a to pointer variable ptr

printf("Value of a: %d\n", a);

printf("Address of a: %p\n", &a);

printf("Value of a using pointer: %d\n", *ptr); // Accesses the value of a using pointer variable ptr


?: (null coalescing operator):

 It is used to assign a default value to a variable if it is null or undefined.

Example:

int a = 0, b = 5;

int c = a ?: b; // If a is zero, c will be assigned the value of b, otherwise c will be assigned the value of a.

, (comma operator): It is used to separate multiple expressions in a single statement.

Example:

int a = 10, b = 5, c;

c = (a++, b++, a+b); // Evaluates a++, b++, and a+b in order and assigns the result to variable c

printf("Value of c: %d\n", c);


These are just a few examples of the many operators available in C language.



We are about this post is all the operators in c language with examples and any questions please do the comments.


Thank you


Post a Comment

0 Comments