Exercise: Functions

Questions for: Functions

What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i=0;
    i++;
    if(i<=5)
    {
        printf("ExamAdept");
        exit(1);
        main();
    }
    return 0;
}
A:
Prints "ExamAdept" 5 times
B:
Function main() doesn't calls itself
C:
Infinite loop
D:
Prints "ExamAdept"
Answer: D

Step 1: int i=0; The variable i is declared as in integer type and initialized to '0'(zero).

Step 2: i++; Here variable i is increemented by 1. Hence i becomes '1'(one).

Step 3: if(i<=5) becomes if(1 <=5). Hence the if condition is satisfied and it enter into if block statements.

Step 4: printf("ExamAdept"); It prints "ExamAdept".

Step 5: exit(1); This exit statement terminates the program execution.

Hence the output is "ExamAdept".

What will be the output of the program?
#include<stdio.h>
int fun(int);
int main()
{
    float k=3;
    fun(k=fun(fun(k)));
    printf("%f\n", k);
    return 0;
}
int fun(int i)
{
    i++;
    return i;
}
A:
5.000000
B:
3.000000
C:
Garbage value
D:
4.000000
Answer: A
No answer description is available. Let's discuss.
What will be the output of the program?
#include<stdio.h>

int fun(int i)
{
    i++;
    return i;
}

int main()
{
    int fun(int);
    int i=3;
    fun(i=fun(fun(i)));
    printf("%d\n", i);
    return 0;
}
A:
5
B:
4
C:
Error
D:
Garbage value
Answer: A

Step 1: int fun(int); This is prototype of function fun(). It tells the compiler that the function fun() accept one integer parameter and returns an integer value.

Step 2: int i=3; The variable i is declared as an integer type and initialized to value 3.

Step 3: fun(i=fun(fun(i)));. The function fun(i) increements the value of i by 1(one) and return it.

Lets go step by step,

=> fun(i) becomes fun(3) is called and it returns 4.

=> i = fun(fun(i)) becomes i = fun(4) is called and it returns 5 and stored in variable i.(i=5)

=> fun(i=fun(fun(i))); becomes fun(5); is called and it return 6 and nowhere the return value is stored.

Step 4: printf("%d\n", i); It prints the value of variable i.(5)

Hence the output is '5'.

What will be the output of the program?
#include<stdio.h>
int fun(int(*)());

int main()
{
    fun(main);
    printf("Hi\n");
    return 0;
}
int fun(int (*p)())
{
    printf("Hello ");
    return 0;
}
A:
Infinite loop
B:
Hi
C:
Hello Hi
D:
Error
Answer: C
No answer description is available. Let's discuss.
If int is 2 bytes wide.What will be the output of the program?
#include <stdio.h>
void fun(char**);

int main()
{
    char *argv[] = {"ab", "cd", "ef", "gh"};
    fun(argv);
    return 0;
}
void fun(char **p)
{
    char *t;
    t = (p+= sizeof(int))[-1];
    printf("%s\n", t);
}
A:
ab
B:
cd
C:
ef
D:
gh
Answer: B

Since C is a machine dependent language sizeof(int) may return different values.

The output for the above program will be cd in Windows (Turbo C) and gh in Linux (GCC).

To understand it better, compile and execute the above program in Windows (with Turbo C compiler) and in Linux (GCC compiler).

Ad Slot (Above Pagination)
Quiz