CS2211a Lab No. 5 Solved

30.00 $ 15.00 $

Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment


5/5 - (1 vote)

Introduction to C

The objective of this lab is:

  • To practice the C compilation process
  • To practice C formatted input/output, as well as C expressions and selection statements If you would like to leave, and at least 30 minutes have passed, raise your hand and wait for the TA.

Show the TA what you did. If, and only if, you did a reasonable effort during the lab, he/she will give you the lab mark.


  1. Type in (using an editor) and then compile (using gcc) the following c program. If you do not have any compilation errors, the compiler will generate an executable program called a.out. To execute this program, simply type a.out and hit enter.

#include <stdio.h> int main(void)

{ printf((“Hello world\n”);


  1. If you want to name the generated executable file with something other than out, you should use gcc with -o option, e.g., gcc –o hello hello.c (in this case, you should type hello to execute the program)
  2. By default, gcc compiles programs using C89. To compile using C99, you should use “gcc -std=c99”.

Recompile hello.c program using C99 instead of C89. Do you get a warning message from the compiler?  If so, what needs to be done to make it go away?

  1. It is a good practice to use “gcc -Wall” to check all warnings in your program.

Try again to compile the original hello.c program with “gcc -Wall” using C89 and C99.

  1. You can force gcc to stop after the preprocessing stage by using -E

Type in the following simple.c program. Execute the following two commands:  gcc –E simple.c  gcc –E –std=c99 simple.c

What is the difference between the two outputs?

#define MIN 0 #define MAX 100 int main(void) { int i, j, m, n;   i = MAX;

/* First command line

*/   j = MIN;

// Second command line   m = i + j;   n = MAX + MIN;   return 0;



  1. Write a program that declares several int and float variables (without initializing them) and then prints their values. Is there any pattern in the values?
  2. What will happen when you attempt to print an arithmetic expression using the wrong conversion specifier, i.e., printing int expression using %f or float expression using %d?
  3. Mentally evaluate the following printf function calls and write your answer on a piece of paper. Write a program that prints the values of the output.  Verify your answers with the program answers
    • printf(“*%20.10d*\n*%-20.10d*\n*%.10d*\n*%-20d*\n”,



  • printf(“%.4f\n”, 83.162);


  • printf(“%12.5e\n”, 30.253);


  • printf(“%-6.2g\n”, .0000009979);
  1. Mentally evaluate the following expressions and write the answer on a piece of paper.

Write a program that prints the value of these expressions.  Verify your answers with the program answers.

  • 13 % 4, -13 % 4, 13 % -4, and -13 % -4
  • 13 / 4, -13 / 4, 13 / -4, and -13 / -4
  1. Show the output produced by the following program fragment. int i = 1, j = 2; int k = 3, m = 4; i %= j++ % (k += –m);

printf(“%d %d %d %d\n”, i++, ++j, k–, –m);

  1. Show the output produced by the following program fragment. int i = 1, j = 2; int k = 3, m = 4; i *= j / – (k -= ++m);

printf(“%d %d %d %d\n”, i++, ++j, k–, –m);

  1. De Morgan’s laws state that the expression !(condition1 && condition2) is logically equivalent to  the expression (!condition1 || !condition2).

In addition,

the expression !(condition1 || condition2) is logically equivalent to  the expression (!condition1 && !condition2).

Use De Morgan’s laws to write equivalent expressions for each of the following, and then write a program to show that both the original expression and the new expression in each case are equivalent.

(a)  !(x <  5) && !(y >= 7) (b)  !(a == b) || !(g != 5)

  • !((x <= 8) && (y >  4))
  • !((i > 4) ||  (j <= 6))
  1. Write a program to implement the following flowchart segment. The program should get the value of pH from the user. Test your program to make sure that it produces correct answers.
  2. Rewrite the following program fragment by replacing the switch statement with

(a) Nested  if .. else  statement (b) A series of if statements without else

Be careful to deal with the default case properly.


#include <stdio.h>


int main(void)

{ int i;

for(i=1; i<=8; i++)

{    switch(i)     {       case 1:       case 2:  printf(“%d ==> a\n”, i);                break;

case 3: case 4:       case 5:  printf(“%d ==> b\n”, i);                break;


case 6:  printf(“%d ==> c\n”, i);                break;


default: printf(“%d ==> d\n”, i);

}   }   return 0;


  • lab-5-aww6yd.zip