Exercise 1 - Compiling Your First C Program: Printf /N Printf
Exercise 1 - Compiling Your First C Program: Printf /N Printf
Lab 2
Exercise 1 – Compiling your first C program
#include <stdio.h>
int main( )
{
printf( "Welcome to C!\n" );
return 0;
}
Remove “\n” in the printf statement of the code created in Exercise 1. Write down
what happens?
Modify the printf statement to have the program print out your name.
Modify the program by adding printf statements to print out your name, address and
phone number. Make a proper output format to your printout (e.g., for address tag). You
may use the following escape sequences.
Work with the above escape sequences and notice the output.
Write down the output of the following printf statement:
printf(“The n\na\bme o\tf our course is \”CS\n111\”\b”);
Test your answer by typing the above statement to your code and run the code.
Page 1 of 3
CS 1160, Computing Fundamentals Lab
Please note the warning and error statements. This will allow you to recognize them in future
labs and correct your own errors!
Instead of deleting code statements, you can comment the lines by adding // at the
beginning of the line that should be skipped from the compilation process.
#include<stdio.h>
this statement tells the compiler that you wish to use some of the many input output
functions (IO) that C does provide. The function that will be used is “printf”.
Now remove the #include statement, and note what will be the output when you
run the program?
Compare the line number that is denoted in the error message with the line number
where you have removed the ; Can you explain the difference?
Add the ; again and remove the f in printf. Write down the error message?
Add the f again in printf and remove return 0; Write down the warning
message?
Add return 0; again and change the 0 to a 1. Run the program. What will be the
message of your IDE at the end of the run?
Page 2 of 3
CS 1160, Computing Fundamentals Lab
Note: with return 0 you indicate that the program was run without any problems.
Whenever your program returns a different value (in this case a 1) you indicate that
something was wrong. And normally your IDE will tell you this (i.e. indicate the return value
in a different color, e.g. red, to warn you)
Exercise 4:
Write a C program that outputs the course names and their instructors you have registered for this
semester. Present the output of your program to your instructor before you leave the lab.
Page 3 of 3