0% found this document useful (0 votes)
61 views

Exercise 1 - Compiling Your First C Program: Printf /N Printf

This document provides instructions for four exercises in an introductory C programming lab. The first exercise has students compile and run a simple "Hello World" program. The second explores formatting with printf statements. The third examines the importance of code segments like #include and main(). Students are instructed to make errors to see warning and error messages. The fourth asks students to write a program printing their registered course names and instructors.

Uploaded by

Mouhnad dkaidek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Exercise 1 - Compiling Your First C Program: Printf /N Printf

This document provides instructions for four exercises in an introductory C programming lab. The first exercise has students compile and run a simple "Hello World" program. The second explores formatting with printf statements. The third examines the importance of code segments like #include and main(). Students are instructed to make errors to see warning and error messages. The fourth asks students to write a program printing their registered course names and instructors.

Uploaded by

Mouhnad dkaidek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS 1160, Computing Fundamentals

Lab 2
Exercise 1 – Compiling your first C program

 Make a new file.


 Save it as task1.c
 Write the following program / code in it.

#include <stdio.h>
int main( )
{
printf( "Welcome to C!\n" );
return 0;
}

 Build your program, and correct your errors (if any!)


 Run the program

Exercise 2 – Understanding printf (part 1)

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

Escape sequence Meaning


\n make a new line
\t make a tabulator
\" output “
\’ output ‘
\\ output \
\b make a backspace

 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

Exercise 3 – Understanding the program structure

To examine the importance of the code segments in Exercise 1, we will thoroughly go


through each one of them. For the purpose of this exercise we will produce some errors.

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?

 Now add the #include statement again to your program.


 int main()
{
}
This is the main function of our program. C starts to execute the code by executing
the very first statement of the main() function.
 Remove the ; at the end of the printf statement. Write down the error
message?

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

You might also like