簡體   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