0

I want to read some user input and do something with each input. I do that using this code:

char c;
while(1){
    printf("input");
    c = scanf ( "%s", &c ) ;
}

This works fine . But I need it to accept even an empty input. But it just continues to next line and expects an not-empty input. How could I do that?

Current situation:

input:asdf
input:b
input:c
input:d
input:e
input:

fjhkjh

Expected :

input:asdf
input:b
input:c
input:d
input:e
input:blabla
input:f
input:
input:

Just how the cmd console works like... UPDATE: I don't read only one single charcater, that was an example

4
  • You want %c instead of %s, and to get a simple char is better to use getchar and print with putchar, declare c as int Commented Sep 10, 2016 at 10:27
  • 1
    char c[3];//or more .. fgets(c, sizeof c, stdin); Commented Sep 10, 2016 at 10:31
  • When I use c = getchar() it will print an input: for each charcater introduced on the line before... Commented Sep 10, 2016 at 10:32
  • @BLUEPIXY that worked, can you post it as an answer ? :) Commented Sep 10, 2016 at 10:33

4 Answers 4

1

First of all You shouldn't use %s to store input in a single character as %s is for string of characters terminated by null character(\0). Don't use & with %s in scanf(). To store a single character make use of:

scanf("%c",&c);

or

c = getchar();
Sign up to request clarification or add additional context in comments.

5 Comments

Good to say that c should be declared as int instead of char using getchar() (in order to detect EOF)
@asdf, this was answered before your last edit, we can't read your mind :P
@AlterMann i know, my example was missleading, sorry about that
@asdf, no problem ;), then you want (as pointed out by @BLUEPIXY) fgets, but notice that the trailing newline is also included in the array.
Yes, I used that and chomped the newline. Thank you!
0

It doesn't internally work like that, you have declared it as single char but you have used %s which is a char array. the char you get as input are stored as null values.. every character you gave a b c d e are taken as null.. if you have doubt, add a printf("%s",c); below the scanf();

1 Comment

The char you get as input are stored as null values. or not stored, or stored as garbage ... you can not predict the behavior of undefined behavior
0

First of all, in your code,

  scanf ( "%s", &c ) ;

invokes undefined behavior. , as you cannot fit an incoming string inside a char.

That said, %s skips the leading white-spaces and reads the non-white-spaces up to the immediate white-space and ignores the last white-space, so you cannot read only white-spaces using %s.

Quoting C11, chapter §7.21.6.2, fscanf(), (emphasis mine)

s Matches a sequence of non-white-space characters.

If no l length modifier is present, the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character, which will be added automatically.

So, we can clearly see, c is no sufficient to hold even a single char input, either, it will be out of bound access to store the terminating null.

You seem to be in need to getchar() or similar.

Comments

0

First you must have to use getchar(); or %c in scanf for a single character then you can use a if for test which is the input character

char c;
while(1)
{
 c=getchar();
 if(c==' ')
  break();
}

This is only for single character input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.