Ads

Other Operators


SIZEOF OPERATOR:

- C provides size of() operator to know the size of the identifiers we use in our program.

- Size of data types may change from computer to computer depending on the type of processor the computer is using, thus for portability purposes and knowing the size of data types, functions and variables we use sizeof() operator.

- sizeof() operator can also be used on structures, arrays, enum (user defined datatypes) etc.

Program:


/* Program to explain sizeof operator */

#include<stdio.h>
int main()
{
 int i;
 char c;
 float f;
 double d;

 printf("\nsize of int is %d",sizeof(int));
 printf("\nsize of int is %d",sizeof(i));
 printf("\nsize of char is %d",sizeof(char));
 printf("\nsize of char is %d",sizeof(c));
 printf("\nsize of float is %d",sizeof(float));
 printf("\nsize of float is %d",sizeof(f));
 printf("\nsize of double is %d",sizeof(double));
 printf("\nsize of double is %d\n",sizeof(d));

 return 0;
}

Output:

size of int is 4
size of int is 4
size of char is 1
size of char is 1
size of float is 4
size of float is 4
size of double is 8
size of double is 8

- we can use direct datatype or related variable in sizeof() operator.

 

COMMA Operator:

- Denoted by comma (,) and also known as separator.

- We can separate the expressions using comma operator.

- We can declare more than one expression in a single line as shown below.

Example:

int a;
int b;
int c;
Instead we can declare them in one line as
int a,b,c;

- We can also assign the variables at same time

int a=4,b=7,c=8;

- We can also use it to decrease the no of statements as shown below

a=4;
b=5;
c=9;
y=a+b+c;

- Instead I will write all the four statements in a single line as

a=4,b=5,c=9,y=a+b+c;
- In above statement first 4 is assigned to a, next 5 to b, next 9 to c and the result of a+b+c to y

Program:


/* Comma Operator */
#include<stdio.h>
int main()
{
 int a,b,c,y;
 a=4,b=5,c=9,y=a+b+c;
 printf("\n The value in y is %d",y);

 return 0;
}

Ouput:

 The value in y is 18



 ADDRESS OPERATOR:

-  The address operator denoted by ampersand & is used to denote the address of a variable.

Syntax

&<variable>

- Angle brackets are used for demonstration purpose only and should not be used in your c program.

- We use this address operator mostly in scanf function to supply the address of a variable to store the data.
scanf("%d",&x);

- In pointers concept address operator is used to get the address of the variable.  

Program:


/* Write a program to print the address of a variable. */
#include<stdio.h>
int main()
{
 int x=5;
 printf("\nThe value in x is %d",x);
 printf("\nThe address of x is %p",&x);

 return 0;
}

Output:

The value in x is 5
The address of x is 0xbfe0e95c



- You might get some different address because address of variable is not predictable.


No comments:

Post a Comment