This question already has answers here:
Is scanf("%d%d", &x, &x) well defined?
(3 answers)
Closed 4 years ago.
I'm trying to handle user input for 2 following commands:
quit
open <n>
where is an integer.
Right now, my solution is the following:
char input_string[10];
int n;
int trail_index;
//<user input here>
sscanf(input_string, "%s%n %d%n", command, &trail_index, &n, &trail_index);
The trail_index is assigned correctly for me (4 in case of quit command, 6 in case of "open 1"), but since the program may be used with different compilers and platforms, the question is: is the behavior of sscanf guaranteed to work this way when you use the same variable multiple times, or is this undefined behavior that just happens to work for Visual C?
From this scanf (and family) reference
There is a sequence point after the action of each conversion specifier; this permits storing multiple fields in the same "sink" variable.
So this is indeed well-defined behavior and allowed.
Related
This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 5 years ago.
So i really want this to happen. I have an integer variable and i want to use that variable to give spacing in my printf function but C doesn't give me permission to do that , is there any way around it.
#include<stdio.h>
int main(void){
int s = 5;
printf("%sd",s);
}
Thanks a lot in advance!
The * in a format specifier means "I'll pass this number as an argument."
int s = 5;
printf("--%*d", s, s);
output
-- 5
Nice page with details here https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
Using * for the width will cause printf to take the width from the next argument, which should have type int:
printf("%*d", s, ValueToBePrinted);
You can find information about printf and other C features in the C standard. In the C 2011 version, formatted I/O is covered in clause 7.21, about <stdio.h>. 7.21.6.1 discusses fprintf, which uses the same format syntax as printf and is referred to from the printf clause, 7.21.6.3. Paragraph 5 discusses using * for a field width.
What do you mean by spacing here ? You can simply put ‘\t’ before ‘%d’ to give a tab before printing the value of the variable. Also ‘%sd’ doesn’t mean anything. Here %d is replaced by the value of your variable stated after the comma, ie, in your case it’s variable s.
This question already has answers here:
Is scanf("%d%d", &x, &x) well defined?
(3 answers)
Closed 6 years ago.
I'm using scanf to extract very regular tabular data. I need to go over it in two passes and can safely ignore some of the arguments some of the time.
I want to allocate space for only the strings I care about and a single "discard" string for the others. If I pass this argument multiple times as output, is the behavior defined?
A silly minimal example:
char label[MAX_SIZE], discard[MAX_SIZE];
sscanf(input, "%s %s %s %s", discard, label, discard, discard);
I cannot find any language in the C11 standard that makes your intended usage invalid.
There seems to be a better solution, though. If you place a * (“assignment suppression character”) after the % in the format string, then the scanf function will parse but not store the input item. Hence, you don't have to (must not, actually) provide a destination argument for it. Seems to be exactly what you need here.
If I pass this argument multiple times as output, is the behaviour defined?
Citing C11:
7.21.6.2 The fscanf function
int fscanf(FILE * restrict stream,
const char * restrict format, ...);
The fscanf function executes each directive of the format in turn. When all directives have been executed, or if a directive fails (as detailed below), the function returns.
The fscanf function (and therefore sscanf) executes each directive of the format in turn, therefore there should be no problems assigning to discard parameters multiple times, as the final assignment would have the final effect (overwriting the previous assignments).
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:
Wrong output from printf of a number
(8 answers)
Closed 8 years ago.
main()
{
printf ("%d",(3.0/2)*2) ;
}
The output of the following C program should be 3. Why am I getting 0?
The directive %d expects an integer (of type int), but you're passing a floating-point value (of type double).
Depending on the compiler, the processor, the exact content of the program, and the phase of the moon, this could do anything (it's undefined behavior): crash, print some bogus value, make daemons fly out of your nose… Here, it happens that the compiler generates code that fetches an integer value from somewhere which happens to contain the value 0 at that point.
To print the floating-point value, change the printf directive:
int main(void)
{
printf ("%f", (3.0/2)*2);
}
To print an integer, convert the argument with a cast:
int main(void)
{
printf ("%d", (int)((3.0/2)*2));
}
Good compilers warn you when you make such mistakes. Make sure to turn up your compiler's warning level.
Change 3.0/2 to 3.0/2.0
printf("%d",(int)((3.0/2.0)*2));
This question already has answers here:
Turbo C++: Why does printf print expected values, when no variables are passed to it?
(5 answers)
Closed 9 years ago.
I have just tried to know the output without supplying the variable instead just %d and there is no error in compiling the program but i wonder how the output displayed like below.
#include <stdio.h>
int main()
{
printf("%d");
return 0;
}
The output became 7288368
"why formatting input output requires variable to be supplied?"
Because the implementation of printf demands so. From the manual page of printf:
"Each conversion specification is introduced by the character %, and ends with a conversion specifier... The arguments must correspond properly (after type promotion) with the conversion specifier."
You have used "%d" format string, which expects a integral argument appropriate for decimal conversion, yet you haven't provided any arguments, which resulted in an undefined behavior