-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc7f5ce
commit c71f09a
Showing
46 changed files
with
2,087 additions
and
201 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
//一个写代码让人看不懂的小技巧. | ||
int main (void) | ||
{ | ||
int x = 30; //原始 x, 函数内通用, 不会被块内的 x 更改值. | ||
|
||
printf ("x in outer block: %d at %p\n", x, &x); | ||
{ | ||
int x = 77; //定义在块内, 离开块时被销毁. | ||
printf ("x in inner block: %d at %p\n", x, &x); | ||
} | ||
printf ("x in outer block: %d at %p\n", x, &x); | ||
while (x ++ < 33) | ||
{ | ||
int x = 100; //定义在 while 块内. | ||
x ++; | ||
printf ("x in while loop: %d at %p\n", x, &x); | ||
} | ||
//每次离开循环时, while 中的 x 被销毁, 进入时被重新定义. | ||
printf ("x in outer block: %d at %p\n", x, &x); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include <stdio.h> | ||
int main (void) | ||
{ | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <stdio.h> | ||
int main (void) | ||
{ | ||
int n = 8; | ||
|
||
printf ("Intially, n = %d at %p\n", n, &n); | ||
for (int n = 1; n < 3; n ++) | ||
printf ("loop 1: n = %d at %p\n", n, &n); | ||
printf ("After loop 1, n = %d at %p\n", n, &n); | ||
for (int n = 1; n < 3; n ++) | ||
{ | ||
printf ("loop 2 index n = %d at %p\n", n, &n); | ||
int n = 6; | ||
printf ("loop 2: n = %d at %p\n", n, &n); | ||
n ++; | ||
printf ("loop 2 - 2: n = %d at %p\n", n, &n); | ||
} | ||
printf ("After loop 2, n = %d at %p\n", n, &n); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <stdio.h> | ||
int fade = 1; | ||
void trystat (void); | ||
int main (void) | ||
{ | ||
int count; | ||
for (count = 1; count <= 3; count ++) | ||
{ | ||
printf ("Here comes iteration %d: \n", count); | ||
trystat (); | ||
} | ||
|
||
return 0; | ||
} | ||
void trystat (void) | ||
{ | ||
static int stay = 1; | ||
|
||
printf ("fade = %d and stay = %d\n", fade ++, stay ++); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <stdio.h> | ||
//extern 使用方法 | ||
void swe (void); | ||
int main (void) | ||
{ | ||
extern int coal; | ||
|
||
printf ("%d\n", coal); | ||
swe(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#include <stdio.h> | ||
int coal = 6; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "12.4_5.h" | ||
static int i = 0; | ||
extern void swe (void) | ||
{ | ||
extern int coal; | ||
|
||
for ( ; i < coal; i ++) | ||
{ | ||
printf ("hello c world\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <stdio.h> | ||
//示例1: | ||
int hocus; | ||
int magic (); | ||
int main (void) | ||
{ | ||
extern int hocus; | ||
... | ||
} | ||
int magic () | ||
{ | ||
extern int hocus; | ||
... | ||
} | ||
//上文中一共出现了一个hocus, 后面两次声明起强调作用, 可以省略. | ||
//示例2: | ||
int hocus; | ||
int magic (void); | ||
int main (void) | ||
{ | ||
extern int hocus; | ||
... | ||
} | ||
int magic (void) | ||
{ | ||
//未声明变量 | ||
... | ||
} | ||
//示例2中出现两个hocus, 第一个是文件头部的全局hocus变量, 第二个是main函数里的局部hocus变量. | ||
//全局hocus变量对main函数不可见, 因为被局部hocus变量挡住了. | ||
//虽然magic函数没有声明hocus变量, 它可以调用头部的全局hocus变量. | ||
//示例3: | ||
int hocus; | ||
int magic (void); | ||
int main (void) | ||
{ | ||
int hocus;//默认被声明为自动变量. | ||
... | ||
} | ||
int pocus;//因为它被声明在了main函数后面, 所以它虽然全局存在但对main函数不可见. | ||
int magic (void) | ||
{ | ||
auto int hocus;//现实声明为自动变量, 效果和上面差不多. | ||
... | ||
} | ||
//示例3中一共出现四个变量, 全局变量hocus, main函数中局部变量hocus, | ||
//magic函数中局部变量hocus和对main函数不可见的全局变量pocus. | ||
//全局变量对main函数和magic函数均不可见. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stdio.h> | ||
int units = 0; | ||
void critic (void); | ||
int main (void) | ||
{ | ||
extern int units; | ||
|
||
printf ("how many pounds to firkin of butter? \n"); | ||
scanf ("%d", &units); | ||
while (units != 56) | ||
{ | ||
critic (); | ||
} | ||
printf ("you must have looked it up. \n"); | ||
|
||
return 0; | ||
} | ||
void critic (void) | ||
{ | ||
printf ("no luck, my friend. try again. \n"); | ||
scanf ("%d", &units); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
void report_count(); | ||
void accumulate(int k); | ||
int count = 0; | ||
int main (void) | ||
{ | ||
int value; | ||
register int i; | ||
|
||
printf ("enter a positive integer (0 to quit): \n"); | ||
while (scanf ("%d", &value) == 1 && value > 0) | ||
{ | ||
++ count; | ||
for (i = value; i >= 0; i --) | ||
{ | ||
accumulate (i); | ||
} | ||
printf ("enter a positive integer (0 to quit): \n"); | ||
} | ||
report_count (); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <stdio.h> | ||
extern int count; | ||
static int total = 0; | ||
void accumulate (int k) | ||
{ | ||
static int subtotal = 0; | ||
|
||
if (k <= 0) | ||
{ | ||
printf ("loop cycle: %d\n", count); | ||
printf ("subtotal: %d; total: %d\n", subtotal, total); | ||
subtotal = 0; | ||
}else | ||
{ | ||
subtotal += k; | ||
total += k; | ||
} | ||
} | ||
void report_count () | ||
{ | ||
printf ("loop executed %d times. \n", count); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.