溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

sizeof運算符和size_t類型、取模運算符%、增量和減量運算符

發(fā)布時間:2020-07-04 16:40:40 來源:網(wǎng)絡 閱讀:721 作者:lillian_trip 欄目:編程語言

sizeof運算符以字節(jié)為單位返回其操作數(shù)的大?。ㄔ赾中,1個字節(jié)被定義為char類型所占用空間的大小。在過去,一個字節(jié)通常是8位,但是一些字符集可能使用更大的字節(jié))

sizeof實例程序:

#include<stdio.h>
int main()
{

int n=0;
size_t intsize;
intsize=sizeof(int);//c規(guī)定sizeof返回size_t類型的值,這是一個無符號整數(shù)類型,但不是一個新類型,
printf("n=%d,n has %u bytes:all ints have %u bytes.\n",n,sizeof n,intsize);//我的系統(tǒng)%zd無法實現(xiàn),所以用%u(%lu)來替代。
return 0;
//c有個typedef機制,它允許您為一個已有的類型創(chuàng)建一個別名。如:typedef double real;  使得real 稱為duble 的別名,real deal;編譯器看到real,回想起typedef 語句把real定義為double的別名,于是把deal創(chuàng)建為一個double類型的變量。

}

運行結果:

sizeof運算符和size_t類型、取模運算符%、增量和減量運算符

2、取模運算:while()循環(huán):

實例程序如下:

//min_sec.c把秒數(shù)轉換為分鐘和秒
#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;//得到分鐘數(shù);
	left=sec%SEC_PRE_MIN;//取模運算得到秒數(shù);
	printf("%d seconds is %d minuts and %d seconds.\n",sec,min,left);
}
printf("please stop convert!\n");
return 0;
}

運形結果:

sizeof運算符和size_t類型、取模運算符%、增量和減量運算符

3、

Profix前綴模式++i就完全等價于i=i+1;先加1后賦值。所以顯而易見,i++就是先賦值后加1;--等同。

4、本章總結,用一個綜合的例子來結尾,其中要注意的問題,就是程序一定要細心,不可犯低級錯誤,打錯忘定義之類的!

實例程序如下:

//綜合示例程序:running.c
#include<stdio.h>
#define S_PER_M 60//每分鐘的秒數(shù)
const int S_PER_H =3600;//每小時的秒數(shù)
const double M_PER_K =0.62137;//每公里的英里數(shù)
int main(void)
{
double distk,distm;//分別以公里和英里記得跑過的距離
double rate;//以英里每小時位單位的平均速度
int min,sec;//跑步時用的分鐘數(shù)和秒數(shù)
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 類型的數(shù)值
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類型、取模運算符%、增量和減量運算符

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI