| Main |< C main() and printf() functions 2 | C/C++ Variable, Operator & Type 2 >| Site Index | Download |


 

 

 

 

 

 

 

C LAB WORKSHEET 5

C/C++ Variable, Operator And Type 1

 

 

 

 

 

 

 

Items in this page:

 

  1. Variable, data type, operator and assignment.

  2. Basic data type.

  3. Standard and non-standard C libraries.

  4. Tutorial references are: C/C++ intro & brief history, C/C++ data type 1, C/C++ data type 2, C/C++ data type 3 and C/C++ statement, expression & operator 1, C/C++ statement, expression & operator 2 and C/C++ statement, expression & operator 2.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C Operators

 

For the following C code:

 

int p, q, r;

p = q + r;

  • q and r are operands and + is an operator. Also, in this case p, q and r are variables. The p = q + r; statement is evaluated from right to left.

  • The C operators are a subset of the C++ operators thus in C++ you will find more operators. There are three types of operators based on the number of operand that they operate on: unary, binary and ternary.

  1. A unary expression consists of either a unary operator prepended to an operand such as ++x or p--, or the sizeof keyword followed by an expression. The expression can be either the name of a variable or a cast expression such as (float)x where x is an integer. If the expression is a cast expression, it must be enclosed in parentheses.

  2. A binary expression consists of two operands joined by a binary operator for example: x + y.

  3. A ternary expression consists of three operands joined by the conditional-expression operator for example: x  =  y  ?  1  :  100;.

  • C includes the following unary operators:

Symbol

Name

-, ~, !

Negation and complement operators

*, &

Indirection and address-of operators

sizeof

Size operator

+

Unary plus operator

++, --

Unary increment and decrement operators

  • Binary operators associate from left to right. C provides the following binary operators:

Symbol

Name

*, /, %

Multiplicative operators

+, -

Additive operators

<<,   >>

Shift operators

<,   >,   <=,   >=,   ==,   !=

Relational operators

&,   |, ^

Bitwise operators

&&,   ||

Logical operators

,

Sequential-evaluation operator

Operator Precedence and Order of Evaluation

Precedence and Associativity of C Operators

Symbol1

Type of Operation

Associativity

[ ] ( ) . –> postfix ++ and postfix --

Expression

Left to right

prefix ++ and prefix -- sizeof &   *   + – ~ !

Unary

Right to left

typecasts

Unary

Right to left

* / %

Multiplicative

Left to right

+ –

Additive

Left to right

<< >>

Bitwise shift

Left to right

< > <= >=

Relational

Left to right

== !=

Equality

Left to right

&

Bitwise-AND

Left to right

^

Bitwise-exclusive-OR

Left to right

|

Bitwise-inclusive-OR

Left to right

&&

Logical-AND

Left to right

||

Logical-OR

Left to right

? :

Conditional-expression

Right to left

= *= /= %=

+= –= <<= >>= &=

^= |=

Simple and compound assignment2

Right to left

,

Sequential evaluation

Left to right

1: Operators are listed in descending order of precedence. If several operators appear on the same line or in a group, they have equal precedence.

2: All simple and compound-assignment operators have equal precedence.

x && y++

Expression

Automatic Binding

a & b || c

(a & b) || c

a = b || c

a = (b || c)

q && r || s--

(q && r) || s--

Illegal Expression

Default Grouping

p == 0 ? p += 1: p += 2

( p == 0 ? p += 1 : p ) += 2

( p == 0 ) ? ( p += 1 ) : ( p += 2 )

// needed for printf()

#include <stdio.h>

// needed for strcpy(), strcpy_s() - a secure version and their family

#include <string.h>

 

int main()

{

      // declare variables and initialize some of them

      // some compiler ask you to initialize all the variables...

      // in this case just initialize them with dummy value..

      // for example: int x = 0, float y = 0.00 etc.

      int i, j = 4, k;

      float x, y = 8.8; // warning, can change to double...

      char a, b ='V';

      char q[20], r[20] = "Incredible Hulk";

      printf("Declared and initialized some variables...\n"

            "...and see the action...\n\n");

      // READ AND EVALUATE C/C++ EXPRESSION FROM RIGHT TO LEFT

      i = 5;            // i becomes 5

      i = i + j;        // i becomes 9 (5 +4)

      i = i + y;        // i becomes 17 (9+8.8), warning...

      k = (5 + (6/2)) * (3-1);

      printf("i = %d, k = %d\n", i, k);

      i = 3;

      x = (j/i);        // integer divided by integer is an integer, warning

      y = (j*1.0) / i;  // warning...

      k = (j*1.0) / i;  // warning...

      printf("x = %.2f, y = %.2f, k = %d\n", x, y, k);

      a = 'Z';

      printf("a = %c\nb = %c\n", a, b);

      // using secure version instead of strcpy(), previously

      // these functions generate a buffer overflow problems...

      // we have strcpy(dest, source), wide character version

      // - wcscpy(dest, source), secure version - wcscpy_s(dest, dest size, source) and

      // strcpy_s(dest, dest size, source) another one is multibyte version - _mbscpy(...), find it yourself...

      strcpy_s(q, 20, r);

      strcpy_s(r, 20, "CopiedString");

      printf("q = %s\nr = %s\n", q, r);

      return 0;

}

 

A sample output:

C programming - basic data type program output sample

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      // declare variables and initialize some of them

      // Some compiler ask you to initialize all the variables...

      // in this case just initialize them with dummy value..

      // for example: int x = 0, float y = 0.00 etc.

      int i, j = 18, k = -20;

      printf("Initially, given j = 18 and k = -20\n");

      printf("Do some operations..."

                  "i = j / 12, j = k / 18 and k = k / 4\n");

      i = j / 12;

      j = k / 8;

      k = k / 4;

      printf("At the end of the operations...\n");

      printf("i = %d, j = %d and k = %d\n", i, j, k);

      return 0;

}

 

A sample output:

C programming - data type program output sample

// needed for printf()

#include <stdio.h>

 

int main()

{

      // declare variables and initialize some of them

      // some compiler ask you to initialize all the variables...and it is a good habit

      // in this case just initialize them with dummy value..

      // for example: int x = 0, float y = 0.00 etc.

      float x, y = 20.5;

      printf("Given y = 20.5\n");

      printf("Then do some operations..."

            "x = y / 8, y = (y + 18) * 3\n");

      x = y / 8;

      y = (y + 18) * 3;

      printf("The results are: x = %.2f and y = %.2f\n", x, y);

      return 0;

}

 

A sample output:

C programming - float data type program output sample

// needed for printf()

#include <stdio.h>

 

int main()

{

      // declare variables and initialize some of them

      // some compiler ask you to initialize all the variables...

      // in this case just initialize them with dummy value..

      // for example: int x = 0, float y = 0.00 etc.

      int j = 18;

      float x, y = 20.5;

      printf("Given j = 18, y = 20.5\n");

      printf("The operations: x = j / 7, y = y / 7\n");

      x = j / 7;

      y = y / 7;

      printf("The result are: x = %.2f     and y = %.2f\n", x, y);

      printf("But we can force or promote the int to float - type promotion...\n");

      x = ((float) j / 7);

      printf("The result are: x = %.2f     and y = %.2f\n", x, y);

      return 0;

}

 

A sample output:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

----------------------------------------------------------------------------

C programming - more on data type program output sample

// needed for printf()

#include <stdio.h>

 

int main()

{

      // declare variables and initialize some of them

      // some compiler ask you to initialize all the variables...

      // in this case just initialize them with dummy value..

      // for example: int x = 0, float y = 0.00 etc.

      int i, k = 10;

      float x, y, z = 20.5;

      printf("Given k = 10, z = 20.5\n");

      printf("Operations are: x=z/6, y=k/6, i=z/6, z=k/6.2\n");

      x = z / 6;       // Statement #1

      y = k / 6;      // Statement #2 - warning: int to float

      i = z / 6;       // Statement #3 - warning: float to int

      z = k / 6.2;  // Statement #4 - warning: int to float

      printf("The results are: x = %.2f\ty = %.2f\tz = %.2f\ti = %d\n", x, y, z, i);

      printf("Try the type promotion...\n");

      y = (float) k / 6;        // promote to float, OK

      i = (int) z / 6;            // promote to int, not OK...

      z = (float) k / 6.2;    // promote to float, OK

      printf("The results are: x = %.2f\ty = %.2f\tz = %.2f\ti = %d\n", x, y, z, i);

      printf("So be careful with precision...\n");

      return 0;

}

 

A sample output:

C programming - more on data type program output

// needed for printf()

#include <stdio.h>

 

int main()

{

      // declare variables and initialize some of them

      // some compiler ask you to initialize all the variables...

      // in this case just initialize them with dummy value..

      // for example: int x = 0, float y = 0.00 etc.

      int i = 3, j;

      float x = 2.5, y;

      printf("Given int i = 3, float x = 2.5\n");

      printf("Operations are: y=(i+x)*0.5-2.0, j=i\n");

      y = ((i + x) * 0.5) - 2.0; // warning

      j = y; // float to integer...failed!!!

      printf("The results are: j = %d, y = %.2f\n", j, y);

      return 0;

}

 

A sample output:

C programming - more on data type program output sample

// needed for printf()

#include <stdio.h>

 

int main()

{

      // declare variables and initialize some of them

      // some compiler ask you to initialize all the variables...

      // in this case just initialize them with dummy value..

      // for example: int x = 0, float y = 0.00 etc.

      int i = 5, j = 8;

      printf("Given i = 5, j = 8\n");

      printf("Operations and results...\n");

      printf(" i + j = %d\n", i + j);

      printf(" i + 1 = %d, j + 1 = %d\n", i + 1, j + 1);

      printf(" i - j * (i - 7) = %d\n", i - j * (i - 7));

      // operator precedence...which operator that compiler execute first?

      // 5 - 8 * (5  - 7) = 5 - 8 * (-2) = 5 - (-16) = 21

      // better to use parentheses: i - (j * (i - 7))

      // start from the innermost then to the outermost of the parentheses

      printf(" i = %d, j = %d\n", i, j);

      return 0;

}

 

A sample output:

C programming - data type program output sample

 

 

 

 

 

 

 

 

  

| Main |< C main() and printf() functions 2 | C/C++ Variable, Operator & Type 2 >| Site Index | Download |


The C Variables, Operators and Data Types: Part 1 | Part 2 | Part 3