【1】字符串常量有哪些特性?
字符串常量之所以稱之為常量,因為它可以看作是一個沒有命名的字符串且為常量。存儲於靜態數據區。
常量就意味着它具有“只讀”屬性,不允許被修改。
靜態數據區,是相對於堆、棧等動態數據區而言的。
靜態數據區存放的是全局變量和靜態變量。
全局變量分為常量和一般變量。
因為字符串常量不允許進行修改,放在靜態內存區會提高效率。
而且其不可改變的性質與靜態變量相當類似。更准確表述:存儲在常量數據區。
【2】字符串常量與字符常量有何區別?
(1)字符常量由單引號括起來,字符串常量由雙引號括起來。
示例代碼如下:
1 char ch='a'; 2 char *str="abcedef";
(2)字符常量只能是單個字符,字符串常量則可以含一個或多個字符。
(3)可以把一個字符常量賦予一個字符變量,但不能把一個字符串常量賦予一個字符變量。在C語言中沒有相應的字符串變量。
示例代碼如下:
1 char ch; 2 ch='s'; 3 ch="s";//error!!
(4)字符常量占一個字節的內存空間。字符串常量占的內存字節數等於字符串中字節數加1。
增加的一個字節中存放字符"\0"(ASCII碼為0)。這是字符串結束的標志。
例如:字符串 "C program"在內存中所占的字節為:C program\0。
字符常量'a'和字符串常量"a"雖然都只有一個字符,但在內存中的情況是不同的。
'a'在內存中占一個字節,可表示為:a;
"a"在內存中占二個字節,可表示為:a\0 。
(5)C語言規定,每一個字符串總是以“\0”作為字符串的結束標志。
C語言中的字符串用字符數組來表示。
示例代碼如下:
char str[]="abcdefghijk";
(6)當一個字符串常量出現在表達式中,它的值實質是個指針常量。
編譯器把這些指定字符的一份拷貝存儲於內存的某個位置,並存儲一個指向第一個字符的指針。
而且,當數組名用於表達式中時,它們的值也是指針常量。我們可以對它們進行下標引用、間接訪問、指針運算。
對於絕大多數程序員而言,它看上去象垃圾。它好象試圖在一個字符串上面執行某種類型的加法運算。
但是,當你記得字符串常量實際上是個指針時,它的意義就變得清楚了。這個表達式計算“指針值加上1”的值。
它的結果是個指針,指向字符串中的第二個字符。
為了更充分的理解內容。請參照示例代碼:
1 #include<iostream>
2 using namespace std; 3
4 void main() 5 { 6 char *s="abcdefgh"; 7 //*s='p'; //編譯順利,運行崩潰..
8 cout<<"abcde"+1<<endl; //bcde
9 cout<<*"abcde"<<endl; //a
10 cout<<*("abcde")<<endl; //a
11 cout<<s<<endl; //abcdefgh
12 cout<<"abcdefgh"+7<<endl; //h
13 cout<<"abcdef"[3]<<endl; //d
14 cout<<*("abcdef"+4)<<endl; //e
15 cout<<*"abcde"+2<<endl; //99
16 cout<<"c"<<endl; //c
17 cout<<'c'<<endl; //c
18 cout<<(int)'c'<<endl; //99
19 ///////////////////////
20 int anum='a'; 21 cout<<anum<<endl; //97
22
23 char ach='a'; 24 cout<<ach<<endl; //a 25 //字符數組
26 char ch[]="abcdefghijk"; 27 cout<<ch<<endl; //abcdefghijk
28 cout<<ch+2<<endl; //cdefghijk
29
30 char str1[] = "abc"; 31 char str2[] = "abc"; 32 const char str3[] = "abc"; 33 const char str4[] = "abc"; 34 const char* str5 = "abc"; 35 const char* str6 = "abc"; 36 char* str7 = "abc"; 37 char* str8 = "abc"; 38
39 cout << ( str1==str2 ) << endl; // 輸出0
40 cout << ( str3==str4 ) << endl; // 輸出0
41 cout << ( str5==str6 ) << endl; // 輸出1
42 cout << ( str7==str8 ) << endl; // 輸出1
43
44 }
【3】神秘函數如何實現?
利用字符串常量的特性,根據參數值的一定比例打印相應數量的星號。它比傳統的循環方案要容易的多,效率也高得多。
示例代碼如下:
1 #include<stdio.h>
2 using namespace std; 3 void mystery(int n) 4 { 5 n+=5; 6 n/=10; 7 printf("%s\n","***********"+10-n); 8 } 9 void main() 10 { 11 mystery(1); 12 mystery(10); 13 mystery(20); 14 mystery(30); 15 mystery(40); 16 mystery(50); 17 mystery(60); 18 mystery(70); 19 mystery(80); 20 mystery(90); 21 mystery(100); 22 } 23
24 //Out print
25 /*
26 * 27 ** 28 *** 29 **** 30 ***** 31 ****** 32 ******* 33 ******** 34 ********* 35 ********** 36 *********** 37 */
【4】整型值轉換為字符輸出如何實現?
示例代碼如下:
1 #include<stdio.h>
2 using namespace std; 3
4 void binary_to_ascii(unsigned int value) 5 { 6 unsigned int quotient; 7 quotient = value/10; 8 if(quotient!=0) 9 binary_to_ascii(quotient); 10 putchar(value % 10 + '0'); 11 } 12
13 void main() 14 { 15 int a=100; 16 binary_to_ascii(a); //100
17 }
【5】把十進制數據轉換為十六進制如何實現?
示例代碼如下:
1 /*
2 *把十進制數轉換為十六進制 3 */
4
5 #include<stdio.h>
6 using namespace std; 7
8 void binary_to_ascii(unsigned int value) 9 { 10 unsigned int quotient; 11 quotient = value/16; 12 if(quotient!=0) 13 binary_to_ascii(quotient); 14 putchar("0123456789ABCDEF"[value % 16]); 15
16 } 17
18 void main() 19 { 20 binary_to_ascii(10); 21 binary_to_ascii(20); 22 binary_to_ascii(30); 23 binary_to_ascii(40); 24 binary_to_ascii(16); 25 binary_to_ascii(17); 26 binary_to_ascii(100); 27 } 28 //運行結果如下:
29 /*
30 A 31 14 32 1E 33 28 34 10 35 11 36 64 37 */