Copyright © tutorialspoint.com

Latest C Tutorial

C - Logical Operators Examples

previous

sizeof Operator:

Try following example to understand sizeof operators. Copy and paste following C program in test.c file and compile and run this program.

main()
{
   int a;
   short b;
   double double c;
   char d[10];

   printf("Line 1 - Size of variable a = %d\n", sizeof(a) );
   printf("Line 2 - Size of variable b = %d\n", sizeof(b) );
   printf("Line 3 - Size of variable c= %d\n", sizeof(c) );
   printf("Line 4 - Size of variable d= %d\n", sizeof(d) );
   /* For character string strlen should be used instead of sizeof */
   printf("Line 5 - Size of variable d= %d\n", strlen(d) );
}

This will produce following result


Line 1 - Size of variable a = 4
Line 2 - Size of variable b = 2
Line 3 - Size of variable c= 8
Line 4 - Size of variable d= 10
Line 5 - Size of variable d= 10

& and * Operators:

Try following example to understand & operators. Copy and paste following C program in test.c file and compile and run this program.

main()
{
   int i=4;		/* variable declaration 	*/
   int* ptr;		/* int pointer			*/

   ptr = &i;	/* 'ptr' now contains the
			   address of 'i'		*/
   printf("  i is  %d.\n", i);
   printf("*ptr is %d.\n", *ptr);
}

This will produce following result


  i is  4.
*ptr is 4.

? : Operator

Try following example to understand ? : operators. Copy and paste following C program in test.c file and compile and run this program.

main()
{
   int a , b;

   a = 10;
   b = (a == 1) ? 20: 30;
   printf( "Value of b is %d\n", b );

   b = (a == 10) ? 20: 30;
   printf( "Value of b is %d\n", b );
}

This will produce following result


Value of b is 30
Value of b is 20

previous

Copyright © tutorialspoint.com