Anda di halaman 1dari 21

Chapter 2

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

The birth of C and Unix took place in the early


1970s.

Did you know Linux was originally written in C+


+, was then rewritten in C to increase its
execution efficiency?

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.

*Image from http://www.futuretech.vuurwerk.nl/ 4


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 chronicles of C
Late 1960s - Martin Richards designed BCPL.
1970 - Based on BCPL, Ken Thompson designed B (A stripped
down version of BCPL).
1972 - Based on B, C was designed by Dennis Ritchie. He then
published the first book on C with Brian Kernighan. First edition of C
was known as K&R C, after the authors of this book.
1972 to 80s - C gained wide popularity. Many versions of C
surfaced.
1989 - American National Standards Institute (ANSI) established the
X3J11 commitee to standardize C. Standard is defined, known as
ANSI-C, Standard C or C89.
1990 ANSI-C was adopted as an international standard: ISO
9899:1990, a.k.a. C90 (C89 = C90).
1999 - C standard was revised to become ISO 9899:1999, a.k.a.
C99.

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

Brian Kernighan and


Dennis Ritchie published
the first book on C in 1978.

This book became the de


facto standard for C.

Best seller, sold more than


500k copies.

*Image from http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)


6
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.2 Skeleton of a C Program

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

a text editor such as notepad to write the


C source code.
a C compiler to transform the code into an
executable C program.
In this section, we are going to introduce
Knoppix and its GNU C/C++ compiler to
compile C source code into a 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

Standard desktop of Knoppix version 4.0


11
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 write and run C program,


1. Open a text editor called KWrite to write your
C code. KWrite is found in the menu Editors-
>KWrite.
2. Save your C code as filename.c (extension
is .c) in the home directory (click on the
Home icon). You can save it in other
directory, but home directory is the default
directory when you open the Shell prompt to
compile your C code.
12
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
3. To compile your C code (filename.c), click on the
icon with the highlight name Konsole. This will
open a shell. You can check whether filename.c is
in the correct directory by typing ls, which lists all
directories and files in the current directory (default
is home directory).

Location of Konsole (shell)

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

When gcc command is invoked, it normally does


preprocessing, compilation, assembly and linking of
filename.c into an executable file
(executable_name) with extension .exe.
5. If there is no error message after compiling using
gcc command, run the program by typing
./executable_name
14
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
6. If there is error(s) in
C code, there will be
error message(s)
when you compile
your C code. Correct
the error(s) as Error messages when compiling hello.c

indicated in the
message(s) before
compiling the C
code again.

Hello World source code that has errors 15


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
The first error message is
Your program filename Column number Description of the error

hello.c:1:17: stdio: No such file or directory.


Line number

In this case, the error starts from column 17 of line 1.


The C compiler cannot find the file stdio. The correct
file should be stdio.h.

To view line number in KWrite, go to View->Show Line


Numbers or press F11.
16
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
Exercise: Correct
the rest of the
errors indicated.

Error messages when compiling hello.c

Hello World source code that has errors

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

jGRASP Windows/Linux/ Unix http://www.eng.auburn.edu/department/cse/


research/grasp/
Bloodshed Dev-C++ Windows http://www.bloodshed.net/devcpp.html
Turbo C++ Windows http://www.turboexplorer.com/cpp
Microsoft Visual C++ Windows http://www.microsoft.com/express/vc/
2008 Express Edition
Code::Blocks Windows/Linux/ Mac http://www.codeblocks.org/

Anjuta DevStudio GNU/Linux http://www.anjuta.org/

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;
}

2. Tools used when writing and running a C program are:


a text editor to write C code, and
a C compiler to transform the C code into executable program.

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.

4. printf and scanf are functions in stdio.h to display


message within the double quotes on the monitor and
to store data entered by the user respectively.

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)

Anda mungkin juga menyukai