HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
Lec 02. C Program Structure / C Memory Concept
1. Lecture 2Lecture 2
Version 1.0Version 1.0
Program StructureProgram Structure
Memory ConceptMemory Concept
2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Analyzing C Program Structure IAnalyzing C Program Structure I
#include <stdio.h>#include <stdio.h>
#include <conio.h>#include <conio.h>
void main(){void main(){
clrscr();clrscr();
printf(“This is my first C program n”);printf(“This is my first C program n”);
getch();getch();
}}
3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh
Header fileHeader file
#include <stdio.h>#include <stdio.h>
C Pre-processorC Pre-processor
Lines beginning with # are processed by the pre-Lines beginning with # are processed by the pre-
processor before the program is compiledprocessor before the program is compiled
This line tells the pre-processor to include theThis line tells the pre-processor to include the
contents of the standard input output header filecontents of the standard input output header file
To compileTo compile library functionlibrary function printf(), it is required.printf(), it is required.
4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh
Header fileHeader file
#include <conio.h>#include <conio.h>
Required for clrscr() and getch().Required for clrscr() and getch().
5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh
main ( ) functionmain ( ) function
void main( )void main( )
It is a part of every C programIt is a part of every C program
The parenthesis after main indicates that main is aThe parenthesis after main indicates that main is a
program building block called aprogram building block called a functionfunction
C programs are composed of one or many functionsC programs are composed of one or many functions
like it but main is a mustlike it but main is a must
6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh
Library functionLibrary function
clrscr();clrscr();
C library function.C library function.
Clears the contents present in the screen.Clears the contents present in the screen.
7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh
Library functionLibrary function
printf(“This is my first C program n”);printf(“This is my first C program n”);
C statement.C statement.
Statements are always terminated with aStatements are always terminated with a semi-colonsemi-colon
This statement has a library function called printf( )This statement has a library function called printf( )
It takes aIt takes a stringstring inside of it within twoinside of it within two quotation marksquotation marks
Whatever is in between them, will be printed on theWhatever is in between them, will be printed on the
screenscreen
TheThe backslash ()backslash () character is calledcharacter is called Escape CharacterEscape Character
The escape sequence n meansThe escape sequence n means new linenew line
8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh
Library functionLibrary function
getch();getch();
Another C library functionAnother C library function
When the program compiles and provides an output,When the program compiles and provides an output,
it waits for getting a character from the keyboardit waits for getting a character from the keyboard
When it gets, it returns from the output screen toWhen it gets, it returns from the output screen to
source codesource code
9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh
Analyzing C Program Structure IIAnalyzing C Program Structure II
/* This program takes two integers/* This program takes two integers
and provides the sum to the user */and provides the sum to the user */
#include <stdio.h>#include <stdio.h> // header file// header file
#include <conio.h>#include <conio.h> //header file//header file
// main function starts// main function starts
void main(){void main(){
clrscr();clrscr(); // clearing the output screen// clearing the output screen
int a,b,sum;int a,b,sum; // declaring three integers a, b and sum// declaring three integers a, b and sum
printf(“Enter integer one: n”);printf(“Enter integer one: n”); // printing on output// printing on output
scanf (“%d”,&a);scanf (“%d”,&a); //taking the input value into a//taking the input value into a
printf(“Enter integer two: n”);printf(“Enter integer two: n”); // printing on output// printing on output
scanf (“%d”,&b);scanf (“%d”,&b); // taking the input value into b// taking the input value into b
sum=a+b;sum=a+b; // taking their summation into sum// taking their summation into sum
printf(“Sum is: %d”, sum);printf(“Sum is: %d”, sum); // displaying the sum// displaying the sum
getch();getch(); //waiting for a character to return//waiting for a character to return
}}
10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh
Multiline commentsMultiline comments
/* This program takes two integers/* This program takes two integers
and provides the sum to the user */and provides the sum to the user */
The characters /* … … … */ is calledThe characters /* … … … */ is called multi-linemulti-line
commentingcommenting
When C finds this character, it omits what is insideWhen C finds this character, it omits what is inside
that and goes to the next instructionthat and goes to the next instruction
Comments are required to make the program moreComments are required to make the program more
readable and understandable if anyone analyzes thereadable and understandable if anyone analyzes the
codecode
11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh
Single line commentsSingle line comments
// main function starts// main function starts
The characters // is calledThe characters // is called single line commentingsingle line commenting
Again, when C finds this character, it omits that line ofAgain, when C finds this character, it omits that line of
codecode
12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh
Variable declarationVariable declaration
int a, b, sum;int a, b, sum;
It is aIt is a declarationdeclaration
The letters a, b and sum are names ofThe letters a, b and sum are names of variablesvariables
A variable is a location in memory where a value canA variable is a location in memory where a value can
be stored for use by a programbe stored for use by a program
This declaration specifies that the variables a, b andThis declaration specifies that the variables a, b and
sum are of typesum are of type intint which means that these variableswhich means that these variables
will hold integer valueswill hold integer values
All variables must be declared with aAll variables must be declared with a namename and aand a datadata
typetype
13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh
Notes on variable namesNotes on variable names
A variable name in C is anyA variable name in C is any identifieridentifier. An identifier is a. An identifier is a
series of characters consisting of letters, digits andseries of characters consisting of letters, digits and
underscores (_) that does not begin with a digit.underscores (_) that does not begin with a digit.
It can be of any length. But C will understand the firstIt can be of any length. But C will understand the first
31 characters only.31 characters only.
C isC is case sensitivecase sensitive. Variable names like A1 and a1 are two. Variable names like A1 and a1 are two
different variables.different variables.
14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh
Library functionLibrary function
scanf (“%d”,&a);scanf (“%d”,&a);
scanf () function to obtain a value from the userscanf () function to obtain a value from the user
The scanf () here has twoThe scanf () here has two argumentsarguments.. “%d”“%d” and &and &aa..
The % is Escape Character and %d is EscapeThe % is Escape Character and %d is Escape
SequenceSequence
The second argument & (ampersand)- calledThe second argument & (ampersand)- called addressaddress
operatoroperator in C followed by the variable name- tells‑in C followed by the variable name- tells‑
scanf() the location in memory in which the variable ascanf() the location in memory in which the variable a
is stored.is stored.
The computer then stores the values forThe computer then stores the values for aa at thatat that
locationlocation
15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Use of binary operatorsUse of binary operators
sum=a+b;sum=a+b;
It calculates the sum of variablesIt calculates the sum of variables aa andand bb and assignsand assigns
the result to variable sum using the assignmentthe result to variable sum using the assignment
operator =operator =
The + and = operators are calledThe + and = operators are called binary operatorsbinary operators asas
they require twothey require two operandsoperands
The two operands of + are a and b and two operandsThe two operands of + are a and b and two operands
of = are sum and a+bof = are sum and a+b
16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh
Memory conceptMemory concept
int a,b,sum;int a,b,sum;
18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Memory conceptMemory concept
scanf (“%d”,&a);scanf (“%d”,&a);
Say the input is 100Say the input is 100