Little tricky programme output [duplicate] - c

This question already has answers here:
using nested printf statements giving strange output
(3 answers)
Closed 8 years ago.
For the snippet I thought but couldn't find reason.
printf("%d",printf("tim"));
Why is this printing 3 with the output.the output is tim3. Why??

printf() returns the number of characters successfully it printed.So in this case it is 3
Check the man:
http://linux.die.net/man/3/printf
"tim" is 3 characters.
The inner printf() returned 3 after printing tim the outer printf() is using this value and printing out 3.

There are two printf()s.
the inner printf() [printf("tim")]executes first, prints "tim" and retuns the number of successfully printed characters[3 for tim].
The return of first printf() is input to the second printf() [printf("%d",printf("tim"));], effectively making it printf("%d", 3);. So, its printing 3.
I suggest you checking the retrun value of printf() in the man page. It states
Upon successful return, these functions return the number of
characters printed (excluding the null byte used to end output to
strings).

Related

strtok() as a parameter in printf() printing value in reverse [duplicate]

This question already has answers here:
function parameter evaluation order
(5 answers)
Closed 5 years ago.
If you have a string as below
str="insert 111,name,123456789"
when you pass it to strtok and try to print the values, they are output in reverse.
For example:
char* token=strtok(str," ");
printf("%s %s %s %s\n",token,strtok(NULL,","),strtok(NULL,","),strtok(NULL,","));
output: insert 1234567 name 1111
instead of:insert 111 name 123456789
Why is this happening? How can this be fixed?
parameters are pushed to the stack in the Calling Convention order, which in your case, reverse... therefore parameter 5 is first to evaluate and pushed in the stack, then 4,3,2 and the format string.
as many comments before suggested, your call style is very discouraged,
and should be avoided.

How do I explain the output of this simple C code?

#include<stdio.h>
int main()
{
int i=10;
printf("%d",printf("%d",i));
return(0);
}
Output in Turbo C
102
I am a beginner. So can you explain how this code works?
The documentation for printf states that it will return an integer that represents the number of characters written to the output stream.
That means you can use the return value of printf to satisfy a %d format specifier in another call to printf, and the second (outer) call will print out the number of characters written in the first call.
i is equal to 10, so the first call to printf outputs the number 10 and returns 2 (number of characters in the string "10"), which is passed to the second call to printf, which prints 2, giving you the final output 102.
Let's take apart the top level statement that produces the output:
printf("%d",printf("%d",i));
We have a function call of printf at the top level, passing two arguments to the function
The first argument of the top-level printf is the format string "%d"
The second argument of the top-level printf is the result of invoking printf("%d",i)
The argument of top-level printf, i.e. printf("%d",i), needs to be evaluated prior to making the call. The expression has a value, and a side effect. The side effect is printing "10" to the output, and the value is the number of characters printed, i.e. 2.
Since the arguments are evaluated prior to making a call, the printf("%d",i) is invoked first, producing the output 10. Now the top-level printf is invoked, and it produces the output 2, completing the "102" sequence that you see.
printf() is a C function. It returns an int value equal to the number of bytes it prints.
In your case, the INNER printf printed "10", so it wrote 2 bytes and will return 2.
The OUTER printf will therefore print "2".
Final result: "102" ("10" of the INNER followed by "2" of the OUTER).
Quoting C11, chapter §7.21.6.1
The fprintf function returns the number of characters transmitted, or a negative value
if an output or encoding error occurred.
In your case, the inner printf() call is the argument to the outer printf(), so the inner function call will be executed, as per the rule of function parameter evaluation.
So, in your case, first the inner printf() executes, printing the value of i, i.e., 10 (2 characters) and the return value of the printf() call is used as the argument to the %d format specifier in outer printf(), printing 2.
As there is no visual separator present, you see the outputs adjacent to each other, appearing as 102.

Sprintf function + order arguments [duplicate]

This question already has answers here:
Can the input and output strings to sprintf() be the same?
(2 answers)
Closed 7 years ago.
I have two string ta and tb with certain value, then I use the function sprintf to concatenate the two both in the variable ta, when I write
sprintf(ta,"%s+%s",ta,tb);
I get the string 1+2. but I need store in ta the string 2+1 then I trying
sprintf(ta,"%s+%s",tb,ta);
but I get the string 2+2+2+2+. I don't understand why happens that, Could you help me please?. Below the complete code
int main() {
char ta[5];
char tb[5];
sprintf(ta,"%d",1);
sprintf(tb,"%d",2);
sprintf(ta,"%s+%s",ta,tb);
//sprintf(ta,"%s+%s",tb,ta); uncomment for the second case
printf("taid:%s",ta);
}
sprintf(ta,"%s+%s",ta,tb);
sprintf(ta,"%s+%s",tb,ta);
Both lines of calling sprintf have undefined behavior. You are trying to copy ta to ta itself.
C11 §7.21.6.6 The sprintf function
The sprintf function is equivalent to fprintf, except that the output is written into an array (specified by the argument s) rather than to a stream. A null character is written at the end of the characters written; it is not counted as part of the returned value. If copying takes place between objects that overlap, the behavior is undefined.

return type of printf() in c [duplicate]

This question already has answers here:
Return value of printf() function in C
(13 answers)
Closed 8 years ago.
What would be the output of the following C program? (Is it a valid C program?)
#include <stdio.h>
int main()
{
int i=43;
printf("%d\n",printf("%d",printf("%d",i)));
return 0;
}
printf On success, the total number of characters written is returned
then console is :
4321
;)
This is explained in the documentation of printf eg printf(3) :
Return value
Upon successful return, these functions return the number of
characters printed (excluding the null byte used to end output to
strings).

what is the o/p of that program & how it happenes [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Confusion about the output..
#include<stdio.h>
void main()
{
int i=1,j=-1;
if(printf("%d",i)<printf("%d",j))
printf("%d",i);
else
printf("%d",j);
}
here in this program what is the output & how ?
printf returns the total number of characters written. "-1" is longer than "1". So…
The program invokes undefined behaviour because main has to be defined at least as int main() and return either an int or exit has to be called. Especially the output might not be flushed and thus empty.
Supposing the full output appears, the exact output is undefined because the operands of the < can be evaluated in either order. The rest looks trivial.
The if statement needs to be evaluated to take the branch. For this both of the printf calls (made in if statement) will be executed in either order. This will cause 1 and -1 to be printed in o/p buffer but not guaranteed in which datum will be printed first.
Now once value of if condition is known (false), the printf call inside else branch will be executed. It will print 1 in the buffer. At the end as part of exit handler, the o/p buffer will be flushed. This will cause 1-11 or -111 to be printed on the stdout.
answer is <<< 1 -1 1 >>>. Because printf statement returns int value ie number of character successfully written on screen{In if condition, first printf returns 1 and second printf return 2}. So that if condition becomes 1 < 2. This condition is true. So, execute the true block.

Resources