scanf 和转换说明符的问题 [英] problems with scanf and conversion specifiers
问题描述
在这里你可以看到我的源代码:
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;
}
我正在尝试一些我不知道的东西,但奇怪的是它对我不起作用.有什么问题?另外,为什么它需要是 %s 而不是 %c?如果我改用 %c 则不起作用!
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!
它说:你 %s 几岁了?它没有写我的名字,而是写着oy"
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 编程的基础知识,我强烈建议您买一本像样的书 - Dennis Ritchie 的 The C Programming Language 将是一个好的开始.
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.
一个
char
只能包含一个字符,比如 'A' 或 'a' 或类似的东西.当您扫描姓名时,它将是一组字符,例如E"、d"、d"、y".要存储多个字符,您需要使用字符数组.另外,用于扫描/打印字符的格式说明符是%c
,%s
用于需要扫描一组字符时,也称为字符串到数组中.
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.
当您使用 printf
时,您没有提供指向要打印的变量的指针(&x
是指向变量 x
).指针是一个 32/64 位整数,这可能是您在尝试打印时看到随机整数的原因.printf("%c\n", charVar)
就足够了.
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.
scanf
在使用 %s
作为格式说明符时不需要 &
,假设您已经传递了一个字符数组作为论据.原因是,scanf
需要知道在哪里存储您从输入中读取的数据——这是由指向内存位置的指针给出的.当你需要扫描一个整数时,你需要传递一个 &x
- 这意味着,指向 x 的内存位置的指针.但是当你传递一个字符数组的时候,它已经是内存地址的形式了,不需要在前面加&.
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. :)
这篇关于scanf 和转换说明符的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!