ICS 103: Computer Programming in C
Handout-2 Topic: Overview of C.
O!ective:
To know about C Language Elements.
To know about general format and syntax of simple C program.
To know about variable declaration / identifiers and reserved key words in C.
C "#$%&#%' '"'('$TS:
Preprocessor Directives:
#include Directive for Defining identifiers from Standard Libraries :
Syntax :
#include<standard header file >
e.g
#include <stdio.h>
#include<math.h>
#define Directive for creating constant :
Syntax :
#define NAME value
e.g
#define PI 3.14153
#define MI!E"#PE$#%M &.'(13)
#define MA*#!EN+,- 1&&
.rom this each identifier NAME is re/laced 01 the constant value and this value 2ill 0e remainin3
fi4ed 5constant6 throu3hout the 2hole /ro3ram.
Example:
C program to find addition of two numbers is:
/ !o find sum of two numbers /
#include <stdio.h> // preprocessor directive
int main 5void6 // main function
7
int num0er18 num0er(8 sum9 // variable declarations
/rintf5:Please in/ut t2o num0ers ; :69 // to display message
scanf5:<d <d:8=num0er18 =num0er(69 // to read 2 numbers
sum>num0er1?num0er(9 // to calculate sum
/rintf5:,he sum of t2o num0ers is ; <d : 8sum69 // to print sum
Pa3e 1 of 3
return &9
@ // end of main
Overview of C Program:
!"e general format of any C program is :
/re/rocessor directives
int main5void 6
7
varia0le declarations9
statements9
@
Complete overview of an C Program is explained in following example :
/ Displays t"e user#s nic$name and t"e current year in a welcoming message. /
#include <stdio.h> /* printf, scanf definitions */
int main5void6 // main function
7
char letter#18 letter#(8 letter#3 8 letter#49 /* four letters */
int 1ear9 /* current year */
/rintf5:Enter a 4Aletter nicBname and /ress return ; :69
scanf5:<c <c <c <c:8 =letter#18 =letter#(8 =letter#38 =letter#469
// to read 4 letters from keyboard
/rintf5:Enter the current 1ear and /ress return ;:69
scanf5:<d:8 =1ear69 // to read year from keyboard
/rintf5:Celcome8 <c<c<c<c . <d is a 3reat 1ear to stud1 DEFn:8 letter#18 letter#(8
letter#38letter#48 1ear69
return 5&69
@ // end of main
Sample %utput:
Enter a 4Aletter nicBname and /ress return ; Gmar
Enter the current 1ear and /ress return ; (&&)
Celcome8 Gmar. (&&) is a 3reat 1ear to stud1 DE
)e*erved +e, -ord*:
All reserved Be1 2ords in D lan3ua3e a//ear in lo2er case 9 the1 have s/ecial meanin3 in D and cannot
0e used for an1 other /ur/oses at an1 /lace .
In our /revious /ro3rams reserved Be1 2ords are ; int 8 char 8 void 8 dou0le 8 return etc.
A com/lete list of reserved Be1 2ords is /resent in 1our te4t 0ooB at A//endi4 + .
Pa3e ( of 3
.aria/e dec/aration* in C Program* :
,he memor1 cells used for storin3 a /ro3ramHs in/ut data and its com/utational results are called
varia0les 0ecause the values stored in varia0les ma1 chan3e 5 and usuall1 do 6 as the /ro3ram e4ecutes .
Iaria0le names are also Bno2n as identifiers.
Syntax for variable Declaration :
int varia0le#list 9
dou0le varia0le#list 9
char varia0le#list 9
float varia0le#list 9
e.g
int count8 lar3e 9
dou0le 48 18 J9
char first#initial8 second#initial 9
float a8 08 c 9
)u/e* for (a0ing an &*er 1efined Identifier2 .aria/e name :
An identifier name can not 0e3in 2ith a di3it.
An identifier name must consist onl1 of letters8 di3its8 or underscores.
A D reserved Be1 2ord 5e.3 return 8 dou0le 8 int etc6 cannot 0e used as an identifier name.
An identifier defined in a D standard li0rar1 5 e.3 /rintf 8 scanf 6 should not 0e redefined.
&or example : 4 name8 int8 t2oA01Afour8 scanf are invalid identifiers name .
&pperca*e and "owerca*e "etter* :
In D K !an3ua3e there is 0i3 difference 0et2een lo2ercase and u//ercase letters .
e.3 int num 9 and int NLM 9 0oth are different .
"i*t of Some )e*erved +e, -ord* and Identifier* :
'eserved (ey )ords : int 8 void 8 dou0le 8 char 8 return 8 etc.
*dentifiers from Standard Libraries + Standard *dentifiers , : /rintf8 scanf.
-[Link] *dentifiers : PI8 radius8 area8 letter#18 circum8 1ear8 main etc.
Pa3e 3 of 3