Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the effect of trailing white space in a scanf() format string?

Tags:

c

scanf

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);?

like image 829
Vikas Verma Avatar asked Oct 21 '13 15:10

Vikas Verma


People also ask

Why does scanf stop white-space?

scanf() just stops once it encounters a whitespace as it considers this variable "done".

Does scanf consume whitespace?

Further it does not apply to "%s" as "%s" consumes leading white-space with or without a leading space. All scanf() input specifiers ignore leading spaces, except 3: "%c" , "%n" , "%[]" . Simple directives do benefit with the leading space as in the following. Previous left-over white-space is consumed before '$' .

What action is carried out by scanf if a user enters any white-space characters such as tabs and newlines?

Notes: 1. The user can enter the values separated by any white-space, that is, separated by spaces, tabs or newlines, as desired, in any mix. The scanf function skips over (and ignores) white-space except when doing a %c format specifier.

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.


1 Answers

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.

like image 65
Chris Dodd Avatar answered Sep 28 '22 08:09

Chris Dodd