LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-15-2006, 03:51 PM   #1
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
basic scanf question


I read you can limit number of character get with %s by inserting a number(e.g. %5s").

But the below program still generate seg fault. Any comment?

Code:
#include <stdio.h>

int main()
{
  char myString[5];
  scanf("%5s", myString);

}
 
Old 07-15-2006, 04:01 PM   #2
urzumph
Member
 
Registered: Jan 2004
Location: Australia
Distribution: Debian
Posts: 168

Rep: Reputation: 30
I'm surprised that even compiles. scanf only takes pointers... you are giving it a char instead of a pointer to a char.

Edit : To little sleep, not enough reading before posting... myString is an array and therefore equivalent to a pointer

Also, I am reasonably sure that you need to make the array one char larger to allow for the NULL terminator.

Last edited by urzumph; 07-15-2006 at 04:02 PM.
 
Old 07-15-2006, 04:03 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
you could use fgets...

char *fgets(char *s, int size, FILE *stream);

btw.. i dont know if there is a way to make scanf stop at a size..
 
Old 07-15-2006, 04:03 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
First fgets() is better than scanf() for reading a line from stdin.
Second, you should be char mystring[6] to read a 5 char string, need room for the null terminator on the end.
Third, it should not segfault the way you have it because the compiler pads 5 bytes to 8 or 16 depending on architecture.
 
  


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
C string scanf question????????? skie_knite007 Programming 6 12-12-2005 03:07 PM
scanf question exvor Programming 5 07-24-2005 04:36 PM
scanf question jnusa Programming 4 01-07-2005 09:04 AM
array of structures scanf entry question ... purpleburple Programming 21 10-01-2002 11:08 PM
Very Basic Question Terri Linux - Newbie 6 10-17-2001 01:31 AM

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

All times are GMT -5. The time now is 04:15 AM.

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