LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2013, 11:04 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
scanf and sscanf difference


I'm wondering if someone can explain why I have to hit ENTER twice if using the scanf() code line, but only once when using sscanf()code line in the following code.
Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
char phone[13];

printf("Enter phone number for worker\n");
fgets(phone, sizeof(phone), stdin);
scanf("%s", &phone);
//sscanf(phone,"%s", &phone);
printf("phone# is %s\n", phone);

return 0;
}
I don't see anything in MAN that describes this different action.

Last edited by atlantis43; 06-30-2013 at 11:05 AM.
 
Old 06-30-2013, 11:19 AM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by atlantis43 View Post
I'm wondering if someone can explain why I have to hit ENTER twice if using the scanf() code line, but only once when using sscanf()code line in the following code.
that's because scanf() reads input from stdin, while sscanf() reads from a string. This means that scanf() replaces fgets() and sscanf(). If you leave fgets() and still use scanf(), as your code shows ...

Code:
printf("Enter phone number for worker\n");
fgets(phone, sizeof(phone), stdin);
scanf("%s", &phone);
//sscanf(phone,"%s", &phone);
printf("phone# is %s\n", phone);
.. then what happens is this:
First fgets() reads a line from stdin and stores it in phone. That needs pressing the ENTER key once to finish the input
Then scanf attempts to read a second line of input from stdin. If you don't enter anything, but just hit ENTER again, tha previous value stored in phone remains unchanged. However, if you enter at least one character before the second ENTER, this will be your new input you receive in phone.

I strongly recommend the combination of fgets() and sscanf(), because that way you can prevent buffer overflows. Using scanf() only, you'll get a buffer overflow if the actual input is longer than the buffer you reserved for it, because there is no length check (AFAIK even scanf("%10s", ...) isn't safe, but I'm not absolutely sure about that). The result can be anything from unpredictable behavior to a segmentation fault.

[X] Doc CPU
 
1 members found this post helpful.
Old 06-30-2013, 01:01 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The main problem with scanf is that the user may enter anything they want, without your program being able to control it.
Example:

Code:
Enter your age:<wait for input> 10 20 30
What's your name:<no wait>
Hi, 20, where do you live:<no wait>
So you live in 30.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sscanf akif05 Programming 4 03-07-2011 09:13 AM
sscanf strangeness exvor Programming 11 05-04-2010 08:16 AM
sscanf kornerr Programming 4 09-16-2006 09:00 AM
another sscanf Q jnusa Programming 10 10-27-2004 02:33 AM
sscanf help jpc82 Programming 8 11-04-2003 02:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration