Pages

Saturday, September 23, 2017

Easy tutorial for C / C++ Operator - 3

Easy tutorial for C / C++ Operator - 3

Ternary operator:

- It is used in the form a ? b : c. If the result of a is true, b is executed; if false, c is executed.

Example Code

#include <iostream>
using namespace std;

int main() {

     int a, b;

          a = 10 ;
          b = 1 ;
    
     a > b ? cout<<" true ! "<<endl : cout<<" false"<<endl ;

          return 0;
}

Results:

true !

- Declare integer variables a and b, substitute 10 for a, and 1 for b. Since the result of the condition a> b is true, you can see that the sentence before the : symbol is executed and the sentence "true" is printed.

size-of operator:

- The size-of operator is an operator used in previous chapters to check the size of a datatype.

How to use:

1. sizeof (int) or sizeof (double): put the data type in parentheses

2. sizeof(a): put variables in parentheses
<- Where a is a predefined variable.

3. sizeof(100): put a constant in parentheses

cast operator:

- The cast operator converts one type of data temporarily to another.

How to use:

- (data type) variable
- (data type) constant
- (data type) equation

No comments:

Post a Comment