Discarding Unwanted White Space in scanf - C Language Basics

C examples for Language Basics:scanf

Introduction

A white-space character in the control string causes scanf( ) to skip over one or more leading white-space characters.

A white-space character is either a space, a tab, vertical tab, formfeed, or a newline.

One white-space character in the control string causes scanf( ) to skip any number including zero of white -space characters.

Demo Code

#include <stdio.h>

int main(void)
{
   int i;//from ww  w  .  ja  v  a  2 s.c  om
   char str[80], str2[80];

   printf("skip white space testing, input one integer, two strings and lots of space in between.\n");

   scanf("%d %s %s", &i, str, str2);

   printf("%d %s %s", i, str, str2);

   return 0;
}

Result


Related Tutorials