一转眼c primer plus快学完了,我才意识到一件事,以后关于c语言的学习很难再找到一个全面详细的指引,尤其是编程的思维,理念,对程序的优化
但是我可以将未来一段时间未完成部分的笔记记下了
第12章 编程练习
练习一
我的答案
#include <stdio.h>
//int units = 0;
void critic (int *a);
int main (void)
{
//extern int units;
int units;
int *a;
a=&units;
printf ("How many pounds to a firkin of butter?\n");
scanf ("%d", &units);
while (units != 56)
critic (a);
printf ("You must have looked it up!\n");
return 0;
}
void critic (int *a)
{
printf ("No luck, chummy. Try again.\n");
scanf ("%d", a);
}
书上的答案
#include <stdio.h>
void critic(int * u);
int main(void)
{
int units; /* units now local */
printf("How many pounds to a firkin of butter?\n");
scanf("%d", &units);
while ( units != 56)
critic(&units);
printf("You must have looked it up!\n");
return 0;
}
void critic(int * u)
{
printf("No luck, chummy. Try again.\n");
scanf("%d", u);
}
// or use a return value:
// units = critic();
// and have critic look like this:
/*
int critic(void)
{
int u;
printf("No luck, chummy. Try again.\n");
scanf("%d", &u);
return u;
}
*/
// or have main() collect the next value for units
13章 文件输入/输出
13.1 和文件进行通信
文件是什么?一个文件通常就是磁盘上的一段命名的存储区。
UNIX是C的发源地,哈哈,LIKE linux
文件写入如果重新读取不要忘了rewind();
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char ch;
char a[100];
FILE *fp;
if ((fp = fopen ("ceshi.red","a+")) == NULL)
{
printf ("打开失败\n");
exit (1);
}
while ((ch = getchar ()) != '\n')
{
putc (ch,fp);
printf ("p\n");
}
printf ("R\n");
rewind (fp);
while ((ch = getc (fp)) != EOF)
{
putchar (ch);
printf ("a\n");
}
return 0;
}
几经测试如果丢失rewind();是可怕的