第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

printf為浮點數(shù)指定整數(shù)格式的字符串

printf為浮點數(shù)指定整數(shù)格式的字符串

C
ABOUTYOU 2019-12-06 10:30:31
#include <stdio.h>int main(){    float a = 5;    printf("%d", a);    return 0;}這給出了輸出:0為什么輸出為零?
查看完整描述

3 回答

?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

它不會打印5,因為編譯器不知道會自動轉(zhuǎn)換為整數(shù)。你需要(int)a自己做。


也就是說,


#include<stdio.h>

void main()

{

float a=5;

printf("%d",(int)a);

}

正確輸出5。


將該程序與


#include<stdio.h>

void print_int(int x)

{

printf("%d\n", x);

}

void main()

{

float a=5;

print_int(a);

}

由于的聲明,編譯器直接知道將float轉(zhuǎn)換為int的位置print_int。


查看完整回答
反對 回復(fù) 2019-12-06
?
倚天杖

TA貢獻1828條經(jīng)驗 獲得超3個贊

正如其他人所說,您需要使用%f格式字符串或?qū)⑵滢D(zhuǎn)換a為int。


但我想指出的是,您的編譯器可能知道printf()的格式字符串,并且可以告訴您您使用的格式錯誤。經(jīng)過適當調(diào)用(-Wall包括-Wformat)的我的編譯器說:


$ /usr/bin/gcc -Wformat tmp.c

tmp.c: In function ‘main’:

tmp.c:4: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’


$ /usr/bin/gcc -Wall tmp.c

tmp.c: In function ‘main’:

tmp.c:4: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’


$

哦,還有另一件事:您應(yīng)該在中包含'\ n',printf()以確保將輸出發(fā)送到輸出設(shè)備。


printf("%d\n", a);

/*        ^^ */

或fflush(stdout);在printf()。之后使用。


查看完整回答
反對 回復(fù) 2019-12-06
  • 3 回答
  • 0 關(guān)注
  • 686 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號