KEMBAR78
Sample for Simple C Program - R.D.Sivakumar | PPTX
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..!!

Sample for Simple C Program - R.D.Sivakumar

  • 1.
    Mr. R.D.SIVAKUMAR, M.Sc.,M.Phil.,M.Tech., AssistantProfessor 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 CProgram 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 CProgram 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 CProgram 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 CProgram 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 everyline 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 assignmentstatement 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 DecrementStatements 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 occursusually 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 PrefixIncrement 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 OutputStatements 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 OutputStatements 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 Afunction 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
  • 14.