| Main |< C/C++ Variable, Operator & Type 1 | C/C++ Variable, Operator & Type 3 >| Site Index | Download |


 

 

 

 

 

 

 

C LAB WORKSHEET 5_1

C/C++ Variable, Operator And Type 2

 

 

 

 

 

 

 

Items in this page:

 

  1. In this page more variable, data type and operator program examples, questions and activities.

  2. Character and string data types.

  3. 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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 
  • Character is another basic data type and strings are combinations of characters. Show the output for the following program.

// 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.

      char b = '$', c = '2', d;

      printf("Given b = \'$\' and c = \'2\'\n");

      printf("Operations are: d=c, c=b and b = \'c\'\n");

      d = c;      // Statement #1

      c = b;      // Statement #2

      b = 'c';    // Statement #3

      printf("See the results...\n");

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

      return 0;

}

 

A sample output:

C programming - char data type program output sample

  • In statement 3 the character ‘c’ is being assigned and not the variable c, as it was in statement 1. Also note that in statement 1, d is being assigned the character 2 and not the integer 2.

  • To assign the string s to the string t (copy a string), one may use strcpy_s(t, dest. size, s) function family and the equal sign as before. Show the output of the following program.

// needed for printf()

#include <stdio.h>

// needed for strcpy_s() 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.

      char q[20], r[20] = "Tom Hanks", s[20] = "Kevin Kostner";

      printf("Given: r[20] = \"Tom Hanks\", s[20] = \"Kevin Kostner\"\n");

      printf("\nThe operations: strcpy_s(q, 20, s), strcpy_s(s, 20, r), \n"

            "strcpy_s(r, 20, \"Sarah\")\n");

      // the destination string must be large enough to hold the

      // source string, including the terminating null character.

      // if the non-secure version is used, such as strcpy()

      // compiler may generate warning...

      strcpy_s(q, 20, s);

      strcpy_s(s, 20, r);

      strcpy_s(r, 20, "Sarah");

      printf("\nThe results: q = %s, r = %s and s = %s\n", q, r, s);

      return 0;

}

 

A sample output:

C programming - strcpy() and strcpy_s()  program output sample

3.1234             =             ____________________   Ans: float

"3214.55"       =              ____________________   Ans: string

'5'                     =              ____________________   Ans: char

1243                =              ____________________   Ans: int

// needed for printf()

#include <stdio.h>

// needed for strcpy_s() 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 = 8;

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

      i = i + 1;

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

      i = i + 2;

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

      i = i + 3;

      printf("At the end of the operation i = i + 3 = %d\n", i);

      return 0;

}

 

A sample output:

C programming - standard output program sample

 

Questions And Activities:

  1. The data type for the variable i is an integer or a whole number.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i = 77;

      printf("Initially, an integer i = %d\n", i);

      i = i + 1;

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

      return 0;

}

  1. What does the statement i = i + 1; do to the value of i?

 

 

C variable and data type

  1. The i = i + 1 add 1 to the current value of integer i (77).

   
  1. Show the following program output. The lines with the equal signs ( = ) are called assignment statements.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i = 77;

      i = i + 3;

      printf("Initially, an integer i = %d\n", i);

      i = i + i + 1;

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

      return 0;

}

  1. Why is i printed 80 instead of 77 in the first printf()?

  2. What is the value of i while the last printf() is being called? Why?

C, variable and assignment operator

  1. Because of the statement i = i + 3; Then 77 + 3 = 80. So the current value of i is 80.

  2. It is 161. it is because of the statement i = i + i + 1; The current value of variable i before this statement been executed is 80, then 80 + 80 + 1 = 161.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. Show the output and answer the following questions.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i = 77, j, k;

      j = i + 1;

      k = j + 1;

      i = k + j;

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

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

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

      return 0;

}

 

    1. What is the final value of j and how did it obtain that value?

    2. What is the final value of k and how did it obtain that value?

    3. What is the final value of i and how did it obtain that value?

C, assignment statement and operators

  1. 78. From the assignment of j = i + 1; statement and the current value of i is 77 then j = 77 + 1 = 78.

  2. 79. It is obvious that k = j + 1 and the current value of j is 78, so k = 78 + 1 = 79.

  3. 157. The assignment statement for i is i = k + j. The current value of k is 79 and j is 78 so i = 79 + 78 = 157.

In this example we can see that the value of variable change during the program execution. It depend on the operation(s) done on the variable and the order of the statement in the program. C code executed sequentially, line by line. What we concern here is the current value of the variable before the statement been executed.

   
  1. Try the following codes and answer the questions.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i = 77;

      printf("Given i = 77...\n");

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

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

      printf("Do it in different way...\n");

      i = i + 3;

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

      return 0;

}

  1. In which statement is i declared as an integer?

  2. What is the initial value of i?

  3. Did the first printf() assign a new value to i?

  4. Did i = i + 3; assign a new value to i?

C assignment, operator and variables

  1. int i = 77;

  2. 77

  3. No. This just a literal string printed out to the standard output by printf().

  4. Yes. This is an assignment statement. Add 3 to the current value of i and assigned the new value to variable i.

   
  1. Run the following program and answer the questions.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i = 12;

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

      printf("Then, i * 3 - 4 = %d\n", i * 3 - 4);

      printf("Different answer? i * (3 - 4) = %d\n", i * (3 - 4));

      printf("Another one, i / 2 + 4 * 2 = %d\n", i / 2 + 4 * 2);

      printf("Similar expression but different answer? i / (2 + 4) * 2 = %d\n", i / (2 + 4) * 2);

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

      return 0;

}

 

    1. After i was initialized to 12, was it value ever changed?

    2. What is the symbol used to multiply? To divide?

    3. In the second printf(), which operation, multiply or subtract was done first?

    4. In the third printf(), which was done first, multiply or subtract? Why?

    5. In fourth printf(), which operation was done last?

    6. Which of the following has the highest, next highest and the lowest precedence/priority? That is which one will be done first, next and last?

 

* and /; + and -; ()

 

The priority execution of the expression in C/C++ depends on the operator precedence and is done automatically by compiler if the parentheses/brackets ( () ) are not used. In order to make your code readable or to produce a correct output, it is better to use parentheses/brackets that having the highest priority. So, forget about operator precedence just use brackets.

 

C mathematical operators and precedence

  1. Yes.

  2. Multiply - * (asterisk) and divide - / (forward slash)

  3. Multiply was done first. This is a operator precedence. Multiply is higher precedence than subtract operator.

  4. Subtract first because of the bracket. Bracket is higher than mathematical operators.

  5. Multiplication followed by division and then addition.

  6. *, /, + and - (from highest to lowest.

 

 

   
  1. Playing with floating point numbers. Try the following code and answer the questions.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      printf("Printing an integer: %d\n", 10);

      printf("Printing a float: %.2f\n", 10);

      printf("Printing a float: %.2f\n", 10.00);

      printf("Printing an integer: %d\n", 10.00);

      return 0;

}

 

    1. Which line of codes that print correct values? Keep in mind that your compiler doesn’t generate any warning or error but some of the output is rubbish or zero if format specifier is not used properly.

    2. Do you use a %d or a %.2f to print an integer?

    3. Do you use a %d or a %.2f to print a float?

 

C programming - floating point program output sample

  1. printf("Printing an integer: %d\n", 10); and printf("Printing a float: %.2f\n", 10.00);

  2. %d.

  3. %.2f.

   
  1. More floating point program. Run and answer the questions.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i = 10;

      float x = 10.5;

      printf("Given int i = 10, float x = 10.5\n");

      printf("Print as integer: i / 3 = %d\n", i / 3);

      printf("Print as float: i / 3 = %.2f\n", i / 3);

      printf("Print as integer: x / 3 = %d\n", x / 3);

      printf("Print as float: x / 3 = %.2f\n", x / 3);

      return 0;

}

  1. To print an integer result, what format specifier must be used?

  2. To print a floating point result, what format specifier must be used?

C floating point and integer types

  1. %d.

  2. %f.

 

   
  1. Remember that i is defined to be an integer or a whole number. It cannot store values with decimals (for fractions). Notice in these printf()’s that some expressions are evaluated as integers and some as floats. Also, some format specifiers are integers and some are floats.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i;

      i = 10;

      printf("Given int i = 10\n");

      printf("Print as int: i / 2 = %d\n", i / 2);

      printf("Print as int: i / 3 = %d\n", i / 3);

      printf("Print as float: i / 3 = %.2f\n", i / 3);

      printf("Print as float: i / 3.0 = %.2f\n", i / 3.0);

      printf("Print as float: (i + 0.0) / 3 = %.2f\n", (i + 0.0) / 3);

      printf("Print as int: i * 3 = %d\n", i * 3);

      printf("Print as float: i * 3.0 = %.2f\n", i * 3.0);

      return 0;

}

 

    1. In C/C++, why do you think 10 divided by 3 is equal to 3 and not 3.33?

    2. i/3 evaluates to an integer or a float?

    3. i/3.0 evaluates to an integer or a float?

    4. How do you think (i * 1.0)/3 will be evaluated?

    5. To see the fractional part after dividing two items, what must be true of one of those items?

C integers and floating point examples

  1. Because a format specifier %d, that is for integer was used instead of %f for float and integers numbers (for numerator and denominator) were used instead of floats in the division.

  2. an integer.

  3. a float.

  4. Evaluated as a float because 1.0 was used.

  5. One of the items must be a float.

   
  1. Try the following program example.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i = 10;

      float x = 10.5; // a dummy initial value

      x = i / 3;  // warning, int to float

      printf("Given int i = 10, float x = 10.5\n");

      printf("Print as float: i / 3 = %.2f\n", x);

      x = i / 3.0;      // warning, double to float

      printf("Print as float: i / 3.0 = %.2f\n", x);

      return 0;

}

  1. In x = i/3, was the evaluation of i/3 an integer value or a floating point value?

  2. Why was the result from the second printf() more accurate than the result from the first printf()?

C float and int division operation

  1. A floating point value because the result for the division was set to floating point. However the value has been truncated because items (numerator and denominator) used in the division are integers. Compiler will do an automatic conversion to float for float and/or integer items.

  2. Because one of the item (denominator) used in the division is a float.

   
  1. Let try the following program.

 

// needed for printf()

#include <stdio.h>

 

int main()

{

      int i = 10;

      float x = 10.5;

      i = x / 3;  // warning

      printf("Given int i = 10, float x = 10.5\n");

      printf("Print as float: i / 3 = %.2f\n", i); // rubbish

      printf("Print as int: i = %d\n", i);

      i = x / 3.0;      // warning

      printf("Print as float: i = %.2f\n", i); // rubbish

      return 0;

}

 

    1. In i = x/3, was the evaluation of x/3 an integer value or a floating point value?

    2. Can we store a floating point value in an integer variable?

    3. In an arithmetic expression, such as i/2 + 1, if all the items are integers, then the result is of what type?

    4. If at least one item is a float, then the result is of what type?

    5. When dividing two integers, the result will contain a fractional portion only if it is assigned to a floating point variable. True or False?

    6. When dividing two items and one is a float, the result will be an integer. True or False?

C language integers and float data types

  1. The output is rubbish.

  2. No.

  3. Integer.

  4. Float.

  5. True.

  6. False.

 

 

 

 

 

 

 

   

| Main |< C/C++ Variable, Operator & Type 1 | C/C++ Variable, Operator & Type 3 >| Site Index | Download |


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