Introduction to C Programming
1
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Objectives
Learn the history of C and its evolvement.
Learn the basics of C programming.
Learn to use C programming tools.
Learning Outcomes
Students will have basic knowledge about the history of
C and its evolvement.
Able to identify basic syntax in a simple C program.
Able to use C programming tools to write, compile and
test a simple C program.
2
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
2.1 History of C
C was invented for the purpose of writing the
Unix system
3
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
The Fathers of C
Two researchers at Bell Telephone Laboratories. Ken
Thompson invented B, a precursor to Dennis Ritchies
C.
5
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
The first C book
7
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
2.3 Tools to Write & Run C Program
8
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Tools to write & run C program: Knoppix
Linux operating system that is based on Debian
and can be booted and run directly from a CD or
DVD.
Has a collection of GNU/Linux software,
automatic hardware detection, and support for
many graphics cards, sound cards, SCSI and
USB devices and other peripherals.
Can be downloaded from http://www.knoppix.org
9
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Tools to write & run C program: Knoppix
To run Knoppix,
1. Adjust the boot list in the BIOS Setup of your
computer to boot from CD/DVD.
2. Insert Knoppix CD/DVD into the computer,
and reboot.
3. At the boot: prompt, type knoppix lang=en
to start Knoppix in English.
4. You will be brought to a standard desktop.
10
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Tools to write & run C program: Knoppix
13
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Tools to write & run C program: Knoppix
4. In the shell prompt, type
gcc g filename.c o executable_name
Enable debugging information Place output in executable_name
indicated in the
message(s) before
compiling the C
code again.
17
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Tools to write & run C program
Most contemporary versions of C tools include a text
editor in the development environment a.k.a integrated
development environment (IDE).
List of IDEs for C
IDE Platform Website
18
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
2.4 Example with C Code
#include <stdio.h>
int main( ){
/* Declare three variables (x, y and sum) to hold integer data (int) */
int x, y, sum;
//Display message "Please enter 2 integers: " on monitor
printf ("Please enter 2 integers: ");
//Store integers into variables x and y respectively
scanf("%d %d", &x, &y);
//make variable sum to hold value of x + y
sum = x+y;
//Display value of x + y that is stored in variable sum
printf ("Sum of x and y value is %d", sum);
//End your program here
return 0;
}
Sample output (Bold indicates users input)
Please enter 2 integers: 2 3
Sum of x and y value is 5
19
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
2.5 Summary
1. Every C program consists of the following skeleton:
#include <stdio.h>
int main( )
{
//body of main( )
return 0;
}
20
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)
Summary
3. To let C compiler bypass any comments, one can use
either // for single line of comment or /* comments
*/ for comments that require more than one line.
21
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)