United International University: Lab Sheet 1 Introduction To C
United International University: Lab Sheet 1 Introduction To C
Lab Sheet 1
Introduction to C
Outcomes
After finishing this lab students should be able to
Contents
1 A theoretical overview 2
1.1 Computer Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Levels of programming language: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 The Translator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
8 Programming Examples 7
9 Practice session 8
10 Lab Assignments 9
EEE 122: Structured Programming Laboratory 2
1 A theoretical overview
1.1 Computer Programming
Computer programming (often shortened to programming) is a process that leads from an original
formulation of a computing problem to executable computer programs.
Source code is written in one or more programming languages.
1. MACHINE LANGUAGES: Computers are made to understand only two states pulse and no
pulse (or 1 and 0) conditions. Computers do not understand English, Bangla or any other
common language. They respond only to 1 and 0.A language composed of only 1 and 0 is called
the machine language. Since computers are not identical in architecture, each computer has its
own machine language.
Thus the programmer himself should convert his code to language composed of 1 and 0(i.e. to
machine language). For example if one wants to make understand ADD 0184 to an early IBM
machine, it would have been written :
000100000000000000000000000010111000
Again, every computer has its own machine language, the programmers cannot communicate
with other computers, if he does not know his machine language.
2. ASSEMBLY LANGUAGES:
In assembly language instructions are given in English like words, such as MOV, ADD, SUB
etc. Hence it is easy to write and understand assembly programs. As you know computer can
understand only machine level language. Hence assembly language program must be translated
into machine language. The translator which is used for translating is called assembler.
In assembly language data are stored in the computer registers. Every computer has different
set of registers. Hence the assembly language program is also not portable.
Since the low level language is related with hardware, the execution time of low level program
is faster.
3. HIGH LEVEL LANGUAGES:High level language is designed keeping in mind the features of
portability, means these languages are machine independent. These are the English like lan-
guage, so it is easy to write and understand the programs of high level language. For translating
high level language program into machine language, compiler or interpreter is used. Every lan-
guage has its own compiler or interpreter. The language in this category is FORTRAN, COBOL,
BASIC, PASCAL etc.
C is the middle level language which is in between these two categories. In C program can be
written same as other high level language and it may be also possible to interact this with low
level language. For this reason, some call C the high level language.
1. Compiler: Compilers are used to convert high level languages (like C, C++ ) into machine code
(i.e. gcc , Microsoft Visual Studio)
2. Assembers: Assembler are used to convert assembly language code into machine code.
3. Interpreter:An interpreter is a computer program which executes a statement directly (at run-
time). (i.e.python , LISP, Ocamle)
2. It starts just like any other program does: Locate its icon on the Start button menu, or you may
also find the Code::Blocks shortcut icon on the desktop, which is the easiest way to start the IDE
in Windows 7/8/10.
EEE 122: Structured Programming Laboratory 5
5. Choose C as the language you want to use, and then click the Next button.
C is quite different from C++, you can do things in one language that arent allowed in the other.
7. Click the Browse button to the right of the text box titled Folder to Create Project In.
8. Use the Make New Folder button in the Browse for Folder dialog box to create a project folder.
9. Click the OK button to select the folder and close the dialog box.
# i n c l u d e < s t d i o . h>
i n t main ( void ) {
printf ( ” Hello world ! \ n” ) ;
return 0;
}
1. Ensure that the project you want to build is activated in the Management window.
Activated projects appear in bold text. If you have more than one project shown in the Projects
window, activate the one you want to work with by right-clicking the project name (by the
Code::Blocks icon) and choosing the Activate Project command.
3. Choose Build–>run from the menu. You see the terminal window appear, listing the programs
output, plus some superfluous text as shown in following figure -
EEE 122: Structured Programming Laboratory 7
Congratulation !!! You have completed your first program, and you became a
programmer. ”Welcome to Programming world!!!”
A handy way to save everything all at once is to use the Save Everything command. This command
is found on the File menu, and its handy keyboard shortcut is Alt+Shift+S. If you havent yet saved
the project, do so now.
You can also close the current project by choosing File–>Close Project.
8 Programming Examples
In this first lab, be familiar with the environment of C editor, create and run some elementary C
programs as the instructor suggests.
Example: 1
Description: Write a C program that asks the user to enter two integers and find their sum.
Source Code Output
# i n c l u d e < s t d i o . h> Enter two integers: 10 20
i n t main ( void ) { 10 + 20 = 30
i n t a , b , sum ;
printf ( ” E n t e r two i n t e g e r s : ” ) ;
scanf ( ”%d %d” ,&a ,&b ) ;
sum = a + b ;
printf ( ”%d + %d = %d” , a , b , sum ) ;
return 0;
}
EEE 122: Structured Programming Laboratory 8
Example: 2
Write a C program that converts a temperature in Centigrade to its Fahrenheit equivalent. The relation
between Centigrade and Fahrenheit scale is:
C
5 = F−32
9
Source Code Output
Enter a temperature in centigrade:
# i n c lu d e <s t d i o . h> 36.9
i n t main ( void ) { The equivalent temperature in
float c ,f;
printf ( ” \ nEnter a temperature i n c e n t i g r a d e : ” ) ;
Fahrenheit is 98.42.
scanf ( ”%f ” ,&c ) ;
f = 1 . 8 * c +32;
printf ( ” \nThe e q u i v a l e n t temperature i n F a h r e n h e i t i s %.2 f . \ ←-
n” , f ) ;
return 0;
}
9 Practice session
Students will type the following codes in individual file and analyze the output. They will help
students understand beginner level syntax of C programs.
Sl Source Code
Practice 1 # i n c l ud e <s t d i o . h>
i n t main ( void ) {
float c ,f;
printf ( ” \ nEnter a temperature i n c e n t i g r a d e : ” ) ;
scanf ( ”%f ” ,&c ) ;
f = 1 . 8 * c +32;
printf ( ” \nThe e q u i v a l e n t temperature i n F a h r e n h e i t i s %.2 f . \ n” , f ) ;
return 0;
}
10 Lab Assignments
1. Write a C program which prints a line of text ”Welcome to EEE”.
2. The base and height of a triangle are given. Find it’s area.
3. Accept the radius of a circle and calculate the area and perimeter of the circle.
4. Write a C program which will swap two integers. Swapping means exchange values. For
example, If a=10 and b=20; after swapping, a=20 and b=10.
Acknowledgment
First OBE version, prepared by: Second Update , prepared by:
B.K.M. Mizanur Rahman, Nazmul Alam,
Assistant Professor, Part Time Faculty,
Department of EEE, UIU Department of EEE, UIU