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 1 of 4.
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.
If you use a prefix the Answer Is different output is BC.
(1)
Pallavi Kedarkar said:
4 years ago
Is it possible to declare user define a function in main function?
Please anyone explain.
Please anyone explain.
(1)
Pravallika said:
7 years ago
First, we print a value after we perform post-increment operation (i.e) output become a AB.
(1)
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?
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)
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.
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.
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.
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.
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);
}
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);
}
SVP said:
10 years ago
What wiil be the answer when if we replace a++ to ++a ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers