英文:
Unexpected Output plus segmentation error with my C program
问题
以下是翻译好的部分:
我正在尝试学习编程,并尝试以下C代码。然而,正如您下面所看到的,我没有得到预期的输出,而在尝试解决方案时出现了分段错误:
我感觉在声明名字的时候出了问题,但是找不出错在哪里。
我的代码:
#include <stdio.h>;
int main(void) {
char first_name;
printf("Enter your name: \n");
scanf("%c", &first_name);
printf("Hello, %c", first_name);
}
实际输出
这是我当前得到的输出:
Enter your name:
asdasfasf
Hello, a
预期输出
Enter your name:
asdasfasf
Hello, asdasfasf
请注意,代码中存在一些问题,你可能需要更改它才能获得预期的输出。
英文:
I am trying to learn programming and was trying the following code in C. However, As you can see below I am not getting the expected output and I am getting a segmentation error while trying out solutions :
I feel I am doing something wrong with the declaration of First name or something. But not able to figure it out.
My Code:
#include <stdio.h>
int main(void){
char first_name;
printf("Enter your name: \n");
scanf("%c", &first_name);
printf("Hello, %c", first_name);
}
Actual Output
This is the output I am currently getting:
Enter your name:
asdasfasf
Hello, a
Expected Output
Enter your name:
asdasfasf
Hello, asdasfasf
答案1
得分: 3
这是我的答案
#include <stdio.h>
int main(void){
char first_name[100] = {0};
printf("输入您的名字:(最大长度为99)\n");
scanf("%99s", first_name);
printf("你好,%s\n", first_name);
}
运行它将输出
输入您的名字:(最大长度为99)
asdasfasf
你好,asdasfasf
这是我的另一个解决方案,可以处理 @Andreas Wenzel 指出的“名字可能由多个单词组成”:
#include <stdio.h>
int main()
{
char first_name[100] = {0};
char c = 0;
printf("输入您的名字:(最大长度为99)\n");
// 读取99次,每次一个字符,当读取到'\n'时暂停,否则将字符保存到first_name数组中。
for(int m = 0; m < 99; m++){
if((c = getchar()) != '\n'){
first_name[m] = c;
}else{
break;
}
}
printf("你好,%s\n", first_name);
}
运行它将输出
输入您的名字:(最大长度为99)
asdsfd
你好,asdsfd
或者像这样:
输入您的名字:(最大长度为99)
dsds dggffgg
你好,dsds dggffgg
或者也可以像这样:
输入您的名字:(最大长度为99)
ssd dsdfds dsfdfgfdg ddfdsfdf sfsdfsdf
你好,ssd dsdfds dsfdfgfdg ddfdsfdf sfsdfsdf
英文:
Here is my answer
#include <stdio.h>
int main(void){
char first_name[100] = {0};
printf("Enter your name:(Max length is 99)\n");
scanf("%99s", first_name);
printf("Hello, %s\n", first_name);
}
Run it will output
Enter your name:(Max length is 99)
asdasfasf
Hello, asdasfasf
Here is my other solution that can handle @Andreas Wenzel point out that "it is possible for a first name to consist of several words":
#include <stdio.h>
int main()
{
char first_name[100] = {0};
char c = 0;
printf("Enter your name:(Max length is 99)\n");
// read 99 times, once a character, it suspend when read '\n',
// otherwise save to first_name array when read character.
for(int m = 0; m < 99; m++){
if((c = getchar()) != '\n'){
first_name[m] = c;
}else{
break;
}
}
printf("Hello, %s\n", first_name);
}
Run it will output
Enter your name:(Max length is 99)
asdsfd
Hello, asdsfd
or like this:
Enter your name:(Max length is 99)
dsds dggffgg
Hello, dsds dggffgg
or also like this:
Enter your name:(Max length is 99)
ssd dsdfds dsfdfgfdg ddfdsfdf sfsdfsdf
Hello, ssd dsdfds dsfdfgfdg ddfdsfdf sfsdfsdf
答案2
得分: 2
使用scanf
与%c
转换格式说明符只会读取一个字符。
为了读取一个完整的单词,你可以使用%s
说明符。然而,在英文中,名字可能由多个单词组成,比如John Paul。在这种情况下,“Paul”不是一个中间名,而是名字的一部分。对于输入"John Paul"
,scanf
只会从输入流中读取"John"
,并将" Paul"
留在输入流中。这可能是不可取的。
因此,最好的做法可能是读取并存储整行输入,而不是仅仅一个单词。虽然这也可以用scanf
实现,但我建议使用函数fgets
。
在读取由多个字符组成的字符串时,你必须提供一个指向有足够分配空间来存储字符串的内存位置的指针。提供一个指向单个char
的指针是不够的,因为这样的内存位置只能存储一个字符。这意味着这个内存位置只能存储一个由一个空字符结尾的空字符串。
为了分配足够的空间来存储一个字符串的一种简单方法是声明一个char
数组。当将这样一个数组传递给一个函数时,该数组将自动衰变为指向数组第一个元素的指针。
以下是一个例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char first_name[200];
//提示用户输入
printf( "Enter your name: " );
//读取一行输入
if ( fgets( first_name, sizeof first_name, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
//如果存在,从输入中移除换行符,通过用空字符结尾字符覆盖它
first_name[strcspn(first_name,"\n")] = '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char first_name[200];
//提示用户输入
printf( "Enter your name: " );
//读取一行输入
if ( fgets( first_name, sizeof first_name, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
//如果存在,从输入中移除换行符,通过用空字符结尾字符覆盖它
first_name[strcspn(first_name,"\n")] = '\0';
//将输入返回给用户
printf( "Hello, %s.\n", first_name );
}
';
//将输入返回给用户
printf( "Hello, %s.\n", first_name );
}
这个程序的行为如下:
Enter your name: John Paul
Hello, John Paul.
英文:
Using scanf
with the %c
conversion format specifier will only read a single character.
In order to read a whole word, you can use the %s
specifier instead. However, in the English langauge, it is possible for a first name to consist of several words, such as John Paul. In that case, "Paul" is not a middle name, but part of the first name. With the input "John Paul"
, scanf
would only read "John"
from the input stream and leave " Paul"
on the input stream. This would probably be undesirable.
For this reason, it would probably be better to read and store a whole line of input, instead of only a single word. Although this is also possible with scanf
, I recommend to use the function fgets
instead.
When reading in a string consisting of several characters, you must provide a pointer to a memory location that has sufficient allocated space for storing the string. Providing a pointer to a single char
is not sufficient, as such a memory location is only able to store a single character. This means that the memory location is only able to store an empty string consisting only of a null terminating character.
One simple way of allocating sufficient space for storing a string is to declare a char
array. When passing such an array to a function, the array will automatically decay to a pointer to the first element of the array.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char first_name[200];
//prompt user for input
printf( "Enter your name: " );
//read one line of input
if ( fgets( first_name, sizeof first_name, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
//remove newline character from the input, if it exists,
//by overwriting it with a null terminating character
first_name[strcspn(first_name,"\n")] = '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char first_name[200];
//prompt user for input
printf( "Enter your name: " );
//read one line of input
if ( fgets( first_name, sizeof first_name, stdin ) == NULL )
{
fprintf( stderr, "Input error!\n" );
exit( EXIT_FAILURE );
}
//remove newline character from the input, if it exists,
//by overwriting it with a null terminating character
first_name[strcspn(first_name,"\n")] = '\0';
//print back input to user
printf( "Hello, %s.\n", first_name );
}
';
//print back input to user
printf( "Hello, %s.\n", first_name );
}
This program has the following behavior:
Enter your name: John Paul
Hello, John Paul.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论