Operators in c

sizeof() operator

sizeof() operator is a single word operator in C. It return the size of data or size of data type. sizeof() operator is case sensitive.

sizeof() operator also used in dynamic memory allocation.

Syntax of sizeof operator:

	
    variable_name = sizeof(expression);
    

In above code 'expression' return integer value.

Example of declaring the sizeof() oprerator:

	
    int x, y, z;
    int a = 10;
    float b = 10;
    char ch = 'a';
    
    n = sizeof(a);
    printf("Size of Integer Datatype %d ",x);
    
    n = sizeof(b);
    printf("Size of Float Datatype %d ",y")
    
    n = sizeof(ch);
    printf("Size of Character Datatype %d ",z);
    
    //other way to show data type size by passing sizeof() operator in printf function
    printf(" %d %d %d",sizeof(x),sizeof(y),sizeof(z));
    

Output:

	
    Size of Integer Datatype 2		//size of integer data type.
    Size of Float Datatype 4		//size of float data type.
    Size of Character Datatype 1	//size of character data type.
    
    2	4	1	//in online output
    

In above example you will see the different type of data types and using sizeof() operator it will give you size of the data types.

type casting operator

type casting is a special operator that convert the one data type value to another data type value.

type casting operator example:

	
    type = data type.
    casting = conversion of data.
    

Two types of casting are there

implicit type casting

implicit type casting is a type conversion which perform by the compiler. It store data type (lower data type to higher data type).

Example of implicit type casting:

	
    int a = 10;
    float b;
    b = a;
    

explicit type casting

explicit type casting is a type conversion which perform by the programmer not by the compiler. It store data type higher data type to lower data type.

Example explicit type casting:

	
    float a = 2.5;
    int b;
    b = (int)a;
    

Output:

	
    2
    

In example of explicit type casting we are storing the 'float' data type value into 'int' data type value. explicit type casting will not allow to you to store the 'float' data type into 'int' data type and but it will store only 2 value.

Because it store the higher data type to lower data type and we are storing '4 byte' float data type value into '2 byte' int data type. Chances of memory loss.

bitwise operator

bitwise operator perform the operation on bit-level. In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction, addition and division are done in bit-level. To perform bit-level operations in C programming, bitwise operators are used.

Because internally only bitwise operator work on bit (binary language). Faster than any other operators and directly work on binary language.

Table of bitwise operator

Operators Meaning of Operators Example
& bitwise AND (A & B) = 14 i.e., 0000 1110
| bitwise OR (A ∣ B) = 21 i.e., 0001 0101
^ bitwise Exlusive OR (A ^ B) = 48 i.e., 0011 000
~ bitwise Compliment (~A ) = 61 i.e., 1100 0011 in 2's complement form.
<< bitwise Left Shift A << 2 = 240 i.e., 1111 0000
>> bitwise Right Shift A >> 2 = 15 i.e., 0000 1111

bitwise AND operator &

AND & operator only applies on int, short, char not on any other data-types.

Post Your Comment