Chinese Proverb says……..

Post on 23-Feb-2016

84 views 0 download

Tags:

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

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 .

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

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

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

void display(int k){

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

}

Advantages

Advantages

Reduces Complexity & increases Readability

Makes Program size smaller

Debugging becomes very easy

can be reused in other programs

Types of FunctionsBuilt in or Library

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

Eg. Prime( ) , fibonaci( ) , etc

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 .

Brain Teaser 1 func( );

main( ){

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

return (0);}

Brain Teaser 2main( ){

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

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

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

d );

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

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

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

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

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

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

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

Recursion

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

Recursion

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

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

loops ?

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

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

Automatic VariableBy DefaultGiven Garbage value if

uninitializedThey r local

Brain Teaser

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

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

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

Register VariableFaster AccessLoop Control variablesCant be pointed by some

pointers

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

}

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

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

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

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

A function cannot be the target of an assignment

You cannot define func inside another function .

Brain Teaser int a; void main( ) {

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

return (a);}

Brain Teaservoid main( ) {

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

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

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

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