-3

I want to read inputs individually. That means that I enter a character, press enter and then ask for the second input. The problem is that when the program asks me to input the character it asks for both at the same time.

This is my code:

#include <stdio.h>

int main(){
    printf("Enter a character \n");

    char a = 0;
    a = getchar();

    printf("You entered: %c \n",a);

    putchar(a);

    printf("\n");

    char b[2];
    puts(b);
    gets(b);

    printf("You entered: %c \n",b[0]);

    puts(b);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Do not use `gets`, this is an accident waiting to happen. Use `fgets` instead. – Pablo Jan 27 '18 at 21:14
  • Follow the link tor the full story on [Why `gets()` is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) Using it with `char b[2];` is beyond foolhardy. – Jonathan Leffler Jan 27 '18 at 21:17
  • 2
    1. The computer does exactly what you tell it to. Nothing more, nothing less. If you want to get one character at a time, you must use the right code to do so. 2. `getchar()` reads only one character. When the user types a character then presses enter, there are **two** characters in the input: the character you want plus a "new line" character. You must consume this newline character in order to continue wiht other input. – Code-Apprentice Jan 27 '18 at 21:17
  • 1
    Note that the first `puts(b);` outputs indeterminate data; the array `b` is not initialized, so you've no idea what will be printed. Also note that `getchar()` returns an `int`, not just a `char`. Assigning the value to a `char` will lead to problems. – Jonathan Leffler Jan 27 '18 at 21:18

3 Answers3

0

Use a for loop, within a while loop

char b[2];
int x;

while(b)
{
    for(x=0;x<2;x++)
    {
    printf("Enter a character: ");
    scanf("%c",b[x]);
    printf("%c",b[x]);
    }
 b++;
}return;
Pablo
  • 13,271
  • 4
  • 39
  • 59
utsav0195
  • 1
  • 4
0

There are several errors in your code.

First, and this is very important: Do not use gets() ever again. This is a dangerous function: Why is the gets function so dangerous that it should not be used?

Besides gets is not included in C11 any more.

Second, you need to understand what happens when you input data with the keyboard. When you enter a text you have to press ENTER for the reading routing to continue. The ENTER also gets stored in the input buffer as the newline ('\n') character. So if you enter abc the buffer will contain1:

+---+---+---+----+
| a | b | c | \n |
+---+---+---+----+

This is important to remember, because for example fgets stop reading from the buffer when it encounters the newline character2. Other functions like scanf may read the newline depending on the conversion specifier used.

You use getchar.

man getchar

#include <stdio.h>

int fgetc(FILE *stream);

int getc(FILE *stream);

int getchar(void);

DESCRIPTION

fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.

getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once.

getchar() is equivalent to getc(stdin).

it is clear from the documentation that getchar() reads one character only. At this point it's important to notice that all reading functions like fgets, scanf, etc. will first try to read from the input buffer and only when the input buffer is empty and they still need to keep reading, then they will wait for user input. That means if a previous read function left characters in the input buffer, the next read function might not wait for user input, because it might read from the input buffer.

So, let's say you entered aENTER. The input buffer will look like this:

+---+----+
| a | \n |
+---+----+

The input buffer will have at least 2 characters in it, 'a' and the newline. getchar reads the first and returns. The input buffer look now like this:

+----+
| \n |
+----+

Then you execute gets(b);, it will see that there are characters left in the input buffer, hence it will read from it. It reads the newline and gets stops reading, because gets stops reading, because of the newline that was in the input buffer. gets didn't need to wait for user input. As an user you have the impression that the gets call was skipped.

Finally:

char b[2];
puts(b);

This is undefined behaviour, because b is an uninitialized array, meaning that the values b[0] and b[1] are undefined (unknown, they may be 0, or -2323 or 2829281, nobody knows). puts expects a String. In C a string is sequence of non-zero bytes that end with the value 0 (the so called '\0'-terminating byte). b is not a string, so doing puts(b) yields undefined behaviour.


Footnotes

1I don't know if the input buffer also stores a '\0'-terminating byte after the newline. In any case it is not important here.

2Provided that the destination buffer is large enough. If there is no more space in the destination buffer, fgets stop reading anyway.

Pablo
  • 13,271
  • 4
  • 39
  • 59
0

Use safe fgets instead of gets

#include <stdio.h>
#include <stdlib.h>

int main(){

    char a[16];

    printf("1) Enter a character: \n");
    fgets ( a, sizeof (a), stdin );
    printf("1) You entered: %c \n",a[0]);
    puts(a);


    printf("2) Enter a character: \n");
    char b[16];
    fgets(b, sizeof(b), stdin);
    printf("2) You entered: %c \n",b[0]);

    puts(b);
}

Input:

1
B

Output:

1) Enter a character: 
1) You entered: 1 
1

2) Enter a character: 
2) You entered: B 
B
sg7
  • 6,108
  • 2
  • 32
  • 40
  • It's much safer to do `fgets(b, sizeof b, stdin);`. If for some reason you change the dimension of `b` (let say to 10) and don't remember to change the `fgets`s (because they are apart by several lines), you might have a buffer overflow. Also `fgets` always stores is `'\0'`-terminated string, there is no point in reading 1 less byte. – Pablo Jan 27 '18 at 23:02
  • @Pablo Thank you! Very good point! I have edited the example. – sg7 Jan 27 '18 at 23:10