一.字符串常量
字符串是內(nèi)存中一段連續(xù)的char空間,以’?’(數(shù)字0)結(jié)尾。
字符串是由雙引號(hào)括起來(lái)的。
字符串常量與字符常量不同:‘a(chǎn)’為字符常量,“a”為字符型常量,每一個(gè)字符串結(jié)尾,編譯器會(huì)自動(dòng)添加一個(gè)結(jié)束標(biāo)志位 ‘?’。
#include int main(){char ch= “hello world”;printf(“%sn”,ch);Return 0;}此時(shí)屏幕上會(huì)輸出hello world,但是在ch定義為“hello ?world”,這是由于
%s的作用是遇到“?”就停止
,屏幕上只會(huì)輸出hello
二.printf的附加格式和putchar函數(shù)
- “—”代表左對(duì)齊
#include
int
main(){
int
a = 10;printf(
"===%5d===n"
, a);
return
0;}
沒(méi)加“—”,運(yùn)行如下:

#include
intmain()
{
inta = 10;
printf("===%-5d===n", a);
return0;
}
使用“—”運(yùn)行結(jié)果:

如果所賦的值超出限定范圍,就會(huì)正常原本不動(dòng)的輸出。此代碼限定范圍為4
#include
intmain()
{
inta = 123456;
printf("===%4d===n", a);
return0;

數(shù)字“0”的作用是將輸出的前面補(bǔ)上0,直到占滿指定列寬為止,不可以搭配使用“—”
#include
intmain()
{
inta = 1;
printf("===%09d===n", a);
return0;
}

putchar函數(shù)打印字符。
Putchar(ch)
Putchar(‘a(chǎn)’)
Putchar(97)
Putchar(‘n’)
三.Scanf函數(shù)和getchar函數(shù)
- #include
int
main(){
int
a, b;scanf(
"%d,%d"
, &a, &b);printf(
"%dt%d"
, a, b);
return
0;}

如果使用2022版本的visual studio,直接使用會(huì)報(bào)錯(cuò),需要換成
scanf_s
- getchar作用:接受鍵盤獲取字符
#include
int
main(){char ch;ch=getchar();putchar(ch);
return
0;}
-
內(nèi)存
+關(guān)注
關(guān)注
8文章
3117瀏覽量
75115 -
字符串
+關(guān)注
關(guān)注
1文章
589瀏覽量
21219 -
char
+關(guān)注
關(guān)注
0文章
11瀏覽量
3808
發(fā)布評(píng)論請(qǐng)先 登錄
LABVIEW中的printf函數(shù)---格式化寫入字符串函數(shù)
labview獲取DateTime格式時(shí)間字符串
labview 的格式化字符串,請(qǐng)問(wèn)選項(xiàng)的參數(shù)是0x%02x是什么意思?
C語(yǔ)言技巧 sprintf()函數(shù):將格式化的數(shù)據(jù)寫入字符串
2.6 python字符串格式化
剖析提升字符串格式化效率的小技巧

評(píng)論