C - scanf,printf 名称和年龄程序 [英] C - scanf,printf name and age program

查看:47
本文介绍了C - scanf,printf 名称和年龄程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

int main ()
{
   char yourname;
   int yourage;

    printf("Whats your name?\t");
    scanf("%c",&yourname); 
    printf("How old are you?\t");
    scanf("%d",&yourage);
    printf("You are %d years old and your name is %c\n\n\n",yourage,yourname);
    system("pause");
    return(0);
}

我想让这个程序询问用户名和年龄,然后打印出来..

I want this program to ask for the username and age, and then print them..

推荐答案

当您使用 scanf 时,%c 旨在获取单个字符.如果要获取字符串,需要使用%s.

when you use scanf, %c is intended to get a single character. If you want to get a string, you need to use %s.

此外,在 C 语言中,字符串只是字符数组.所以需要声明一个char数组.

Also, in C langage, string are just char arrays. So you need to declare a char array.

#include <stdio.h>

int main ()
{
   char yourname[100];
   int yourage;

   printf("Whats your name?\t");
   scanf("%s",yourname); //i let you read the doc to avoid overflow :)
   printf("How old are you?\t");
   scanf("%d",&yourage);
   printf("You are %d years old and your name is %s \n\n\n",yourage,yourname);
   system("pause");
   return(0);
}

这篇关于C - scanf,printf 名称和年龄程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆