Chapter 3: Introduction to C Programming
C development environment
A simple program example
Characters and tokens
Structure of a C program
comment and preprocessor directives
basic data types
data declarations
• Basic functions
C Development Environment
Phase 1 :
Phase 2 :
Phase 3 :
Phase 4 :
Program is
created in the
Editor and
stored on Disk.
processes the
creates object
code and stores
it on Disk.
Linker links object
code with libraries,
creates a.out and
stores it on Disk.
Phase 5 :
Phase 6 :
Loader puts
Program in
CPU takes each
instruction and
executes it, storing
new data values as
the program executes.
From code to executables
Source Code
Assembly Code
Object Code
Executable Code
A Simple Program Example
#include <stdio.h>
printf("Programming in C is easy.\n");
Sample Program Output
Programming in C is easy.
In C, lowercase and uppercase characters are very
important! All commands in C must be lowercase. The C
programs starting point is identified by the word
This informs the computer as to where the program
actually starts. The brackets that follow the keyword main
indicate that there are no arguments supplied to this
program (this will be examined later on).
The two braces, { and }, signify the begin and end
segments of the program.
The purpose of the statement
#include <stdio.h>
is to allow the use of the printf statement to provide
program output. Text to be displayed by printf() must be
enclosed in double quotes. The program has only one
printf("Programming in C is easy.\n");
printf() is actually a function (procedure) in C that is used
for printing variables and text. Where text appears in
double quotes "", it is printed without modification. There
are some exceptions however.
This has to do with the \ and % characters. These
characters are modifiers, and for the present the \ followed
by the n character represents a newline character.
Thus the program prints
Programming in C is easy.
and the cursor is set to the beginning of the next line. As
we shall see later on, what follows the \ character will
determine what is printed, ie, a tab, clear screen, clear line
etc. Another important thing to remember is that all C
statements are terminated by a semi-colon ;
• Summary of major points so far
 program execution begins at main()
 keywords are written in lower-case
 statements are terminated with a semi-colon
 text strings are enclosed in double quotes
Characters and tokens
• Characters are the basic building blocks in C
program, equivalent to ‘letters’ in English
• Includes every printable character on the standard
English language keyboard except `, $ and @
• Example of characters:
Numeric digits: 0 - 9
Lowercase/uppercase letters: a - z and A - Z
Space (blank)
Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
• A token is a language element that can be used in
forming higher level language constructs
• Equivalent to a ‘word’ in English language
• Several types of tokens can be used to build a
higher level C language construct such as
expressions and statements
• There are 6 kinds of tokens in C:
Reserved words (keywords)
String literals
Reserved Words
• Keywords that identify language entities such as
statements, data types, language attributes, etc.
• Have special meaning to the compiler, cannot be
used as identifiers in our program.
• Should be typed in lowercase.
• Example: const, double, int, main, void, while, for,
else (etc..)
• Words used to represent certain program entities
(program variables, function names, etc).
• Example:
– int my_name;
• my_name is an identifier used as a program variable
– void CalculateTotal(int value)
• CalculateTotal is an identifier used as a function name
• Entities that appear in the program code as fixed
• 4 types of constants:
– Integer constants
• Positive or negative whole numbers with no fractional part
• Example:
– const int MAX_NUM = 10;
– const int MIN_NUM = -90;
– Floating-point constants
• Positive or negative decimal numbers with an integer part, a
decimal point and a fractional part
• Example:
– const double VAL = 0.5877e2; (stands for 0.5877 x 102)
– Character constants
• A character enclosed in a single quotation mark
• Example:
– const char letter = ‘n’;
– const char number = ‘1’;
– printf(“%c”, ‘S’);
» Output would be: S
– Enumeration
• Values are given as a list
• Example:
en u m L ang u ag e {
M alay ,
E n g lish ,
A rab ic
String Literals
• A sequence of any number of characters
surrounded by double quotation marks.
• Example:
– “My name is Salman”
• Example of usage in C program:
– printf(“My room number is BN-1-012\n”);
• Output: My room number is BN-1-012
Punctuators (separators)
• Symbols used to separate different parts of the C
• These punctuators include:
– [ ] ( ) { } , ; “: * #
• Usage example:
void m ain (void)
int num = 10;
printf (“% i”, num );
• Tokens that result in some kind of computation or
action when applied to variables or or other
elements in an expression.
• Example of operators:
– *+=-/
• Usage example:
– result = total1 + total2;
Structure of a C program
# in c lu d e < std io .h >
Preprocessor directive (header file)
v o id m a in (v o id )
p rin tf(“\n H e llo W o rld \n ” );
# in clu d e < std io .h >
# d efin e V A L U E 10
in t g lo b al_v ar;
Program statement
Preprocessor directive
Global variable declaration
v o id m ain (v o id )
/* T h is is th e b eg in n in g of th e pro gram * /
in t lo cal_ v ar;
Local variable declaration
lo cal_ v ar = 5 ;
g lo b al_ v ar = lo cal_v ar + V A L U E ;
Variable definition
p rin tf (“T o tal su m is: % d \n ”, g lo b al_ v ar); // P rin t o u t th e resu lt
• Explanations or annotations that are included in a
program for documentation and clarification
• Completely ignored by the compiler during
compilation and have no effect on program
• Starts with ‘/*’ and ends with ‘*/’
• Some compiler support comments starting with
Preprocessor Directives
• The first thing to be checked by the compiler.
• Starts with ‘#’.
• Tell the compiler about specific options that it
needs to be aware of during compilation.
• There are a few compiler directives. But only 2 of
them will be discussed here.
– #include <stdio.h>
• Tell the compiler to include the file stdio.h during compilation
• Anything in the header file is considered a part of the program
– #define VALUE 10
• Tell the compiler to substitute the word VALUE with 10 during
Basic Data Types
• 3 examples of basic data types:
– int (used to declare numeric program variables of
integer type)
– char (used to declare character variable)
– double (used to declare floating point variable)
• In addition, there are float, void, short, long, etc.
• Declaration: specifies the type of a variable.
– Example: int local_var;
• Definition: assigning a value to the declared
– Example: local_var = 5;
• A variable can be declared globally or locally.
• A globally declared variable can be accessed from
all parts of the program.
• A locally declared variable can only be accessed
from inside the function in which the variable is
• A specification of an action to be taken by the
computer as the program executes.
• In the previous example, there are 2 lines
following variable declaration and variable
definition that terminate with semicolon ‘;’.
– global_var = local_var + VALUE;
– printf (“Total sum is: %d\n”, global_var);
• Each line is a statement.
Basic Functions
• A C program consists of one or more functions
that contain a group of statements which perform a
specific task.
• A C program must at least have one function: the
function main.
• We can create our own function or use the
functions that has been created in the library, in
which case we have to include the appropriate
header file (example: stdio.h).
• In this section, we will learn a few functions that
are pre-defined in the header file stdio.h
• These functions are:
– printf()
– scanf()
– getchar() & putchar()
• In addition to those functions, we will also learn
about Format Specifier and Escape Sequence
which are used with printf() and scanf().
• Used to send data to the standard output (usually
the monitor) to be printed according to specific
• General format:
– printf(“control string”, variables);
• Control string is a combination of text, format
specifier and escape sequence.
• Example:
– printf(“Thank you”);
– printf (“Total sum is: %d\n”, global_var);
• %d is a format specifier
• \n is an escape sequence
Format Specifier
Form at Specifier
O utput T ype
Signed decim al integer
Signed decim al integer
U nsigned octal integer
U nsigned decim al integer
U nsigned hexadecim al (sm all letter)
U nsigned hexadecim al (capital letter)
Integer including decim al point
Signed floating point (using e notation)
Signed floating point (using E notation)
T he shorter betw een % f and % e
T he shorter betw een % f and % E
C haracter
O utput E xam ple
7.6000e+ 01
7.6000E + 01
Tells the printf() function the format of the output to be printed put.
Escape Sequence
E scape S equence
E ffect
B eep sound
B ackspace
F orm feed (for printing)
N ew line
C arriage return
T ab
V ertical tab
B ackslash
“ sign
O ctal decim al
H exadecim al
Escape sequence is used in the printf() function to do something to
the output.
• Read data from the standard input device (usually
keyboard) and store it in a variable.
• General format:
– scanf(“Control string”, &variable);
• The general format is pretty much the same as
printf() except that it passes the address of the
variable (notice the & sign) instead of the variable
itself to the second function argument.
• Example:
int age;
printf(“Enter your age: “);
scanf(“% d”, & age);
getchar() and putchar()
• getchar() - read a character from standard input
• putchar() - write a character to standard output
• Example:
#include <stdio.h>
void main(void)
char my_char;
printf(“Please type a character: “);
my_char = getchar();
printf(“\nYou have typed this character: “);

Chapter 3: Introduction to C Programming Language