Why this output of this C program? - c

Program:
int main( )
{
printf("%d",printf("%d %d",5,5)&printf("%d %d",7,7));
return 0;
}
Output:
5 57 73
I am new to C, I could guess where the 5 57 7 came from, but no idea where the 3 came from. Can someone explain the output?

If you apply binary AND to 3 and 3 (which are the return values of both nested printf calls) you get 3 as result.
Note that the code actually contains undefined behaviour, since the order of the nested calls isn't defined.

The return value of the printf function is the number of characters transmitted, or a negative value if there is an error.
printf("%d %d",5,5) returns 3 if there is no error
printf("%d %d",7,7) also returns 3 if there is no error
So printf("%d %d",5,5) & printf("%d %d",7,7) is 3 & 3 which is evaluated to 3.

3 is the Bitwise AND of the values returned by two printf.
printf returns the numbers of characters printed.
In your case, printf("%d %d",5,5) has printed three characters that are two 5 and one space, similarly printf("%d %d",7,7) is also printing two 7 and one space. Hence both printf is returning 3.
so, 3 is the result of 3 & 3

as you can see here : http://en.wikipedia.org/wiki/Printf_format_string, printf return the number of printed chars, so:
printf("%d",printf("%d %d",5,5)&printf("%d %d",7,7));
is composed of :
printf("%d %d",5,5) return 3 (5 space and 5) and print 5 5
printf("%d %d",7,7) return 3 (7 space and 7) and print 7 7
At this stage we got : 5 57 7
And 3 & 3 = 3, finally you got this output:
5 57 73
Regards.

Related

Condtitions in void functions

Can someone please explain to me why in the following code, after it prints out 1 consecutively , the value for a increases again? Shouldn't it stop there, after the second 1?
#include <stdio.h>
void f(int a)
{
printf ("%d\n",a*a);
if (a>1)
f(a-1);
printf ("%d ",a*a);
}
int main()
{
f(5);
return 0;
}
The output is
25
16
9
4
1
1 4 9 16 25
Each recursively called function prints the passed value multiplied by itself two times
void f(int a)
{
printf ("%d\n",a*a);
//...
printf ("%d ",a*a);
}
In the last recursive call the value of a is not greater than 1. So the function outputs (I do not take into account the difference in the calls of printf relative to the new line character '\n')
25 <- first call
16 <- second call
9 <- third call
4 <- forth call
1 <- fifth call
1 <- fifth call
4 <- forth call
9 <- third call
16 <- second call
25 <- first call
That happens because you're printing twice. Just need once. Test:
#include <stdio.h>
void f(int a) {
printf ("%d\n",a*a);
if(a>1)
f(a-1);
}
int main() {
f(5);
return 0;
}
The output is now:
25
16
9
4
1
You are presenting a really nice example of recursion.
What is happening here is that the first print, the one at the top of the function is printing the first sets os a*a, while the final print comes from the end of the recursion, let's say, when the recursion is done is printing in its way out.
Printouts before the recursion calls.
25
16
9
4
1
1 4 9 16 25 -> line from recursion.

Not understanding a recursive function in C

#include <stdio.h>
void PrintNumPattern(int x, int y)
{
if (x > 0)
{
printf("%d ", x);
PrintNumPattern(x - y, y);
printf("%d ", x);//idk why this makes it work...why does it add?
} else {
printf("%d ", x);
}
}
int main(void) {
int num1;
int num2;
scanf("%d", &num1);
scanf("%d", &num2);
PrintNumPattern(num1, num2);
}
So currently I am learning about recursion and how it works. The code above is supposed to get an input, for example, 12, 3 and then output 12 9 6 3 0 3 6 9 12.so far I am so confused. Why does the printf,that I put a comment on, start adding after 0? The program only gets told to subtract not add. Also how does the program know to stop at 12?
start adding after 0?
It never adds anything. It's just that there are two printf calls in the first condition that print the same number - once before the recursive call and once after it.
Also how does the program know to stop at 12
The recursive call stops at 0. Then it returns to the parent call which will print it's number.
It will help if you write out the call tree. I've tried to visualise it as shown below. Each indent represents a function call from a parent function. On the right hand column I show the resulting output.
PrintNumPattern(12, 3)
printf(12) -------------------------> 12
PrintNumPattern(9, 3)
printf(9) ----------------------> 9
PrintNumPattern(6, 3)
printf(6) ------------------> 6
PrintNumPattern(3, 3)
printf(3) --------------> 3
PrintNumPattern(0, 3)
printf(0) ----------> 0
printf(3) --------------> 3
printf(6) ------------------> 6
printf(9) ----------------------> 9
printf(12) -------------------------> 12
Another way to look at it which may (or may not) be helpful is to visualise just one level of the recursive call. This clearly shows that there is no "addition" but only a repetition.
PrintNumPattern(12, 3)
printf(12) -------------------------> 12
PrintNumPattern(9, 3) --------------> 9 6 3 0 3 6 9
printf(12) -------------------------> 12
The reason the second printf was necessary is that you want to repeat what you printed on the way down to 0. It may look like you're adding but all you're doing is repeating your printf calls in reverse. Visualizations either by hand or tool can help build an intuition for recursive functions. See this example for a visualization of PrintNumPattern(2, 1)

Not getting desired output. Is there something inherently wrong in the logic?

the input depicts no of tests followed by size of input array followed by the array and the output either gives -1 or the square of the largest prime in the given array.
I am providing the code and the expected and actual output along with sample standard input used.
standard input:
3
5
1 4 6 8 10
3
2 2 9
2
156 13
expected output | getting
-1 -1
4 4
169 -1
#include <stdio.h>
int main(){
int test,size;
int i,j;
scanf("%d\n",&test);
while(test>=1){
scanf("%d\n",&size);
int data[size],factors=0,max=0;
for(i=0;i<size;i++){
scanf("%d ",&data[i]);
for(j=1;j<=data[i];j++){
if(data[i]%j==0){
factors+=1;
}
}
if((factors==2) && (data[i]>max)){
max=data[i];
}
}
if(max>=2){
printf("%d\n",max*max);
}else{
printf("%d\n",-1);
}
max=0;
test-=1;
}
}
ok debugged and got the answer. Factors needs to be initialized inside the next for loop.

I get an Unexpected Output

In my book , this code is given.They say that the output is 2 2 2 2 2 2 3 4 6 5
Please explain is this correct or not ? If not then what is the correct o/p?
#include <stdio.h>
#include <string.h>
main()
{
int c[]={2,8,3,4,4,6,7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++){
printf(" %d",*c);
++q;
}
for(j=0;j<5;j++){
printf(" %d",*p);
++p;
}
}
In first for-loop you are printing *c instead of *q:
printf(" %d",*c); // outputs `2 2 2 2 2` as first element, five times
should be:
printf(" %d",*q);
as I notice you increments q
output is 2 2 2 2 2 in first loop because of *c, c decays into address of fist element in this expression.
Edit
According to your code output should be as suggested by #ChronoTrigge (I notice latter):
First loop outputs five times 2 as I explained above
second loop will output first five elements in array a[] so output should be: 2 8 3 4 4
complete output: 2 2 2 2 2 2 8 3 4 4

Why does this recursive function generate this output?

I am trying to understand the output of the program printed below. When I look at it, I see that when printnum() is called with an argument of 1, "1" will be printed and then since 1<7, the function will call itself. This process will continue until "6" is printed and then printnum(7) is called. So, now "7" is printed and the if condition is not satisfied, so that code is skipped and we move to the second printf("%d", x) function where "7" is printed out again. There is nothing after the second printf("%d", x), so why doesn't everything end there? What makes the program keep going to print the numbers again in descending order?
#include <stdio.h>
int printnum ( int x )
{
printf("%d", x);
if ( x < 7 )
{
printnum ( x + 1 );
}
printf("%d",x);
}
int main()
{
printnum(1);
}
Output:
12345677654321
printnum(8) is never called, because it isn't true that 7 < 7.
The reason that you get the numbers printed in descending order once x = 7 is reached is, that each recursive call ends, leaving the previous call to continue.
Consider what it does for x = 1:
Print 1
Call recursively with x = 2
Print 1
If we expand this one level more:
Print 1
Print 2
Call recursively with x = 3
Print 2
Print 1
And one more:
Print 1
Print 2
Print 3
Call recursively with x = 4
Print 3
Print 2
Print 1
If you continue this expansion, you can see you get numbers in ascending order before the recursive call, and in descending order after the recursive call.
This happens because the second printf is called after your recursion exits at each level.
As your final recursive call printfs and ends, control transitions to the function that called it - the second-to-last recursive call. This call then exits the scope of the if statement, and calls printf, and then ends - upon which control transitions to the function that called it - the third-to-last recursive call. Repeat until you are inside the call to printnum(1), whose return takes you back to main.
It's recursion. When you enter the function you call printf, then you enter another level in the recursion, then you enter the printnum, so you call the printf with x+1 and so on.
When you arrive at stop condition (x==7), the function goes till the second printf (so 7 will be displayed again).
The function printnum terminates, so the program returns at the level above, then printnum at level 6 can printf again, terminate and return and so on.
Comments in-line!
int printnum ( int x ) x = 1 x = 2 x=6 x=7
{
printf("%d", x); prints 1 prints 2 prints 6 prints 7
if ( x < 7 ) Yes Yes Yes No
printnum ( x + 1 ); calls printnum(2) calls printnum(3) .... calls printnum(7) N/A
printf("%d",x); print 2 and return to print 6 and return prints 7 and returns to the
the caller which is the previous caller to the previous previous caller which is
main() which is printnum(1) caller which is printnum(x=6)
printnum(5)
}
Please check the following to print in ascending and descending order passing the starting value and the limit.. (please note that the error checking is not done!)
#include <stdio.h>
int printnum_123(int x, int limit)
{
printf("%d ", x);
if (x<limit)
printnum_123(x+1, limit);
return;
}
int printnum_321(int x, int limit)
{
if (x<limit)
printnum_321(x+1, limit);
printf("%d ", x);
return;
}
int main(void)
{
printnum_123(1, 10); printf("\n");
printnum_321(1, 10); printf("\n");
return 0;
}
$ ./a.out
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
$

Resources