Some advanced uses of scanf

View previous topic View next topic Go down

C Some advanced uses of scanf

Post by BIT0102-Mohaimin on Wed May 04, 2011 2:15 am

Scanf is perhaps more powerful than most of us think. I am presenting some advanced use of scanf I learnt recently, expecting to be helpful specially to Contest Problem-solvers.

Reading some predefined characters


You can read a string of some predefined characters with scanf. Suppose you want to read a string with only characters B, A, N, G, L, A, D, E, S, H.
Do this:
Code:
scanf("%[BANGLADESH]", s);
scanf will stop reading when ever something other than the characters within the square brackets will be read.
If the input is "ABDXBSDFS", the scanf will read "ABD".

The %[...] format specifier is called scanset.

Reading a line containing whitespaces


The '^' symbols means NOT inside the square brackets after %. Do this to read a line containing whitespace.
Code:
scanf("%[^\n]", s);
scanf will return only when it reads a newline character.
So, say good bye to gets() which produces a warning!

Skipping some characters when reading


Clockhand is an easy problem in UVa online judge. Most bigginers find it dificult because of the input specification. One way to take the input is:
Code:
int h, m;
scanf("%d", &h);
getchar();
scanf("%d", &m);
Read the hour, skipp the colon, read the minute.
You can do the whole thing with a single scanf
Code:
scanf("%d:%d", &h, &m);
The first integer will be read, then if found, a colon will be skipped, then another integer will be read.
You can skip an unknown data with a %* specifier.
Code:
scanf("%d%*c%d", &h, &m);
This will read two integer and skip a character between them.

Suppose an input specification is:
A floting point number in a line, then a character in another line.

And a sample input is
Code:
1.2
x
If you do:
Code:
scanf("%lf%c", &d, &ch);
you will read a double and a new line!
You can do this insteed to read the correct input:
Code:
scanf("%lf\n%c", &d, &ch);

Recall the way to read a line. With that, the new line at the end of line will not be read. So, you will need to add a '\n'. Like this:
Code:
scanf("%[^\n]\n", s);

The idea of putting the colon was given by Alim (bit0103-Ovid) (without knowing that it realy works!). I used this technique in many of the solutions those had extra characters in input than is needed.

Reading a fixed number of characters


Code:
scanf("%10s", s);
will read 10 characters.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

C Re: Some advanced uses of scanf

Post by BIT0122-Amit on Wed May 04, 2011 2:18 am

My Salutes to you dude. This is an awesome topic. BRAVO!!!! Clapping salute

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

C Re: Some advanced uses of scanf

Post by BIT0220-Iftekhar on Wed May 04, 2011 2:50 am

This is a fantastic topic. I didn't know (or the idea didn't come to my mind/even forgotten) most of the tricks,except the last one. It does help.
Hats off to Mohaimin vai.
It deserves a rep++
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

C Re: Some advanced uses of scanf

Post by BIT0115-Efat on Thu May 05, 2011 2:37 am

this is really a awesome post.
really I didn't know some of those before.
I am speechless to say something about it.
and not get enough salutes to give you a thanx and rep++
I guess it will not enough for you.
avatar
BIT0115-Efat
Service Release
Service Release

Course(s) :
  • BIT

Blood Group : O+
Posts : 779
Points : 1120

View user profile

Back to top Go down

C Re: Some advanced uses of scanf

Post by BIT0208-Shuvo on Fri May 06, 2011 12:47 am

That's really a great post , I didn't know any of them before you had shown me some of them . And here is all of it. It's really very much helpful to me .
And finally your treat Very Happy rep++
avatar
BIT0208-Shuvo
Global Moderator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 87
Points : 124

View user profile

Back to top Go down

C Re: Some advanced uses of scanf

Post by jeniferjohn on Wed Jul 27, 2011 10:26 pm

scanf("%[^\n]",line); // line is a string[/color]

This tells `scanf()` to read everything until the newline. Newline is
left in the input buffer, and may be read by subsequent calls.

> as an example, where scanf() acts like gets()


Making anything behave like `gets()` must be EVIL!

Think of what happens when there's more charaters in the input buffer
than you allowed for in your string buffer.

You're better off using `fgets()` and `sscanf()`.

> I try to look of more of this "advanced features" in documentation but
> couldn't found any.


Try the C Standard. You should be able to download the current version
of C99 plus TCs as N1124.pdf. Google for it.

> What "advance features" do you of these functions (printf and scanf)?


In my line of work, exactly none (don't even use the functions, let
alone their "advanced" features).

jeniferjohn
Pre-Alpha Release
Pre-Alpha Release

Course(s) :
  • N/A

Blood Group : NULL
Posts : 3
Points : 3

View user profile

Back to top Go down

C Re: Some advanced uses of scanf

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum