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 4 of 4.
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.
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.
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'.
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'.
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.
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.
Christopher said:
1 decade ago
I agree with Bagesh Kumar singh Point.
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
and again a++ a(3) value is 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.
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.
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.
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.
Anila said:
2 decades ago
a++ increment its index t0 2.By *a means its content.so prints B.similarly next printf.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers