Skip Non-White-Space Characters in scanf - C Language Basics

C examples for Language Basics:scanf

Introduction

A non-white-space character causes scanf() to read and discard matching characters.

For example, "%d,%d" causes scanf( ) to read an integer, read and discard a comma, and then read another integer.

If the specified character is not found, scanf( ) terminates.

If you want to read and discard a percent sign, use %% in the control string.

Demo Code

#include <stdio.h>

int main(void)
{
   int i;//from  w w  w  .  java 2 s  . co  m
   char str[80], str2[80];

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

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

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

   return 0;
}

Result


Related Tutorials