1 / 36

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 . Advantages. void main( ) { int n , k , i ; printf (“<br> Enter number:-”); scanf (“%d”, &amp;n); for( i =2 ; i &lt;=(n/2) ; i ++ ){

fonda
Télécharger la présentation

Chinese Proverb says……..

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 .

  2. Advantages void 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

  3. Advantages int prime (int); intAskFromUser( ); void display( ); void main( ) { int n , k , i ; n=AskFromUser( ); k=prime(n); display(k); } OR void main( ){ display ( prime( AskFromUser( ))); }

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

  5. Advantages void display(int k) { if (k==1) printf(“Not Prime”); else printf(“Prime”); }

  6. Advantages Reduces Complexity & increases Readability Makes Program size smaller Debugging becomes very easy can be reused in other programs

  7. Types of Functions Built in or Library Eg. Printf( ) , Scanf ( ) , etc User Defined Eg. Prime( ) , fibonaci( ) , etc

  8. 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 .

  9. Brain Teaser 1 func( ); main( ) { printf( “ %d” , func); } func( ) { return (0); }

  10. Brain Teaser 2 main( ) { printf( “ %u” , main( ) ); }

  11. Three main things….. • Function Declaration ( prototype) • Function call • Function Definition

  12. Types of Arguments(Parameters) • Formal Arguments • Actual Arguments Eg : • Call  func ( 10 ,20 ,30 , 40); • Defi.  func (int a, int b ,int c ,int d );

  13. ANSI Method • void main(){ • Func(10,20); • } • Func(inta,int b) • { • Printf (“ Sum=%d ”,(a+b)); • }

  14. K & R Method • void main(){ • Func(10,20); • } • Func(a,b) • int a; • int b; • { • printf (“ Sum= %d ”,(a+b)); • }

  15. 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…… }

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

  17. Call By Value & Call By Reference void main( ){ intfunc(int ) ; int j=10 , i ; i=fun( j ); printf (“ %d %d ”,--i , j ); } int fun(int j){ return ( j++ ); }

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

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

  20. Question ? • What is required in Recursion ? • What is better Recursion or using loops ?

  21. Scope (active), Visibility& Longetivity ( alive) inti=10; void main( ){ Func( ); Printf ( “%d ”, i ); } // main ends Func( ){ inti=20; // scope of this i is only inside this // func . Global i is not visible here // but it is alive Printf(“ %d ” , i); }

  22. Type of Variables • Automatic ( local or internal) • Static (can be local or external) • Register • Extern • Global ( external )

  23. Automatic Variable • By Default • Given Garbage value if uninitialized • They r local

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

  25. Static Variable • Retains its value during function calls • Can be local or external • Difference between global varaible and external static variable • Gets default value =0 • A func can also be static

  26. Brain Teaser void main( ){ inti; for(i=0;i<3;i++){ Func( ); } Func( ){ Static inti=0; i++; Print f(“%d” , i); }

  27. Register Variable Faster Access • Loop Control variables • Cant be pointed by some pointers

  28. Brain Teaser void main( ) { register inti=10; int *p; p=&i; p=(*p)++; printf (“%d”,*p); }

  29. Extern Variable #include<stdio.h> void main( ) { extern int x; printf( “ %d ”, x ); } int x=10;

  30. Extern Variable #include<stdio.h> void main( ) { extern int x; printf( “ %d ”, x ); }

  31. A function cannot be the target of an assignment • You cannot define func inside another function .

  32. Brain Teaser int a; void main( ) { Func( )=10; printf( “ %d ” , a); } Func( ) { return (a); }

  33. Brain Teaser void main( ) { void func( ) • { • printf( “ Hello C ”); • } func(); }

  34. Function returning Pointers • int *func(); • void main(){ • inti,*p; • p=func(); • } • int *func(){ • int x=10,*q; • q=&x; • return (q); • }

  35. Pointer To Function • void func( ); • void main(){ • int (*p)( ); • p=func; • (*p)( ); • } • void func( ){ • Printf(“Hello LD”); • }

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

More Related