Ax前導(dǎo)程序
//開(kāi)始學(xué)第四章了,字符串的輸出和輸入。這是第一個(gè)簡(jiǎn)單程序
#include<stdio.h>
#include<string.h> //提供trlen()函數(shù)的原型
#define DENSITY 62.4 //人體密度 density
int main()
{
float weight, volume;
int size, letters;
char name[40]; //name是一個(gè)可容納40個(gè)字符的數(shù)組,但是只能儲(chǔ)存39個(gè),最后一個(gè)是空NULL或者\(yùn)0
//[40]表明該數(shù)組有40個(gè)元素?cái)?shù)量
printf("Hi! What's your first name?\n",name);
/*%s轉(zhuǎn)換說(shuō)明來(lái)處理字符串的輸入和輸出,沒(méi)有&*/
scanf("%s",name);
printf("%s,what's your weight in pounds?\n",name);
scanf("%f",&weight);
size = sizeof 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);
return 0;
}
運(yùn)行結(jié)果如下:
Hi! What's your first name?
enomothem
enomothem,what's your weight in pounds?
120
Well, enomothem, your volume is 1.92 cubic feet.
Also,your first name has 52 letters,
and we have 60 bytes to store it.
新特性
- 數(shù)組(array)儲(chǔ)存字符串(character string),該程序的數(shù)組占用內(nèi)存的40個(gè)字節(jié)。
- %s字符轉(zhuǎn)換用來(lái)處理字符串的輸入和輸出。name沒(méi)有&
- #define預(yù)處理器定義DENSITY字符常量為62.4
- 用C函數(shù)strlen()獲取字符串長(zhǎng)度
Bx字符串簡(jiǎn)介
字符串(character string)是一個(gè)或多個(gè)字符的序列。
例如:“kfwejfoiawhjfil;awjfjwaeofjioaewfj” // 隨便打的 *-*
字符串必須創(chuàng)建數(shù)組,最后一位為NULL,計(jì)算機(jī)會(huì)處理好,注意不要溢出就行。
char類(lèi)型數(shù)組和NULL字符
//使用不同類(lèi)型的字符串
#include<stdio.h>
#define PRAISE "You are an extraordinary being."
#define LJ "這個(gè)好,輕松。"
//不用在字符串末尾加Null空字符串,編譯器會(huì)自動(dòng)加喲!
//你就是一個(gè)非凡的存在!
int main(void)
{
char name[40];
printf("What's your name?\n");
scanf("%s",name); //scanf()中的字符串只能識(shí)別一個(gè)字符串,如有空格,就不會(huì)識(shí)別空格后面的。
printf("Hello,%s. %s\n",name, PRAISE); //%s告訴rintf()打印一個(gè)字符串。
printf("%s,%s",name, LJ);
return 0;
}
運(yùn)行結(jié)果如下:
What's your name?
xing he
Hello,? You are an extraordinary being.
xing,這個(gè)好,輕松。
字符串和字符
區(qū)別于字符是'',是基本類(lèi)型(char),字符串是" "是派生類(lèi)型(char 數(shù)組),字符串后面加上NULL 。
strlen()函數(shù)
strlen()函數(shù)給出字符串中的字符長(zhǎng)度
/* trlen */
#include<stdio.h>
#include<string.h>
#define PRAISE "You are an extraodinary being."
int main(void)
{
char name[42];
printf("What's your name?");
scanf("%s",name);
printf("Hello, %s. %s\n",name,PRAISE);
printf("Your name of %zd letters occupies %zd memory cells.\n"
,strlen(name),sizeof name);
//sizeof 和 strlen 都是重要的編程工具。
printf("The phrase of praise has %zd letters",strlen(PRAISE));//不計(jì)算NULL
printf("and occupies %zd memory cells.\n",sizeof(PRAISE)); //計(jì)算NULL,括號(hào)可有可無(wú),好習(xí)慣是加上
return 0;
}
運(yùn)行結(jié)果如下:
What's your name?yun ge
Hello, yun. You are an extraodinary being.
Your name of 3 letters occupies 42 memory cells.
The phrase of praise has 30 lettersand occupies 31 memory cells.
程序中加入頭文件
#include<string.h>
當(dāng)中,sizeof運(yùn)算符使用%zd轉(zhuǎn)換說(shuō)明,對(duì)于strlen()同樣適用。
還有就是括號(hào),sizeof(char);這是對(duì)于類(lèi)型,對(duì)于特定量,則不用。
下面我來(lái)學(xué)習(xí)define指定
常量和C預(yù)處理器
關(guān)鍵詞:符號(hào)常量/常量(或明示常量)/編譯時(shí)替換/常量的命名
在計(jì)算是,有一些是固定不變的,我們稱(chēng)為常量,那么我們?cè)贑里面也會(huì)給一個(gè)變量定義為某個(gè)固定的數(shù)字,不過(guò)會(huì)隨著程序的變化而改變,所以我們需要一個(gè)常量來(lái)保證常量的不變,所以就有了一個(gè)Define指令。
常量稱(chēng)為 符號(hào)常量(symbolic constant),使用它會(huì)更方便
1.常量名比數(shù)字表達(dá)更多信息。
--創(chuàng)建符號(hào)常量?
float xxx;
xxx = 0.013;
但xxx是一個(gè)變量,程序可能會(huì)改變它的值,所以C提供了更好的方案--C預(yù)處理器
預(yù)處理器也可以來(lái)定義常量
define TAXRATE 0.015
編譯程序時(shí),TAXRATE都會(huì)編譯成0.015.這一過(guò)程稱(chēng)為編譯時(shí)替換(compile-timesub stitution)
也稱(chēng)為 明示常量(manifest constant)= 符號(hào)常量
定義的格式:
define NAME value //用大寫(xiě)定義C語(yǔ)言常量是一貫的傳統(tǒng),提高可讀性
//在比薩餅程序中使用定義的常量
#include<stdio.h>
#define PI 3.14159 // 圓周率
int main(void)
{
float area, circum, radius;
printf("What is the radius of your pizza?\n");
scanf("%f",&radius);
a rea = PI *radius * radius;
circum = 2.0 * PI * radius;
printf("Your basic pizza parameters are as follows: \n");
printf("circumference = %1.2f,area = %1.2f\n",circum,area);
return 0;
}
運(yùn)行結(jié)果如下:
What is the radius of your pizza?
12
Your basic pizza parameters are as follows:
circumference = 75.40,area = 452.39
注意格式,和命名,還有必要加上注釋?zhuān)@些都是良好的習(xí)慣。
const限定符
const限定了變量不可更改,只可讀。
明示常量
// efines.c -- 使用limit.h和float頭文件中定義的明示常量
#include<stdio.h>
#include<limits.h>
#include<float.h>
int main(void)
{
printf("Some number limits for this system:\n");
printf("Biggest int: %d\n", INT_MAX );
printf("Smallest long long:%lld\n", LLONG_MIN);
printf("One byte = %d bits on this system\n",CHAR_BIT);
printf("Largest double: %e\n",DBL_MAX);
printf("Smallest normal float: %e\n",FLT_MIN);
printf("float precision =%d digits\n", FLT_DIG);
printf("float epsilon = %e\n",FLT_EPSILON);
return 0;
}
輸出如下:
Some number limits for this system:
Biggest int: 2147483647
Smallest long long:-9223372036854775808
One byte = 8 bits on this system
Largest double: 1.797693e+308
Smallest normal float: 1.175494e-038
float precision =6 digits
float epsilon = 1.192093e-007
Dx_printf()和scanf()
printf()是輸出函數(shù),scanf()是輸入函數(shù),可以讓用戶(hù)交互,簡(jiǎn)稱(chēng)為I/O.
不同版本的I/O不同。
printf()函數(shù)
請(qǐng)求printf()函數(shù)打印數(shù)據(jù)的指令要于待打印的類(lèi)型相匹配,這個(gè)在前面的第三章里講到過(guò),打印整數(shù)時(shí)使用%d,打印字符時(shí)使用%c,此類(lèi)符號(hào)稱(chēng)之為轉(zhuǎn)換說(shuō)明(conversion specification)
| 轉(zhuǎn)換說(shuō)明 | 輸出 |
|---|---|
| %a | 浮點(diǎn)數(shù)、十六進(jìn)制和p計(jì)數(shù)法 |
| %A | 浮點(diǎn)數(shù)、十六進(jìn)制和p計(jì)數(shù)法 |
| %c | 單個(gè)字符 |
| %d | 有符號(hào)的十進(jìn)制數(shù) |
| %e | 浮點(diǎn)數(shù),e計(jì)數(shù)法 |
| %E | 浮點(diǎn)數(shù),e計(jì)數(shù)法 |
| %f | 浮點(diǎn)數(shù),十進(jìn)制計(jì)數(shù)法 |
| %g | 浮點(diǎn)數(shù),e計(jì)數(shù)法 |
| %G | 浮點(diǎn)數(shù),e計(jì)數(shù)法 |
| %i | 有符號(hào)十進(jìn)制整數(shù)(與%d相同) |
| %o | 無(wú)符號(hào)八進(jìn)制數(shù) |
| %p | 指針 |
| %s | 字符串 |
| %u | 無(wú)符號(hào)十進(jìn)制整數(shù) |
| %x | 無(wú)符號(hào)十六進(jìn)制整數(shù),使用十六進(jìn)制數(shù)0F |
| %X | 無(wú)符號(hào)十六進(jìn)制整數(shù),使用十六進(jìn)制數(shù)0F |
| %% | 打印一個(gè)百分號(hào) |
使用printf()函數(shù)
// printf轉(zhuǎn)換說(shuō)明
#include<stdio.h>
#define PI 3.141593
int main(void)
{
int number =7;
float pies = 12.75;
int cost = 7800;
printf("The %d contestants ate %f berry pies.\n", number,
pies);
printf("The value of pi is %f.\n",PI);
printf("Farewell! thou art too dear for my possessing,\n");
printf("%c%d\n",'$',2*cost);
return 0;
}
運(yùn)行程序:
The 7 contestants ate 12.750000 berry pies.
The value of pi is 3.141593.
Farewell! thou art too dear for my possessing,
$15600
printf()函數(shù)的格式
printf(格式字符串,待打印項(xiàng)1,待打印項(xiàng)2(包括表達(dá)式)....);
e.g
printf("The %d contestants ate %f berry pies. \n",number,pies);
上面的語(yǔ)句中包含了兩個(gè)轉(zhuǎn)換說(shuō)明。
如果想要打印%,使用%轉(zhuǎn)換即可,也就是%%。
printf()的轉(zhuǎn)換說(shuō)明修飾符
在%和轉(zhuǎn)換字符之間插入修飾符可修飾轉(zhuǎn)換說(shuō)明。
https://blog.csdn.net/whmnirvana/article/details/54580407
printf()標(biāo)記
| 標(biāo)記 | 含義 |
|---|---|
| - | 待打印項(xiàng)左對(duì)齊 |
| + | 有符號(hào)值為正則顯示+,負(fù)數(shù)則加上- |
| 空格 | 有符號(hào)值為正則顯示一個(gè)空格,為負(fù)數(shù)則加上 - |
| # | 把結(jié)果轉(zhuǎn)換為另一種形式。如果是%o格式,則以0開(kāi)始;如果是%x格式,則以0x開(kāi)始,對(duì)于浮點(diǎn),即使沒(méi)有小數(shù)點(diǎn)后,也打印小數(shù)點(diǎn)字符,對(duì)于G和g個(gè)是防止后面的0被刪除。 |
| 0 | 對(duì)于數(shù)值格式,用前導(dǎo)0代替空格填充字段寬度。對(duì)于整數(shù)格式,如果出現(xiàn) - 標(biāo)記或指定精度,則忽略該標(biāo)記 |
程序4-7(test 字段寬度)
//字段寬度
#include<stdio.h>
#define RAGES 985
int main(void)
{
printf("*%d*\n",RAGES);
printf("*%2d*\n",RAGES);
printf("*%10d*\n",RAGES);
printf("*%-10d*\n",RAGES);
return 0;
}
*985*
*985*
* 985*
*985 *
程序4-8(一些浮點(diǎn)數(shù)的修辭符的組合)
/* - floats -- 一些浮點(diǎn)型修辭符的組合*/
#include<stdio.h>
int main(void)
{
const double RENT = 3852.99; //const變量,限定為常量的作用
printf("*%f*\n", RENT);
printf("*%e*\n", RENT); //默認(rèn)小數(shù)點(diǎn)左邊1位,右邊6位,但是打印次數(shù)多!
printf("*%4.2f*\n", RENT);
printf("*%3.1f*\n", RENT); //控制后面打印的位數(shù)為1
printf("*%10.3f*\n", RENT);//控制小數(shù)點(diǎn)后面打印位數(shù)為3
printf("*%10.3E*\n", RENT); // 四舍五入
printf("*%+4.2f*\n", RENT);
printf("*%010.2f*\n", RENT);
return 0;
}
*3852.990000*
*3.852990e+003*
*3852.99*
*3853.0*
* 3852.990*
*3.853E+003*
*+3852.99*
*0003852.99*
程序4-9(演示一些格式標(biāo)記)
// 演示一些格式標(biāo)記
#include<stdio.h>
int main(void)
{
printf("%x %X %#x\n",31,31,31); //%x打印十六進(jìn)制數(shù),#則標(biāo)記十六進(jìn)制數(shù)
printf("**%d**% d**% d**\n", 42, 42, -42); //演示了空格的生成,負(fù)數(shù)則不產(chǎn)生
printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6, 6, 6);//這里使用了精度,前導(dǎo)0和精度一起出現(xiàn)0會(huì)被忽略
return 0;
}
1f 1F 0x1f
**42** 42**-42**
** 6** 006**00006** 006**
程序4-10(字符串格式)
// stringf ---字符串格式
#include<stdio.h>
#define BLURB "Authentic imitation!"
int main(void)
{
printf("[%2s]\n", BLURB);
printf("[%24s]\n", BLURB);
printf("[%24.5s]\n",BLURB);
printf("[%-24.5s]\n",BLURB);
return 0;
}
該程序輸出如下:
[Authentic imitation!]
[ Authentic imitation!]
[ Authe]
[Authe ]
轉(zhuǎn)換說(shuō)明的意義(就是不當(dāng)?shù)牟僮鲿?huì)出現(xiàn)問(wèn)題)
轉(zhuǎn)換說(shuō)明把以二進(jìn)制格式儲(chǔ)存在計(jì)算機(jī)中的值轉(zhuǎn)換成一系列的字符(字符串)形式以便顯示。
轉(zhuǎn)換(conversion) 是翻譯說(shuō)明,而不是原始值替換成轉(zhuǎn)換后的值。無(wú)論如何,無(wú)論顯示說(shuō)明,值還是那個(gè)被儲(chǔ)存在計(jì)算機(jī)里的二進(jìn)制。
a.轉(zhuǎn)換不匹配
(1)一些不匹配的整型轉(zhuǎn)換
// 一些不匹配的整型轉(zhuǎn)換
#include<stdio.h>
#define PAGES 336
#define WORDS 65618
int main()
{
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",mnum, mnum);
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);
return 0;
}
num as short and unsigned short: 336 336
-num as short and unsigned short: -336 65200
num as int and char: 336 P
WORDS as int , short , and char: 65618 82 R 82是65618除以65535的余數(shù)
(2) 不匹配的浮點(diǎn)型轉(zhuǎn)換
/* loatcnv -- 不匹配的浮點(diǎn)型轉(zhuǎn)換*/
#include <stdio.h>
int main(void)
{
float n1 =3.0;
double n2 = 3.0;
long n3 = 20000000000;
long n4 = 12345567890;
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);
return 0;
}
3e+000 3e+000 -6e+153 7e+264
-1474836480 -539333998
0 1074266112 0 1074266112
b.Printf()的返回值
(1)printf()返回值
// printf()返回值
#include<stdio.h>
int main(void)
{
int bph2o = 212;
int rv;
rv = printf("%d F is water 's boiling point. \n",bph2o); //將printf()的返回值賦值給rv。
printf("The printf() function printed %d characters.\n",
rv);
return 0;
}
212 F is water 's boiling point.
The printf() function printed 34 characters.
c.打印較長(zhǎng)的字符串
有時(shí),printf()語(yǔ)句太長(zhǎng),在屏幕上不方便閱讀,所以需要寫(xiě)成多行。
有幾點(diǎn)要注意的是斷行不能在“”之間斷行,要在待打印項(xiàng)中間的逗號(hào)后面換行。
程序 - 打印較長(zhǎng)的字符串
// longstrg - -- 打印較長(zhǎng)的字符串
#include<stdio.h>
int main(void)
{
printf("Here's one way to print a ");
printf("long string.\n");
printf("Here's another way to print a \
long string.\n");
printf("Here's the newest way to print a "
"long string.\n"); /* ANSI C*/
return 0;
}
Here's one way to print a long string.
Here's another way to print a long string.
Here's the newest way to print a long string.
方法一:使用多個(gè)printf語(yǔ)句。
方法二:用反斜杠(\)和 Enter 鍵組合。
方法三: ANSI C 引入了字符串連接,在兩個(gè)雙引號(hào)括起來(lái)的字符串之間用空白隔開(kāi)
例如:
printf("Hello,young");
printf("Hello" "young");
printf("Hello"
"young");
以上三個(gè)是等效的。
使用scanf()
scanf()與printf()相反,是輸入函數(shù),是C庫(kù)里面通用的一個(gè)函數(shù)。鍵盤(pán)輸入的都是文本,因?yàn)殒I盤(pán)只能輸入文本字符,字母、數(shù)字、標(biāo)點(diǎn)符號(hào)。
scanf()把輸入的字符串轉(zhuǎn)為整數(shù)、浮點(diǎn)數(shù)、字符、字符串。
printf()把整數(shù)、浮點(diǎn)數(shù)、字符、字符串轉(zhuǎn)換為在屏幕上顯示的文本。
它們兩個(gè)的格式類(lèi)似,也是用格式字符串和參數(shù)列表。scanf()使用的是指向變量指針,這里不需要知道指針,知道下面兩個(gè)規(guī)則。
- scanf()讀取基本變量類(lèi)型的值,在變量名前面加&;
- scanf()把字符串讀入字符數(shù)組中,不要使用&;
程序 使用&的規(guī)則
// input --何時(shí)使用&
#include<stdio.h>
int main(void)
{
int age; // 變量
float assets; //變量
char pet[30]; //字符數(shù)組,用于儲(chǔ)存字符串
printf("Enter your age, assets , and favorite pet.\n");
scanf("%d %f", &age , &assets); //這里要用&
scanf("%s", pet ); //字符數(shù)組不使用&
printf("%d $%.2f %s\n", age, assets, pet);
return 0;
}
Enter your age, assets , and favorite pet.
123
323
是的
123 $323.00 是的
在輸入的同時(shí)可以輸入換行符、空格或制表符分成兩行。
唯一不同的是%c,會(huì)讀取每個(gè)字符,包括空格、制表符、換行符
scanf()的轉(zhuǎn)換說(shuō)明
| 轉(zhuǎn)換說(shuō)明 | 含義 |
|---|---|
| %c | 把輸入解釋成字符 |
| %d | 把輸入解釋成十進(jìn)制整數(shù) |
| %e、%f、%g、%a | 把輸入解釋成浮點(diǎn)數(shù) |
| %E、%F、%G、%A | 把輸入解釋成浮點(diǎn)數(shù) |
| %i | 把輸入解釋成十進(jìn)制整數(shù) |
| %o | 把輸入解釋成有符號(hào)八進(jìn)制整數(shù) |
| %p | 把輸入解釋成指針(地址) |
| %s | 把輸入解釋成字符串。從第一個(gè)非空白字符開(kāi)始,到下一個(gè)字符串之前都是輸入。 |
| %u | 把輸入解釋成無(wú)符號(hào)十進(jìn)制數(shù) |
| %x、%X | 把輸入解釋成有符號(hào)十六進(jìn)制數(shù) |
scanf()轉(zhuǎn)換說(shuō)明
| 轉(zhuǎn)換說(shuō)明 | 含義 |
|---|---|
| * | 抑制賦值 |
| 數(shù)字 | 最大字段寬度。達(dá)到最大字段寬度處,或遇到空白字符停止 |
| hh | 把整數(shù)作為signed cahr或unsigned char 類(lèi)型讀取 |
| ll | 把整數(shù)作為long long 或unsigned long long類(lèi)型讀取 |
| h\l\L | %hd和%hi表明把對(duì)應(yīng)值存儲(chǔ)為short int 類(lèi)型;%ho、%hx、%hu表明把對(duì)應(yīng)的值儲(chǔ)存為unsigned short int 類(lèi)型;%ld和%li表示把對(duì)應(yīng)的值儲(chǔ)存為long類(lèi)型;%lo、%lx、%lu表示把對(duì)應(yīng)的值存儲(chǔ)為unsigned long類(lèi)型;%le、%lf、%lg表明double類(lèi)型,如果在e、f和g前面使用L而不是l,表明把對(duì)應(yīng)的值儲(chǔ)存為long double類(lèi)型。如果沒(méi)有修飾符,d、i、o和x表明把對(duì)應(yīng)的值被儲(chǔ)存為 int 類(lèi)型。f和g表明把對(duì)應(yīng)的值儲(chǔ)存為float類(lèi)型。 |
| j | 在整形轉(zhuǎn)換后面時(shí),表明使用intmax_t或uintmax_t類(lèi)型 |
| z | 在整形轉(zhuǎn)換說(shuō)明后面時(shí),表明sizeof的返回類(lèi)型 |
| t | 在整形轉(zhuǎn)換后面時(shí),表明使用表示兩個(gè)指針差值的類(lèi)型 |
1.從scanf()角度看輸入
C語(yǔ)言還有其它的輸入函數(shù),如getchar()、fgets()。但是這兩個(gè)函數(shù)更適合處理一些特殊的情況,scanf()函數(shù)可以讀取整數(shù)、小數(shù)、字符和字符串。
2.格式字符串中的普通字符
scanf()函數(shù)允許普通字符放在格式字符串中。
3.scanf()的返回值
scanf()函數(shù)返回成功讀取的項(xiàng)數(shù)。
printf()和scanf()的*修飾符
printf和scanf都使用*修辭符,但用法不一樣,如果不想預(yù)先設(shè)定指定字寬,就可以使用*來(lái)代替。
printf()*程序
/* arwid -- 使用變寬輸入字段 */
#include<stdio.h>
int main(void)
{
unsigned width,precision;
int number = 256;
double weight = 242.5;
printf("Enter a field width:\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");
return 0;
}
Enter a field width:
2
The number is :256:
Now enter a width and a precision:
12 2
Weight = 242.50
Done!
scanf()*程序
// 通過(guò)輸入中的兩個(gè)整數(shù)
#include<stdio.h>
int main(void)
{
int n;
printf("Please enter three integers:\n");
scanf("%*d %*d %d", &n);
printf("The last integer was %d\n",n);
return 0;
}
Please enter three integers:
123 1235 666
The last integer was 666
》在讀取特定的內(nèi)容時(shí),逃過(guò)功能很有效。*放在%和轉(zhuǎn)換字符之間時(shí),會(huì)使得scanf()跳過(guò)相應(yīng)的輸出項(xiàng)。
printf()的用法提示
printf("%d %d %d\n", val1,val2 ,val3);
打印出來(lái)的數(shù)字會(huì)參差不齊,所以使用足夠大的固定字段寬度可以讓輸出整齊美觀。
printf("%9d %9d %9d\n", val1, val2 ,val3);
這樣就美觀了。
程序 :printf用法,更美觀的輸出
// printf用法,更美觀的輸出
#include<stdio.h>
#define a 234
#define b 12
#define c 1
int add(void);
int main(void)
{
int one , two , thr;
one = 1212;
two = 2312;
thr = 456243;
printf("%d %d %d\n",one,two,thr);
printf("%d %d %d\n",a , b ,c);
add();
printf("%9d %9d %9d\n",one,two,thr);
printf("%9d %9d %9d\n",a , b ,c);
return 0;
}
int add(void)
{
int w1,w2, w3;
w1 = 12;
w2 = 3223;
w3 = 4523423;
printf("%d %d %d\n",w1,w2,w3);
printf("%9d %9d %9d\n",w1,w2,w3);
}
1212 2312 456243
234 12 1
12 3223 4523423
12 3223 4523423
1212 2312 456243
234 12 1
本地化設(shè)置
就是有些地方的小數(shù)中間不是用點(diǎn),而是用都好,比如荷蘭,所以C語(yǔ)言對(duì)這些特殊的地方提供了本地的語(yǔ)言環(huán)境,所以這個(gè)我們是比較常規(guī)的,不同考慮。
關(guān)鍵概念
C語(yǔ)言用char類(lèi)型表示單個(gè)字符,用字符串表示字符序列.字符串常量是一種字符串形式,不管是存儲(chǔ)在字符串?dāng)?shù)組還是字符常量中,都以一個(gè)NULL結(jié)尾。
在程序中,盡量使用明示常量,不管用#define也好,還是用const限定變量都可以。提高程序的可讀性和可維護(hù)性。
C語(yǔ)言中,標(biāo)準(zhǔn)輸入函數(shù)和輸出函數(shù)I/O都使用一個(gè)相同,scanf和printf。其重點(diǎn)都是轉(zhuǎn)換說(shuō)明必須與后序參數(shù)中的值相匹配。例如,int轉(zhuǎn)換為%d與一個(gè)浮點(diǎn)值匹配會(huì)產(chǎn)生奇怪的結(jié)果。必須格外小心。
小結(jié)
字符串是一系列被視為一個(gè)處理單元的字符。字符串是以NULL結(jié)尾的一系列字符。
可以把字符串存儲(chǔ)在字符數(shù)組里。數(shù)組是一系列同類(lèi)型的項(xiàng)或元素。
字符串常量是用雙引號(hào)括起來(lái)的字符序列。
scanf()函數(shù)可用于獲得字符串的長(zhǎng)度。%s可讀取一個(gè)單詞
C預(yù)處理器為預(yù)處理器指令,查找源代碼程序,并在編譯之前處理它們,根據(jù)include指定把另一個(gè)文件中的內(nèi)容添加到該指定所在的位置。#define指令可以創(chuàng)建明示常量,limits.h和float.h頭文件用#define定義了一組表示整數(shù)和浮點(diǎn)型不同屬性的符號(hào)常量。另外還可以使用const限定符創(chuàng)建定義不可修改的只讀變量。
printf()和scanf()函數(shù)對(duì)輸入和輸出提供多種支持。兩個(gè)函數(shù)都使用格式字符串,其中包括轉(zhuǎn)換說(shuō)明表明待讀取或待打印數(shù)據(jù)項(xiàng)的數(shù)量和類(lèi)型。另外,可以使用轉(zhuǎn)換說(shuō)明控制輸出的外觀:寬度、小數(shù)點(diǎn)、字段的布局。
作業(yè),本人不喜歡做作業(yè),但是很可以的是有答案,嘻嘻
1.請(qǐng)輸入名和姓(根據(jù)英文的書(shū)效果,中間又一個(gè)空格),會(huì)發(fā)生說(shuō)明情況,為什么?
#include<stdio.h>
#include<string.h> //提供trlen()函數(shù)的原型
#define DENSITY 62.4 //人體密度 density
int main()
{
float weight, volume;
int size, letters;
char name[40]; //name是一個(gè)可容納40個(gè)字符的數(shù)組,但是只能儲(chǔ)存39個(gè),最后一個(gè)是空NULL或者\(yùn)0
printf("Hi! What's your first name?\n",name);
/*%s轉(zhuǎn)換說(shuō)明來(lái)處理字符串的輸入和輸出,沒(méi)有&*/
scanf("%s",name);
printf("%s,what's your weight in pounds?\n",name);
scanf("%f",&weight);
size = sizeof 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);
return 0;
}
Hi! What's your first name?
ge yun
ge,what's your weight in pounds? //只出現(xiàn)了ge,yu沒(méi)有打印。
Well, ge, your volume is 0.00 cubic feet.
Also,your first name has 52 letters,
and we have 40 bytes to store it.
程序只能讀到名,而姓還在緩沖區(qū),下一條scanf輸入緩沖區(qū)查找時(shí),從上次讀入結(jié)束的地方開(kāi)始讀取,這樣就把留在緩沖區(qū)的姓作為體重來(lái)讀取,導(dǎo)致scanf()讀取失敗。另一方面,如果輸入lasha 134,那么程序會(huì)把134作為用戶(hù)體重。
2.在C中如何輸出雙引號(hào)
轉(zhuǎn)義就行了
3.找錯(cuò)誤
define B booboo
define X 10
main(int)
{
int age;
cahr name;
printf("Please enter your first name.");
scanf("%s",name);
printf("All right, %c ,what's your age?\n",name);
xp = age+ X;
printf("That's a %s! You must be at least %d\n",name);
rerun 0;
}