C-02
-
Upload
rushdi-shams -
Category
Education
-
view
59 -
download
0
description
Transcript of C-02
Lecture 2Lecture 2Version 1.0Version 1.0
Program StructureProgram Structure
Memory ConceptMemory Concept
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
Analyzing C Program Analyzing C Program Structure IStructure 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();
}}
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 Lines beginning with # are processed by
the pre-processor before the program is the pre-processor before the program is compiled compiled
This line tells the pre-processor to This line tells the pre-processor to include the contents of the standard include the contents of the standard input output header file input output header file
To compile To compile library functionlibrary function printf(), it is printf(), it is required.required.
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().
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 The parenthesis after main indicates that
main is a program building block called a main is a program building block called a functionfunction
C programs are composed of one or C programs are composed of one or many functions like it but main is a must many functions like it but main is a must
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
Library functionLibrary function
clrscr();clrscr(); C library function. C library function. Clears the contents present in the Clears the contents present in the
screen.screen.
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 a Statements are always terminated with a semi-semi-
coloncolon This statement has a library function called printf( ) This statement has a library function called printf( ) It takes a It takes a stringstring inside of it within two inside of it within two quotation quotation
marksmarks Whatever is in between them, will be printed on Whatever is in between them, will be printed on
the screen the screen The The backslash (\)backslash (\) character is called character is called Escape Escape
CharacterCharacter The escape sequence \n means The escape sequence \n means new linenew line
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
Library functionLibrary function
getch();getch(); Another C library function Another C library function When the program compiles and When the program compiles and
provides an output, it waits for getting a provides an output, it waits for getting a character from the keyboard character from the keyboard
When it gets, it returns from the output When it gets, it returns from the output screen to source code screen to source code
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
Analyzing C Program Analyzing C Program Structure IIStructure II
/* This program takes two integers/* This program takes two integersand 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 screenint a,b,sum;int a,b,sum; // declaring three integers a, b and sum// declaring three integers a, b and sumprintf(“Enter integer one: \n”);printf(“Enter integer one: \n”); // printing on output// printing on outputscanf (“%d”,&a);scanf (“%d”,&a); //taking the input value into a//taking the input value into aprintf(“Enter integer two: \n”);printf(“Enter integer two: \n”); // printing on output// printing on outputscanf (“%d”,&b);scanf (“%d”,&b); // taking the input value into b// taking the input value into bsum=a+b;sum=a+b; // taking their summation into sum// taking their summation into sumprintf(“Sum is: %d”, sum);printf(“Sum is: %d”, sum); // displaying the sum// displaying the sumgetch();getch(); //waiting for a character to return//waiting for a character to return
}}
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 called The characters /* … … … */ is called multi-multi-line commentingline commenting
When C finds this character, it omits what is When C finds this character, it omits what is inside that and goes to the next instruction inside that and goes to the next instruction
Comments are required to make the Comments are required to make the program more readable and understandable program more readable and understandable if anyone analyzes the code if anyone analyzes the code
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
Single line commentsSingle line comments
// main function starts// main function starts
The characters // is called The characters // is called single line single line commentingcommenting
Again, when C finds this character, it Again, when C finds this character, it omits that line of code omits that line of code
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
Variable declarationVariable declaration
int a, b, sum;int a, b, sum;
It is a It is a declarationdeclaration The letters a, b and sum are names of The letters a, b and sum are names of
variablesvariables A variable is a location in memory where a A variable is a location in memory where a
value can be stored for use by a program value can be stored for use by a program This declaration specifies that the variables This declaration specifies that the variables
a, b and sum are of typea, b and sum are of type int int which means which means that these variables will hold integer values that these variables will hold integer values
All variables must be declared with a All variables must be declared with a namename and a and a data typedata type
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
Notes on variable namesNotes on variable names
A variable name in C is any A variable name in C is any identifieridentifier. An . An identifier is a series of characters identifier is a series of characters consisting of letters, digits and consisting of letters, digits and underscores (_) that does not begin with underscores (_) that does not begin with a digit. a digit.
It can be of any length. But C will It can be of any length. But C will understand the first 31 characters only.understand the first 31 characters only.
C is C is case sensitivecase sensitive. Variable names like . Variable names like A1 and a1 are two different variables.A1 and a1 are two different variables.
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
Library functionLibrary function
scanf (“%d”,&a); scanf (“%d”,&a);
scanf () function to obtain a value from the scanf () function to obtain a value from the user user
The scanf () here has two The scanf () here has two argumentsarguments. . “%d”“%d” and &and &aa. .
The % is Escape Character and %d is Escape The % is Escape Character and %d is Escape Sequence Sequence
The second argument & (ampersand)- called The second argument & (ampersand)- called address operatoraddress operator in C‑ followed by the in C‑ followed by the variable name- tells scanf() the location in variable name- tells scanf() the location in memory in which the variable a is stored. memory in which the variable a is stored.
The computer then stores the values for The computer then stores the values for aa at at that location that location
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 variables It calculates the sum of variables aa and and b b and assigns the result to variable sum and assigns the result to variable sum using the assignment operator = using the assignment operator =
The + and = operators are calledThe + and = operators are called binary binary operatorsoperators as they require two as they require two operandsoperands
The two operands of + are a and b and The two operands of + are a and b and two operands of = are sum and a+b two operands of = are sum and a+b
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
Memory conceptMemory concept
int a,b,sum;int a,b,sum;
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
Memory conceptMemory concept
scanf (“%d”,&a);scanf (“%d”,&a);
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
19Rushdi Shams, Dept of CSE, KUET, Bangladesh
Memory conceptMemory concept
scanf (“%d”,&b);scanf (“%d”,&b);
20Rushdi Shams, Dept of CSE, KUET, Bangladesh
Memory conceptMemory concept
scanf (“%d”,&b);scanf (“%d”,&b);Say the input is 50Say the input is 50
21Rushdi Shams, Dept of CSE, KUET, Bangladesh
Memory conceptMemory concept
sum=a+b;sum=a+b;