Operators in C programming (Source Codes)

First.c

#include<stdio.h>
int main() {
    int a;
    printf("Enter a number: ");
    scanf("%d",&a);
    printf("You entered %d .",a);
    return 0;
}


Theory

#include<stdio.h>
#include<math.h>
#define pi 3.1415
int main()
{
    // float a=pow(pi,3);
    // printf("%f\n",a);
    // float a;
    // typedef float dec;
    // dec b;
    /*
    Charecter Sets:
    1. alphabets: A,B ... Z, a,b,...z
    2. numbers -1,2,3,...9,0
    3. special character: ,,.,#,<,>,+,-
    4. white space:  sapce, horizental tab, new line

    Identifiers: 
    int rollno;
    char name;

    keyword: int, char, if, if else, else, for, while, do, return;

    Data Types: 
    primary: int, char, float, double, void
    int a;
    char name,lname;
    user defined: typedef float dec;
    derived data type: 
    struct var1{
        int sn;
        char name[10];
        double phoneno;
    }
    Constant:
    int sn = 5;
    char name[6] = "Subash";
    sn++;

    variable: 
    int sn;
    char name[];

    Preprocessor directive: 
    pow(2,5);


    escape sequence: 

    
    
    
    */
   printf("Program run successfully.");
   return 0;
}



printf()

#include<stdio.h>
int main()
{
    char a[] = "Subash";
    printf(" value of a is %s.",a);
    return 0;
}



scanf()

#include<stdio.h>
int main()
{
    char name[10];
    printf("Enter your name: ");
    scanf("%s",&name);
    // operation
    printf("Hi %s",name);
}



Airthmetics Operator

#include<stdio.h>
int main()
{
    // +, -, *, / ,% 
    // int a=3,b=4;
    printf("mode = %d",10%5);
}


Unary Operator

#include<stdio.h>
int main()
{
    int x=9;
    printf("result =  %d\n",x--);
    printf("result =  %d",x);
}


Binary Operator

#include<stdio.h>
int main()
{
    printf("%d ",4*5);
}


Ternary Operator

#include<stdio.h>
int main()
{
    // int a=0,b=10,c=20;
    // condional operator    result=condition?true:false;
    // printf("%d",a?b:c);
    int a= 30;
    int b =20;
    int max;
    max = (a>b)?a:b;
    printf("%d ",max);
}


Division Rule

#include<stdio.h>
int main()
{
    /* int, float
    int/int = int
    float/int, int/float, float/float = float
    */
   printf("%d",-1/4);
//    truncation rule.   -9,......,-2,-1,0,1,2,3, ........9
}


Relational Operator

#include<stdio.h>
int main()
{
    printf("%d\n",2>3);
    printf("%d\n",2<3);
    printf("%d\n",2<=3);
    printf("%d\n",2<=2);
    printf("%d\n",2!=2);
}
    // >,<,>=,<=,==,!=
    // 2>3 => 0
    // 2<3 => 1


Logical Operator

#include<stdio.h>
int main()
{
    printf("It is %d",!(2<3));
}
    // And (&&), Or (||), Not (!a)
    // 0 => False
    // 1 => true


Assignment Operator

#include<stdio.h>
int main()
{
    // int a;
    // char b;
    // a = (2+3-4*8/16);
    // a=2;
    // b= 'c';
    // +=, -=, *=, /=, %=
    // a+=b => a=a+b
    // a-=b => a=a-b
    // a*=b => a=a*b
    // a/=b => a=a/b
    // a%=b => a=a%b
    int a=9;
    // a-=6;   // => a=a-6
    // a*=6;   // => a=a*6
    a%=6;   // => a=a%6
    printf("%d",a);
}


Increment / Dectrmenet Operator

#include<stdio.h>
int main()
{
    // --, ++
    int a=5;
    printf("%d\n",a--);
    printf("%d",a);
}


Conditional Operator

#include<stdio.h>
int main()
{
    int a;
    a = 2<3 ? 2 : 3;
    printf("%d",a);
}
    // value = condition ? true_expression : false_expression;


Bitwise Logical Operators

#include<stdio.h>
int main()
{
    int a=15, b=10;
    printf("%d",a^b);
}
    // AND, OR, XOR
    // 15 => F => 1111(binary)
    // a=15  =>   0000 0000 0001 0101
    // b=10  =>   0000 0000 0001 0000
    // a^b   =>   0000 0000 0000 0101   =>0005
    // a|b   =>   0000 0000 0001 0101   =>0015
    // a&b   =>   0000 0000 0001 0000   =>0010


Bitwise Shift Operators

#include<stdio.h>
int main()
{
    int a=14;
    printf("%d",a>>1);
}
//0000 0000 0001 0100  => 0000 0000 0000 1010 =>10    -1,-2,-3,-4,-5,-6,-7
// 0000 1010 1001 1100   <<
// 0001 0101 0011 1000    >>


Bitwise One's Complement Operator

#include<stdio.h>
int main()
{
    int a=15;
    printf("%d",~a);
    return 0;
}
// 1001 => 0110
// 0101 => 1010  = 10(-6)   1000
// ~n


Specianl Operators

1. Comma Operator

#include<stdio.h>
int main()
{
    int sum,a,b;
    // sum =(a=5,b=6,a+b);
    printf("%d",sum);
    return 0;
}


2. sizeof() Operator

#include<stdio.h>
int main()
{
    // int,char,arry, => 0
    int a;
    printf("%d",sizeof(float));
    return 0;
}


Precedence and Associativity

// Precedence and Associativity
#include<stdio.h>
int main(){
    int a = 2+4-6/8*9;
    return 0;
}


Type Conversion (Type Casting)

// Data Type Conversion / Type Casting
#include<stdio.h>
int main(){
    int a = 1;
    float b = 10.34;
    int c = a+b;   //a=1=> 1.0000+10.34 = 11.34
    // Implicit Type Conversion
    printf("%d",c);
    // Explicit Type Conversion
    float a;
    a = (float)10/3;
    printf("%f",a); 
    return 0;
}


Post a Comment

Previous Post Next Post