Sie sind auf Seite 1von 64

Review of Structured Programming in C

Variables & Constants


A variable is a tool to reserve space in computers memory Constants are stored in this reserved space i=5 5 is constant which is being stored in a location which has given a name i

Datatypes
Depending on the purpose for which you want to utilize memory, C allows you to decide how much memory to allocate to a variable. char(.-.) int(.-) float(.-)

sizeof
Memory occupied by the datatype can be found out using sizeof operator sizeof( float )4 sizeof( int )2

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 the operation is carried out. And hence it always yields a float 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

printf() and scanf()


printf() is a standard library function used to display the output on the screen 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 variables from 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 &

Questions????
First character in any variable name must always be an .. C variables are case.(sensitive/ insensitive)

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

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

Questions????
main() { float a=5,b=2; int c=a%b; printf(%d,c); } main() { int g=300*300/300; printf(g=%d,g); }

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

Questions????
main() { int a,b; printf(Enter values of a and b); scanf( %d%d ,&a,&b); printf(a=%db=%da,b); }

Loop
A loop involves repeating some portion of the program either a specified number of times, or until a particular condition is being satisfied. Looping is achieved in C through a for or a while or a do-while

Loop
The statement within the loop would keep getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the loop. The condition being tested may use relational or logical operators.

Loop
As a rule a loop must test a condition that eventually becomes false, otherwise the loop would be executed forever Instead of incrementing a loop counter, we can decrement 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 and incrementation may be dropped, but still the semicolons are necessary.

Precedence of Operators
Description Increment/dec Negation Unary minus Size in bytes Multiplication Division Mod Addition Subtraction Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to Logical AND Logical OR Conditional Assignment Comma Operator ++ -! sizeof * / % + < <= > >= == != && || ?: = %= += -= *= /= , Associativity R2L R2L R2L R2L L2R L2R L2R L2R L2R L2R L2R L2R L2R L2R L2R L2R L2R L2R R2L R2L L2R

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

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

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() , whereas getName() 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

Facts
There is no restriction on the number of return statements that may be present in a function. Also, the return statement need not always be present at the end of the called function Any C function by default returns an int value. Otherwise it is necessary to explicitly mention so in the calling function as well as called function.

Call by Value and Call by Reference


Functions can be called either by value or by reference
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); }

Call by reference
main() { 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); }

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); else f=x*rec(x-1); return(f); }

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

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

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

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

Questions???
main() { int i; printf(In the year of lord\n); for(i=1;i<=10;i++) main(); }

Questions???
main() { int i; for(i=1;i<=10;i++) { main(); printf(In the year of lord\n); }

Arrays
Array enables the user to combine similar datatypes into a single entity.
10 20 30 40 50 60

4002

4004 4006 4008 4010 4012

Notes
Array elements are stored in contiguous memory locations The size of the array should be mentioned while declaring it Array elements can be accessed using the position 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}

Pointers & Arrays


Use of pointers give another way of looking at arrays, because: Array elements are always stored in contiguous memory locations A pointer when incremented always points to the immediately next location of its type

Two dimensional Arrays


Two dimensional array is also called a matrix main() { int s[4][2],i; for(i=0;i<=3;i++) { printf(\nEnter roll no. and marks); scanf(%d %d, &s[i][0],&s[i][1]); } for(i=0;i<=3;i++) printf(%d %d\n,s[i][0],s[1]); }

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 row number and j represents the column number The arrangement of array elements into rows and columns is only conceptually true, since in memory there are no rows and columns. Hence 2-D array elements are arranged linearly in the memory While declaring & intialization take place at the same time , mentoning the row dimension is optional int s[][4]={12,23, 23,44 };

Facts.
A 2-D array can be considered as an array of a number of 1-D arrays. s[2]+1 would give the base address of the first 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]

Questions???
main() { int a[5],i; static int b[5]; for(i=0;i<5;i++) printf(%d %d %d\n,i,a[i],b[i]); }

Questions???
main() { static int sub[5]={10,20,30,40,50}; int i; for(i=0;i<=4;i++); { if(i<=4) { sub[i]=i*i; printf(%d\n,sub[i]); } } }

Questions???
main() { int size=10; int arr[size]; for(i=1;i<=size;i++) { scanf(%d, &arr[i]); printf(\n%d,arr[i]); } }

Questions???
main() { static int a[]={2,4,6,8,10}; int i; for(i=0;i<=4;i++) { *(a+i)=a[i]+i[a]; printf(%d,*(i+a)); } }

Questions???
main() { int arr[]={0,1,2,3,4}; int i,*p; for(p=arr,i=0;p+i<=arr+4;p++,i++) printf(%d,*(p+i)); }

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 4001 I 4002 E 4003 T 4004 \0 4005

Program
main() { static char name[]=JIET; int i=0; while(name[i]!=\0) { printf(%c,name[i]); i++; } }

String elements can also be accessed using ptrs


main() { static char name[]=JIET char*ptr; while(*ptr!=\0) { printf(%c,*ptr); ptr++; } }

%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); }

Array of Pointers to Strings


main() { static char *names[]={ CSE, ECE, CHE, MECH }; int i; for(i=0;i<=3;i++) printf(%s\n,names[i]); }

Standard Library Functions


strlenFinds length of a string strcpyCopies one string into another strlwrConverts a string to lower case struprConverts a string to upper case strcmpCompares two strings strrevReverses a string strstrFinds first occurrence of a given string in another string

Questions???
main() { static char s[]=Rendezvous!; printf(%d,*(s+strlen(s))); }

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<=9;i++) { if(*s) printf(%c,*s); s++; } }

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

Questions???
main() { static char str[]=Limericks; char*s; s=&str[6]-6; while(*s) printf(%c,*s++); }

Questions???
main() { static char str[]=MalayalaM; char *s; s=str+8; while(s>=str) { printf(%c,*s); s--; } }

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])); }

Structures
We usually deal with a collection of ints, chars and floats rather than isolated entities. Book is a collection of things like a title, an author, a publisher, number of pages, date of publication, price etc. A structure gathers together different atoms of information that form a given entity.

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

Facts
Structure elements are always stored in contiguous memory locations
a1.no
375 4001

a1.name
R a h u l \o 4003

a1.bal
1234.5 4018

What if???
If we were to store data of 100 accounts, we would be required to use 100 different structure variables from a1 to a100

Array of structures
main() { struct employee { int no; float bal; }; struct employee a[10]; int i; for(i=0;i<=9;i++) { printf(Enter account number and balance); scanf(%d %f, &a[i].no,&a[i].bal); } for(i=0;i<-9;i++) printf(%d%f\n,a[i].no,a[i].bal); }

More Facts
Declaration of structure type and structure variable can be combined in one statement struct player { char name[20]; int age; } p1={Raj Prem, 44}; struct { char name[20]; int age; }p1={Raj Prem, 44};

Facts
The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator. It is not necessary to copy the structure elements piecemeal. struct player { char name[20]; int age; }; struct player p2, p1={ Raj Prem, 44}; p2=p1;

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

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

Questions???
main() { struct { char name[25]; char language[10]; }a; static struct a={Hacker, C}; printf(%s %s,a.name,a.language); }

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

Questions???
#define NULL 0 main() { 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); }

Das könnte Ihnen auch gefallen