21

I am just learning C and making a basic "hello, NAME" program. I have got it working to read the user's input but it is output as numbers and not what they enter?

What am I doing wrong?

#include <stdio.h>

int main()
{
    char name[20];

    printf("Hello. What's your name?\n");
    scanf("%d", &name);
    printf("Hi there, %d", name);

    getchar();
    return 0;
}
Callum Whyte
  • 2,379
  • 11
  • 36
  • 55

4 Answers4

34

You use the wrong format specifier %d- you should use %s. Better still use fgets - scanf is not buffer safe.

Go through the documentations it should not be that difficult:

scanf and fgets

Sample code:

#include <stdio.h>

int main(void) 
{
    char name[20];
    printf("Hello. What's your name?\n");
    //scanf("%s", &name);  - deprecated
    fgets(name,20,stdin);
    printf("Hi there, %s", name);
    return 0;
}

Input:

The Name is Stackoverflow 

Output:

Hello. What's your name?
Hi there, The Name is Stackov
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • 6
    Note that fgets() will also pick-up the newline character (\n) at the end of the input. So an input of "Bob" is captured as "Bob\n". If this is unacceptable, then the newline character has to be manually removed. You'll need a line of code like: name[ strlen( name)-1] = '\0'; – Babar-Baig Feb 27 '21 at 20:58
7
#include <stdio.h>

int main()
{
char name[20];

printf("Hello. What's your name?\n");
scanf("%s", name);
printf("Hi there, %s", name);

getchar();
return 0;
}
MONTYHS
  • 926
  • 1
  • 7
  • 30
  • @user2036031 check [this link](http://stackoverflow.com/questions/1391548/why-doesnt-getchar-wait-for-me-to-press-enter) I think thats why your programme close right after message. – Omer Obaid Feb 27 '14 at 10:39
  • You wouldn't address-of the array label, i.e. the `name` while `scanf`ing a string. OP better learns that correctly and does not get misinformed by these answers... – Utkan Gezer Feb 27 '14 at 10:46
1

When we take the input as a string from the user, %s is used. And the address is given where the string to be stored.

scanf("%s",name);
printf("%s",name);

hear name give you the base address of array name. The value of name and &name would be equal but there is very much difference between them. name gives the base address of array and if you will calculate name+1 it will give you next address i.e. address of name[1] but if you perform &name+1, it will be next address to the whole array.

Rahul
  • 3,479
  • 3
  • 16
  • 28
-2

change your code to:

int main()
{
    char name[20];

    printf("Hello. What's your name?\n");
    scanf("%s", &name);
    printf("Hi there, %s", name);

    getchar();
    getch();                  //To wait until you press a key and then exit the application
    return 0;
}

This is because, %d is used for integer datatypes and %s and %c are used for string and character types

  • You wouldn't address-of the array label, i.e. the `name` while `scanf`ing a string. OP better learns that correctly and does not get misinformed by these answers... – Utkan Gezer Feb 27 '14 at 10:47