21
Lecture 2 Lecture 2 Version 1.0 Version 1.0 Program Structure Program Structure Memory Concept Memory Concept

C-02

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: C-02

Lecture 2Lecture 2Version 1.0Version 1.0

Program StructureProgram Structure

Memory ConceptMemory Concept

Page 2: C-02

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();

}}

Page 3: C-02

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.

Page 4: C-02

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().

Page 5: C-02

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

Page 6: C-02

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.

Page 7: C-02

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

Page 8: C-02

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

Page 9: C-02

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

}}

Page 10: C-02

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

Page 11: C-02

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

Page 12: C-02

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

Page 13: C-02

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.

Page 14: C-02

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

Page 15: C-02

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

Page 16: C-02

16Rushdi Shams, Dept of CSE, KUET, Bangladesh

Memory conceptMemory concept

int a,b,sum;int a,b,sum;

Page 17: C-02

17Rushdi Shams, Dept of CSE, KUET, Bangladesh

Memory conceptMemory concept

scanf (“%d”,&a);scanf (“%d”,&a);

Page 18: C-02

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

Page 19: C-02

19Rushdi Shams, Dept of CSE, KUET, Bangladesh

Memory conceptMemory concept

scanf (“%d”,&b);scanf (“%d”,&b);

Page 20: C-02

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

Page 21: C-02

21Rushdi Shams, Dept of CSE, KUET, Bangladesh

Memory conceptMemory concept

sum=a+b;sum=a+b;