章1 导言(上 )学习笔记

1 入门

helloworld程序

#include<stdio.h>
int main(){
        printf("hello,world\n");

        return 0;
}

运行该程序取决于所使用的系统,在其他系统中,编译,加载,运行等规则有所不同。

 

下面是在Linux系统中编译运行的结果:

可执行文件a.out

[root@YUN8525115 ~]# gcc hello.c   #编译器gcc  生成可执行文件a.out
[root@YUN8525115 ~]# ls
anaconda-ks.cfg  a.out  hello.c
[root@YUN8525115 ~]# ./a.out
hello,world
[root@YUN8525115 ~]# ls -l
总用量 20
-rw-------. 1 root root 1194 9月  29 2017 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 8511 5月  27 14:47 a.out
-rw-r--r--. 1 root root   70 5月  27 14:40 hello.c

  注:                                                                                                                                               

变量用于存储计算过程中使用的值。

每个程序都从main函数的起点开始执行。

字符,字符串,转义字符

 

1.2 变量与算术表达式

实例

#include<stdio.h>
/*
当fahr=0, 20....300时,分别打印华氏温度和摄氏度对照表
*/
int main(){
        int far,celsius;
        int lower,upper,step;

        lower =0;  //温度表下限
        upper =300;//温度表上限
        step=20;   //步长

        far  = lower;
        while (far<=upper){
                celsius = 5 *(far-32) / 9;
                printf("%d\t%d\n",far, celsius);
                far += step;
        }

}
[root@YUN8525115 ~]# vi hello2.c
[root@YUN8525115 ~]# gcc hello2.c 
[root@YUN8525115 ~]# ./a.out 
0	-17
20	-6
40	4
60	15
80	26
100	37
120	48
140	60
160	71
180	82
200	93
220	104
240	115
260	126
280	137
300	148

注释被编译忽略。

在C语言中,所有变量都必须声明后使用。通常放在函数起始处,在任何可执行语句之前。

声明  用于说明变量的属性,他由一个类型名和一个变量表组成。

数据类型char short long double 大小也取决于具体的机器。

 

1.3 for语句

实例

#include<stdio.h>


#define LOWER   0
#define UPPER  300


int main(){
        int far;

        for(far =LOWER; far <= UPPER; far +=20)
        printf("%3d  %6.1f\n",far ,(5.0/9.0)*(far-32) );
        
}

 

1.4符号常量

#define命令可以把符号名(或者符号常量)定义为一个特定的字符串:

#define  名字 替换文本

符号常量名通常大写字母拼写。

[root@YUN8525115 ~]# ./a.out 
  0   -17.8
 20    -6.7
 40     4.4
 60    15.6
 80    26.7
100    37.8
120    48.9
140    60.0
160    71.1
180    82.2
200    93.3
220   104.4
240   115.6
260   126.7
280   137.8
300   148.9

 

1.5字符输入/输出

无论文本从何输入,输出到何处,其输入/输出都是按照字符流的方式处理,

文本流 由多行字符构成的字符序列,而每行字符则由0个或多个字符组成,行末是一个换行符。

最简单的是getchar和putchar两个函数。

 

转载于:https://my.oschina.net/primerliu/blog/1819479

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值