Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whitespace in the format string (scanf)

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?

like image 337
Vaibhav Agarwal Avatar asked Oct 11 '12 08:10

Vaibhav Agarwal


People also ask

Does scanf ignore whitespace?

This happens because scanf skips over white space when it reads numeric data such as the integers we are requesting here. White space characters are those characters that affect the spacing and format of characters on the screen, without printing anything visible.

How does scanf treat spaces?

Adding a whitespace character in a scanf() function causes it to read elements and ignore all the whitespaces as much as it can and search for a non-whitespace character to proceed. scanf("%d "); scanf(" %d"); scanf("%d\n"); This is different from scanf("%d"); function.

What is whitespace in string?

Whitespace is a string that has been pre-initialized and is used as a string constant. Whitespace is simply a character that is used for spacing and has an “empty” representation. It refers to tabs and spaces in the context of Python (it also includes exotic Unicode spaces).

What is scanf format string?

A scanf format string (scan formatted) is a control parameter used in various functions to specify the layout of an input string. The functions can then divide the string and translate into values of appropriate data types. String scanning functions are often supplied in standard libraries.


2 Answers

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.

like image 184
LihO Avatar answered Oct 06 '22 03:10

LihO


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"

like image 27
anatolyg Avatar answered Oct 06 '22 04:10

anatolyg