Lecture 2
Analyzing C Program Structure
#include
<stdio.h>
#include
<conio.h>
void
main(){
clrscr();
printf(“This is my first C program \n”);
getch();
}
#include
<stdio.h>
C
pre-processor. Lines beginning with # are processed
by the pre-processor before the program is compiled. This line tells the
pre-processor to include the contents of the standard input output header file.
To compile library function printf(), it is required.
#include
<conio.h>
Required for clrscr() and getch().
void
main()
It is a part of
every C program. The parenthesis after main indicates that main is a program
building block called a function. C programs are composed of one or many
functions like it but main is a must.
clrscr();
C library
function. Clears the contents present in the screen.
printf(“This
is my first C program \n”);
C statement. Statements are always terminated with a semi-colon. This
statement has a library function called printf(). It takes a string
inside of it within two quotation marks. Whatever is in between them,
will be printed on the screen. The backslash (\) character is called Escape
Character. When C encounters it, it looks for the next character (here it
is n) to make an Escape Sequence. Here, the escape sequence \n means new
line. After printing the line This is my first C program, the cursor
goes to the new line.
getch();
Another C
library function. When the program compiles and provides an output, it waits
for getting a character from the keyboard. When it gets, it returns from the
output screen to source code.
/*
This program takes two integers
and
provides the sum to the user */
#include
<stdio.h> // header file
#include
<conio.h> //header file
//
main function starts
void
main(){
clrscr(); //
clearing the output screen
int a,b,sum; // declaring three integers a, b and sum
printf(“Enter integer one: \n”); // printing on output
scanf (“%d”,&a); //taking the input value into a
printf(“Enter integer two: \n”); // printing on output
scanf (“%d”,&b); // taking the input value into b
sum=a+b; //
taking their summation into sum
printf(“Sum is: %d”, sum); // displaying the sum
getch(); //waiting
for a character to return
}
/*
This program takes two integers
and
provides the sum to the user */
The characters
/* … … … */ is called multi-line commenting. When C finds this
character, it omits what is inside that and goes to the next instruction.
Comments are required to make the program more readable and understandable if
anyone analyzes the code.
//
main function starts
The characters
// is called single line commenting. Again, when C finds this character,
it omits that line of code.
int
a,b,sum;
It is a declaration.
The letters a, b and sum are names of variables. A variable is a
location in memory where a value can be stored for use by a program. This
declaration specifies that the variables a, b and sum are of type int
which means that these variables will hold integer values. All variables must
be declared with a name and a data type immediately after the
left brace that begins the body of main () before they can be used in program.
There are other data types in C besides int. Several variables with the
same data type can be declared in one declaration. We could declare that in
separate declaration as well-
int a;
int b;
int sum;
Notes on
variable names-
- A variable name in C is any identifier.
An identifier is a series of characters consisting of letters, digits and
underscores (_) that does not begin with a digit.
- It can be of any length. But C will
understand the first 31 characters only.
- C is case sensitive. Variable
names like A1 and a1 are two different variables.
scanf
(“%d”,&a);
It uses scanf ()
function to obtain a value from the user. The scanf () function takes input
from the key-board. The scanf () here
has two arguments. “%d” and &a. The first argument, the
format control string, indicates that the data should be an integer (d
stands for decimal integer). The % is Escape Character and %d is Escape Sequence.
The second argument & (ampersand)- called address operator in C‑ followed
by the variable name. The ampersand when combined with variable name tells
scanf() the location in memory in which the variable a is stored. The computer
then stores the values for a at that location.
When the
computer executes the preceding scanf(), it waits for the user to enter a value
for variable a. the user responds by typing an integer and then pressing the return
key (Enter key) to send the number to computer. The computer then assigns
the value to the variable a.
sum=a+b;
It calculates
the sum of variables a and b and assigns the result to variable
sum using the assignment operator =. The + and = operators are called binary
operators as they require two operands. The two operands of + are a
and b and two operands of = are sum and a+b.
Memory Concept in C
int
a,b,sum;

scanf
(“%d”,&a);
Say, the user put 100

scanf
(“%d”,&b);
Say, the user put 50

sum=a+b;
sum is now 100+50=150

0 comments:
Post a Comment