problems with scanf and conversion specifiers

2019-09-22 06:51发布

Here you can see my source code:

#include <stdio.h>

int main()
{
    char yourname;
    char yoursex;
    int yourage = 0;

    printf("Hey, what's your name?\n");
    printf("My name is: ");
    scanf("%s", &yourname);
    printf("Oh, hello %s! \n\n", &yourname);

    printf("Are you a boy or a girl?: ");
    scanf("%s", &yoursex);
    printf("Nice to know you are a %s! \n\n", &yoursex);

    printf("How old are you %s? I am ", &yourname);
    scanf("%d", &yourage);
    printf("I see you are %d, you have many years then!", &yourage);

    return 0;
}

I was trying things that I didn't knew, and strangely it is not working for me. What's the problem? Also, why it needs to be %s and not %c? If I use %c instead it does not work!

Where it says: How old are you %s? instead of putting my name, it says ''oy''

and instead of showing my age in the last line, it shows a big number.

标签: c printf scanf
5条回答
虎瘦雄心在
2楼-- · 2019-09-22 07:09

This is a single character:

char yourname;

But %s indicates that the variable is a string (i.e., an array of characters terminated by a NUL). That's why you need %c. If you really did mean to use a string, then define the variable like

char yourname[32];  /* just pick a big enough size */

Also, you are correct to use the address of the variable with scanf(), but printf() needs the value. So instead of

printf("I see you are %d, you have many years then!", &yourage);

use

printf("I see you are %d, you have many years then!", yourage);

The "big number" is the memory address.

查看更多
beautiful°
3楼-- · 2019-09-22 07:09

The expression char yourname; only holds space for a single character, so quite likely you end up corrupting the memory space when scanning for yourname. You should allocate a bigger buffer and make sure that you don't overrun its length by setting a maximum number of characters to be read with the scanf function; as described in some of the other answers.

The fact that the following printf print correctly the name doesn't mean that the memory doesn't get corrupted; as C/C++ don't really check the boundary of any strings or arrays used at runtime.

As suggested by others, starting by reading a good book about C and/or C++ wouldn't a bad idea.

查看更多
萌系小妹纸
4楼-- · 2019-09-22 07:20

Make sure you read the comments in code!

#include <stdio.h>

int main()
{
    char yourname[10];
    char yoursex[5]; // boy or girl + null terminator
    int yourage = 0;

    printf("Hey, what's your name?\n");
    printf("My name is: ");
    scanf("%s", &(*yourname)); // & and * cancel each other out,
    // thus take a look at the next scanf()
    printf("Oh, hello %s! \n\n", yourname); // yourname is now an array

    printf("Are you a boy or a girl?: ");
    scanf("%s", yoursex);
    printf("Nice to know you are a %s! \n\n", yoursex);

    printf("How old are you %s? I am ", yourname);
    scanf("%d", &yourage); // ok
    printf("I see you are %d, you have many years then!", yourage); // here you don't
    // need the address of the variable!

    return 0;
}
查看更多
Luminary・发光体
5楼-- · 2019-09-22 07:25

%s is for reading a string -- multiple characters delimited by whitespace. %c is for reading a single char.

You declare your yourname and yoursex vars as characters, and then try to read strings into them. The string read will overwrite random other things in the stack frame and misbehave or crash.

You want to declare yourname and yoursex as character arrays, so they can hold strings:

char yourname[32];
char yoursex[32];

then, when reading into them, you want to include a length limit so they don't overflow:

scanf("%31s", yourname);
查看更多
ゆ 、 Hurt°
6楼-- · 2019-09-22 07:31

These are the very basics of C Programming, and I strongly advise you to get a decent book - The C Programming Language by Dennis Ritchie would be a good start.

There are numerous errors in your code.

  1. A char can contain only one character, like 'A', or 'a' or something like that. When you're scanning a name, it is going to be a group of characters, like 'E', 'd', 'd', 'y'. To store multiple characters, you need to use a character array. Also, the format specifier used to scan/print characters is %c, %s is for when you need to scan a group of characters, also called a string into an array.

  2. When you use printf, you do not supply a pointer to the variable you are trying to print (&x is a pointer to variable x). The pointer is a 32/64-bit integer, which is likely why you see a random integer when trying to print. printf("%c\n", charVar) is sufficient.

  3. scanf does not need an & while using %s as the format specifier, assuming you have passed a character array as the argument. The reason is, scanf needs to know where to store the data you are reading from the input - and that is given by a pointer to the memory location. When you need to scan an integer, you need to pass an &x - which means, pointer to memory location of x. But when you pass a character array, it is already in the form of a memory address, and doesn't need to be preceded by an ampersand.

I once again recommend you look up some decent tutorials online, or get a book (the one I mentioned above is a classic). Type the examples as given in the material. Experiment. Have fun. :)

查看更多
登录 后发表回答