LoneRetriever
LoneRetriever

Reputation: 119

Scanf changing the input in C

I have a fairly basic problem but I can't manage to solve it.

I'm trying to get an input from a user like this :

int main() {
    char coord[2];

    fflush(stdin);
    scanf("%c", coord);
}

When i'm trying this code with printf("%c", coord);, it displays a completely different string from what I typed. For example, if I type "g6", it prints "Ê". I really have no clue why it's happening.

Thanks for helping me !

Upvotes: 0

Views: 408

Answers (4)

Adrian Mole
Adrian Mole

Reputation: 51864

The line char coord[2]; declares coord as an array of characters (also known as a "string"). However, the %c (in both scanf and printf) reads/writes a single character.

For strings, you need to use the %s format.

Also, if you want to store/read/print the string, "g6", you will need to allocate (at least) three characters to your coord array, as you must terminate all C-strings with a nul character.

Furthermore, calling fflush on the stdin stream is not effective (actually, it causes undefined behaviour, so anything could happen) - see here: I am not able to flush stdin.

So, a 'quick fix' for your code would be something like this:

#include <stdio.h>
int main()
{
    char coord[3]; // Allow space for nul-terminator
//    fflush(stdin); // don't do it
    scanf("%2s", coord); // The "2" limits input to 2 characters
    printf("%s\n", coord);
    return 0; // ALways good practice to return zero (success) from main
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

For starters this statement

fflush(stdin);

has undefined behavior and shall be removed.

The conversion specifier %c of printf expects an argument of the type char while you are passing an expression of the type char * to which the array designator is implicitly converted

printf("%c", coord);

you have to write either

printf("%c", *coord);

or

printf("%c", coord[0]);

Pay attention to that using this call of scanf

scanf("%c", coord);

you can enter only a single character. You can not enter a string.

If you want to enter a string in the array coord that has only two elements then you have to write

scanf( "%1s", coord);

In this case the array will be filled with a string of length equal to 1.

In this case you can output it like

printf("%s", coord);

If you want to enter a string like this "g6" then you have to declare the array like

char coord[3];

and write the following call of scanf

scanf( "%2s", coord);

Upvotes: 1

Roberto Caboni
Roberto Caboni

Reputation: 7490

First of all avoid using fflush (stdin);. Standard input flashing is undefined behavior, according to C standard, and may lead to big issues .

Then, you are trying to get an input string using %c format, that is supposed to acquire a single character. Furthermore, your coord array has not enough room for the string terminator character (\0).

The format to be used in order to acquire a string with scanf (and to print it with printf) is %s:

int main() {
    char coord[3] = {0};

    scanf("%2s", coord);

    printf ("%s\n", coord);
}

The "2" added to the format makes sure that at most two characters are read (exactly those you can have in you string array without overwriting the last character).

Upvotes: 2

SM2A
SM2A

Reputation: 525

If you want to get string(char array) from user you should do this :

scanf("%s",coord);

%c is for single char

Upvotes: 2

Related Questions