第一章 导言
1.1 入门
废话不多说,hello world 走起!
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!");
system("pause");
return 0;
}#include 包含标准库信息
main函数 -- 程序入口
printf 标准库IO函数,打印的库函数
system 调用系统函数,方便显示窗口,不会一闪而过
return 函数返回0 正常
注意1:在printf函数中,只能\n表示换行,不能用程序代码的换行代替,否则出现编译错误信息。
printf(“hello, world
“); // 编译器会给出错误信息
注意2:printf不会自动换行
printf(“hello, “);
printf(“world”);
printf(“\n“);
这三个表达式等价于printf("Hello world!");
注意3:\n代表一个字符,转义字符, \t 水平制表符 ,\b退格符,\”双引号,\\反斜杠
1.2 变量与表达式
打印0到300以20为步长的摄氏华氏温度:℃ = (5 / 9)* ( F - 32)
代码如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int fa, ce; //fa为华氏温度 ;ce为 摄氏温度
int lower, upper, step; //lower下限;upper上限;step步长
//设置初始值
lower = 0;
upper = 300;
step = 20;
fa = lower;
while(fa <= upper)
{
ce = 5*(fa - 32)/9;
printf("%d\t%d\n", fa, ce);
fa = fa + step;
}
system("pause");
return 0;
}注意1::/* 此为注释内容 */
注意2:变量必须先声明后使用,声明通常在函数起始处,在任何可执行语句之前,声明用来说明一个变量的属性,由类型和变量名组成。可一次声明多个变量。
如,int a, b, c ; //用逗号隔开即可
注意3:int 和float取值范围取决于机器本身,int通常为16位,取值范围为 -215 ~ 215-1
(-32768 ~ 32767),也有32位的int类型;float通常为32位,至少六位有效数字
c语言提供的基本数据类型还有:
char 字符
short 短整型
int 整型
long 长整型
float 浮点型
double 双精度浮点型
注意3:整数除法操作舍弃小数部分, 5/9 = 0;浮点不舍弃 5.0/9.0得到的是浮点数
注意4:printf不是c语言的一部分,是ANSI标准库的一个函数而已
注意5:%d是转换说明符,规定输出的格式
%3.0f --- 打印的浮点数至少占3个字符宽度,且不带小数点和小数部分
%6.1f --- 打印的数至少六个字符宽,且小数点后面1个字符宽
1.3 for语句
for语句实现上述while循环
#include <stdio.h>
#include <stdlib.h>
int main()
{
int fa;
for(fa = 0;fa <= 300;fa += 20)
{
printf("%3d\t%6.1f\n",fa,(5.0/9.0)*(fa-32));
}
system("pause");
return 0;
}1.4 符号常量
#define 名字 替换文本
eg:#define STEP 20
注意:替换文本可以为任何字符序列 不仅仅局限于数字;通常名字大写且后面无逗号
1.5 字符输入输出
标准库IO:按照字符流方式处理
getchar():从文本流中读取下一个输入字符,并作为结果值返回
putchar(): 打印一个字符,通常为屏幕
字符:在机器内部以位模式存储,char为专门的字符型数据,int也可以
EOF:end of file 文件结束符,定义在stdio.h中,是个整型数,不属于任何char类型字符,windows下同时crtl+z等于输入EOF
文件复制代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
c = getchar();
while(c != EOF)
{
putchar(c);
c = getchar();
}
system("pause");
return 0;
}优化后:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
while((c = getchar()) != EOF)
{
putchar(c);
}
system("pause");
return 0;
}while((c = getchar()) != EOF):读入一个字符赋值给c,然后判断是否是EOF,如果不是执行循环体,如果是退出循环。
字符计数:while版本
#include <stdio.h>
#include <stdlib.h>
int main()
{
long num;
num = 0;
while((getchar()) != EOF)
{
++num;
}
printf("%ld",num);
system("pause");
return 0;
}字符计数:for版本
#include <stdio.h>
#include <stdlib.h>
int main()
{
double num;
for(num = 0;getchar() != EOF;num++ )
{
;
}
printf("%.0f",num);
system("pause");
return 0;
}注意:for循环必须至少有一个循环体,所以用;表示一个空语句
行计数:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, num;
num = 0;
while((c = getchar()) != EOF)
{
if(c == '\n')
num++;
}
printf("%d",num);
system("pause");
return 0;
}‘\n’ :一个字符常量,有对应的ASCII值
单词计数:综合行计数和字符计数
#include <stdio.h>
#include <stdlib.h>
#define IN 1 /* inside a word */
#define OUT 0
int main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while((c = getchar()) != EOF)
{
nc++;
if(c == '\n')
nl++;
if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
}
else if(state == OUT)
{
state = IN;
nw++;
}
}
printf("%d %d %d",nl,nw,nc);
system("pause");
return 0;
}注意:有赋值和值的时候,从右到左结合
a = b = c = 0 ; 等价于 a = (b = (c = 0 ));
1.6 数组
程序目标:统计各个数字、空白符(空格、制表、换行)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int c; //遍历所有字符
int i; //循环变量,
int ndigit[10]; //数组用来循环存储各个数字的个数
int nwhite,nother; //空白和其他
nwhite = nother = 0;
for(i = 0;i < 10;i++)
{
ndigit[i] = 0;
}
while((c = getchar()) != EOF)
{
if(c >= '0' && c <= '9')
{
ndigit[c - '0']++;
}
else if(c == ' ' || c == '\n' || c == '\t')
{
nwhite++;
}
else
{
nother++;
}
}
printf("digits =");
for(i = 0;i < 10;i++)
{
printf("%d ",ndigit[i]);
}
printf("white space = %d",nwhite);
printf("other = %d",nother);
system("pause");
return 0;
}注意:int ndigit[10]; //声明ndigit变量为由10个整型构成的数组,c语言数组下标从0开始,10个元素分别为 ndigit[0], ndigit[1],.....ndigit[9]; 所以可以通过循环遍历数组所有元素。
数组下标可以是任何整型表达式,变量和常量都可。
1.7 函数
标准库函数:如printf
自定义函数:eg:int power(m, n) 求m的n次幂
#include<stdio.h>
#include<stdlib.h>
int power(int m,int n); //声明函数,函数原型,必须和定义一致
int main()
{
printf("5的3次幂是:%d\n",power(5,3));
printf("15的2次幂是:%d",power(15,2));
system("pause");
return 0;
}
int power(int m, int n)
{
int i;
int result;
result = 1;
for(i = 1;i <= n;i++)
{
result = result * m;
}
return result;
}注意:函数定义可以出现在一个源文件或多个源文件中,函数的参数名字只在函数内部有效,对其他任何函数不可见。
形式参数:函数定义中圆括号中出现的变量m和n
实际参数:函数调用与形式参数对应的值,5和3,51和2
返回值:power函数计算的结果返回给main函数,return 表达式
1.8 参数 --- 传值调用
c语言中,被调函数不能直接修改主调函数中变量的值,只能修改其私有的临时副本的值。传值调用。
必要时候,函数可以修改主调函数中的变量 -- 提供设置值的变量的地址(指针),这时候被调用函数需要将对应的参数声明为指针类型
如果是数组参数 -- 把数组名做参数时,等同于传递给函数数组起始元素的地址,这时不是赋值数组元素了,在被调用函数中,可以通过下标等修改数组元素的值。
1.9 字符数组
程序目标:读入一组文本行,打印出最长的文本行
#include<stdio.h>
#include<stdlib.h>
#define MAXLENGTH 100
int getline(char line[],int maxline);
void copy(char to[], char from[]);
int main()
{
int currentLength;
int maxLengthSoFar;
char line[MAXLENGTH];
char longest[MAXLENGTH];
maxLengthSoFar = 0;
while((currentLength = getline(line,MAXLENGTH)) > 0)
{
if(currentLength > maxLengthSoFar)
{
maxLengthSoFar = currentLength;
copy(longest,line);
}
}
if(maxLengthSoFar > 0)
{
printf("%s",longest);
}
system("pause");
return 0;
}
int getline(char line[],int maxline)
{
int c;
int i;
for(i = 0;i <= maxline && (c=getchar()) != EOF && c != '\n';i++)
{
line[i] = c;
}
if(c == '\n')
{
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
{
i++;
}
}注意:c语言中”hello\o” 的字符串常量时候,将以字符数组的形式存储,以’\0’标志结束。printf函数中的格式规范%s对应的参数必须是这种形式的字符串。
1.10 外部变量与作用域
函数的私有变量:函数中的变量,局部变量只在函数调用时候存在,函数执行完毕退出时消失。
自动变量auto值在函数调用执行期间存在,且在每次进入函数时候要显式赋值。
定义在函数外部的变量:在所有函数中都可以通过变量名访问这种类型的变量,外部变量,在全局范围内访问,因此函数之间可以通信。
外部变量:定义在所有函数之外,只能定义一次,定义后分配存储单元,在需要访问外部变量的函数中,必须声明相应的外部变量,可以用extern语句显式声明。如果外部变量的定义出现在函数之前,那可以省略掉extern声明,通常所有外部变量的定义放在源文件的开始处。如果程序包含多个源文件,变量的file1中定义,那么在file2中使用需要extern声明来建立变量与其定义之间的关系。
通常:把变量和函数的extern声明放在头文件中.h;在源文件的开头#include进来。
定义:创建变量或分配存储单元;
声明:说明变量的性质,并不分配存储单元;
过分依赖外部变量不好,会导致一定的风险,使得程序中的数据关系模糊不清,因为外部变量的值可能会意外或不经意修改,而程序的修改变的十分困难。失去了通用性。
本博客介绍C语言的基础概念,包括变量、表达式、循环、字符串输入输出、数组和函数等内容。详细阐述了如何使用C语言进行基本的编程操作,提供了多种编程技巧和优化方法,帮助初学者快速掌握C语言的基础知识。

被折叠的 条评论
为什么被折叠?



