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.

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.

MONOJIT CHATTERJEE said:   1 decade ago
Something is putting with 'A'or'B' that means ASCII value of that number is transferred. The value is 67 or 68. The address of the beginning of that array is taken by some pointer *a.

After that the address is incremented. Now the pointer is pointing to 2nd element. So it will print the *a (value of the address a). Again the address incremented and again the value of that address is got printed. So "%c" of ASCII value 68 and 67 means 'B', 'C'got printed.

Vishwas said:   1 decade ago
Here address of the first element of the character array a[100] is passed to the function,i.e. &a[0]
so a++ will increment the pointer to point to the next location of the array i.e. now pointer stores address of second element i.e. (&a[1]),
*a==> contents of the pointer is now a[1]= 'B'
similarly another increment will point to the next element
*a==>'C'
therefore, output will be BC

Shashi prakash said:   1 decade ago
1: We pass the address of a[0].

2. Let address of a (&a) is 500.

3. Then we increment the address of a (++a).

4. ++a (500+1) means then it goes to next address 502 (500+1 or 502).

5. Print the a[1] value B.

6. Again a++ (502+1) /*its update the &a 500 to 502*/ means then it goes to next address 502 (502+1 or 504).

7. Print the a[2] value C.

8. O/P = BC.

Kamlesh Sahu said:   1 decade ago
Step1: We pass the address of a[0].

Step2: Now when function called then value at a is 'A'.

Step3: Post increment a++ increment value of array a, now new value is 'B'.

Step4: Print the value stored at array a.

Step5: Again post increment the value of a array a, now new value is 'C'.

Step6: Print the value stored at array a.

So the output of the program is 'BC'.

Gagan Jeet Singh Walia said:   9 years ago
Anyone can explain that question clearly?

Because here this is post increment not pre-increment.
So if a++ then it will use the value of 'a' after a increments by 1.
So from 1st printf we should get 'A'.

Then again a++ again it will first use the value and then increment.
So from 2nd printf we should get 'B'.

Am I right?
(1)

Anjali said:   10 years ago
Hello can anyone explain?

void fun(char *a)
{
a++;//How increment a++ have output B? Because its 1st value is initialized so a is A then increment so the a=B...so how it print a=B first????
printf("%c",*a);
a++;
printf("%c",*a);
}

Bagesh Kumar Singh said:   1 decade ago
Here we pass the array in function. We pass the base address of the base array which is zero. a[0]=A, a[1]=B, a[2]=C, a[3]=D.

Now a++ it means increment the address of the address 1 which store the value is B. In same a++ it will be 2, which print C.

K vinayak said:   1 decade ago
Here in this program we are passing the starting address of array so it is received in *a and on pointers we can perform arithmatic operations a is incremented once and printed so the result is B it is again incremented and printed so the result C.

Sridhar said:   1 decade ago
*a means first we should see wat is address and 2ndly for which var address belongs to and wat is the value....here the address of *a is 1 and next a++ *a+1 is a(2) value is b
and again a++ a(3) value is c


Post your comments here:

Your comments will be displayed after verification.