Wednesday, 27 January 2016

Write C++ Program to demonstrate the working of sizeof & ternary operators

Leave a Comment
Questions:

Write C++ Program to demonstrate the working of sizeof & ternary operators

Explanation:

Besides the operators discussed above, there are few a other important operators including sizeof and ? : supported by the C Language.

Operator Description Example
sizeof() Returns the size of a variable. sizeof(a), where a is interger, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
* Pointer to a variable. *a;
? : Conditional Expression If Condition is true ? then value X : otherwise value Y


Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/

#include <iostream>
Using namespace std;
int main()
{
   int a = 4;
   Short b;
   Double c;
   int* ptr;

   /* example of sizeof operator */
   cout<< "Line 1 - Size of variable a = " << sizeof(a) << endl;
   cout<< "Line 2 - Size of variable b = " << sizeof(b) << endl;
   cout<< "Line 3 - Size of variable c = " << sizeof(c) << endl;

   /* example of & And * operators */
   ptr = & a; /* 'ptr' now contains the address of 'a'*/
   cout<< "value of a is = " << a << endl;
   cout<< "*ptr is  ." <<* ptr << endl;

   /* example of ternary operator */
   a = 10;
   b = (a == 1) ? 20: 30;
   cout<< "Value of b is = " << b << endl;

   b = (a == 10) ? 20: 30;
   cout<< "Value of b is = " << b << endl;
}



Output:



If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: