Chinese Proverb says……..

36
Chinese Proverb says…….. A Person who asks Questions will remain fool for few seconds but one who never asks remains fool for the entire life .

description

Chinese Proverb says……. A Person who asks Questions will remain fool for few seconds but one who never asks remains fool for the entire life . Advantages. void main( ) { int n , k , i ; printf (“\n Enter number:-”); scanf (“%d”, &n); for( i =2 ; i

Transcript of Chinese Proverb says……..

Page 1: Chinese Proverb says……..

Chinese Proverb says……..

A Person who asks Questions will remain fool

for few seconds but one who never asks remains fool for

the entire life .

Page 2: Chinese Proverb says……..

Advantagesvoid main( ) {

int n , k , i ;printf(“\n Enter number:-”);scanf(“%d”, &n);for(i=2 ; i <=(n/2) ; i++ ){if ( n % i = =0 ) {k=1; } // if ends

} // for ends if ( k = = 1 ){printf(“Not Prime”); } else{printf (“ Prime ” );}

} // main ends

Page 3: Chinese Proverb says……..

Advantagesint prime (int );int AskFromUser( );void display( );void main( ) {

int n , k , i ;n=AskFromUser( );k=prime(n);display(k);

} ORvoid main( ){display ( prime( AskFromUser( )));}

Page 4: Chinese Proverb says……..

int prime( int n){int flag=0,i;for(i=2 ; i <=(n/2) ; i++ ){if ( n % i = = 0 ) {flag=1;}return(flag); }int AskFromUser( ){int n; Printf(“\nEnter A number:”);Scanf(“%d”,&n);}

Advantages

Page 5: Chinese Proverb says……..

void display(int k){

if (k==1)printf(“Not Prime”);elseprintf(“Prime”);

}

Advantages

Page 6: Chinese Proverb says……..

Advantages

Reduces Complexity & increases Readability

Makes Program size smaller

Debugging becomes very easy

can be reused in other programs

Page 7: Chinese Proverb says……..

Types of FunctionsBuilt in or Library

User Defined Eg. Printf( ) , Scanf ( ) , etc

Eg. Prime( ) , fibonaci( ) , etc

Page 8: Chinese Proverb says……..

Questions ?What is the Type of main ( ) ?What type of language is C ?

and why ?How many types of datatype

classes r there in C ?What r keywords ?

Functions names are stored in the same way as variables .

Page 9: Chinese Proverb says……..

Brain Teaser 1 func( );

main( ){

printf( “ %d” , func);}func( ){

return (0);}

Page 10: Chinese Proverb says……..

Brain Teaser 2main( ){

printf( “ %u” , main( ) );}

Page 11: Chinese Proverb says……..

Three main things…..Function Declaration ( prototype)Function callFunction Definition

Page 12: Chinese Proverb says……..

Types of Arguments(Parameters)Formal ArgumentsActual ArgumentsEg :Call func ( 10 ,20 ,30 , 40);Defi. func (int a, int b ,int c ,int

d );

Page 13: Chinese Proverb says……..

ANSI Methodvoid main(){Func(10,20);}Func(int a,int b){

◦Printf (“ Sum=%d ”,(a+b));}

Page 14: Chinese Proverb says……..

K & R Methodvoid main(){Func(10,20);}Func(a,b)int a;int b;{

◦printf (“ Sum= %d ”,(a+b));}

Page 15: Chinese Proverb says……..

How Arguments r passed ?

In TC , the argument calling convention is from right to left

For Eg:

func(10,20,30,40); // function call

func( int a , int b ,int c , int d) // function Defi.{……..some code……}

Page 16: Chinese Proverb says……..

How Arguments r passed ?

void func(int , int ,int , int );void main( ){

int a=5;func( a , a++ , ++a , --a );

}void func(int a, int b ,int c ,int d ){

printf(“ %d %d %d %d ”, a, b , c , d );}

Page 17: Chinese Proverb says……..

Call By Value & Call By Referencevoid main( ){int func(int ) ;int j=10 , i ; i=fun( j ); printf (“ %d %d ”,--i , j );}int fun(int j){return ( j++ );}

Page 18: Chinese Proverb says……..

Recursion

void main ( ) {int i=0;func(i );}void func(int i ){i++;printf(“%d” , i );if ( i!=5 )func( i);}

Page 19: Chinese Proverb says……..

Recursion

void main ( ) {int i=0;func(i );}void func(int i ){i++;if ( i!=5 )func( i);printf(“%d” , i);}

Page 20: Chinese Proverb says……..

Question ?What is required in Recursion ?What is better Recursion or using

loops ?

Page 21: Chinese Proverb says……..

Scope (active), Visibility& Longetivity ( alive) int i=10;void main( ){Func( );Printf ( “%d ”, i );} // main endsFunc( ){int i=20; // scope of this i is only inside

this // func . Global i is not visible here // but it is alive

Printf(“ %d ” , i); }

Page 22: Chinese Proverb says……..

Type of VariablesAutomatic ( local or internal)Static (can be local or external)Register ExternGlobal ( external )

Page 23: Chinese Proverb says……..

Automatic VariableBy DefaultGiven Garbage value if

uninitializedThey r local

Page 24: Chinese Proverb says……..

Brain Teaser

void main(){int a;Func(a);Printf(“%d ”, a);}Func(int a) {a=++a || a++ || a-- && --a;return(a);}

Page 25: Chinese Proverb says……..

Static VariableRetains its value during function

callsCan be local or externalDifference between global

varaible and external static variable

Gets default value =0A func can also be static

Page 26: Chinese Proverb says……..

Brain Teaservoid main( ){int i;for(i=0;i<3;i++){Func( );}Func( ){Static int i=0;i++;Print f(“%d” , i);}

Page 27: Chinese Proverb says……..

Register VariableFaster AccessLoop Control variablesCant be pointed by some

pointers

Page 28: Chinese Proverb says……..

Brain Teaservoid main( ){register int i=10;int *p;p=&i;p=(*p)++;printf (“%d”,*p);

}

Page 29: Chinese Proverb says……..

Extern Variable #include<stdio.h> void main( )

{ extern int x; printf( “ %d ”, x ); }int x=10;

Page 30: Chinese Proverb says……..

Extern Variable #include<stdio.h> void main( )

{ extern int x; printf( “ %d ”, x ); }

Page 31: Chinese Proverb says……..

A function cannot be the target of an assignment

You cannot define func inside another function .

Page 32: Chinese Proverb says……..

Brain Teaser int a; void main( ) {

Func( )=10;printf( “ %d ” , a);}Func( ){

return (a);}

Page 33: Chinese Proverb says……..

Brain Teaservoid main( ) {

void func( )◦{◦printf( “ Hello C ”);◦}func();}

Page 34: Chinese Proverb says……..

Function returning Pointersint *func();void main(){int i,*p;p=func();}int *func(){int x=10,*q;q=&x;return (q);}

Page 35: Chinese Proverb says……..

Pointer To Functionvoid func( );void main(){int (*p)( );p=func;(*p)( );}void func( ){Printf(“Hello LD”);}

Page 36: Chinese Proverb says……..

Brain Teaserint *func( );void main(){int *p;p=func();printf(“\n”);printf( “ %d ” , *p);}int *func( ){int k=35;return ( &k);}