Friday, January 27, 2012

Confusing code of function in c

A little introduction
 
A block of statement perform a coherent task and exhicute when it called. Each and every c program contain at least one function that is main function. Yes it also possible to write c program without main function but it is nothing but calling the main function with patterns.
Generally a typical function has three phases
1)Function prototype
It gives the idea about function ,Defines it’s return type ,name and the parameter type of the function.It may specify parameter name or may not. But specify parameter type prominently.
2)Function call
Call the function when it’s necessary. Just use the function name and pass actual parameter.
3)Function definition
Actual body of function .That contain your login and code for operation. Formal parameter takes the copy of actual one, and function body works with formal parameter.
In a nutshell function also contain space in memory in case of c language just like a simple variable.
The mechanism of function call is pretty cool .When it need to call the program counter value is pushes into stack and control goes to particular memory location .When the function body finished it’s exhicution ,control come back the previous memory location by poping programme counter value from stack.
Such overhead can elemenated by inline function.
Advantage of inline function:-
1)Speed  up exhicution.
Disadvantage
1)Takes more space than simple function because it replace it’s body in place of function call location. So source code size became increase.

Some very confusing code of function
Q1)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
      int p=printf("Hello c");
      printf("%d",p);
      getch();
}
Ans:-Hello c7
Discussion:-Here at first printf function print the message Hello c then it will return the number of character it has printed. Here printf function print 7 character and 7 will store in variable p.

Q2)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
      int a,b;
      int p=scanf("%d%d",&a,&b);
      printf("%d%d%d",a,b,p);
      getch();
}
Ans:- At first scan 2 number then it will print 2.
Discussion:-Scanf function will scan two variable and then it will return number of variable it has scaned.

Q3)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
      main();
      getch();
}
Ans:-Nothing will print and program will not terminate until system stack is overflow.
Discussion:- Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.

Q4)Output of the progam
#include "stdio.h"
#include "conio.h"
void abc()
{
 printf(" abc");
}
void xyz()
{
 abc();
 printf(" xyz");   
}
void pqr()
{
     printf("pqr");
     xyz();
}
main()
{
      pqr();
      getch();
}
Ans:-pqr abc xyz
 Discussion:-From main function pqr() will call after execution of pqr() it will call to xyz() .And xyz() will call to abc function. After execution of abc function control will return into xyz function and the statement of xyz() will execute.


 Q5)Output of the program
#include "stdio.h"
#include "conio.h"
void showarray(int a[])
{
     for(int i=0;i<3;i++)
     printf("%d",a[i]);
}
main()
{
      int a[]={10,20,30};    
      showarray(a);
      getch();
}
Ans:-10,20,30
Discussion:-Here showarray function takes the base address of array and within showarray function the array will print.

Q6)Output of the program.
#include "stdio.h"
#include "conio.h"
#include "string.h"

char* myfunc1()
{
 char *temp = "string";
 return temp;
};

char* myfunc2()
{
 char temp[] = {'s', 't', 'r', 'i', 'n', 'g', '\0'};
 puts(temp);
}

int main()
{
  puts(myfunc1());
  myfunc2();
 getch();
}
And:-string string
Discussion:-Here myfunction is called as a argument of puts() and myfunction1 will retun the base address of the string. And the address will come into puts function. Again myfunction2() will call from the main function and in body of myfunction2() print the value of temp character array.
Q7) Difference between calloc and malloc
There are two differences. First, is in the number of arguments.
Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments (number of variables to allocate memory, size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

Q8)Output of the progr
 #include "stdio.h"
 #include "conio.h"
 #include "string.h"
 #define swap(a,b) a=a+b;b=a-b;a=a-b;
   int swap2(int a, int b)
    {
        int temp;
        temp=a;
        b=a;
        a=temp;
        return 0;

    }
    main()
    {
        int x=5, y=10;
        swap (x,y);
        printf("%d %d" ,x,y);
        swap2(x,y);
        printf("%d %d" ,x,y);
        getch();
    }

Ans:-5 10 5 10
Discussion:-here swap() is defined in pre processor directive. And swap2() is defined as normal function. First swap() will swap the value of a and b and swap2() will swap the value of a and b again.

 Q9) Output of the program
#include "stdio.h"
#include "conio.h"
    main()
    {
        char *ptr = "Cisco Systems";
        ptr++;
        printf("%s \n",ptr);
        ptr++;
        printf("%s",ptr);
        getch();
    }
Ans:-isco System
                Sco System
Discussion:-ptr is character pointer and it hold the base address of string Cisco System. When ptr++ is perform it hold the second character address of the string. And again when ptr++ is perform it hold the address of third character.

Q10)Output of the program
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"
    main()
    {
        char *p1;
        char *p2;
        p1=(char *)malloc(25);
        p2=(char *)malloc(25);
        strcpy(p1,"Cisco");
        strcpy(p2,"systems");
        strcat(p1,p2);
        printf("%s",p1);
        getch();

    }
Ans:-Ciscosystem
Discussion:-Here p1 and p2 are two character pointer, and p1 hold the base address of Cisco and p2 hold the base address of system, and strcal function concatenate two string, and print the output as concatenation of two string.


 Q11)Output of the program.
#include "stdio.h"
#include "conio.h"
 main()
 {
   int i=0;               
   printf("%d%d%d",++i,++i,i);
   getch();
 }
Ans:-2 1 0
Discussion:-The output depends upon function calling conversion. printf() starts to execute from right side, so from right side it will print the value of i  i,e 0.Then ++i operation and again ++I operation will perform.

Q12)output of the program.
#include "stdio.h"
#include "conio.h"
void swap(int *p,int *q)
{
     int t;
     t=*p;
     *p=*q;
     *q=t;   
}
main()
 {
  
   int a=10,b=20;
   swap(&a,&b);
   printf("%d ",a);
   printf("%d ",b);
   getch();
 }
Ans:-20 10
Discussion:-It is example of call by reference. Here swap function is called by the references of the variable a and b. As swap function is call by argument the value of a and b is changed permanently.


 Q13)Output of the program
#include "stdio.h"
#include "conio.h"
void swap(int p,int q)
{
     int t;
     t=p;
     p=q;
     q=t;   
}

main()
 {
  
   int a=10,b=20;
   swap(a,b);
   printf("%d ",a);
   printf("%d ",b);
   getch();
 }
Ans:-10 20
Discussion:-

Q14)
#include "stdio.h"
#include "conio.h"
main( int argc ,char argv[])
{
 printf("%d",argc);
 for(int i=0;i<argc;i++)
 printf("%s",argv[i]);
 getch();
}
And:-It will work in Xp ,TC platform but not work in Windows7 ,DEV C++ platform. It will print the number of argument the program has taken(argc as argument count) and the arguments(argv[ ] for argument vector).

Q15)parpuse of atoi(),itoa() and atof() function
atoi() converts string to integer
itoa() converts integer to string
atof() converts string to float



Q16) Difference between exit() and return .
First of all return is not a function, it is a statement. The exit() function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit() function are similar.


Q17)Output of the program.
#include "stdio.h"
#include "conio.h"
int findbig(int a,int b)
{
      (a>b?return a:return b);
}
main()
{
      int a=findbig(10,20);
      printf("%d",a);
      getch();
}
Ans:-It is a erroneous code.
Discussion:- Because we cannot return value from ternary  condition operator. It violate the rule of C syntax

Q18)Output of the program.
#include "stdio.h"
#include "conio.h"
main()
{
     if(!5)
     printf("C");
     else
     printf("C++");
     getch();
}
Ans:-C++
Discussion:-Here !5 =0. Because not operator invert the value of variable. And if(0) means condition is false. So that C++ will execute.


 Q19)Output of the program
#include "stdio.h"
#include "conio.h"
int print()
{
     return 10;
     return 20;
}
main()
{
     printf("%d",print());
     getch();
}

Ans:-In Vindow7 and DEV C++ platform it will work fine. And output is 10;
Discussion:-Print function contains more than one return statement. And first statement return 10.Second statement will never execute.


Q20)Output  of the program
#include "stdio.h"
#include "conio.h"
main()
{
    printf("%u",main);
    getch();
}
And:-It will print the address of main() function.

Q21)
#include "stdio.h"
#include "conio.h"
int show(int n)
{
    return n++;
}  
main()
{

 int p=show(10);
 printf("%d",--p);
 getch();
}
Ans:-9;
Discussion:-Here in show function n++ is a post increment operation .The return statement will return 10 to main function.
Q22)Outpur of the program.
#include "stdio.h"
#include "conio.h"

int increment(int n)
{
    n++;
    return n;
}
main()
{
 int p=10;    
 int k=increment(k=increment(k=increment(p)));    
 printf("%d",k);
 getch();
}
Ans:-13
Discussion:-Here three times the increment function will call recursively. So the value of p will increment by three.

Q23)Output of the program.
#include "stdio.h"
#include "conio.h"

void hello()
{
     printf("Hello");
}
main()
{
  int i;
  while(i)
  {
          hello();
          main();
  }
 
 getch();
}
Ans:-Infinite time it will print Hello.
Discussion:-Here nothing is initializing in i so compiler will assign garbage value to i.So while loop will execute infinite number of time. and each time Hello will print.

 
Q24)Output of the program.
#include <stdio.h>
#include<conio.h>
int main(void);     //Main function prototype
int main(void)      //Main function definition
{
     printf("Let's get redirected!\n");
     getch();
}
Ans:- Let's get redirected!
Discussion:-Here main function is declared and define .But prototype declaration of main function is not needed.



No comments:

Post a Comment