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 2 of 4.

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)

Niharika said:   9 years ago
Not getting this, Can anyone explain me?

Manisha said:   9 years ago
Please explain clearly.

Anitha said:   9 years ago
Please, anyone explain, what is the exact difference between pre increment and post increment?

SVP said:   10 years ago
What wiil be the answer when if we replace a++ to ++a ?

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

Dinesh kumar said:   10 years ago
They didn't declared the prototype of fun. How can we call the function inside the main? Answer was no output.

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.

Atib said:   1 decade ago
The function is not declared before it is invoked, why is the code still running fine?

Rakhi said:   1 decade ago
Should it be an error? Since compiler acknowledge an array by its base address, changing the address will cause the error.


Post your comments here:

Your comments will be displayed after verification.