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

Anila said:   2 decades ago
a++ increment its index t0 2.By *a means its content.so prints B.similarly next printf.

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

Haritha said:   1 decade ago
First a is initialized to 0. Then post increment follows.

Ans:BC.

Syed Azar said:   1 decade ago
Is this the value of the a[0] changes with a++?

Abhilasha said:   1 decade ago
What about the step fun(&a[0]);?

Rishabh said:   1 decade ago
@Abhilasha.

fun(&a[0]) means we pass the address of first element of an array..\

And when we increment it like a++ it means we just move to next element of array i.e., a[1].

Csk said:   1 decade ago
Why we not use fun(*a[0])instead of fun(&a[0])?

@Rishabh. Please tell me.

Sunny said:   1 decade ago
Initially pointer is set to 0.

And pointer is incremented in function.

So,
A of zero is A.
A++ will be A.

Again incremented.
That is C.

So BC.

Pavan said:   1 decade ago
Can we declare a function within main?

SWAGATH9849636443 said:   1 decade ago
I have a doubt.

a++ is post decrements. so it does not store change D the value.
So a++ is as same as a. How it increment?


Post your comments here:

Your comments will be displayed after verification.