首页 > 解决方案 > 如何接受我的姓名首字母作为输入并在 C 中连同我的姓名一起打印?

问题描述

过去几天,我一直在努力用 C 语言打印我的全名。经过几次尝试,这就是我想出的。

#include <stdio.h>

int main(void)
{
    char yourname[20];
    int yourage;
    char initial;

    printf("Whats your name? ");
    scanf("%18[^\n]s", yourname);

    printf("And your initial is? ");
    scanf("%c", &initial);

    yourname[19] = '\0';
    fflush(stdin);

    printf("How old are you? ");
    scanf(" %d",&yourage);

    printf("Your name is %s %c and you are %i years old.\n", yourname, 
    initial, yourage);


    return(0);
}

当我编译并运行程序时,这就是我得到的。我正在编译时像这样打开了完整的警告

make name

clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 - 
std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    name.c  -lcrypt - 
-lm -o name

请帮忙..

Whats your name?        Alokananda
And your initial is? How old are you?   35
Your name is Alokananda 
and you are 35 years old.

标签: cstringscanf

解决方案


推荐阅读