字符和字串
1. 請問字串 “I love sunyu!” 在記憶體中總共占用多少個位元組?
答:一共有13個位元組,還有一個 ‘\0’ 字符表示結束,
2. 如有變數 char name[10] = “SunYu”,請寫出變數 name 在記憶體中的存盤形式?
答:由于 name 變數是宣告了 10 個位元組的字串,而 “SunYu” 只需 6 個位元組的空間來存放,因此多余的空間用 ‘\0’ 進行填充,所以 name 變數在記憶體中的存盤形式應該是:‘S’,‘u’,‘n’,‘Y’,‘u’,’\0’,’\0’,’\0’,’\0’,’\0’,
3. 在 Linux 系統上如何快速查看 ASCII 字符表?
答:輸入命令 man ascii,按下字母 ‘q’ 可退出,
dym@ubuntu:~$ man ascii
4. 問題:寫一個華氏度到攝氏度的轉換程式,用戶輸入華氏度,程式計算并輸出對應的攝氏度,攝氏度 =(1華氏度 – 32) 5 / 9*
答:代碼如下:
#include <stdio.h>
int main()
{
float fahrenheit;
float centigrade;
printf("please input Fahrenheit:");
scanf("%f",&fahrenheit);
centigrade = (fahrenheit - 32) * 5 / 9;
printf("the centigrade is %f\n",centigrade);
return 0;
}
運行結果:
dym@ubuntu:~/project/c_proj/FishC/test$ gcc test.c -o test && ./test
please input Fahrenheit:150
the centigrade is 65.555557
- 要求輸出結果為:

代碼如下:
#include <stdio.h>
int main()
{
char name[100];
float height;
float weight;
printf("please input your name:");
scanf("%s",name);
printf("please input your height(cm):");
scanf("%f",&height);
printf("please input your weight(kg):");
scanf("%f",&weight);
printf("------change------\n");
height = height / 2.54;
weight = weight / 0.453;
printf("%s's height is %f(in) and weight is %f(lb)\n",name,height,weight);
return 0;
}
運行結果如下:
dym@ubuntu:~/project/c_proj/FishC/test$ gcc test.c -o test && ./test
please input your name:SunYu
please input your height(cm):180
please input your weight(kg):65
------change------
SunYu's height is 70.866142(in) and weight is 143.487854(lb)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/248159.html
標籤:其他
上一篇:NB-IOT模塊與MQTT.fx使用MQTT協議通訊
下一篇:STM32學習筆記(四)
