SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
Structure Of a C Program
David Livingston J,
davidjlivingston@gmail.com &
Staff members of
Department of CE/IT, M.S.P.V.L PC
Structure of a C program
#include <stdio.h>
void main (void)
{
printf(“nHello Worldn”);
}
Preprocessor directive (header file)
Program statement
#include <stdio.h>
#define VALUE 10
int global_var;
void main (void)
{
/* This is the beginning of the program */
int local_var;
local_var = 5;
global_var = local_var + VALUE;
printf (“Total sum is: %dn”, global_var); // Print out the result
}
} Preprocessor
directive
Global variable declaration
Comments
Local variable declaration
Variable definition
Preprocessor Directives
• The first statement to be checked by the compiler
• Preprocessor Directives always preceded with ‘#’ sign
• They contain information to the compiler which are required by
the compiler during compilation.
• There are a few compiler directives. But only 2 of them will be
discussed here.
– #include <stdio.h>
• Tells the compiler to include the file stdio.h during compilation
• Anything in the header file will be included a part of the program
– #define VALUE 10
• Tells the compiler to substitute the word VALUE with 10 during compilation
Preprocessor Directives
#define PI 3.141592654
main() {
…..
perimeter = 2*PI*radius;
area = PI*radius*radius;
…...
}
main() {
…..
perimeter = 2* 3.141592654 *radius;
area = 3.141592654 *radius*radius;
…...
}
The result of the compilation is the
same for both C program (One with
#define and the other without it).
Which one is preferred (less typing)?
Which one is more readable?
The one with constant definition using
#define preprocessor directive.
Before compilation, the pre-processor
will replace all PI with 3.141592654.
Comments
• Comment means explanations or annotations that are
included in a program for documentation and
clarification purpose.
• Comments are completely ignored by the compiler
during compilation and have no effect on program
execution.
• Comments starts with ‘/*’ and ends with ‘*/’
• Some compiler support comments starting with ‘//’
Basic Data Types
• There are 3 Basic data types in C:
– 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.
• Variables are declared before they are used in a program.
Declaration specifies the type of a variable.
– Example: int local_var;
• Once defined variables are used for storing a value.
Variable
• A variable can be declared globally or locally.
• A globally declared variable can be accessed from
any part of the program.
• A locally declared variable can only be accessed
from inside the function in which the variable is
declared.
Statements
• A specification of an action to be taken by the
computer as the program executes is called a
Statement.
• In the previous example, there are 2 lines following
variable declaration that terminate with semicolon ‘;’
are statements:
global_var = local_var + VALUE;
printf (“Total sum is: %dn”, global_var);
• Each line is a statement that end with a semicolon is a
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 declared in C library (called Predefined function).
• In order to use Predefined functions, 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().
printf()
• Used to send data to the standard output (usually the
monitor) to be printed according to specific format.
• 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: %dn”, global_var);
• %d is a format Specifier
• n is an escape sequence
Format Specifier
No Format Specifier Output Type Output Example
1 %d Signed decimal integer 76
2 %i Signed decimal integer 76
3 %o Unsigned octal integer 134
4 %u Unsigned decimal integer 76
5 %x Unsigned hexadecimal (small letter) 9c
6 %X Unsigned hexadecimal (capital letter) 9C
7 %f Integer including decimal point 76.0000
8 %e Signed floating point (using e notation) 7.6000e+01
9 %E Signed floating point (using E notation) 7.6000E+01
10 %g The shorter between %f and %e 76
11 %G The shorter between %f and %E 76
12 %x Character ‘7’
13 %s String ‘76'
Tells the printf() function the format of the output to be printed put.
Escape Sequence
Escape Sequence Effect
a Beep sound
b Backspace
f Formfeed (for printing)
n New line
r Carriage return
t Tab
v Vertical tab
 Backslash
” “ sign
o Octal decimal
x Hexadecimal
O NULL
Escape sequence is used in the printf() function to do something to
the output.
scanf()
• Reads data from the standard input device (usually keyboard)
and store it in a variable. The General format is:
– 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() - reads a character from standard input
• putchar() - writes 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: “);
putchar(my_char);
}
The End
Thank U

More Related Content

What's hot (20)

PPT
Operators in C Programming
programming9
 
PPT
RECURSION IN C
v_jk
 
PPT
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
PPTX
Data types in C
Tarun Sharma
 
PPTX
User defined functions in C
Harendra Singh
 
PPTX
Presentation on Function in C Programming
Shuvongkor Barman
 
PPTX
Functions in C
Kamal Acharya
 
PPT
Introduction to Basic C programming 01
Wingston
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
Unit 3. Input and Output
Ashim Lamichhane
 
DOC
Notes of c programming 1st unit BCA I SEM
Mansi Tyagi
 
PPT
File handling in c
David Livingston J
 
PPTX
File in C language
Manash Kumar Mondal
 
PPTX
Control statements in c
Sathish Narayanan
 
PPT
Basics of C programming
avikdhupar
 
PPT
Basics of c++ Programming Language
Ahmad Idrees
 
PPTX
Functions in c language
tanmaymodi4
 
PPTX
Function in c program
umesh patil
 
PPTX
Strings in C language
P M Patil
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Operators in C Programming
programming9
 
RECURSION IN C
v_jk
 
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
Data types in C
Tarun Sharma
 
User defined functions in C
Harendra Singh
 
Presentation on Function in C Programming
Shuvongkor Barman
 
Functions in C
Kamal Acharya
 
Introduction to Basic C programming 01
Wingston
 
Strings in C
Kamal Acharya
 
Unit 3. Input and Output
Ashim Lamichhane
 
Notes of c programming 1st unit BCA I SEM
Mansi Tyagi
 
File handling in c
David Livingston J
 
File in C language
Manash Kumar Mondal
 
Control statements in c
Sathish Narayanan
 
Basics of C programming
avikdhupar
 
Basics of c++ Programming Language
Ahmad Idrees
 
Functions in c language
tanmaymodi4
 
Function in c program
umesh patil
 
Strings in C language
P M Patil
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 

Viewers also liked (8)

PPT
Structure c
thirumalaikumar3
 
PPT
Structure in C
Fazle Rabbi Ador
 
PPTX
Arrays in C language
Shubham Sharma
 
PPTX
Array in c language
home
 
PPT
String c
thirumalaikumar3
 
DOC
String in c
Suneel Dogra
 
Structure c
thirumalaikumar3
 
Structure in C
Fazle Rabbi Ador
 
Arrays in C language
Shubham Sharma
 
Array in c language
home
 
String in c
Suneel Dogra
 
Ad

Similar to Structure of a C program (20)

PDF
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
PPTX
Basics of c Nisarg Patel
TechNGyan
 
PPTX
Introduction to C Programming language Chapter02.pptx
foxel54542
 
PPSX
C basics 4 std11(GujBoard)
indrasir
 
PPTX
4 Introduction to C.pptxSSSSSSSSSSSSSSSS
EG20910848921ISAACDU
 
PPT
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
PPTX
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
PPTX
c_pro_introduction.pptx
RohitRaj744272
 
DOCX
fds unit1.docx
AzhagesvaranTamilsel
 
PPT
Chapter3
Kamran
 
PPT
270_1_ChapterIntro_Up_To_Functions (1).ppt
GayathriShiva4
 
PPT
Survey of programming language getting started in C
ummeafruz
 
PPT
270_1_CIntro_Up_To_Functions.ppt
Alefya1
 
PPT
270_1_CIntro_Up_To_Functions.ppt
UdhayaKumar175069
 
PPT
270 1 c_intro_up_to_functions
ray143eddie
 
PPTX
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
PPT
CIntro_Up_To_Functions.ppt;uoooooooooooooooooooo
muhammedcti23240202
 
PPT
270_1_CIntro_Up_To_Functions.ppt
JoshCasas1
 
PPT
Chap 1 and 2
Selva Arrunaa Mathavan
 
Module 1_Chapter 2_PPT (1)sasaddsdsds.pdf
anilcsbs
 
Basics of c Nisarg Patel
TechNGyan
 
Introduction to C Programming language Chapter02.pptx
foxel54542
 
C basics 4 std11(GujBoard)
indrasir
 
4 Introduction to C.pptxSSSSSSSSSSSSSSSS
EG20910848921ISAACDU
 
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
Unit No 2.pptx Basic s of C Programming
Vaibhav Parjane
 
c_pro_introduction.pptx
RohitRaj744272
 
fds unit1.docx
AzhagesvaranTamilsel
 
Chapter3
Kamran
 
270_1_ChapterIntro_Up_To_Functions (1).ppt
GayathriShiva4
 
Survey of programming language getting started in C
ummeafruz
 
270_1_CIntro_Up_To_Functions.ppt
Alefya1
 
270_1_CIntro_Up_To_Functions.ppt
UdhayaKumar175069
 
270 1 c_intro_up_to_functions
ray143eddie
 
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
CIntro_Up_To_Functions.ppt;uoooooooooooooooooooo
muhammedcti23240202
 
270_1_CIntro_Up_To_Functions.ppt
JoshCasas1
 
Ad

More from David Livingston J (8)

PPTX
Performing Addition and Subtraction on Integers
David Livingston J
 
PPT
Introduction to Bluetooth technology
David Livingston J
 
PPT
Wireless LAN Technoloy
David Livingston J
 
PPT
Past, Present and Future of Mobile Computing
David Livingston J
 
PPT
Introduction & history of mobile computing
David Livingston J
 
PPT
Frequently asked questions in c
David Livingston J
 
PPT
Frequently asked questions in c
David Livingston J
 
PPT
Problem solving using Computer
David Livingston J
 
Performing Addition and Subtraction on Integers
David Livingston J
 
Introduction to Bluetooth technology
David Livingston J
 
Wireless LAN Technoloy
David Livingston J
 
Past, Present and Future of Mobile Computing
David Livingston J
 
Introduction & history of mobile computing
David Livingston J
 
Frequently asked questions in c
David Livingston J
 
Frequently asked questions in c
David Livingston J
 
Problem solving using Computer
David Livingston J
 

Recently uploaded (20)

PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 

Structure of a C program

  • 1. Structure Of a C Program David Livingston J, [email protected] & Staff members of Department of CE/IT, M.S.P.V.L PC
  • 2. Structure of a C program #include <stdio.h> void main (void) { printf(“nHello Worldn”); } Preprocessor directive (header file) Program statement #include <stdio.h> #define VALUE 10 int global_var; void main (void) { /* This is the beginning of the program */ int local_var; local_var = 5; global_var = local_var + VALUE; printf (“Total sum is: %dn”, global_var); // Print out the result } } Preprocessor directive Global variable declaration Comments Local variable declaration Variable definition
  • 3. Preprocessor Directives • The first statement to be checked by the compiler • Preprocessor Directives always preceded with ‘#’ sign • They contain information to the compiler which are required by the compiler during compilation. • There are a few compiler directives. But only 2 of them will be discussed here. – #include <stdio.h> • Tells the compiler to include the file stdio.h during compilation • Anything in the header file will be included a part of the program – #define VALUE 10 • Tells the compiler to substitute the word VALUE with 10 during compilation
  • 4. Preprocessor Directives #define PI 3.141592654 main() { ….. perimeter = 2*PI*radius; area = PI*radius*radius; …... } main() { ….. perimeter = 2* 3.141592654 *radius; area = 3.141592654 *radius*radius; …... } The result of the compilation is the same for both C program (One with #define and the other without it). Which one is preferred (less typing)? Which one is more readable? The one with constant definition using #define preprocessor directive. Before compilation, the pre-processor will replace all PI with 3.141592654.
  • 5. Comments • Comment means explanations or annotations that are included in a program for documentation and clarification purpose. • Comments are completely ignored by the compiler during compilation and have no effect on program execution. • Comments starts with ‘/*’ and ends with ‘*/’ • Some compiler support comments starting with ‘//’
  • 6. Basic Data Types • There are 3 Basic data types in C: – 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. • Variables are declared before they are used in a program. Declaration specifies the type of a variable. – Example: int local_var; • Once defined variables are used for storing a value.
  • 7. Variable • A variable can be declared globally or locally. • A globally declared variable can be accessed from any part of the program. • A locally declared variable can only be accessed from inside the function in which the variable is declared.
  • 8. Statements • A specification of an action to be taken by the computer as the program executes is called a Statement. • In the previous example, there are 2 lines following variable declaration that terminate with semicolon ‘;’ are statements: global_var = local_var + VALUE; printf (“Total sum is: %dn”, global_var); • Each line is a statement that end with a semicolon is a
  • 9. 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 declared in C library (called Predefined function). • In order to use Predefined functions, we have to include the appropriate header file (example: stdio.h).
  • 10. • 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().
  • 11. printf() • Used to send data to the standard output (usually the monitor) to be printed according to specific format. • 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: %dn”, global_var); • %d is a format Specifier • n is an escape sequence
  • 12. Format Specifier No Format Specifier Output Type Output Example 1 %d Signed decimal integer 76 2 %i Signed decimal integer 76 3 %o Unsigned octal integer 134 4 %u Unsigned decimal integer 76 5 %x Unsigned hexadecimal (small letter) 9c 6 %X Unsigned hexadecimal (capital letter) 9C 7 %f Integer including decimal point 76.0000 8 %e Signed floating point (using e notation) 7.6000e+01 9 %E Signed floating point (using E notation) 7.6000E+01 10 %g The shorter between %f and %e 76 11 %G The shorter between %f and %E 76 12 %x Character ‘7’ 13 %s String ‘76' Tells the printf() function the format of the output to be printed put.
  • 13. Escape Sequence Escape Sequence Effect a Beep sound b Backspace f Formfeed (for printing) n New line r Carriage return t Tab v Vertical tab Backslash ” “ sign o Octal decimal x Hexadecimal O NULL Escape sequence is used in the printf() function to do something to the output.
  • 14. scanf() • Reads data from the standard input device (usually keyboard) and store it in a variable. The General format is: – 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);
  • 15. getchar() and putchar() • getchar() - reads a character from standard input • putchar() - writes 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: “); putchar(my_char); }