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.

Pratik said:   1 decade ago
Wee when we are sending the addres of a[i], and incrementing it, so automaticaly address of a[i] will shift to a[i+1]. And hence it will print the content of net address a[i+1] as *a[i] is printed.

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].

Amar said:   1 decade ago
Since the array is declared of "char" type an increment i.e. a++ would make it point to the next of its type i.e. a[1] wich is B. Same for the next one.

MOHIT HACKEZ said:   1 decade ago
Here first we incremented it by i++. So a[0] will be a[1]. After tht again we incremented it by i++. So a[1] will be a[2]. So it will print as a[1]a[2] means BC.

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)

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.

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?

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.

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.

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.