Saturday, December 21, 2013

SCANF() - Some important features.

scanf()
The scanf()is very powerful function. We can use it to scan the input but we can add something in it's arguments and make it very powerful function.

Assume that we have one variable : a[100];

To read a string:
             scanf("%[^\n]\n", a);
            // it means read until you meet '\n', then trash that '\n'
 
 
To read till a coma:
             scanf("%[^,]", a);
            // this one doesn't trash the coma

             scanf("%[^,],",a);
           // this one trashes the coma
 
If you want to skip some input, use * sign after %. 
For example you want to read last name from "John Smith" :
          scanf("%s %s", temp, last_name);
         // typical answer, using 1 temporary variable

         scanf("%s", last_name);
         scanf("%s", last_name);
        // another answer, only use 1 variable, but calls scanf twice

          scanf("%*s %s", last);
        // best answer, because you don't need extra temporary variable nor 
           calling scanf twice 
 
 

To know, Why should u not use turbo c++ ? click here

 
C language
  

0 comments:

Post a Comment