SlideShare a Scribd company logo
Mr. R.D.SIVAKUMAR, M.Sc.,M.Phil.,M.Tech.,
Assistant Professor of Computer Science &
Assistant Professor and Head, Department of M.Com.(CA),
Ayya Nadar Janaki Ammal College,
Sivakasi – 626 124.
Mobile: 099440-42243
e-mail : sivamsccsit@gmail.com
website: www.rdsivakumar.blogspot.in
A Sample C Program
A Sample C Program
A program is defined as a set of instructions to be executed sequentially to obtain the
desired result. A program cannot be written without a function. A function may be pre-
defined or user defined.
We will see the first program in C. The C program code is given below:
#include <stdio.h>
main()
{
printf(“Hello World”);
}
This program, upon execution, will display the message Hello World on the screen.
There must be a function defined as main().
The main() function is a user-defined one. The user has to define the main() function
to provide necessary code. When a C program runs, the control is transferred to this
function. This is called the program’s entry point. The program has two functions, one is
the user-defined main() function which is the entry point and the other one is printf()
function which is pre-defined and used to display the results on the standard
output(screen or monitor)
A Sample C Program
Simple parentheses () are used to represent a function. Here main() is the calling
function and printf() is the called function. The “Hello World” program with necessary
explanations is given below.
The first line in the program #include <stdio.h> is a preprocessor statement. #include
is a preprocessor directive. The preprocessor is a software program that will expand the
source code while the program is compiled.
The #include <stdio.h> statement includes the contents of the stdio.h file (standard
input and output header file) globally, that is, prior to the main() function. The contents
of the stdio.h file are the function declaration statements of the pre-defined output and
input functions like printf() and scanf() etc.
A Sample C Program
The declarations of the functions printf() and scanf() are as follows:
int printf(char *, …);
int scanf(char *, …);
The ellipses (…) represent that the above two functions can take variable number of
parameters. But the first parameter is always a string. A parameter is a data or
information passed on to the called function. Zero or more number of such parameters
are passed to any function.
They are given one after another within the brackets, which come after the name of
the function. In the program shown above, the printf() assumes only one parameter
which is a string to be displayed on the monitor. The first parameter type (string type) is
char * which will be read as “character pointer” and will be discussed later.
A Sample C Program
The C compiler is able to recognize the pre-defined functions only because of
their declarations in the appropriate header files. The following program
illustrates the use of header files. The program while it is being executed clears
the contents of the screen before displaying hello on the monitor.
The function clrscr() is a pre-defined one whose prototype (declaration) is available
in conio.h file and hence it has been included. If the statement #include <conio.h> is
not included, the C compiler expects the definition of clrscr() function from the
programmer and if the definition is not provided, it reports an error
Statements
Each and every line of a C program can be considered as a statement. There are
generally four types of statements. They are:
Preprocessor statement
• Function header statement
• Declaration statement
• Executable statement
As you know already, the preprocessor statement is used to expand the source
code by including the function declaration statements from the specified header
files. The function header statement is used as a first line in the definition of a
function. The declaration statements are further classified into variable
declaration statements and function declaration statements.
Assignment Statements
The assignment statement has the following form:
variable = expression
 Its purpose is saving the result of the expression to the right of the assignment
operator to the variable on the left. Here are some rules:
The expression is evaluated first with the rules discussed in the single mode or
the mixed mode expressions pages.
If the type of the expression is identical to that of the variable, the result is saved in
the variable.
Otherwise, the result is converted to the type of the variable and saved there.
If the type of the variable is INTEGER while the type of the result is REAL,
the fractional part, including the decimal point, is removed making it an integer
result.
If the type of the variable is REAL while the type of the result is INTEGER,
then
 a decimal point is appended to the integer making it a real number.
Once the variable receives a new value, the original one disappears and is no more
available. CHARACTER assignment follows the rules stated in the discussion of
the PARAMETER attribute.
Increment and Decrement Statements
Increment operators are used to increase the value of the variable by one and
decrement operators are used to decrease the value of the variable by one in C
programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : - – i ; i – - ;
Expression
An expression occurs usually on the right hand side of an assignment statement.
It has a value when it is evaluated. There are many forms of expressions and some
of them are shown below:
int a,b,c; variable declaration statement
a = 10;
On the right hand side, a constant value is used and hence it is a constant
expression whose value is 10.
b = a;
A variable expression is used here whose value is 10. The right hand side of this
assignment statement has been associated with two values: a variable’s value and
an expression value. In this case both values are same.
But always remember that the expression value is assigned to the left hand side
variable. The expressions can be named based on the operators used. The other
expressions are:
right hand side
c = a+b; arithmetic expression
c = a > b; relational expression
f = d = e; assignment expression
Postfix and Prefix Increment Expression
++ and -- operator as prefix and postfix If you use ++ operator as prefix like:
++var; then, the value of operand is increased by 1 then, only it is returned but,
if you use ++ as postfix like: var++; then, the value of operand is returned first
then, only it is increased by 1
Input and Output Statements
Input : In any programming language input means to feed some data into program.
This can be given in the form of file or from command line. C programming
language provides a set of built-in functions to read given input and feed it to the
program as per requirement.
Output : In any programming language output means to display some data on
screen, printer or in any file. C programming language provides a set of built-in
functions to output required data.
Input and Output Statements
As we have seen already, the function printf() is used to display the results on the
standard output (screen). We have seen the use of printf() to display the string on
the monitor. Actually, the first parameter of the printf() function is a string which is
used to control the output and hence it can be called as “control string”. This
parameter is used to format the output for display and hence we can also call it as a
“formatting string”.
Example:
To print a value of an integer:
int n; /* variable n is declared as integer*/
n = 10;
printf(“%d”, n);
In the above example, ‘%d’ is used as a formatting character within the control
string of printf() function to display the value of an integer. The control string of
printf() function can take three types of characters.
• Ordinary characters
• Formatting characters
• Escape sequence characters
Input and Output Statements
Ordinary characters within the control string are displayed as such. In the case of
printf(“hello”); statement, the control string has ordinary characters only. They are
displayed as such on the screen. Table 4.4 lists the formatting characters used to
display the values of various types.
As seen already, the escape sequence characters are represented by a backslash
followed by another character. They are in fact single character constants only. They
are stored and manipulated as a single character. Escape sequences allow partial
control over the format of the output. The frequently used escape sequences in the
control string of printf() function are:
User defined functions
A function that is declare, calling and define by the user is called user
define function. Every user define function has three parts as:
Prototype or Declaration
Calling
Definition
Thank you..!!

More Related Content

PPT
Structure of a C program
PPTX
Overview of C Mrs Sowmya Jyothi
PDF
C language for Semester Exams for Engineers
PPSX
Complete C programming Language Course
PPTX
Basic c programming and explanation PPT1
PPTX
C programming
PDF
Learning the C Language
PDF
cp Module4(1)
Structure of a C program
Overview of C Mrs Sowmya Jyothi
C language for Semester Exams for Engineers
Complete C programming Language Course
Basic c programming and explanation PPT1
C programming
Learning the C Language
cp Module4(1)

What's hot (20)

PDF
C Programming
PDF
Cp module 2
PPT
Introduction to Basic C programming 01
PDF
User_Defined_Functions_ppt_slideshare.
DOC
PDF
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
PPTX
C Programming Unit-1
PPT
Fundamental of C Programming Language and Basic Input/Output Function
PPTX
structure of a c program
ODP
OpenGurukul : Language : C Programming
PPT
C the basic concepts
PPTX
Chap 2 structure of c programming dti2143
PPTX
Control Structures in C
PPTX
Function C programming
PDF
CP Handout#2
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
PPTX
Programming in C Presentation upto FILE
PDF
Casa lab manual
PDF
Unit ii chapter 2 Decision making and Branching in C
C Programming
Cp module 2
Introduction to Basic C programming 01
User_Defined_Functions_ppt_slideshare.
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
C Programming Unit-1
Fundamental of C Programming Language and Basic Input/Output Function
structure of a c program
OpenGurukul : Language : C Programming
C the basic concepts
Chap 2 structure of c programming dti2143
Control Structures in C
Function C programming
CP Handout#2
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Programming in C Presentation upto FILE
Casa lab manual
Unit ii chapter 2 Decision making and Branching in C
Ad

Similar to Sample for Simple C Program - R.D.Sivakumar (20)

PPTX
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
PPTX
C Programming - Basics of c -history of c
PPTX
PPTX
C programming slide c02
PPTX
C lang7age programming powerpoint presentation
PPT
c-programming
PDF
Unit 2 introduction to c programming
PDF
C pdf
PDF
Rr
PDF
Introduction to the c programming language (amazing and easy book for beginners)
PPT
PDF
EC2311-Data Structures and C Programming
PPTX
Programming Fundamentals lecture 5
DOCX
UNIT-II CP DOC.docx
PPTX
Unit 2- Module 2.pptx
PPTX
PPTX
C_Programming_Language_tutorial__Autosaved_.pptx
PPTX
What is c
PPT
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
PPTX
Unit No 2.pptx Basic s of C Programming
UNIT 5 C PROGRAMMING, PROGRAM STRUCTURE
C Programming - Basics of c -history of c
C programming slide c02
C lang7age programming powerpoint presentation
c-programming
Unit 2 introduction to c programming
C pdf
Rr
Introduction to the c programming language (amazing and easy book for beginners)
EC2311-Data Structures and C Programming
Programming Fundamentals lecture 5
UNIT-II CP DOC.docx
Unit 2- Module 2.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
What is c
slidenotesece246jun2012-140803084954-phpapp01 (1).ppt
Unit No 2.pptx Basic s of C Programming
Ad

More from Sivakumar R D . (20)

PPTX
Internet Connections and Its Protocols - R D Sivakumar
PPT
Internet - R D Sivakumar.
PPTX
Data Communication - R D Sivakumar
PPTX
NETWORK SERVICES - R D Sivakumar
PPTX
Computer Communications - R D Sivakumar
PPTX
Online Data Protection - R D Sivakumar
PPT
Software Engineering - R.D.Sivakumar
PPT
Different Kinds of Internet Protocols - R.D.Sivakumar
PPT
Internet - R.D.Sivakumar
PPT
Electronic Publishing Tools for E-Learning - R.D.Sivakumar
PPT
E-learning Packages - R.D.Sivakumar
PPT
Digital Communication - R.D.Sivakumar
PPT
Digigogy in Teaching - R.D.Sivakumar
PPT
Cyber Commerce Technology - R.D.Sivakumar
PPT
Video Lesson Creation - R.D.Sivakumar
PPT
Cognitive and Personal Dimensions of Cyber Learning - R.D.Sivakumar
PPT
Innovative Presentation - R.D.Sivakumar
PPT
Open Source in E-Learning - R.D.Sivakumar
PPTX
Tuxpaint - R.D.Sivakumar
PPT
Academic Blog Design - R.D.Sivakumar
Internet Connections and Its Protocols - R D Sivakumar
Internet - R D Sivakumar.
Data Communication - R D Sivakumar
NETWORK SERVICES - R D Sivakumar
Computer Communications - R D Sivakumar
Online Data Protection - R D Sivakumar
Software Engineering - R.D.Sivakumar
Different Kinds of Internet Protocols - R.D.Sivakumar
Internet - R.D.Sivakumar
Electronic Publishing Tools for E-Learning - R.D.Sivakumar
E-learning Packages - R.D.Sivakumar
Digital Communication - R.D.Sivakumar
Digigogy in Teaching - R.D.Sivakumar
Cyber Commerce Technology - R.D.Sivakumar
Video Lesson Creation - R.D.Sivakumar
Cognitive and Personal Dimensions of Cyber Learning - R.D.Sivakumar
Innovative Presentation - R.D.Sivakumar
Open Source in E-Learning - R.D.Sivakumar
Tuxpaint - R.D.Sivakumar
Academic Blog Design - R.D.Sivakumar

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Complications of Minimal Access Surgery at WLH
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Institutional Correction lecture only . . .
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
O5-L3 Freight Transport Ops (International) V1.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Complications of Minimal Access Surgery at WLH
O7-L3 Supply Chain Operations - ICLT Program
Institutional Correction lecture only . . .
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
Sports Quiz easy sports quiz sports quiz
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pre independence Education in Inndia.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis

Sample for Simple C Program - R.D.Sivakumar

  • 1. Mr. R.D.SIVAKUMAR, M.Sc.,M.Phil.,M.Tech., Assistant Professor of Computer Science & Assistant Professor and Head, Department of M.Com.(CA), Ayya Nadar Janaki Ammal College, Sivakasi – 626 124. Mobile: 099440-42243 e-mail : sivamsccsit@gmail.com website: www.rdsivakumar.blogspot.in A Sample C Program
  • 2. A Sample C Program A program is defined as a set of instructions to be executed sequentially to obtain the desired result. A program cannot be written without a function. A function may be pre- defined or user defined. We will see the first program in C. The C program code is given below: #include <stdio.h> main() { printf(“Hello World”); } This program, upon execution, will display the message Hello World on the screen. There must be a function defined as main(). The main() function is a user-defined one. The user has to define the main() function to provide necessary code. When a C program runs, the control is transferred to this function. This is called the program’s entry point. The program has two functions, one is the user-defined main() function which is the entry point and the other one is printf() function which is pre-defined and used to display the results on the standard output(screen or monitor)
  • 3. A Sample C Program Simple parentheses () are used to represent a function. Here main() is the calling function and printf() is the called function. The “Hello World” program with necessary explanations is given below. The first line in the program #include <stdio.h> is a preprocessor statement. #include is a preprocessor directive. The preprocessor is a software program that will expand the source code while the program is compiled. The #include <stdio.h> statement includes the contents of the stdio.h file (standard input and output header file) globally, that is, prior to the main() function. The contents of the stdio.h file are the function declaration statements of the pre-defined output and input functions like printf() and scanf() etc.
  • 4. A Sample C Program The declarations of the functions printf() and scanf() are as follows: int printf(char *, …); int scanf(char *, …); The ellipses (…) represent that the above two functions can take variable number of parameters. But the first parameter is always a string. A parameter is a data or information passed on to the called function. Zero or more number of such parameters are passed to any function. They are given one after another within the brackets, which come after the name of the function. In the program shown above, the printf() assumes only one parameter which is a string to be displayed on the monitor. The first parameter type (string type) is char * which will be read as “character pointer” and will be discussed later.
  • 5. A Sample C Program The C compiler is able to recognize the pre-defined functions only because of their declarations in the appropriate header files. The following program illustrates the use of header files. The program while it is being executed clears the contents of the screen before displaying hello on the monitor. The function clrscr() is a pre-defined one whose prototype (declaration) is available in conio.h file and hence it has been included. If the statement #include <conio.h> is not included, the C compiler expects the definition of clrscr() function from the programmer and if the definition is not provided, it reports an error
  • 6. Statements Each and every line of a C program can be considered as a statement. There are generally four types of statements. They are: Preprocessor statement • Function header statement • Declaration statement • Executable statement As you know already, the preprocessor statement is used to expand the source code by including the function declaration statements from the specified header files. The function header statement is used as a first line in the definition of a function. The declaration statements are further classified into variable declaration statements and function declaration statements.
  • 7. Assignment Statements The assignment statement has the following form: variable = expression  Its purpose is saving the result of the expression to the right of the assignment operator to the variable on the left. Here are some rules: The expression is evaluated first with the rules discussed in the single mode or the mixed mode expressions pages. If the type of the expression is identical to that of the variable, the result is saved in the variable. Otherwise, the result is converted to the type of the variable and saved there. If the type of the variable is INTEGER while the type of the result is REAL, the fractional part, including the decimal point, is removed making it an integer result. If the type of the variable is REAL while the type of the result is INTEGER, then  a decimal point is appended to the integer making it a real number. Once the variable receives a new value, the original one disappears and is no more available. CHARACTER assignment follows the rules stated in the discussion of the PARAMETER attribute.
  • 8. Increment and Decrement Statements Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs. Syntax: Increment operator: ++var_name; (or) var_name++; Decrement operator: – -var_name; (or) var_name – -; Example: Increment operator : ++ i ; i ++ ; Decrement operator : - – i ; i – - ;
  • 9. Expression An expression occurs usually on the right hand side of an assignment statement. It has a value when it is evaluated. There are many forms of expressions and some of them are shown below: int a,b,c; variable declaration statement a = 10; On the right hand side, a constant value is used and hence it is a constant expression whose value is 10. b = a; A variable expression is used here whose value is 10. The right hand side of this assignment statement has been associated with two values: a variable’s value and an expression value. In this case both values are same. But always remember that the expression value is assigned to the left hand side variable. The expressions can be named based on the operators used. The other expressions are: right hand side c = a+b; arithmetic expression c = a > b; relational expression f = d = e; assignment expression
  • 10. Postfix and Prefix Increment Expression ++ and -- operator as prefix and postfix If you use ++ operator as prefix like: ++var; then, the value of operand is increased by 1 then, only it is returned but, if you use ++ as postfix like: var++; then, the value of operand is returned first then, only it is increased by 1 Input and Output Statements Input : In any programming language input means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement. Output : In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data.
  • 11. Input and Output Statements As we have seen already, the function printf() is used to display the results on the standard output (screen). We have seen the use of printf() to display the string on the monitor. Actually, the first parameter of the printf() function is a string which is used to control the output and hence it can be called as “control string”. This parameter is used to format the output for display and hence we can also call it as a “formatting string”. Example: To print a value of an integer: int n; /* variable n is declared as integer*/ n = 10; printf(“%d”, n); In the above example, ‘%d’ is used as a formatting character within the control string of printf() function to display the value of an integer. The control string of printf() function can take three types of characters. • Ordinary characters • Formatting characters • Escape sequence characters
  • 12. Input and Output Statements Ordinary characters within the control string are displayed as such. In the case of printf(“hello”); statement, the control string has ordinary characters only. They are displayed as such on the screen. Table 4.4 lists the formatting characters used to display the values of various types. As seen already, the escape sequence characters are represented by a backslash followed by another character. They are in fact single character constants only. They are stored and manipulated as a single character. Escape sequences allow partial control over the format of the output. The frequently used escape sequences in the control string of printf() function are:
  • 13. User defined functions A function that is declare, calling and define by the user is called user define function. Every user define function has three parts as: Prototype or Declaration Calling Definition