ielino
ielino

Reputation: 19

c - scanf not storing input properly

My code looks like this:

int nameFull;
printf("What is your name?\n");
scanf("%d\n", &nameFull); \\up until here it seems to work
printf("Hello %d", nameFull);

return 0;

But my output every time I run the program is "Hello 0" no matter what I input.

Does anyone know how to fix this?

Upvotes: 1

Views: 2374

Answers (5)

Md. Rezwanul Haque
Md. Rezwanul Haque

Reputation: 2950

%s reads a string of characters.

%d reads a integer.

So, your correct code will be like following code :

#include <stdio.h>

int main(){
    char nameFull[100];
    
    printf("What is your name?\n");
    
    scanf("%99s", nameFull); //to avoid potential buffer overflow
    
    printf("Hello %s\n", nameFull);
    
    return 0;
}

N.B: Check this comment for nice explanation.

Upvotes: 1

0decimal0
0decimal0

Reputation: 3984

First of all scanf() doesn't emit a prompt so its not a good idea to use any trailing whitespace character in the format string like \n here , It will cause it to read and discard character until next non-whitespace character.

To read a name you can do it like :

char name[50];
scanf("%49s",name); // 49 to limit the buffer input to prevent buffer overrun , this is a security issue.

You should also check the return value of scanf to see if the operation was successful. Personally , I don't prefer using scanf() at all because of various potential problems. It takes as input only what the program author expects it to, not considering other inputs which user might accidentally input. Check out here and here. Also check the scanf() man page

A better and safer method would be use fgets(),

fgets(name,sizeof(name),stdin);

Upvotes: 2

gsamaras
gsamaras

Reputation: 73366

You want to read a string, but you are an integer to store the input. That's not the right approach.

A better aproach would be to use an array of characters, to store the string in it.

char nameFull[100]; // can store up to 100 characters, 99 + 1 for the null-terminator ideally

Now, you could use scanf, like this:

scanf(" %99[^\n]", nameFull);

Note that I used 99, as a guard for not overflowing your array nameFull, if the user inputs too many characters for the size of your array. I didn't use %s, which would stop at a whitespace, and you seem to want to input a full name, which is usually two words and a space in between.

An alternative would be to use fgets(), which provides more safety, like this:

fgets(nameFull, sizeof(nameFull), stdin)

It will read the whole line though and store the trailing newline, while scanf() will read a single string.

Moreover, use the string identifier to print, not the integer one (%s is for string, %d is for integers). Like this:

printf("Hello %d", nameFull);

to this:

printf("Hello %s", nameFull);

as discussed about the string format.

Upvotes: 1

Claudio Cortese
Claudio Cortese

Reputation: 1380

You are trying to assign an array of character (commonly referred as string) to an integer variable.

That's not correct.

Just change your variable as such

char nameFull[1024] = {0};

And then use scanf(3) with the appropriate format specifiers for strings, which is %s

scanf("%s", nameFull);

Normally you would check for the return of scanf to know if errors occurs, and in such cases, handle them.

Anyway, I would advice you to use fgets(3) which prevents buffer overflow

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

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte (aq\0aq) is stored after the last character in the buffer.

Upvotes: -1

Jas
Jas

Reputation: 870

Well, int stores a number, a name is not a number. A name is a set of characters (aka strings). So this program would work (no error checking and such since you are in an introductory course):

char name[1024]; // 1024 is more than enough space for a name
scanf("%s", name); // %s reads a string of characters
printf("Hello %s\n", name);
return 0;

Upvotes: -1

Related Questions