繁体   English   中英

为什么这段代码会产生错误的 output?

[英]Why does this code produce incorrect output?

char nation[100];

printf("Enter Your Name : ");
gets(nation);

int size = strlen(nation);

printf("Output = %f\n", size);

我需要解决这个问题。

char nation[100];

printf("Enter Your Name : ");
fgets(nation, sizeof(nation), stdin);  //gets is depreciated use fgets instead

size_t size = strlen(nation);    //strlen returns size_t not int

printf("Output = %zu\n", size);  //%f is to print floats

您需要告诉printf您要打印什么类型:

%c  character
%d  decimal (integer) number (base 10)
%e  exponential floating-point number
%f  floating-point number
%i  integer (base 10)
%o  octal number (base 8)
%s  a string of characters
%u  unsigned decimal (integer) number
%x  number in hexadecimal (base 16)
%%  print a percent sign

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM