operators programs in c

 

1.      Arithmetic operators


#include <stdio.h>

int main() 

{

    int a = 10, b = 3;


    printf("Addition: %d \n", a + b);

    printf("Subtraction: %d \n", a - b);

    printf("Multiplication: %d \n", a * b);

    printf("Integer-Division: %d \n", a / b);

printf("Floating-Division: %f \n", (float)a / b);

    printf("Modulus: %d \n", a % b);

    return 0;

}


2.      Relational operators

#include <stdio.h>

int main() 

{

    int a = 2, b = 3;

    

    printf("a == b : %d \n", a == b);

    printf("a != b : %d \n", a != b);

    printf("a > b  : %d \n", a > b);

    printf("a < b  : %d \n", a < b);

    printf("a >= b : %d \n", a >= b);

    printf("a <= b : %d \n", a <= b);


    return 0;

}


3.      Logical operators

#include <stdio.h>

int main() 

{

    int a = 2, b = 5, c = -3;

    

    printf("Logical AND - (a > 0 && b > 0) = %d \n", (a > 0 && b > 0));

    

    printf("Logical OR - (a > 0 || c > 0) = %d \n", (a > 0 || c > 0));

    

    printf("Logical NOT - !(a > b) = %d \n", !(a > b));


    return 0;

}


4.      Assignment operators

#include <stdio.h>

int main() 

{

int a = 10, b = 5;

    

    printf("Initial a = %d \n", a);

    

    a += b;   

    printf("After a += b, a = %d \n", a);

    

    a -= b;   

    printf("After a -= b, a = %d \n", a);

    

    a *= b;   

    printf("After a *= b, a = %d \n", a);

    

    a /= b;   

    printf("After a /= b, a = %d \n", a);

    

    a %= b;   

    printf("After a %%= b, a = %d \n", a);


    return 0;

}


5.      Bitwise Operators

#include <stdio.h>

int main() 

{

    int a = 5;  // Binary: 00000101

    int b = 3;  // Binary: 00000011


    printf("a & b = %d \n", a & b);    

    printf("a | b = %d \n", a | b);    

    printf("a ^ b = %d \n", a ^ b);   

    printf("~a = %d \n", ~a);          

    printf("a << 2 = %d \n", a << 2);  

    printf("b >> 1 = %d \n", b >> 1); 

    return 0;

}


6.      Increment/Decrement Operators

pre increment

# include <stdio.h>

int main()

{

int x = 5, y ;


printf("before pre-increment x value %d \n", x);

y = ++x;


printf("after pre-increment x and y values:");

printf("x = %d , y = %d \n", x , y);


return 0;

}

post increment

# include <stdio.h>

int main()

{

int x = 5, y ;


printf("before post-increment x value %d \n", x);

y = x++;


printf("after post-increment x and y values:");

printf("x = %d , y = %d \n", x , y);


return 0;

}


pre decrement

# include <stdio.h>

int main()

{

int x = 5, y ;


printf("before pre-decrement x value %d \n", x);

y = --x;


printf("after pre-decrement x and y values:");

printf("x = %d , y = %d \n", x , y);


return 0;

}


post decrement

# include <stdio.h>

int main()

{

int x = 5, y ;


printf("before post-decrement x value %d \n", x);

y = x--;


printf("after post-decrement x and y values:");

printf("x = %d , y = %d \n", x , y);


return 0;

}


7.      Conditional operator

#include <stdio.h>

int main() 

{

    int x = 5;


printf( "The given number is: %s" , (x % 2 == 0) ? "even" : "odd");


    return 0;

}


8.      Special operators

sizeof

#include <stdio.h>


int main() 

{

    int a;

    float b;

    char c;


    printf("Size of int: %zu bytes \n", sizeof(int));

    printf("Size of float: %zu bytes \n", sizeof(float));

    printf("Size of char: %zu bytes \n", sizeof(char));

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


    return 0;

}


comma operator

#include <stdio.h>


int main() 

{

    int a = (5, 10, 20);

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


    int b;

    b = (a++, ++a, a+5); 

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


    return 0;

}


pointer operator

#include <stdio.h>

int main() 

{

    int x = 10;

    int *ptr;   


    ptr = &x;   

    printf("Address of x: %p \n", ptr);

    printf("Value of x using pointer: %d \n", *ptr);


    return 0;

}






Popular posts from this blog

operators in c programming

Variables in c

Cloud Storage and Local Storage: Applications in Business