?? c primer plus
字號:
《C Primer Plus 5th Edition》精簡
此精簡版與原版有些出入。
整理人:風子 QQ:278542295
難點:
1、指針與數組、函數:
int zippo[4][2] = { {2,4}, {6,8}, {1,3}, {5, 7} }; // 二維數組
int (*pz)[2] = zippo; // pz是指針,指向一個包含2個元素的int數組。pz[2][1]==zippo[2][1];pz指向第1行,pz+1指向第2行
int * pax[2]; // pax是數組,是由兩個指向int的指針構成的數組,pax[i]是一個int的地址
printf("%d\n", pz[2][1]);
int (* p2)[6]; // p2指向6個元素的int數組,p2[i]被解釋為一個由6個整數構成的元素,p2[i][j]是一個int
p2 = (int (*)[6])malloc(n * 6 * sizeof(int));
int sum2(int ar[][4], int rows);
int sum4d(int ar[][12][20][30], int rows); // ar是四維數組指針
int sum4d(int (*ar)[12][20][30], int rows); // 與上面的方式等效,其中ar指向一個12x20x30的int數組
typedef int arr4[4]; // aar4是4個int的數組
typedef arr4 arr3x4[3]; // arr3x4 是3個aar4的數組
int sum2(arr3x4 ar, int rows);
typedef char (* FRPTC()) [5]; // 把FRPTC聲明為一個函數類型,函數返回一個指針,此指針指向指向有5個元素的char數組的指針
typedef void(* V_FP_CHARP)(char *);
void show(V_FP_CHARP fp, char *); // 即 void show(void (* fp)(char *), char * str)
V_FP_CHARP pfun; // 即 void (*pfun)(char *);
2、復數
.......
-------------------------------------------------------------
內容 復習題 編程 答案
第一章 概述 12 25 25 551
第二章 C語言概述 26 41 42 551
第三章 數據和C 43 68 69 552
第四章 字符串和格式化輸入 71 95 96 554
第五章 運算符、表達式和語句 98 122 124 556
第六章 C控制語句:循環 126 157 160 558
第七章 C控制語句:分支和跳轉 163 195 197 561
第八章 字符輸入/輸出和輸入確認 199 221 222 564
第九章 函數 223 253 254 564
第十章 數組和指針 255 290 292 566
第十一章 字符串和字符串函數 293 328 330 568
第十二章 存儲類、鏈接和內存管理 332 361 362 570
第十三章 文件輸入/輸出 365 386 387 571
第十四章 結構和其他數據形式 389 430 432 573
第十五章 位操作 457 454 455 576
第十六章 C預處理器和C庫 457 492 493 577
第十七章 高級數據表示 495 549 549 578
附錄A 復習題答案 550
附錄B 參考資料 581
-------------------------------------------------------------
附錄B 參考資料 581
B.1 參閱書籍 581
B.2 C運算符 583
B.3 基本類型 587
B.4 表達式、語句、程序流 590
B.5 ANSI C庫 595
B.5.1 診斷 assert.h 595
B.5.2 復數 complex.h (C99) 595
B.5.3 字符處理:ctype.h 597
B.5.4 錯誤報告:errno.h 597
B.5.5 浮點數環境:fenv.h (C99) 597
B.5.6 整型格式轉換:inttypes.h (C99) 599
B.5.7 本地化:locale.h 599
B.5.8 數學庫:math.h 601
B.5.9 非本地跳轉:setjmp.h 604
B.5.10 信號處理:signal.h 604
B.5.11 可變參數:stdarg.h 605
B.5.12 布爾支持:stdbool.h 606
B.5.13 通用定義:math.h 606
B.5.14 整數類型:stdint.h 607
B.5.15 標準I/O庫:stdio.h 610
B.5.16 通用工具:stdlib.h 611
B.5.17 字符串處理:string.h 615
B.5.18 通用數學函數宏:tgmath.h (C99) 617
B.5.19 日期時間:time.h (C99) 617
B.5.20 擴展的多字節字符和寬字符:wchar.h (C99) 619
B.5.21 寬字符分類和映射工具:wctype.h (C99) 624
B.6 擴展的整數類型 625
B.7 擴展的字符支持 628
B.8 C99的數值計算增強 631
B.9 C和C++的差別 633
-------------------------------------------------------------
第一章 概述
一、C的優點、缺點
1、優點
性能高效:緊湊且運行速度快,接近匯編
可移植性強:可以不同操作系統下的C編譯器中編譯
強大又靈活:UNIX、Linux 都是采用 C 編寫的
2、缺點
C在表達方面的自由會增加風險,尤其是C對指針的使用不當,可能會產生難以追蹤的問題;
C的簡潔性與豐富的運算符相結合,如果不注意,有可能會編寫出極難理解的代碼;
.....
3、C標準
C語言沒有官方的標準,Brian Kermighan 和 Dennis Ritchie 編寫的
《The C Programming Language》第一版(1978)成為大家接受的標準,
通常稱為 K&R C 或 經典C;而 UNIX 實現的C庫成為事實的C標準庫。
美國國家標準化組織(ANSI)于1989年采用C標準--ANSI C
國際標準化組織(ISO)于1990年采用C標準--ISO C
ISO與ANSI C實質上是同一標準,統稱為C89
1994年,C90
1999年,C99
三、C與C++區別
四、各操作系統的C編譯器:
C編譯器和鏈接器是將C語言源代碼轉換為可執行的程序。
編譯器將源代碼(.c)轉為目標文件(.obj);
鏈接器將目標文件(.obj)、系統的啟動代碼(程序與操作的接口)、庫代碼(.lib)結合可執行文件(.exe),
對于庫代碼,鏈接器只從庫(.lib)中提取您使用的函數所需要的代碼。
本書使用的操作系統和編譯器:
Pentinum PC 機器上運行的 Windows XP Professional
VAX 11/750 機器上運行的BSD 4.3版本 UNIX
Pentinum PC 機器上運行的 Linux
Microsoft Visual C++7.1 (包含于Microsoft Visual Studio .NET 2003中)
Metrowerks CodeWarrior Development Studio 9.2
gcc 3.3.3、Comeau 4.3.3
這些版本的編譯器都不完全支持C99的所有特性。
1、UNIX
編譯器:cc
編譯鏈接過程:.c --> .o --> .out
編輯器 emacs、jove、vi、X-Windows
備注:有些UNIX文件名最長8個字符,有些則允許最長255字符
使用舉例:
cc test.c
cc file1.c file2.c
2、Linux
編譯器:gcc
編譯鏈接過程:.c --> .o --> .out
編輯器 emacs、jove、vi、X-Windows
備注:文件名最長255字符
使用舉例:
gcc test.c
gcc file1.c file2.c
隨后,如果file1.c有改動而file2.c不變,可以這么編譯:
gcc file1.c file2.o
3、MS-DOS
編譯器:Borland Trubo C、Microsoft C
編譯鏈接過程:.c --> .obj --> .exe
+.lib
編輯器:edit
備注:文件名最長8個字符
使用舉例:
tcc test.c 或 用 tc 集成開發環境
3、Windows
編譯器:Microsoft Visual C++、Borland C++ Builder、Metrowerks CodeWarrior Development、Digital Mars...
都提供C編譯器,還是集成開發環境(IDE)
備注:調試時,避免控制程序一閃而過,可以加 getchar() 函數
4、Macintosh OS
編譯器:Metrowerks CodeWarrior Development (有 Windows 和 Macintosh 版本)
第二章 C語言概述
#include <stdio.h> /* 包含另一個文件 */
int main(void) /* 主函數,程序入口 */
{
int num; /* 定義一個名為num的變量 */
num = 1; /* 為num賦一個值 */
printf("I am a simple ");
printf("computer.\n");
printf("My favorite number is %d because it is first.\n",num);
return 0; /* 返回,程序結束 */
}
#include <stdio.h>
預處理指令,讓編譯器從庫中抽取某些函數的代碼,或者替代常量等。
stdio.h頭文件包含了有關標準的輸入和輸出函數(例如printf())的調用信息,例如一些常量、函數說明等,
但是函數代碼被包含在一個已經編譯好的庫文件中,而不是在stdio.h頭文件中。
int main(void) C99標準
main() C90勉強接受,但C99不接受
void main() 不標準
/* 多行注釋 */
// 單行注釋 C99新增的注釋方式,C++、Java、Delphi 都支持此注釋方式
int num;
聲明一個int類型的名為num的變量,對應某塊內存。
傳統上,C語言要求必須在一個代碼塊的開始處聲明變量,在其語句中間不聲明;
現在C99遵循C++的慣例,允許把聲明放在代碼塊中的任何位置。
C99允許外部標識符最長31個字符,其他標識符63個字符。
C90允許外部標識符最長6個字符,其他標識符31個字符,有些編譯器最長只允許8字符。
/* two_func.c -- a program using two functions in one file */
#include <stdio.h>
void butler(void); /* ISO/ANSI C function prototyping */
int main(void)
{
printf("I will summon the butler function.\n");
butler();
printf("Yes. Bring me some tea and writeable CD-ROMS.\n");
return 0;
}
void butler(void) /* start of function definition */
{
printf("You rang, sir?\n");
}
第三章 數據和C
一、數據類型關鍵字
1、數據類型關鍵字:
K&R int、long、short、unsigned、char、float、double
C90新增 signed、void
C99新增 _Bool、_Complex復數、_Imaginary虛數
2、存儲單位:
bit 位,只能表示0或1
byte 字節,每字節為8位
word 字,原始Apple機的字為8位,80286兼容機的字為16位,Pentinum PC、Macintosh PowerPC的字是32位,另外還有64位甚至更長
二、基本數據類型
1、整數類型integer
short short int
int
long long int
unsigned unsigned int
C90新增
unsigned long unsigned long int
unsigned short unsigned short int
signed 可與有符號類型一起使用,例如:short、short int、signed short、singed short int 表示同一種類型
C99新增
long long long long int
unsigned long long unsigned long long int
典型系統的整數類型大小(bit)
Macintosh Metrowerks CW
類型 ANSI C 規定的最小值 PC機Linux、Windows XP DOS/Windows3.1
char 8 8 8
int 16 32 16
short 16 16 16
long 32 32 32
long long 64 64
例1、0x10表示十六進制數,020表示八進制數
int x = 100;
printf("dec=%d; octal=%o; hex=%x \n", x, x, x);
printf("dec=%#d; octal=%#o; hex=%#x \n", x, x, x);
輸出結果:
dec=100; octal=144; hex=64
dec=100; octal=0144; hex=0x64
例2、嘗試int超范圍
/* toobig.c-exceeds maximum int size on our system */
#include <stdio.h>
int main(void)
{
int i = 2147483647; // -2147483648(0x80000000)~-1(0xFFFFFFFF),0~2147483647(0x7FFFFFFF)
unsigned int j = 4294967295; // 0~4294967295(0xFFFFFFFF)
printf("%d %d %d\n", i, i+1, i+2);
printf("%u %u %u\n", j, j+1, j+2);
return 0;
}
輸出結果:
2147483647 -2147483648 -2147483647
4294967295 0 1
例3、嘗試不匹配格式打印出不正確的結果
/* print2.c-more printf() properties */
#include <stdio.h>
int main(void)
{
unsigned int un = 3000000000; /* system with 32-bit int */
short end = 200; /* and 16-bit short */
long big = 65537;
long long verybig = 12345678908642;
printf("un = %u and not %d\n", un, un); // %u表示unsigned int,%d表示int
printf("end = %hd and %d\n", end, end); // %hd表示short int
printf("big = %ld and not %hd\n", big, big); // %ld表示long int
printf("verybig= %lld and not %ld\n", verybig, verybig); // %lld表示long long int
return 0;
}
輸出結果:
un = 3000000000 and not -1294967296
end = 200 and 200
big = 65537 and not 1
verybig= 12345678908642 and not 1942899938
在short與int長度不一樣的操作系統中,使用%d打印short效率更高。
整數常量示例
類型 十六進制 八進制 十進制
char \0x41 \0101 N.A
int 0x41 0101 65
unsigned int 0x41u 0101u 65u
long 0x41L 0101L 65L
unsigned long 0x41UL 0101UL 65UL
long long 0x41LL 0101LL 65LL
unsigned long long 0x41ULL 0101ULL 65ULL
可移植的類型:inttypes.h
C99定義了一些整型的別名,例如:
int16_t 表示一個16位有符號整數類型
uint32_t 表示一個32位無符號整數類型
int8_t
int_least8_t
int_fast8_t
intmax_t
uintmax_t
vc 沒有 inttypes.h,可以自己定義
例1、可移植的整數類型名
/* altnames.c -- portable names for integer types */
#include <stdio.h>
#include <inttypes.h> // supports portable types
int main(void)
{
int16_t me16; // me16 a 16-bit signed variable
me16 = 4593;
printf("First, assume int16_t is short: ");
printf("me16 = %hd\n", me16);
printf("Next, let's not make any assumptions.\n");
printf("Instead, use a \"macro\" from inttypes.h: ");
printf("me16 = %" PRId16 "\n", me16); // PRId16在inttypes.h中定義,即"hd"
return 0;
}
2、字符類型char
char實際存儲的是整數,美國最常用的編碼是ASCII碼,此編碼1字節中的7位(0~127),例如字母A的值為65。
Unicode字符集能夠表示世界范圍內的多種字符集,目前已超96000個字符,使用16位甚至32位表示。
Unicode標準與ISO/IEC 10646標準兼容。
國標標準化組織 ISO -- International Organization for Standardization
國際電工技術委員會 IEC -- International Electrotechnical Commission
定義字符變量,并賦值
char grade = 'A'; // 將整數65存儲在一個8位單元grade中
char grade = 65; // 與上一句等效,不過可讀性差
singed char grade = 127; // C90允許加singed或unsigned,-128(0x80)~-1(0xFF),0~127(0x7F)
unsigned char grade = 255; // 0~255(0xFF)
int tag = 'FATE' // 將'FATE'四個字符存儲在32位單元tag中,這是一個技巧
非打印字符,轉義序列(Escape Sequence)
\a 警報,C90新增,ASCII值為7,例如 char beep='\a'; 與 char beep=7; 等效
\b 退格
\f 走紙
\n 換行,ASCII值為10
\r 回車,ASCII值為13
\t 水平制表符,ASCII值為9
\v 垂直制表符
\\ 反斜杠(\)
\' 單引號(')
\" 雙引號(")
\? 問號(?)
\0oo 八進制值(o表示一個八進制數字),例如 char beep='\07'; 與 char beep='\007'; 等效
\xhh 十六進制(h表示一個十六進制數字)
例1、打印字符
/* charcode.c-displays code number for a character */
#include <stdio.h>
int main(void)
{
char ch;
printf("Please enter a character.\n");
scanf("%c", &ch); /* user inputs character */
printf("The code for %c is %d.\n", ch, ch);
return 0;
}
3、布爾類型:_Bool
_Bool類型由C99引入,_Bool類型實際上也是一種整數類型,
1表示true,0表示false,原則上它只需要1位存儲空間。
4、浮點型
浮點變量的指數和尾數部分(有效數字)被分別存儲。
float一般是32位,其中7位為有效數字,取值范圍 3.4e-37~3.4e38
double一般是64位,其中15位為有效數字,取值范圍 1.7e-307~1.7e308
典型系統的浮點類型的有效數字位數、指數
類型 ANSI C 規定的最小值 PC機Linux、Windows XP Macintosh Metrowerks CW
float 6位,-37~37 6位,-37~38 6位,-37~38
double 10位,-37~37 15位,-307~308 18位,-4931~4932
long double 10位,-37~37 18位,-4931~4932 18位,-4931~4932
浮點變量:
float noah, jonah;
double trouble;
float planck = 6.63e-34;
long double gnp;
浮點常量:
默認為double:3.14159、.2、4e16、.8E-5、100.、100.0f
加L為long double:54.31l、4.32e4L
浮點數表示方式:
1000.23 = 1.00025 X 10^3 = 1.00025e3
0.000056 = 5.6 X 10^-5 = 5.6e-5
C99添增浮點數常量的十六進制方式:
0xa.1fp10 = 10.12109375 X 1024 = 10364.0
a表示10;.1f表示1/16+15/256=0.12109375;p10表示2^10=024
并非所有的C編譯器都支持此格式
float、double輸出十進制%f、科學記數%e,另外C99新增十六進格%a
例1、打印浮點值
#include <stdio.h>
int main(void)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -