sizeof運算符以字節(jié)為單位返回其操作數的大小(在c中,1個字節(jié)被定義為char類型所占用空間的大小。在過去,一個字節(jié)通常是8位,但是一些字符集可能使用更大的字節(jié))
創(chuàng)新互聯建站主要從事成都網站制作、成都網站建設、外貿營銷網站建設、網頁設計、企業(yè)做網站、公司建網站等業(yè)務。立足成都服務婺城,十年網站建設經驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575
sizeof實例程序:
#include<stdio.h> int main() { int n=0; size_t intsize; intsize=sizeof(int);//c規(guī)定sizeof返回size_t類型的值,這是一個無符號整數類型,但不是一個新類型, printf("n=%d,n has %u bytes:all ints have %u bytes.\n",n,sizeof n,intsize);//我的系統%zd無法實現,所以用%u(%lu)來替代。 return 0; //c有個typedef機制,它允許您為一個已有的類型創(chuàng)建一個別名。如:typedef double real; 使得real 稱為duble 的別名,real deal;編譯器看到real,回想起typedef 語句把real定義為double的別名,于是把deal創(chuàng)建為一個double類型的變量。 }
運行結果:
2、取模運算:while()循環(huán):
實例程序如下:
//min_sec.c把秒數轉換為分鐘和秒 #include<stdio.h> #define SEC_PRE_MIN 60 int main() { int sec=60; int min,left; printf("please convert seconds into minutes and seconds.\n");/*注意此處/n不可缺一部分,我忘記了n,只有/程序 就一直編譯報錯。*/ printf("enter the number of the seconds(<=0 to quit):\n"); while(sec<=1000) { sec=sec+100; min=sec/SEC_PRE_MIN;//得到分鐘數; left=sec%SEC_PRE_MIN;//取模運算得到秒數; printf("%d seconds is %d minuts and %d seconds.\n",sec,min,left); } printf("please stop convert!\n"); return 0; }
運形結果:
3、
Profix前綴模式++i就完全等價于i=i+1;先加1后賦值。所以顯而易見,i++就是先賦值后加1;--等同。
4、本章總結,用一個綜合的例子來結尾,其中要注意的問題,就是程序一定要細心,不可犯低級錯誤,打錯忘定義之類的!
實例程序如下:
//綜合示例程序:running.c #include<stdio.h> #define S_PER_M 60//每分鐘的秒數 const int S_PER_H =3600;//每小時的秒數 const double M_PER_K =0.62137;//每公里的英里數 int main(void) { double distk,distm;//分別以公里和英里記得跑過的距離 double rate;//以英里每小時位單位的平均速度 int min,sec;//跑步時用的分鐘數和秒數 double mtime;//跑完一英里所用的時間以秒記 int mmin,msec,time;//跑完一英里所用的時間,以分鐘、秒記 printf("this program converts your time for a metric race\n"); printf("to a time for running a mile and to your average\n"); printf("speed in miles per hour.\n"); printf("please enter the kilometers, the distance run .\n"); scanf("%lf",&distk);//lf表示讀取一個double 類型的數值 printf("Next enter your time in minuts and seconds.\n"); printf("begin by entering the minutes.\n"); scanf("%d",&min); printf("now enter the seconds. \n"); scanf("%d",&sec); time=S_PER_M*min+sec;//把時間轉換為全部用秒表示 distm=M_PER_K*distk;//把公里轉化為英里, rate=distm/time*S_PER_H;//時間/距離=跑完每英里的用時 mtime=(double)time/distm; mmin=(int)mtime/S_PER_M; msec=(int)mtime%S_PER_M; printf("you ran %1.2f km (%1.2f miles)in %d min,%d sec.\n",distk,distm,min,sec); printf("that pace corresponds to running a mile in %d min,",mmin); printf("%d sec.\n your average speed was %1.2f mph.\n",msec,rate); return 0; }
運行結果如下:
網頁名稱:sizeof運算符和size_t類型、取模運算符%、增量和減量運算符
文章起源:http://chinadenli.net/article32/gjdjsc.html
成都網站建設公司_創(chuàng)新互聯,為您提供做網站、全網營銷推廣、動態(tài)網站、品牌網站設計、服務器托管、企業(yè)網站制作
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯