POP - Module-1 - Chapter 2-Structure of C Program
POP - Module-1 - Chapter 2-Structure of C Program
Nandini P
1
Importance of the Subject
• ‘C is the base language of any other
programming language.
• To be a Good Programmer one must know
fundamentals of C programming language.
2
History of ‘C’
3
In 1970,by Ken Thompson created a language
called as ‘B’. It used to create early version of Unix.
4
Features Of ‘C’
It is a robust language.
General purpose high level language.
Programs written in ‘C’ are efficient and fast.
(Because of variety of data types and powerful operators)
Highly Portable. (related to OS)
Well suited for structured programming.
Ability to extend itself.
Core language
Documentation section
Link Section
Definition Section
Global Declaration Section
main() function section
{
Declaration part
Executable part
}
Subprogram section
Function1
Function2 … ( user defined function)
6
[Comments ]
Pre-processor Directives
[Global Declarations]
[Function Declarations]
main()
{
Declaration Section
Executable Section
}
7
[Comments ]
Pre-processor Directives
[Global Declarations]
[Function Declarations]
main()
{
Declaration Section
Executable Section
}
8
• consists of a set of comment lines giving the short description explaining the
purpose of each statement or set of statements in a program.
• These comments are useful to the programmer to understand the logic of the
program but are ignored by the compiler.
• Any comment must start with two characters
• Block Line Comments /* and end with */ .
• Single line comments are //
• The symbols /* marks the beginning of the comment and */ marks the end of
the comment.
• The symbols /* and */ are called comment delimiters.
• For eg: /* first C program*/
9
• The preprocessor statements start with the symbol # symbol.
• These statements instruct the compiler to include some of the files in the
beginning of the program.
• For example,
• #include<stdio.h>
• #include<math.h> are the files that the compiler includes in the beginning of
the program. Using the preprocessor directive the user can define the constants
also.
• For example,
• #define MAX 1024
• #define PI 3.1417
• Here MAX and PI are called symbolic constants. 10
In C program execution starts from the function main which
can be written as “void main()” or “int main()”
11
• A series of statements that perform a specific action will be enclosed
within braces { and } and present immediately after the program header
• Declaration section
• Execution section
12
• These are the building blocks of the program.
specific task.
13
14
The statement which instructs the compiler to display or print
the string, expression value or variable value to the console
device is called print statement.
15
simple forms of printf () statement:
16
17
The General form of printf is:
printf(“control strings”,arg1,arg2,arg3……..argn);
18
19
20
/*Documentation Section: program to find the sum of
two numbers*/
#include <stdio.h> /* Pre-processor Directives */
int result; /*global declaration section*/
main()
{
int a,b; /*declaration part*/
printf("Enter two numbers for a and b"); /*
executable part starts here*/
scanf("%d%d",&a,&b);
result= a + b;
printf("The result is=%d",result);
}
23
While working with large projects, need to
separate subroutine from the main()
So option for this is make subroutine and
24
25
They are generated by compiler after
processing source file
Contains binary code(.obj file)
Linker uses this object file to produce an
26
Generated by linker which links various object
Files to produce executable file
27
Create a program
Compiling the program
Linking the programs with functions
Executing the program
28
29
30
Process certain kinds of data and provide
output known as information
Task is accomplished by executing
31
C Character set is a collection of characters supported in C
programming language.
C Programming language has a rich set of characters which are used
to construct c program instructions.
Alphabets
C Language supports all alphabets of English. It supports both
UPPERCASE & lowercase letters
Digits
C Language supports 10 digits to construct numbers. Those 10 digits
are 0,1,2,3,4,5,6,7,8,9
Special Symbols
C supports a rich set of special symbols that include symbols to
perform mathematical operations, condition checking, white space,
back space, etc…
32
In C Programming Language, the character set
follow ASCII (American Standard Code for
Information Interchange) code text format.
- Every character in C language has the
33
34
In a passage of text individual words and
punctuations called tokens
In c smallest individual units are known as C
tokens
6 types and programs written using these
tokens
35
36
Keywords :
37
38
- Identifiers refer to the names of variables, functions and arrays
- User-defined names consists of sequence of letters and digits
with letter as first character
- Allow both upper and lowercase as well as underscore
- Rules for Identifiers:-
1. First character must be an alphabet
2. Must consists of only letters, digits or underscore
3. Only first 31 characters are significant
4. Cannot use a keywords
5. Must not contain white space
39
Refers to fixed values that do not change
during execution of program
40
Integer constants:
Integer constant-3 types of integer
1. Decimal(0 through 9,preceded by optional –or
+ sign)
2. Octal(digits from 0 through 7 with leading 0)
3. Hexadecimal(digits preceded by 0x or 0X and
includes alphabets A through F which represent
numbers 10 through 15)
41
• Using integer numbers it is inadequate to represent
quantities like distances, heights, temperatures, prices…etc
• These quantities can be represented by numbers containing
fractional parts like 12.567,Such numbers are called real
constants
• Real numbers also represented in exponential notation by
format:0.6e4,10e-1,-1.4E-1
mantissa e exponent
• Exponent can be written in either plus or minus sign
• Letter ‘e’ separating the mantissa and exponent can either
lowercase or uppercase
• White space is not allowed
42
Single character constants
Contains single character enclosed within a pair of
String Constants
sequence of characters enclosed in double quotes
Example: ”Hello”,”1986”
43
• Data name used to store a data value
• Take different values at different times during execution
• Name should be meaningful to reflect its function
• Conditions:
1. Begin with letter but some systems permit underscore as first
character
2. Recognizes a length of 31 characters but first eight characters
are significant
3. Upper and lowercase are significant
4. It should not be a keyword
5. White space is not allowed
44
45
46
47
48