C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 7)
7.
What will be the output of the program?
#include<stdio.h>

int main()
{
    void fun(char*);
    char a[100];
    a[0] = 'A'; a[1] = 'B';
    a[2] = 'C'; a[3] = 'D';
    fun(&a[0]);
    return 0;
}
void fun(char *a)
{
    a++;
    printf("%c", *a);
    a++;
    printf("%c", *a);
}
AB
BC
CD
No output
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
40 comments Page 1 of 4.

Sujal panchal said:   2 years ago
The answer is wrong because in this he uses postfix at the use of postfix the output is AB.

If you use a prefix the Answer Is different output is BC.
(1)

Pallavi Kedarkar said:   4 years ago
Is it possible to declare user define a function in main function?

Please anyone explain.
(1)

Swati said:   6 years ago
What will be the answer if pre-increament will be there?

Pravallika said:   7 years ago
First, we print a value after we perform post-increment operation (i.e) output become a AB.
(1)

Pratima said:   8 years ago
a++ is a postfix operation so it should increase the value after the statement, so why BC??

Bhumi said:   8 years ago
void fun(char *a) is not declared before main. So why it is not giving an error?

Shabana said:   8 years ago
Here, we call the function fun by passing parameter which is called by reference, so we have to use pointer variable in the parameter of function "fun(*a)".

Now the fun(*a) is equivalent to
fun(*(&a[0]))=fun('A').

The ASCII value if A is 65.
As it is incremented by 1 i.e a++.
So it becomes 66 which the ASCII value of B and then using printf statement the letter printed is B. Again, the value of a is incremented by 1 n then printed so it becomes 67 which is value of C.

Supriya said:   8 years ago
Yes, I agree. Thank you all.

Jancy rani said:   9 years ago
a++ is post increment.

Sujith said:   9 years ago
Here there is no declaration and moreover return is void. How it will execute?

Please explain clearly.


Post your comments here:

Your comments will be displayed after verification.