1. 程式人生 > 其它 >C 基礎知識簡要複習

C 基礎知識簡要複習

“歡迎回到C語言的世界”

這是我時隔好幾個月再次開啟VS Code後,腦中出現的低沉你而又智慧的陌生聲音。這個聲音帶有驅動和鼓勵,又帶有一絲不滿與勉勵。這個聲音推動人繼續敲擊鍵盤,用指尖點選字母方塊,編織出自己奇妙的王國。

話不多說,繼續在這裡記錄吧。此篇權當是迴歸篇,簡要複習一下基礎,找回手感。

字串和格式化輸入輸出

eg.1:

//演示與使用者互動
#include<stdio.h>
#include<string.h> //提供strlen()函式
#include<windows.h>
#define DENSITY 62.4 //人體密度
int main(void)
{
float weight,volume;
int size,letters;
char name[40];
printf("Hi,what's your first name?\n");
scanf("%s",&name);
printf("%s,what's your weight in pounds?\n",name);
scanf("%f",&weight);
size = sizeof(name);
letters = strlen(name);
volume = weight/DENSITY;
printf("Well,%S,your volume is %2.2f cubic feet.\n",name,volume);
printf("Also,your first name has %d letters,\n",letters);
printf("and we have %d bytes to store it.\n",size);
system("pause");
return 0;
}

eg.2:

//一些不匹配的整形轉換
#include<stdio.h>
#include<windows.h>
#define PAGES 336
#define WORDS 65618
int main(void)
{
short num = PAGES;
short mnum = -PAGES;
printf("num as short and unsigned short: %hd %hu\n",num,num);
printf("-num as short and unsigned short:%hd %hu\n",num,num);
printf("num as int and char:%d %c\n",num,num);
printf("WORDS as int,short,and char:%d %hd %c\n",WORDS,WORDS,WORDS);
system("pause");
return 0;
}
//short 型別與 int 型別的區別:
1)位元組數不同
2)資料範圍不同

eg.3:

//不匹配的浮點型別轉換
#include<stdio.h>
int main(void)
{
float n1 = 3.0;
double n2 = 3.0;
long n3 = 2000000000;
long n4 = 1234567890;
printf("%.le %.le %.le %.le\n",n1,n2,n3,n4);
printf("%ld %ld\n",n3,n4);
printf("%ld %ld %ld %ld\n",n1,n2,n3,n4);
getchar();
return 0;
}
//關於引數傳遞與型別轉換

eg.4:

//input.c---何時使用&
#include<stdio.h>
#include<windows.h>
int main(void)
{
int age;
float assets;
char pet[30];
printf("Enter your age,assets,and favorite pet:\n");
scanf("%d %f",&age,&assets);//scanf()讀取基本變數型別的值,在變數名前加上一個&
scanf("%s",pet);//scanf()把字串讀入字元陣列中,不要使用&
printf("%d $%2.2f %s\n",age,assets,pet);
system("pause");
return 0;
}

eg.5:

//varwid.c---使用變寬輸出欄位
#include<stdio.h>
#include<windows.h>
int main(void)

unsigned width,precision;
int number = 256;
double weight = 242.5;
printf("Enter a field wigth:\n");
scanf("%d",&width);
printf("The number is :%*d.\n",width,number);
printf("Now enter a width and a precision:\n");
scanf("%d %d",&width,&precision);
printf("weight = %*.*f\n",width,precision,weight);
printf("Done!\n");
system("pause");
return 0;
}
/*printf()中可以用*修飾符代替字元寬度
而scanf()中,*則表示跳過相應輸入項*/

運算子、表示式和語句

eg.1:

//shoes.c---計算多個不同鞋碼對應的腳長
#include<stdio.h>
#define ADJUST 7.31
int main(void)
{
const double SCALE = 0.333;
double shoe,foot;
printf("Shoe size(men's) foot length\n");
shoe = 3.0;
while(shoe<18.5)
{
foot = SCALE*shoe+ADJUST;
printf("%10.lf %15.2f inches\n",shoe,foot);
shoe = shoe+1.0;
}
printf("If the shoe fits,wear it.\n");
getchar();
return 0;
}

eg.2:

//sizeof.c---sizeof()的使用
#include<stdio.h>
int main(void)
{
int n = 0;
size_t intsize;
intsize = sizeof(int);
printf("n = %d,n has %zd bytes;all ints have %zd bytes.\n",n,sizeof n,intsize);
getchar();
return 0;
}
/*sizeof返回size_t型別的值
typedef機制允許為現有型別建立別名
'typedef double real'
這樣,real就是double的別名*/

eg.3:

//pound.c---定義一個帶引數的函式
#include<stdio.h>
void pound(int n);//n 為int型別變數
int main(void)
{
int times = 5;
char ch = '!';
float f = 6.0;
pound(times);
pound(ch);
pound(f);
getchar();
return 0;
}

void pound(int n)
{
while(n-->0)
printf("#");
printf("\n");
}

迴圈控制語句

eg.1:

//suming.c--根據使用者鍵入的整數求和
#include<stdio.h>
int main(void)
{
long num;
long sum =0L;//long 型別後要加L
int status;
printf("please enter an integer to be summed");
printf("(q to quit):");
status = scanf("%ld",&num);
while(status == 1)
{
sum += num;
printf("Please enter next integer(q to quit):");
status = scanf("%ld",&num);
}
printf("Those integer sum to %ld.\n",sum);
getchar();
return 0;
}
//注意,scanf()返回值賦給status,如果scanf讀取一個整數,則返回1;若讀取的不是數字,則返回0

eg.2:

//cmpflt.c---浮點數比較
#include<stdio.h>
#include<math.h>
int main(void)
{
const double ANSWER = 3.14159;
double response;
printf("What is the value of pi?\n");
scanf("%lf",&response);
while(fabs(response-ANSWER)>0.01)//fabs()取精度更高的double、float型別的絕對值,在math標頭檔案裡
{
printf("Try again!\n");
scanf("%lf",&response);
}
printf("Close enough!\n");
getchar();
return 0;
}

eg.3:

//truth.c---哪些值是為真
#include<stdio.h>
int main(void)
{
int n=3;
while(n)
printf("%2d is true.\n",n--);
printf("%2d is false.\n",n);
n = -3;
while(n)
{
printf("%2d is true.\n",n++);
}
printf("%2d is false.\n",n);
getchar();
return 0;
}

Ps:關於for語句:

for語句使用三個表示式控制迴圈過程

for(initialize;test;update)
statement

initialize表示式在執行for語句之前執行一次,然後對test表示式求值,接著對update求值。

eg.4:

//entry.c---入口條件迴圈
#include<stdio.h>
int main(void)
{
const int secret_code =13;
int code_entered;
printf("To enter the triskaidekaphobia therapy club,\n");
printf("please enter the secret code number:");
scanf("%d",&code_entered);
while(code_entered != secret_code)
{
printf("To enter the ttc,\n");
printf("please enter the secret code number:");
scanf("%d",&code_entered);
}
printf("Congratulations!You a cured!\n");
return 0;
}

eg.5:

//power.c---計算數的整數冪
#include<stdio.h>
double power(double n,int p);
int main(void)
{
double x,xpow;
int exp;
printf("Enter a number and the positive integer power\n");
printf("to which the number will be raised.Enter q to quit.");
while(scanf("%lf %d",&x,&exp)==2)
{
xpow = power(x,exp);
printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
printf("enter next pair of numbers or q to quit.\n");
}
printf("hope you enjoyed this power trip--byebye!\n");
getchar();
return 0;
}
double power(double n,int p)
{
double pow=1;
int i;
for(i = 1;i<=p;i++)
pow*=n;
return pow;
}

分支和跳轉控制語句

eg.1:

//colddays---統計寒冷天氣百分比
#include<stdio.h>
#include<windows.h>
int main(void)
{
const int FREEZING = 0;
float temperature;
int cold_days = 0;
int all_days = 0;
printf("Enter the list of daily low temperatures.\n");
printf("Use Celsius,and enter q to quit.\n");
while(scanf("%f",&temperature)==1)
{
all_days++;
if(temperature<FREEZING)
{
cold_days++;
}
}
if(all_days!=0)
printf("%d days total:%.lf%% were below freezing.\n",all_days,100.0 * (float) cold_days/all_days);
if(all_days == 0)
printf("No data entered!\nplz try again!");
system("pause");
return 0;
}

eg.2:

//divisors.c---使用巢狀if語句顯示一個數的約數
#include<stdio.h>
#include<stdbool.h>
#include<windows.h>
int main(void)
{
unsigned long num;//待測試的數
unsigned long div;//可能的約束
bool isPrime;
printf("Please enter an integer for analysis;");
printf("Enter q to quit.\n");
while(scanf("%lu",&num)==1)
{
for(div = 2,isPrime = true;(div*div)<=num;div++)
{
if(num%div==0)
{
if((div*div)!=num)
printf("%lu is divsible by %lu and %lu.\n",num,div,num/div);
else
printf("%lu is divisible by %lu and %lu.\n",num,div);
isPrime = false;//該數不是素數
}
}
if(isPrime)
printf("%lu is is prime.\n",num);
printf("Please enter another integer for analysis;");
printf("Enter q to quit.\n");
}
printf("byebye!\n");
system("pause");
return 0;
}

此外關於C語言中分支與跳轉還有許多方法,例如if語句,switch語句,break、continue、goto語句等跳轉語句,用法都類似,貫徹迴圈重複執行任務的思想。

函式

//lethead1.c
#include<stdio.h>
#define NAME "GALATHINK,INC."
#define ADDRESS "101 MEGABUCK PLAZA"
#define PLACE "MEGApolis,CA 94904"
#define WIDTH 40
void starbar(void);
int main(void)
{
starbar();
printf("%s\n",NAME);
printf("%s\n",ADDRESS);
printf("%s\n",PLACE);
starbar();
return 0;
}
void starbar(void)//簡單地用這個函式做個邊框
{
int count;
for(count = 1;count<=WIDTH;count++)
putchar('*');
putchar('\n');
}