whitespace in the format string (scanf) - c

Consider the following code:
#include<stdio.h>
int main() {
int i=3, j=4;
scanf("%d c %d",&i,&j);
printf("%d %d",i,j);
return 0;
}
It works if I give 2c3 or 2 c 3 or 2c 3 as input if I have to change the value of variables. What should I do if I want the user to enter the same pattern as I want means if %dc%d then only 2c3 is acceptable and not 2 c 3 and vice versa if it is %d c %d?

Whitespace in the format string matches 0 or more whitespace characters in the input.
So "%d c %d" expects number, then any amount of whitespace characters, then character c, then any amount of whitespace characters and another number at the end.
"%dc%d" expects number, c, number.
Also note, that if you use * in the format string, it suppresses assignment:
%*c = read 1 character, but don't assign it to any variable
So you can use "%d%*c c%*c %d" if you want to force user to enter: number, at least 1 character followed by any amount of whitespace characters, c, at least 1 character followed by any amount of whitespace characters again and number.

If you want to accept 1c2 but not 1 c 2, use the pattern without the space:
scanf("%dc%d", &x, &y);
If you want to accept 1c2 and 1 c 2 (and also 1 \t \t c \t 2 etc), use the pattern with the space:
scanf("%d c %d", &x, &y);
If you want to accept 1 c 2 but not 1c2, add a fake string containing whitespace:
scanf("%d%*[ \t]c%*[ \t]%d", &x, &y);
Here the format string %[ \t] would mean "read a string that contains any number of space and tab characters"; but using the additional *, it becomes "expect a string that contains any number of space and tab characters; then discard it"

I think I would read the scanf result into different variables (i.e. not reuse i and j) as "%d%s%d". Then check the string you got from the %s and if it matches your requirements, use the other variables to overwrite i and j.

Force a string parsing first :
char a[100], b[100];
scanf("%99s c %99s", a, b);
Then use sscanf() to convert the strings to int.

Related

How does a single space make effect in scanf() function while taking input in an Array? [duplicate]

What is difference between scanf("%d") and scanf("%d ") in this code, where the difference is the trailing blank in the format string?
#include <stdio.h>
int main(void)
{
int i, j;
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d\n", j);
printf("enter a value for i ");
scanf("%d", &i);
printf("i is %d\n", i);
return 0;
}
How does the scanf() function actually work when I add spaces after the format specifier like scanf("%d ", &j);?
A whitespace character in a scanf format causes it to explicitly read and ignore as many whitespace characters as it can. So with scanf("%d ", ..., after reading a number, it will continue to read characters, discarding all whitespace until it sees a non-whitespace character on the input. That non-whitespace character will be left as the next character to be read by an input function.
With your code:
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d \n", j);
it will print the first line and then wait for you to enter a number, and then continue to wait for something after the number. So if you just type 5Enter, it will appear to hang — you need to type in another line with some non-whitespace character on it to continue. If you then type 6Enter, that will become the value for i, so your screen will look something like:
enter a value for j 5
6
j is 5
enter a value for i i is 6
Also, since most scanf %-conversions also skip leading whitespace (all except for %c, %[ and %n), spaces before %-conversions are irrelevant ("%d" and " %d" will act identically). So for the most part, you should avoid spaces in scanf conversions unless you know you specifically need them for their peculiar effect.
A white-space character (space, newline, horizontal and vertical tab) in a format string matches any number of white-space characters in the input.
In your first case
scanf("%d ",&j);
when it encounters the white-space char (WSC) ' ' then it will eat all the white spaces input by user including \n on pressing Enter and it will expect to enter a non-WSC . In this case your program will terminate by pressing Ctrl + Z.
A whitespace character in your scanf format matches any number of whitespace characters as described by isspace. So if you have tailing spaces, newlines, tabulators or any other whitespace character then it will also be consumed by scanf before it returns.
The difference (although obvious) is a different format string. If you enter the following line:
"3  "
scanf() will return successfully. Otherwise, it depends on your input provided. scanf() essentially skips over whitespace (tabs, spaces, newlines), and searches for alphanumeric values in the input stream. Since this is trailing whitespace, it gets lumped in with the trailing newline character at the end of input when pressing ENTER, so it's of little consequence.
scanf() expects the input provided to exactly match the format string you provide to it, with the exception that contiguous whitespace characters are compressed to a single whitespace character. This becomes very important if you want to parse large strings of data with it's string-processing equivalent, sscanf().
A good exercise to further test this would be something like:
#include<stdio.h>
int main(void)
{
int a=0,b=0,c=0;
printf("Enter values for A, B, C, in the format: \"A B - C\"\n");
scanf("%d %d - %d", &a, &b, &c);
printf("Values: A:%d, B:%d, C:%d\n", a, b, c);
}
Afterwards, check and see what the values of these integers are after providing both correctly and incorrectly formatted consoled input (ie: spaces and hyphens). Here are a couple example runs. The first used incorrect input, the second used correctly formatted input. Notice that in the first case, C doesn't even get set, as scanf() will provided unexpected behavior if the input and the format strings don't match up. In general, you are better off using something like fgets() to get a string of input from the user, and then use various search functions (ie: strstr(), strch(), strcat, strcpy, etc) to parse your string, as it is much safer than just using scanf() and assuming the user won't make a mistake, either accidentally or deliberately.
Enter values for A, B, C, in the format: "A B - C"
1 2 3
Values: A:1, B:2, C:0
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Now, consider one last run: You'll see that scanf() compacts multiple consecutive whitespace characters to a single character, hence why these final runs actually succeeds:
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3

Program crashes when trying to store a struct value, and login system doesn't work [duplicate]

What is difference between scanf("%d") and scanf("%d ") in this code, where the difference is the trailing blank in the format string?
#include <stdio.h>
int main(void)
{
int i, j;
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d\n", j);
printf("enter a value for i ");
scanf("%d", &i);
printf("i is %d\n", i);
return 0;
}
How does the scanf() function actually work when I add spaces after the format specifier like scanf("%d ", &j);?
A whitespace character in a scanf format causes it to explicitly read and ignore as many whitespace characters as it can. So with scanf("%d ", ..., after reading a number, it will continue to read characters, discarding all whitespace until it sees a non-whitespace character on the input. That non-whitespace character will be left as the next character to be read by an input function.
With your code:
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d \n", j);
it will print the first line and then wait for you to enter a number, and then continue to wait for something after the number. So if you just type 5Enter, it will appear to hang — you need to type in another line with some non-whitespace character on it to continue. If you then type 6Enter, that will become the value for i, so your screen will look something like:
enter a value for j 5
6
j is 5
enter a value for i i is 6
Also, since most scanf %-conversions also skip leading whitespace (all except for %c, %[ and %n), spaces before %-conversions are irrelevant ("%d" and " %d" will act identically). So for the most part, you should avoid spaces in scanf conversions unless you know you specifically need them for their peculiar effect.
A white-space character (space, newline, horizontal and vertical tab) in a format string matches any number of white-space characters in the input.
In your first case
scanf("%d ",&j);
when it encounters the white-space char (WSC) ' ' then it will eat all the white spaces input by user including \n on pressing Enter and it will expect to enter a non-WSC . In this case your program will terminate by pressing Ctrl + Z.
A whitespace character in your scanf format matches any number of whitespace characters as described by isspace. So if you have tailing spaces, newlines, tabulators or any other whitespace character then it will also be consumed by scanf before it returns.
The difference (although obvious) is a different format string. If you enter the following line:
"3  "
scanf() will return successfully. Otherwise, it depends on your input provided. scanf() essentially skips over whitespace (tabs, spaces, newlines), and searches for alphanumeric values in the input stream. Since this is trailing whitespace, it gets lumped in with the trailing newline character at the end of input when pressing ENTER, so it's of little consequence.
scanf() expects the input provided to exactly match the format string you provide to it, with the exception that contiguous whitespace characters are compressed to a single whitespace character. This becomes very important if you want to parse large strings of data with it's string-processing equivalent, sscanf().
A good exercise to further test this would be something like:
#include<stdio.h>
int main(void)
{
int a=0,b=0,c=0;
printf("Enter values for A, B, C, in the format: \"A B - C\"\n");
scanf("%d %d - %d", &a, &b, &c);
printf("Values: A:%d, B:%d, C:%d\n", a, b, c);
}
Afterwards, check and see what the values of these integers are after providing both correctly and incorrectly formatted consoled input (ie: spaces and hyphens). Here are a couple example runs. The first used incorrect input, the second used correctly formatted input. Notice that in the first case, C doesn't even get set, as scanf() will provided unexpected behavior if the input and the format strings don't match up. In general, you are better off using something like fgets() to get a string of input from the user, and then use various search functions (ie: strstr(), strch(), strcat, strcpy, etc) to parse your string, as it is much safer than just using scanf() and assuming the user won't make a mistake, either accidentally or deliberately.
Enter values for A, B, C, in the format: "A B - C"
1 2 3
Values: A:1, B:2, C:0
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Now, consider one last run: You'll see that scanf() compacts multiple consecutive whitespace characters to a single character, hence why these final runs actually succeeds:
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3

Visual Studio C Scanf scanning twice instead of once, only reading once [duplicate]

What is difference between scanf("%d") and scanf("%d ") in this code, where the difference is the trailing blank in the format string?
#include <stdio.h>
int main(void)
{
int i, j;
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d\n", j);
printf("enter a value for i ");
scanf("%d", &i);
printf("i is %d\n", i);
return 0;
}
How does the scanf() function actually work when I add spaces after the format specifier like scanf("%d ", &j);?
A whitespace character in a scanf format causes it to explicitly read and ignore as many whitespace characters as it can. So with scanf("%d ", ..., after reading a number, it will continue to read characters, discarding all whitespace until it sees a non-whitespace character on the input. That non-whitespace character will be left as the next character to be read by an input function.
With your code:
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d \n", j);
it will print the first line and then wait for you to enter a number, and then continue to wait for something after the number. So if you just type 5Enter, it will appear to hang — you need to type in another line with some non-whitespace character on it to continue. If you then type 6Enter, that will become the value for i, so your screen will look something like:
enter a value for j 5
6
j is 5
enter a value for i i is 6
Also, since most scanf %-conversions also skip leading whitespace (all except for %c, %[ and %n), spaces before %-conversions are irrelevant ("%d" and " %d" will act identically). So for the most part, you should avoid spaces in scanf conversions unless you know you specifically need them for their peculiar effect.
A white-space character (space, newline, horizontal and vertical tab) in a format string matches any number of white-space characters in the input.
In your first case
scanf("%d ",&j);
when it encounters the white-space char (WSC) ' ' then it will eat all the white spaces input by user including \n on pressing Enter and it will expect to enter a non-WSC . In this case your program will terminate by pressing Ctrl + Z.
A whitespace character in your scanf format matches any number of whitespace characters as described by isspace. So if you have tailing spaces, newlines, tabulators or any other whitespace character then it will also be consumed by scanf before it returns.
The difference (although obvious) is a different format string. If you enter the following line:
"3  "
scanf() will return successfully. Otherwise, it depends on your input provided. scanf() essentially skips over whitespace (tabs, spaces, newlines), and searches for alphanumeric values in the input stream. Since this is trailing whitespace, it gets lumped in with the trailing newline character at the end of input when pressing ENTER, so it's of little consequence.
scanf() expects the input provided to exactly match the format string you provide to it, with the exception that contiguous whitespace characters are compressed to a single whitespace character. This becomes very important if you want to parse large strings of data with it's string-processing equivalent, sscanf().
A good exercise to further test this would be something like:
#include<stdio.h>
int main(void)
{
int a=0,b=0,c=0;
printf("Enter values for A, B, C, in the format: \"A B - C\"\n");
scanf("%d %d - %d", &a, &b, &c);
printf("Values: A:%d, B:%d, C:%d\n", a, b, c);
}
Afterwards, check and see what the values of these integers are after providing both correctly and incorrectly formatted consoled input (ie: spaces and hyphens). Here are a couple example runs. The first used incorrect input, the second used correctly formatted input. Notice that in the first case, C doesn't even get set, as scanf() will provided unexpected behavior if the input and the format strings don't match up. In general, you are better off using something like fgets() to get a string of input from the user, and then use various search functions (ie: strstr(), strch(), strcat, strcpy, etc) to parse your string, as it is much safer than just using scanf() and assuming the user won't make a mistake, either accidentally or deliberately.
Enter values for A, B, C, in the format: "A B - C"
1 2 3
Values: A:1, B:2, C:0
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Now, consider one last run: You'll see that scanf() compacts multiple consecutive whitespace characters to a single character, hence why these final runs actually succeeds:
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3

C input 2 time and examine previous value [duplicate]

What is difference between scanf("%d") and scanf("%d ") in this code, where the difference is the trailing blank in the format string?
#include <stdio.h>
int main(void)
{
int i, j;
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d\n", j);
printf("enter a value for i ");
scanf("%d", &i);
printf("i is %d\n", i);
return 0;
}
How does the scanf() function actually work when I add spaces after the format specifier like scanf("%d ", &j);?
A whitespace character in a scanf format causes it to explicitly read and ignore as many whitespace characters as it can. So with scanf("%d ", ..., after reading a number, it will continue to read characters, discarding all whitespace until it sees a non-whitespace character on the input. That non-whitespace character will be left as the next character to be read by an input function.
With your code:
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d \n", j);
it will print the first line and then wait for you to enter a number, and then continue to wait for something after the number. So if you just type 5Enter, it will appear to hang — you need to type in another line with some non-whitespace character on it to continue. If you then type 6Enter, that will become the value for i, so your screen will look something like:
enter a value for j 5
6
j is 5
enter a value for i i is 6
Also, since most scanf %-conversions also skip leading whitespace (all except for %c, %[ and %n), spaces before %-conversions are irrelevant ("%d" and " %d" will act identically). So for the most part, you should avoid spaces in scanf conversions unless you know you specifically need them for their peculiar effect.
A white-space character (space, newline, horizontal and vertical tab) in a format string matches any number of white-space characters in the input.
In your first case
scanf("%d ",&j);
when it encounters the white-space char (WSC) ' ' then it will eat all the white spaces input by user including \n on pressing Enter and it will expect to enter a non-WSC . In this case your program will terminate by pressing Ctrl + Z.
A whitespace character in your scanf format matches any number of whitespace characters as described by isspace. So if you have tailing spaces, newlines, tabulators or any other whitespace character then it will also be consumed by scanf before it returns.
The difference (although obvious) is a different format string. If you enter the following line:
"3  "
scanf() will return successfully. Otherwise, it depends on your input provided. scanf() essentially skips over whitespace (tabs, spaces, newlines), and searches for alphanumeric values in the input stream. Since this is trailing whitespace, it gets lumped in with the trailing newline character at the end of input when pressing ENTER, so it's of little consequence.
scanf() expects the input provided to exactly match the format string you provide to it, with the exception that contiguous whitespace characters are compressed to a single whitespace character. This becomes very important if you want to parse large strings of data with it's string-processing equivalent, sscanf().
A good exercise to further test this would be something like:
#include<stdio.h>
int main(void)
{
int a=0,b=0,c=0;
printf("Enter values for A, B, C, in the format: \"A B - C\"\n");
scanf("%d %d - %d", &a, &b, &c);
printf("Values: A:%d, B:%d, C:%d\n", a, b, c);
}
Afterwards, check and see what the values of these integers are after providing both correctly and incorrectly formatted consoled input (ie: spaces and hyphens). Here are a couple example runs. The first used incorrect input, the second used correctly formatted input. Notice that in the first case, C doesn't even get set, as scanf() will provided unexpected behavior if the input and the format strings don't match up. In general, you are better off using something like fgets() to get a string of input from the user, and then use various search functions (ie: strstr(), strch(), strcat, strcpy, etc) to parse your string, as it is much safer than just using scanf() and assuming the user won't make a mistake, either accidentally or deliberately.
Enter values for A, B, C, in the format: "A B - C"
1 2 3
Values: A:1, B:2, C:0
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Now, consider one last run: You'll see that scanf() compacts multiple consecutive whitespace characters to a single character, hence why these final runs actually succeeds:
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3

What actually happens when format specifier in scanf() is followed by a space? [duplicate]

What is difference between scanf("%d") and scanf("%d ") in this code, where the difference is the trailing blank in the format string?
#include <stdio.h>
int main(void)
{
int i, j;
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d\n", j);
printf("enter a value for i ");
scanf("%d", &i);
printf("i is %d\n", i);
return 0;
}
How does the scanf() function actually work when I add spaces after the format specifier like scanf("%d ", &j);?
A whitespace character in a scanf format causes it to explicitly read and ignore as many whitespace characters as it can. So with scanf("%d ", ..., after reading a number, it will continue to read characters, discarding all whitespace until it sees a non-whitespace character on the input. That non-whitespace character will be left as the next character to be read by an input function.
With your code:
printf("enter a value for j ");
scanf("%d ",&j);
printf("j is %d \n", j);
it will print the first line and then wait for you to enter a number, and then continue to wait for something after the number. So if you just type 5Enter, it will appear to hang — you need to type in another line with some non-whitespace character on it to continue. If you then type 6Enter, that will become the value for i, so your screen will look something like:
enter a value for j 5
6
j is 5
enter a value for i i is 6
Also, since most scanf %-conversions also skip leading whitespace (all except for %c, %[ and %n), spaces before %-conversions are irrelevant ("%d" and " %d" will act identically). So for the most part, you should avoid spaces in scanf conversions unless you know you specifically need them for their peculiar effect.
A white-space character (space, newline, horizontal and vertical tab) in a format string matches any number of white-space characters in the input.
In your first case
scanf("%d ",&j);
when it encounters the white-space char (WSC) ' ' then it will eat all the white spaces input by user including \n on pressing Enter and it will expect to enter a non-WSC . In this case your program will terminate by pressing Ctrl + Z.
A whitespace character in your scanf format matches any number of whitespace characters as described by isspace. So if you have tailing spaces, newlines, tabulators or any other whitespace character then it will also be consumed by scanf before it returns.
The difference (although obvious) is a different format string. If you enter the following line:
"3  "
scanf() will return successfully. Otherwise, it depends on your input provided. scanf() essentially skips over whitespace (tabs, spaces, newlines), and searches for alphanumeric values in the input stream. Since this is trailing whitespace, it gets lumped in with the trailing newline character at the end of input when pressing ENTER, so it's of little consequence.
scanf() expects the input provided to exactly match the format string you provide to it, with the exception that contiguous whitespace characters are compressed to a single whitespace character. This becomes very important if you want to parse large strings of data with it's string-processing equivalent, sscanf().
A good exercise to further test this would be something like:
#include<stdio.h>
int main(void)
{
int a=0,b=0,c=0;
printf("Enter values for A, B, C, in the format: \"A B - C\"\n");
scanf("%d %d - %d", &a, &b, &c);
printf("Values: A:%d, B:%d, C:%d\n", a, b, c);
}
Afterwards, check and see what the values of these integers are after providing both correctly and incorrectly formatted consoled input (ie: spaces and hyphens). Here are a couple example runs. The first used incorrect input, the second used correctly formatted input. Notice that in the first case, C doesn't even get set, as scanf() will provided unexpected behavior if the input and the format strings don't match up. In general, you are better off using something like fgets() to get a string of input from the user, and then use various search functions (ie: strstr(), strch(), strcat, strcpy, etc) to parse your string, as it is much safer than just using scanf() and assuming the user won't make a mistake, either accidentally or deliberately.
Enter values for A, B, C, in the format: "A B - C"
1 2 3
Values: A:1, B:2, C:0
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Now, consider one last run: You'll see that scanf() compacts multiple consecutive whitespace characters to a single character, hence why these final runs actually succeeds:
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3
Enter values for A, B, C, in the format: "A B - C"
1 2 - 3
Values: A:1, B:2, C:3

Resources