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.
Related
This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Using printf with a non-null terminated string
(6 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
char as[4];
*as='0';
*(as+1)='1';
*(as+2)='2';
*(as+3)='3';
printf("%s",as);
return 0;
}
The output i got is : 0123.
In the above program i declared an array of size 4 -> char as[4];
and in that i stored 4 chars 0,1,2,3.
Normally if we declare a char array, '\0' will be stored at last (i.e. as[3]='\0');
but i stored '3' in it. how it did not generate error?.
There is absolutely nothing stopping you from using an array of char as an array of char. There could be any number of reasons to want this.
However, C strings are null-terminated by definition. Using the %s specifier in printf tells it to expect a null-terminated string, so your program will (probably) not work correctly unless you give it such a string.
This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 2 years ago.
// If I have an int, eg
int num=3;
//then how can I do this
printf(" %nums", some_string);
// to get it right aligned by 3 characters
context: I need to use a loop to print statements with variable alignments depending on the order they are printed, if I cant use a variable I cant do that.
Yes, it's in the printf manpage:
Instead of a decimal digit string one may write "*" or "*m$" (for some
decimal integer m) to specify that the field width is given in the
next argument, or in the m-th argument, respectively, which must be of
type int.
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.
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).
This question already has answers here:
Formatted and unformatted input and output and streams
(3 answers)
Closed 9 years ago.
I want to know what is meant by formatted in the printf and scanf functions of the C language. I am new to C programming and did not understand what is meant by formatted. Can anyone please give me the appropriate explanation?
printf("%s %d", "abc", 123);
%s %d is the format string and contains instructions for what to do with the arguments. In this case, it says print string, print a space, then print an integer.
As in formatted output
So you can 'print' say a double called myMoney of 11003.145 as $11,003.15.