review of structured programming in c.ppt
TRANSCRIPT
-
7/28/2019 Review of Structured Programming in C.ppt
1/64
Review of Structured
Programming in C
-
7/28/2019 Review of Structured Programming in C.ppt
2/64
Variables & Constants
A variable is a tool to reserve space incomputers memory
Constants are stored in this reservedspace
i=5
5 is constant which is being stored in alocation which has given a name i
-
7/28/2019 Review of Structured Programming in C.ppt
3/64
Datatypes
Depending on the purpose for which youwant to utilize memory, C allows you todecide how much memory to allocate to avariable.
char(. -.) int(. -) float(. -)
-
7/28/2019 Review of Structured Programming in C.ppt
4/64
sizeof
Memory occupied by the datatype can befound out using sizeof operator
sizeof( float ) 4 sizeof( int ) 2
-
7/28/2019 Review of Structured Programming in C.ppt
5/64
Integer and Float Conversions An arithmetic operation between an integer and integer
always yields an integer result An arithmetic operation between a float and float always
yields a float result
In an arithmetic operation between an integer and float,the integer is first promoted to float and then theoperation is carried out. And hence it always yields afloat result
On assigning a float to an integer( using the = operator)the float is demoted to an integer On assigning an integer to a float, it is promoted to a
float
-
7/28/2019 Review of Structured Programming in C.ppt
6/64
printf() and scanf()printf() is a standard library function used to display the output on thescreen
Forms of printf()printf(Format string, list of variables); printf(%c %d %f, name,age,sal); printf(name=%c age=%d salary=%f, name,age,sal); printf(name=%c \nage=%d\ nsalary=%f,name,age,sal);
scanf() is a standard library function used to receive value of variablesfrom the keyboard
scanf(%d %f, &a,&b); 1. Within the pair of double quotes there should occur only format
specifications like %c, %d, %f etc.2. The variable names must always be preceded by the address of
operator &
-
7/28/2019 Review of Structured Programming in C.ppt
7/64
Questions????
First character in any variable name mustalways be an ..
C variables are case.(sensitive/insensitive)
-
7/28/2019 Review of Structured Programming in C.ppt
8/64
Questions????main(){printf( bytes occupied by 7= %d, sizeof(7)); printf( bytes occupied by 7=%d, sizeof(7)); printf( bytes occupied by 7.0=%d,sizeof(7.0)); }
main(){
int a,b;
a=-3- -3;b=-3- -(-3);printf(a=%d b=%d ,a,b);
}
-
7/28/2019 Review of Structured Programming in C.ppt
9/64
Questions????main(){int x;x=-3*-4%-6/-5;printf( x=%d, x); }
main(){printf( %d,4%3);
printf( %d,4% -3);printf( %d, -4%3);printf( %d, -4%-3);}
-
7/28/2019 Review of Structured Programming in C.ppt
10/64
Questions????main(){float a=5,b=2;int c=a%b;
printf(%d,c); }
main()
{int g=300*300/300;printf(g=%d,g); }
-
7/28/2019 Review of Structured Programming in C.ppt
11/64
Questions????main(){float a;a=4/2;printf(%f %f,a,4/2);
}
main(){float a=4;
int i=2;printf(%f%d, i/a, i/a); printf(%d %f, i/a, i/a); }
-
7/28/2019 Review of Structured Programming in C.ppt
12/64
Questions????main(){int a,b;printf(Enter values of a and b);
scanf( %d%d ,&a,&b); printf(a=%db=%da,b); }
-
7/28/2019 Review of Structured Programming in C.ppt
13/64
Loop
A loop involves repeating some portion of the program either a specified number of times, or until a particular condition isbeing satisfied.
Looping is achieved in C through a for or a while or a do-while
-
7/28/2019 Review of Structured Programming in C.ppt
14/64
Loop
The statement within the loop would keepgetting executed till the condition beingtested remains true. When the conditionbecomes false, the control passes to thefirst statement that follows the body of theloop.
The condition being tested may userelational or logical operators.
-
7/28/2019 Review of Structured Programming in C.ppt
15/64
Loop
As a rule a loop must test a condition thateventually becomes false, otherwise the loopwould be executed forever
Instead of incrementing a loop counter, we candecrement it It is not necessary that a loop counter must only
be an int . It could even be a float
In the for statement the initialization, testing andincrementation may be dropped, but still thesemicolons are necessary.
-
7/28/2019 Review of Structured Programming in C.ppt
16/64
Precedence of Operators Description Operator Associativity
Increment/dec ++ -- R2LNegation ! R2LUnary minus - R2LSize in bytes sizeof R2L
Multiplication * L2RDivision / L2R
Mod % L2R
Addition + L2RSubtraction - L2R
Less than < L2RLess than or equal to L2RGreater than or equal to >= L2R
Equal to == L2RNot equal to != L2R
Logical AND && L2RLogical OR || L2R L2R Conditional ?: R2L
Assignment = %= += -= *= /= R2LComma , L2R
-
7/28/2019 Review of Structured Programming in C.ppt
17/64
Questions???main(){int x=10,y,z;z=y=x;y-=x--;z-=--x;
x-=--x x--;printf(y=%d z= %d x= %d, y, z, x); }
main(){
int x, y, z;
x=y=z=1;z=++x || ++y && ++z;printf(x=%d y=%d z=%d \n, x, y, z);
}
-
7/28/2019 Review of Structured Programming in C.ppt
18/64
Questions???main(){
int x=3,z;z=x++ + x++;printf(x=%d z=%d,x,z);
}
main(){
int x=3,z;
z=x++ + ++x;printf(x=%d z= %d,x,z); }
-
7/28/2019 Review of Structured Programming in C.ppt
19/64
Functions A function is a self-contained block of code that performs
a coherent task of some kind
Facts about functions Functions can be either library functions or user-defined
functions. For example, printf(), scanf() , whereasgetName() may be a user-defined function
There can be any number of functions in a program and
any function can call other function any number of times.However, program execution always begins with main() There are several benefits of using a function in a
program. 1) reusability 2)debugging is easier
-
7/28/2019 Review of Structured Programming in C.ppt
20/64
Facts
There is no restriction on the number of return statements that may be present in afunction. Also, the return statement need
not always be present at the end of thecalled function Any C function by default returns an int
value. Otherwise it is necessary toexplicitly mention so in the calling functionas well as called function.
-
7/28/2019 Review of Structured Programming in C.ppt
21/64
Call by Value and Call by Reference
Functions can be called either by value or byreference
main(){int a=10, b=20;
swapv(a,b);printf(a=%d b=%d \n,a,b); }
swapv(x,y)int x,y;{
int t;t=x;x=y;y=t;printf(x=%d y=%d \n,x,y);
}
-
7/28/2019 Review of Structured Programming in C.ppt
22/64
Call by referencemain(){
int a=10,b=20;swapr(&a,&b);printf(a=%d b=%d \n,a,b);
}swapr(x,y)int *x,*y;
{int t;t=*x;
*x=*y;*y=t;printf(*x=%d *y=%d \n,*x,*y);
}
-
7/28/2019 Review of Structured Programming in C.ppt
23/64
Recursion When some statement in a function calls the same function it is in, we say that recursion has
occurred. Such a function is called a recursive function.main(){int a, fact;printf(Enter any number); scanf(%d, &a);
fact=rec(a)printf(Factorial value=%d,fact); }
rec(x)int x;{int f;if(x==1)
return(1);elsef=x*rec(x-1);
return(f);}
-
7/28/2019 Review of Structured Programming in C.ppt
24/64
Questions???main(){
float area;float radius=2.0;area=areacircle(radius);printf(area=%f,area);
}
areacircle(r)float r;{
float a;a=3.14*r*r;printf(a=%f \n,a); return(a);
}
-
7/28/2019 Review of Structured Programming in C.ppt
25/64
Questions???main(){
int i=3,k,l;k=add(++i);l=add(i++);
printf(i=%d k=%d l=%d,I,k,l); }add(ii)int ii;{
++ii;return(ii);}
-
7/28/2019 Review of Structured Programming in C.ppt
26/64
Questions??main(){
void message();int c;
printf(c before call=%d \n,c); c=message();printf(c after call=%d,c);
}void message(){
printf(only he will survive who is C -fit); }
-
7/28/2019 Review of Structured Programming in C.ppt
27/64
Questions???main(){int i=10,j=20,k;k=addsub(i,j);printf(k=%d,k);
}addsub(c,d)int c,d;{int x,y;
x=c-d;y=c+d;return(x,y);
}
-
7/28/2019 Review of Structured Programming in C.ppt
28/64
Questions???
main(){
int i;printf(In the year of lord \n); for(i=1;i
-
7/28/2019 Review of Structured Programming in C.ppt
29/64
Questions???
main(){
int i;for(i=1;i
-
7/28/2019 Review of Structured Programming in C.ppt
30/64
Arrays
Array enables the user to combine similar datatypes into a single entity.
10 20 30 40 50 6040044002 4006 4008 4010 4012
-
7/28/2019 Review of Structured Programming in C.ppt
31/64
Notes
Array elements are stored in contiguous memorylocations
The size of the array should be mentioned whiledeclaring it
Array elements can be accessed using theposition of the element in the array. e.g n[i]refers to ith element
If the array is initialised where it is declared,mentioning the dimension of the array is optional int n[ ]={2,3,4}
-
7/28/2019 Review of Structured Programming in C.ppt
32/64
Pointers & Arrays
Use of pointers give another way of looking at arrays, because:
Array elements are always stored incontiguous memory locations
A pointer when incremented always pointsto the immediately next location of its type
-
7/28/2019 Review of Structured Programming in C.ppt
33/64
Two dimensional Arrays Two dimensional array is also called a matrix main(){int s[4][2],i;
for(i=0;i
-
7/28/2019 Review of Structured Programming in C.ppt
34/64
Facts about 2- d arrays The elements of the 2-D array can be accessed using
the subscript notation s[i][j] where i represents the rownumber and j represents the column number
The arrangement of array elements into rows and
columns is only conceptually true, since in memory thereare no rows and columns. Hence 2-D array elements arearranged linearly in the memory
While declaring & intialization take place at the sametime , mentoning the row dimension is optional
int s[][4]={12,23,23,44};
-
7/28/2019 Review of Structured Programming in C.ppt
35/64
Facts.
A 2-D array can be considered as an arrayof a number of 1-D arrays.
s[2]+1 would give the base address of thefirst element in the second 1-D array
Value at this address will be *(s[2]+1) s[2] *(s+2) *(s[2]+1) *(*(s+2)+1) s[2][1]
-
7/28/2019 Review of Structured Programming in C.ppt
36/64
Questions???
main(){
int a[5],i;static int b[5];for(i=0;i
-
7/28/2019 Review of Structured Programming in C.ppt
37/64
Questions???main(){
static int sub[5]={10,20,30,40,50};int i;for(i=0;i
-
7/28/2019 Review of Structured Programming in C.ppt
38/64
Questions???
main(){int size=10;int arr[size];for(i=1;i
-
7/28/2019 Review of Structured Programming in C.ppt
39/64
Questions???
main(){static int a[]={2,4,6,8,10};int i;for(i=0;i
-
7/28/2019 Review of Structured Programming in C.ppt
40/64
Questions???
main(){
int arr[]={0,1,2,3,4};int i,*p;for(p=arr,i=0;p+i
-
7/28/2019 Review of Structured Programming in C.ppt
41/64
Strings
Character arrays are often called strings A string in C is always terminated by a null
character( \0) ASCII value of \0 is 0
J I E T \0
4002 4003 4004 40054001
-
7/28/2019 Review of Structured Programming in C.ppt
42/64
Program
main(){
static char name[]=JIET; int i=0;while(name[i]!= \0) {
printf(%c,name[i]);
i++;}}
-
7/28/2019 Review of Structured Programming in C.ppt
43/64
String elements can also beaccessed using ptrs
main(){
static char name[]=JIET char*ptr;while(*ptr!=\ 0) {
printf(%c,*ptr);
ptr++;}}
-
7/28/2019 Review of Structured Programming in C.ppt
44/64
%s- format scpecification
scanf() and printf() offer a simple way of doing string I/O
main()
{char name[25];scanf(%s,name);
printf(%s,name); }
-
7/28/2019 Review of Structured Programming in C.ppt
45/64
Array of Pointers to Stringsmain(){
static char *names[]={CSE,
ECE, CHE, MECH
};int i;for(i=0;i
-
7/28/2019 Review of Structured Programming in C.ppt
46/64
Standard Library Functions
strlen Finds length of a string strcpy Copies one string into another strlwr Converts a string to lower case strupr Converts a string to upper case strcmp Compares two strings strrev Reverses a string strstr Finds first occurrence of a given
string in another string
-
7/28/2019 Review of Structured Programming in C.ppt
47/64
Questions???
main(){
static char s[]=Rendezvous!; printf(%d,*(s+strlen(s)));
}
-
7/28/2019 Review of Structured Programming in C.ppt
48/64
Questions???
main(){
static char str[]={48,48,48,48,48,48,48,48,48,48};char*s;int i;
s=str;for(i=0;i
-
7/28/2019 Review of Structured Programming in C.ppt
49/64
Questions???main(){
static char s[25]=The spider man; int i=0;char ch;
ch=s[++i];printf(%c %d \n,ch,i); ch=s[i++];printf(%c %d \n,ch,i); ch=i++[s];
printf(%c %d \n,ch,i); ch=++i[s];printf(%c %d \n,ch,i);
}
-
7/28/2019 Review of Structured Programming in C.ppt
50/64
Questions???
main(){
static char str[]=Limericks; char*s;s=&str[6]-6;while(*s)
printf(%c,*s++); }
-
7/28/2019 Review of Structured Programming in C.ppt
51/64
Questions???
main(){
static char str[]=MalayalaM; char *s;s=str+8;while(s>=str){printf(%c,*s); s--;}
}
-
7/28/2019 Review of Structured Programming in C.ppt
52/64
Questions???
main(){static char *mess[]={
Some love one, Some love two, I love one, That is you
};printf(%d %d,sizeof(mess),sizeof(mess[1])); }
-
7/28/2019 Review of Structured Programming in C.ppt
53/64
Structures
We usually deal with a collection of ints,chars and floats rather than isolatedentities.
Book is a collection of things like a title, anauthor, a publisher, number of pages, dateof publication, price etc.
A structure gathers together differentatoms of information that form a givenentity.
-
7/28/2019 Review of Structured Programming in C.ppt
54/64
Program main()
{struct account
{int no;char acc_name[15];float bal;
};struct account a1,a2,a3;printf(Enter acc nos., names, and balances \n); scanf(%d %s %f, &a1.no, a1.acc_name,&a1.bal); scanf(%d %s %f, &a2.no,a2.acc_name,&a3.bal);
printf(\n%d %s %f,a1.no,a1.acc_name,a1.bal); printf(\n%d %s %f,a2.no,a2.acc_name,a2.bal); printf(\n%d %s %f,a3.no,a3.acc_name,a3.bal);
}
-
7/28/2019 Review of Structured Programming in C.ppt
55/64
Facts
Structure elements are always stored incontiguous memory locations
375 R a h u l \o 1234.5
4001 40184003
a1.no a1.bala1.name
-
7/28/2019 Review of Structured Programming in C.ppt
56/64
What if???
If we were to store data of 100 accounts,we would be required to use 100 differentstructure variables from a1 to a100
-
7/28/2019 Review of Structured Programming in C.ppt
57/64
Array of structuresmain(){
struct employee{
int no;float bal;
};struct employee a[10];int i;for(i=0;i
-
7/28/2019 Review of Structured Programming in C.ppt
58/64
More Facts Declaration of structure type and structure variable can be combinedin one statement
struct player {
char name[20];int age;} p1={Raj Prem, 44};
struct{
char name[20];int age;
}p1={Raj Prem, 44};
-
7/28/2019 Review of Structured Programming in C.ppt
59/64
Facts
The values of a structure variable can be assigned toanother structure variable of the same type using theassignment operator.
It is not necessary to copy the structure elements piece-meal.
struct player {char name[20];int age;
};struct player p2, p1={ Raj Prem, 44}; p2=p1;
-
7/28/2019 Review of Structured Programming in C.ppt
60/64
Facts To access structure elements through a structure variable we use
the . operator. To access structure elements through a pointer to a structure we
use the -> operator.
struct book{char name[25];int callno;
};struct book b1={Slumdog Millionaire,420}; struct book *b2;printf(%s%d \n,b1.name,b1.callno); b2=&b1;printf(%s %d,b2 ->name, b2->callno);
-
7/28/2019 Review of Structured Programming in C.ppt
61/64
Questions??? main()
{struct employee{char name[25];
int age ;float bs;};
struct employee e;e.name=hacker; e.age=25;printf(%s %d, e.name,e.age);
}
-
7/28/2019 Review of Structured Programming in C.ppt
62/64
Questions???
main(){
struct{char name[25];char language[10];}a;
static struct a={Hacker, C}; printf(%s %s,a.name,a.language); }
-
7/28/2019 Review of Structured Programming in C.ppt
63/64
Questions???
main(){
struct a{char ch[7];char *str;};
static struct a s1={Nagpur,Bombay}; printf(%c %c \n,s1.ch[0],*s1.str); printf(%s %s \n,s1.ch,s1.str);
}
-
7/28/2019 Review of Structured Programming in C.ppt
64/64
Questions???#define NULL 0main()
{struct node{int data;struct node* link;
};struct node *p,*q;p=malloc(sizeof(struct node));q=malloc(sizeof(struct node));p->data=30;p->link=q;q->data=40;q->link=Null;
printf(%d, p ->data);p=p->link;printf(%d, p ->data);
}