Course
CSE101R01 – Problem Solving & Programming in C
Topic
Structure of C Program
Dr. B. Srinivasan
Assistant Professor-III, School of Computing
SASTRA Deemed To Be University
Simple C Program
Problem: Sum of Two Numbers
Input: Two numbers
Output: A number which is the sum of two input numbers.
Assumption: The two numbers are +ve or –ve whole numbers.
Number 1
Number 2 Sum Sum of Number 1 and Number 2
2
Structure of C Program
• Structure of a C Program
3
Simple C Program – Sum of Two Numbers
• The statements proceed with “//” are called as
“Documentation” or “Comments”
• These statements will be ignored during
compilation and execution.
• Means, these statements help only for the
programmer to describe the purpose of the steps
and understanding the flow of the program
4
Simple C Program – Sum of Two Numbers - Output
5
Simple C Program – Step-by-Step
• “stdio.h” is a type of header file.
• It contains the definition of some
functions used for INPUT/OUTPUT
purpose like “printf” and “scanf”.
• If we are using these functions in our
program, we need to include this file
using this syntax.
• The content of “stdio.h” file will be
inserted at this place during pre-
processing step which is the part of
compilation.
6
Simple C Program – Step-by-Step
• These sections are optional for simple
programs.
• So, these sections will be discussed later,
whenever required.
• For simple programs, we can avoid these.
7
Simple C Program
int main()
• “int main()” is STARTING POINT for any C
program.
• The system will starts execution from “int
main()”
• The curly braces “{ }” indicates a BLOCK of
statements.
• The “main()” function must have a set of
statements should be written with in the curly
braces.
8
Simple C Program
Variables Declaration
• Variables – Storage for storing data.
• Here, a, b and c are variables
• int – data type – In programming languages,
any data must belongs to any of the standard
data type of the corresponding programming
language.
• In C language, “int” is “integer” type. In
human language, an integer refers to a
whole number, which can be positive,
negative, or zero.
• Whatever the variables used in our program
must be declared using this syntax.
• The purpose of declaration is to allocate
memory for storing data.
9
Simple C Program
• The declaration statements usually known as
compile time statements.
• It just creates memory during compile time
• Rest of these statements are known as executable
statements.
• “int main()” is the starting point for the execution.
• During execution, the system starts executing
statements one-by-one as follows.
10
Simple C Program
Output Function
Programmer User
OR
Program
printf
Input Function
User Programmer
OR
Program
scanf
11
Simple C Program
Input from User
• “printf” – Function used to convey messages
to the user from our program.
• “scanf” – Obtain input from user to our
program
• PRINTF: PROGRAM TO USER
• SCANF: USER TO PROGRAM
• “%d” refers to “Format Specifier”
• For accepting “int” type of values, we should
use “%d”.
• Other format specifiers (some) are:
• “%f” for float
• “%lf” for double
• “%c” for character
• “%s” for string
12
Step-by-Step Execution
• 1st “printf” statement will print the message
represented within double quote on the screen
for the user.
• During “scanf”, the system will be waiting for
the user response. Because, the scanf expecting
a integer value from the user. So the cursor will
be blinking for user response.
13
Step-by-Step Execution
• Once the user given a number, for example,
“25” followed by “Enter” key, this “25” number
will be stored to the variable “a”
• 2nd “printf” statement will print the message
represented within double quote on the screen
for the user.
• During this 2nd “scanf”, the system will be
waiting again for the user response.
14
Step-by-Step Execution
• Once the user given the 2nd number, for
example, “52” followed by “Enter” key, this “52”
number will be stored to the variable “b”
• During this expression, the content of “a” and
“b” will be added and the answer will be
assigned to the variable “c”
15
Step-by-Step Execution
• This “printf” statement will print the result as a
statement in the screen.
• “%d” refers to “Format Specifier”. In place of the
“%d”, the corresponding variables’ (specified after
the message, here a, b and c) content will be
printed.
• The statement “return 0” is the default statement in
all C programs.
• When this statement is executed, the program
terminates and the control return to the caller with
the value 0.
16 • Means, simply program execution will be ended.
Summary
“int main()” is the starting point for the program
The input, output and logical statements should be written within “main()” block.
In “main()” block, we may have 4 sections,
1. Declaration statement,
2. Input statements,
3. Process/Logical statements,
4. Output Statements
The declaration of variables must be at the beginning of the “main()” function.
The necessary header files must be included using “#include” statement, at the start
of the program file. i.e. before main().
File must be saved with the extension “.c” along with the file name.
Example: “prgSum.c”
Thank You
18