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);
}
Discussion:
40 comments Page 2 of 4.
Shri said:
1 decade ago
a[0]='A' Store a[0]=97.
Then a++=98=B.
a++=99=C.
So that Answer is BC.
Then a++=98=B.
a++=99=C.
So that Answer is BC.
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
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.
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].
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.
@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.
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?
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers