博客推行版本更新,成果積累制度,已經寫過的博客還會再次更新,不斷地琢磨,高質量高數量都是要追求的,工匠精神是學習必不可少的精神。因此,大家有何建議歡迎在評論區踴躍發言,你們的支持是我最大的動力,你們敢投,我就敢肝 ...
基本結構
寫一個簡單但完整的C程式。
利用printf 函數在屏幕上顯示輸出。
簡單C程式的結構。
書寫C程式的基本原則。
代碼設計
#include<stdio.h>
void main(void)
{
printf("This is C!");
}
結果顯示
格式化輸出
格式化輸出
回車
代碼設計
#include<stdio.h>
void main(void)
{
printf("Welcome to");
printf("China!");
printf("\nHow do we\njump\n\ntwo lines?\n");
printf("\n");
printf("It will rain\ntomorrow\n");
}
結果顯示
其他轉義字元
顯示轉義字元
代碼展示
#include <stdio.h>
void main(void)
{
printf("Listen to the beep now.\a");
printf("\nWhere is the 't' in cat \b?\n\n");
printf("I earned $50 \rWhere is the money?\n");
printf("The rabbit jumps \t\t two tabs.\n\n");
printf("Welcome to\
New York!\n\n");
printf("From " "Russia \
with " "Love.\n");
printf("Print 3 double quotes -\" \" \" \n");
}
結果顯示
變數:命名、聲明、賦值和列印值
命名變數
聲明數據類型
使用賦值語句
顯示變數的值
基本的賦值語句
代碼設計
#include <stdio.h>
void main(void)
{
int month;
float expense,income;
month=12;
expense=111.1;
income=100.;
printf("Month=%2d,Expense=$%.2f\n",month,expense);
month=11;
expense=82.1;
printf("For the %2dth month of the year\n"
"the expenses were $%5.2f \n"
"and the income was $%6.2f\n\n",month ,expense,income);
}
結果顯示
算數運算符和表達式
運算數
算數運算符和他們的特點
算數表達式
代碼設計
#include <stdio.h>
void main(void)
{
int i,j,k,p,m,n;
float a,b,c,d,e,f,g,x,y;
i=5; j=5;
k=11;p=3;
x=3.0;y=4.0;
printf("......Initial values ......\n");
printf("i=%4d,j=%4d\nk=%4d,p=%4d\nx=%4.2f,y=%4.2f\n\n",i,j,k,p,x,y);
a=x+y;
b=x-y;
c=x*y;
d=x/y;
e=d+3.0;
f=d+3;
i=i+1;
j=j+1;
printf(".....Section 1 output ......\n");
printf("a=%5.2f,v=%5.2f\nc=%5.2f,d=%5.2f\ne=%5.2f f==%5.2f\ni==%5.d,%5d\n\n",a,b,c,d,e,f,i,j);
m=k%p;
n=p%k;
i++;
++j;
e--;
--f;
printf(".....Section 2 output ......\n");
printf("m=%4d,n=%4d\ni=%4d,j=%4d\ne=%4.2f,f=%4.2f\n",m,n,i,j,e,f);
}
結果顯示
從鍵盤輸入數據
使用scanf()函數
從鍵盤輸人數據
地址操作符&
double數據類型
代碼設計
#include <stdio.h>
void main(void)
{
float income;
double expense;
int month,hour,minute;
printf("What month is it?\n");
scanf("%d",&month);
printf("You have entered month=%5d\n",month);
printf("Please enter your income and expenses\n");
scanf("%f %1f",&income,&expense);
printf("Entered income=%8.2f,expenses=%8.2lf\n",income,expense);
printf("Please enter the time, e.g.,12:45\n");
scanf("%d : %d",&hour,&minute);
printf("Entered Time = %2d:%2d\n",hour,minute);
}
結果顯示
常量巨集及列印變數值的進一步討論
用define指令來定義常量
更多的轉換限定符及組成
科學計數法
轉換限定符中的標誌
代碼設計
#include <stdio.h>
#define DAYS_IN_YEAR 365
#define PI 3.14159
void main(void)
{
float income =1234567890.12;
printf("CONVERSION SPECIFICATIONS FOR INTEGERS \n\n");
printf("Days in year = \n"
"[[%1d]] \t(field width less than actual)\n"
"[[%9d]] \t(field width greater than actual)\n"
"[[%d]] \t(no field width specified) \n\n\n",
DAYS_IN_YEAR, DAYS_IN_YEAR,DAYS_IN_YEAR);
printf("CONVERSION SPECIFICATIONS FOR REAL NUMBERS\n\n");
printf("Cases for precision being specified correctly \n");
printf("PI = \n"
"[[%1.5f]] \t\t(field width less than actual) \n"
"[[%15.5f]] \t(field width greater than acutal) \n"
"[[%.5f]] \t\t(no field width specified) \n\n",
PI,PI,PI);
printf("Cases for field width being specified correctly \n");
printf("PI = \n"
"[[%7.2f]] \t\t(precision less than actual) \n"
"[[%7.8f]] \t\t(precision greater than actual) \n"
"[[%7.f]] \t\t(no precision specified) \n\n",
PI,PI,PI);
printf("PRINTING SCIENTIFIC NOTATION \n\n");
printf("income = \n"
"[[%18.2e]] \t(field width large,precision small) \n"
"[[%8.5e]] \t(field width and precision medium size) \n"
"[[%4.1e]] \t\t(field width and precision small) \n"
"[[%e]] \t(no specifications) \n\n",
income, income,income,income
);
printf("USING A FLAG IN CONVERSION SPECIFICATIONS \n\n");
printf("Days in year= \n"
"[[%-9d]] \t\t(field width large,flag included)\n",
DAYS_IN_YEAR
);
}
結果顯示
混合類型的運算、複合賦值、運算符優先順序和類型轉換
算術運算符的優先順序
初始化變數
算術運算中的陷阱
在算術表達式中混合使用整型數和實型數
類型轉換
副作用
代碼顯示
#include<stdio.h>
void main(void)
{
int i=1,j=1,
k1=10,k2=20,k3=30,k4=40,k5=50,
k,h,m,n;
float a=7,b=6,c=5,d=4,
e,p,q,x,y,z;
printf("Before increment, i=%2d,j=%2d\n",i,j);
k=i++;
h=++j;
printf("After increment, i=%2d,j=%2d\n"
" k=%2d,h=%2d\n\n",i,j,k,h);
m=6/4;
p=6/4;
n=6/4.0;
q=6/4.0;
printf("m=%2d,p=%3.1f\nn=%2d,q=%3.f\n\n",m,p,n,q);
printf("Original k1=%2d, k2=%2d, k3=%2d, k4=%2d,k5=%2d\n",k1,k2,k3,k4,k5);
k1 += 2;
k2 -= i;
k3 *= (8/4);
k4 /= 2.0;
k5 %= 2;
printf("New k1=%2d, k2=%2d,k3=%2d,k4=%2d,k5=%2d\n\n",
k1,k2,k3,k4,k5);
e=3;
x=a+b-c/d*e;
y=a+(b-c)/d*e;
z=((a+b)-c/d)*e;
printf("a=%3.0f,b=%3.0f,c=%3.0f\nd=%3.1f,e=%3.1f\n\n",
a,b,c,d,e);
printf("x= a + b -c /d *e = %10.3f \n"
"y= a +(b -c) /d *e = %10.3f \n"
"z=((a + b)-c /d)*e = %10.3f\n", x,y,z);
}
結果顯示
在黑夜裡夢想著光,心中覆蓋悲傷,在悲傷里忍受孤獨,空守一絲溫暖。 我的淚水是無底深海,對你的愛已無言,相信無盡的力量,那是真愛永在。 我的信仰是無底深海,澎湃著心中火焰,燃燒無盡的力量,那是忠誠永在