0

I am trying to use scanf() for input. It worked perfectly except when I typed a space. Then it doesn't pick up the string -- why?

char idade;
scanf("%c", &idade);

I tried do to this:

scanf("%[^/n]c", &idade);

But it did not work. For example, when I typed in "Hello world", my string only contained "Hello" All I want is for it to recognize the space and take the full string. How can I do that?

6
  • 1
    Could you please update your question? As it is, there is no clear question, and it is really difficult to determine what you are trying to do and what assistance you are asking for. Commented Apr 5, 2014 at 17:17
  • Sorry I edited now... Commented Apr 5, 2014 at 17:28
  • @user3372120 You are reading a character or a string? Commented Apr 5, 2014 at 17:32
  • There is a good discussion about scanning strings with scanf here: stackoverflow.com/questions/5406935/reading-a-string-with-scanf Commented Apr 5, 2014 at 17:38
  • I still don't understand what you are after. You can use %s to read a string. However, since you are using Obj-C, there are better APIs than sscanf... if you can better describe the problem you are trying to solve. Commented Apr 5, 2014 at 17:38

2 Answers 2

2

The %c conversion specifier in the format string of scanf does not skip (read and discard) the leading whitespace characters. If you have a leading whitespace character in the format string, then this means scanf will skip any number of leading whitespace characters in the input which is probably what you want. Assuming you want to read a single character -

char idade;
scanf(" %c", &idade);
   //  ^ note the leading space 

However, if you want to read an input string from stdin, then

char input[50+1];  // max input length 50. +1 for the terminating null byte

// this skips any number of leading whitespace characters and then
// reads at most 50 non-whitespace chars or till a whitespace is encountered - 
// whichever occurs first, then adds a terminating null byte
scanf("%50s", input);  

// if you want to read a input line from the user
// then use fgets. this reads and stores at max 50 chars
// or till it encounters a newline which is also stored - whichever
// occurs first, then it adds a terminating null byte just like scanf
fgets(input, sizeof input, stdin);

Please read the man page of fgets.

Sign up to request clarification or add additional context in comments.

3 Comments

Error! If I digit in the console 'Hello WORLD', in input only have 'hello', so where did the string 'world' was?
@user3372120 "%s" will scan and discard <white-space> and then scan and save <non-white-space characters>. Rarely does one want to scan a "C string" (characters followed by a '\0'). One usually wants to scan/read a "line" (characters followed by a '\n'). To do that try scanf(" %50[^\n]", input); or use fgets().
if it's not asking too much as it would be using the fgets me an example?
1

There's a small error in your line

scanf("%[^/n]c", &idade);

It should read

scanf("%[^\n]", &idade);

Backslash '\' is the escape character, not '/'. You have to put '\n' (line feed character) in the exclusion list or else scanf will not know when to stop parsing. Your expression excluded '/' and 'n' as input characters.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.