Linux C教程(一)

--基础知识补充
C语言的两种标准:
        C89                (ANSI C)
        C99                (ISO)
编译器:
        Unix                        cc
        Linux                gcc
        Windows vc
--helloworld.c
# vim helloworld.c                                 //编写一个helloworld.c文件
# cat helloworld.c                                //文件内容如下
#include <stdio.h>                                                                        //预处理
int main(void){                                                                                //main函数,int为返回值,void为无形参
        printf("Hello world.!\n");                                //调用printf函数在屏幕输出
        return 0;                                                                                //返回值
}                                                                                                                        //main函数结束
# gcc helloworld.c                                //编译helloworld.c文件
# ls                                                                        //使用默认编译会产生一个a.out可执行文件
a.out  helloworld.c
# ./a.out                                                         //执行a.out文件
Hello world.!
# gcc helloworld.c -o helloworld                        //指定gcc输出文件名
# ls                                                                                                        //产生了一个指定文件名的可执行文件
a.out  helloworld  helloworld.c
# ./helloworld 
Hello world.!
//注意:C语言中的每条语句必须以分号(";")结尾.
-----------------------------------------------------------------------------------------------------------------------------------------
--C语言编译过程
1)gcc -E helloworld.c -o helloworld.i                                                                                //展开预处理语句
2)gcc -S helloworld.i -o helloworld.s                                                                                //将文件编译成汇编语言
3)gcc -c helloworld.s -o helloworld.o                                                                                //将文件编译成二进制文件
4)gcc helloworld.o -o helloworld                                                                                                //将二进制文件编译成指定的可执行文件
-----------------------------------------------------------------------------------------------------------------------------------------
--数据类型
类型                                占用字节数                                                                取值范围
char                                1Byte(8bit)                                                        128                                                        //signed (-127 ~ 128) unsigned (255)

int                                4Byte(32bit)                                                2^31 - 1                                                //unsigned ((2^31 - 1)*2)
short                                2Byte(16bit)                                                2^16 - 1
long                                4Byte(32bit)                                                2^32 - 1(32位机)                        //64位机器下可能有64bit宽
long long                8Byte(64bit)                                                2^64 - 1

float                                4Byte(32bit)                                                                                                                //浮点数无signed与unsigned,long只能修饰整形与double形变量
double                        8Byte(64bit)
long double                12Byte(96bit)

void                                1Byte                                                                        1^8 - 1
例如:
# vim test.c 
# cat test.c
#include <stdio.h>
int main(void){
        printf("============================================\n");
        printf("sizeof(char) = %d\n",sizeof(char));                                                                //使用sizeof函数可以计算变量/数据类型的长度
        printf("============================================\n");
        printf("sizeof(int) = %d\n",sizeof(int));
        printf("sizeof(short) = %d\n",sizeof(short));
        printf("sizeof(long) = %d\n",sizeof(long));
        printf("sizeof(long long) = %d\n",sizeof(long long));
        printf("============================================\n");
        printf("sizeof(float) = %d\n",sizeof(float));
        printf("sizeof(double) = %d\n",sizeof(double));
        printf("sizeof(long double) = %d\n",sizeof(long double));
        printf("============================================\n");
}
# gcc test.c -o test
# ./test 
============================================
sizeof(char) = 1
============================================
sizeof(int) = 4
sizeof(short) = 2
sizeof(long) = 4
sizeof(long long) = 8
============================================
sizeof(float) = 4
sizeof(double) = 8
sizeof(long double) = 12
============================================
-----------------------------------------------------------------------------------------------------------------------------------------
--变量
int a;                                //定义一个变量a
--常量
int a = 1;                        //定义一个常量a
#define ABC 1                //定义一个常量ABC
-----------------------------------------------------------------------------------------------------------------------------------------
--进制
二进制:0 1
八进制:0 1 2 3 4 5 6 7
十进制:0 1 2 3 4 5 6 7 8 9 10
十六进制:0 1 2 3 4 5 6 7 8 9 ABCDEF
-----------------------------------------------------------------------------------------------------------------------------------------
--注释
1)单行注释://.........
2)多行注释:/*.........*/
-----------------------------------------------------------------------------------------------------------------------------------------
--算术运算符
+                                        //加
-                                        //减
*                                        //乘
/                                        //除
%                                        //取模(求余)
例如:
# vim test.c 
# cat test.c
#include <stdio.h>
int main(void){
        int a,b;
        printf("Please input a :\t");
        scanf("%d",&a);
        printf("Please input b :\t");
        scanf("%d",&b);

        printf("%d + %d = %d\n",a,b,a+b);
        printf("%d - %d = %d\n",a,b,a-b);
        printf("%d * %d = %d\n",a,b,a*b);
        printf("%d / %d = %d\n",a,b,a/b);
        printf("%d %% %d = %d\n",a,b,a%b);
        return 0;
}
# gcc test.c -o test
# ./test 
Please input a :        5
Please input b :        9
5 + 9 = 14
5 - 9 = -4
5 * 9 = 45
5 / 9 = 0
5 % 9 = 5
--关系运算符
>                                        //大于
<                                        //小于
>=                                        //大于或等于
<=                                        //小于或等于
==                                        //等于
!=                                        //不等于
例如:
# vim test.c 
# cat test.c
#include <stdio.h>
int main(void){
        int a = 1;
        int b = 2;
        printf("true : %d \n",a < b);
        printf("false : %d \n",a > b);
        return 0;
}
# gcc test.c -o test
# ./test 
true : 1                                                 //注意:在C语言中,真值为任意一个不为0的数,假值为0
false : 0 
--逻辑运算符
&&                                        //逻辑与
||                                        //逻辑或
!                                        //逻辑非
例如:
# vim test.c 
# cat test.c 
#include <stdio.h>
int main(void){
        int a = 1;
        int b = 0;

        printf("a = %d \n",a);
        printf("b = %d \n",b);
        printf("%d && %d = %d \n",a,b,a&&b);
        printf("%d || %d = %d \n",a,b,a||b);
        printf("! %d = %d \n",b,!b);
        return 0;
}
# gcc test.c -o test
# ./test 
a = 1 
b = 0 
1 && 0 = 0 
1 || 0 = 1 
! 0 = 1 
--位运算符
&                                        //位与(两位都为1时则为1,,只要有一个0则为0,一般用来置0)
|                                        //位或(两位中只要有一个为1则为1,一般用来置1)
^                                        //位抑或( 两位中有一位为1则为1,同时为1则为0,)
~                                        //取反(将0变成1,1变成0)
<<                                        //左移
>>                                        //右移
例如:
# vim test.c 
# cat test.c 
#include <stdio.h>
int main(void){
        int a = 1;
        int b = 2;

        printf("a = %d \n",a);
        printf("%d << 1 = %d \n",a,a<<1);
        printf("%d >> 1 = %d \n",a,a>>1);
        printf("%d & %d = %d \n",a,b,a&b);
        printf("%d | %d = %d \n",a,b,a|b);
        printf("%d ^ %d = %d \n",a,b,a^b);
        printf("~%d = %d \n",a,~a);
        return 0;
}
# gcc test.c -o test
# ./test 
a = 1 
1 << 1 = 2 
1 >> 1 = 0 
1 & 2 = 0 
1 | 2 = 3 
1 ^ 2 = 3 
~1 = -2 
--赋值运算符(改变左值)
+=                                //加等于
-=                                //减等于
*=                                //乘等于
/=                                //除等于
%=                                //模等于
~=                                //取反等于
&=                                //与等于
|=                                //或等于
^=                                //位抑或等于
>>=                                //右移等于
<<=                                //左移等于
例如:
# vim test.c 
# cat test.c 
#include <stdio.h>
int main(void){
        int a = 1;
        printf("a = %d \n",a);
        a+=1;
        printf("a = %d \n",a);
        a-=2;
        printf("a = %d \n",a);
        return 0;
}
# gcc test.c -o test
# ./test 
a = 1 
a = 2 
a = 0 
--自增自减
a++,++a                                //自增
a--,--a                                //自减
例如:
# vim test.c 
# cat test.c 
#include <stdio.h>
int main(void){
        int a = 1;
        printf("a = %d \n",a);
        printf("a = %d \n",a++);                                //注意:a++为先取值再运算
        printf("a = %d \n",a);
        printf("a = %d \n",--a);                                //注意:++a为先运算再取值
        return 0;
}
# gcc test.c -o test
# ./test 
a = 1 
a = 1 
a = 2 
a = 1 
--三目运算符
判断?为真时的处理:为假时的处理;
例如:
# vim test.c 
# cat test.c 
#include <stdio.h>
int main(void){
        int a = 1;
        int b = 2;
        a > b ? printf("a > b\n"):printf("a < b\n");                //如果a>b则输出a>b,如果a<b则输出a<b
        return 0;
}
# gcc test.c -o test
# ./test 
a < b
-----------------------------------------------------------------------------------------------------------------------------------------
--数据类型转换
--隐式转换
例如:
# vim test.c 
# cat test.c 
#include <stdio.h>
int main(void){
        char a = 'A';
        printf("%d",a);
        return 0;
}
# gcc test.c -o test
# ./test 
65
--显式转换
例如:
# vim test.c 
# cat test.c 
#include <stdio.h>
int main(void){
        long a = 1.1234;
        printf("%d",(int)a);                                //直接在变量前加上(数据类型)则可强制转换为指定的数据类型
        return 0;
}
# gcc test.c -o test
# ./test 
1
-----------------------------------------------------------------------------------------------------------------------------------------
--int printf(const char *format, ...);
FORMAT参数:
        %i                                        //打印int形数据
        %d                                        //打印int形数据
        %c                                        //打印字符形数据
        %h                                        //打印short形数据
        %l                                        //打印long形数据
        %f                                        //打印浮点形数据
        %u                                        //打印无符号形数据
        %s                                        //打印字符串
        %Lf                                //打印long double形数据
        %e                                        //以科学计数法输出(例如:123e+12)
        %o                                        //以八进制输出数据
        %x                                        //以十六进制输出数据
        %p                                        //打印地址
        %10i                                //设定精度为十位,无十位则使用空格自动补齐
        %010i                                //设定精度为十位,无十位则使用0在左边自动补齐
        %-010i                        //设定精度为十位,无十位则使用0在右边自动补齐
        %10.5f                        //设定输出浮点数精度,总长度为十位,小数点后为五位
特殊字符:
        \\                                        //反斜杠
        \a                                        //报警
        \b                                        //退格
        \f                                        //换纸页字符
        \r                                        //回车符
        \n                                        //换行符
        \t                                        //制表符
        \v                                        //水平制表符
        \ooo                                //八进制数字

转载于:https://www.cnblogs.com/jackbutler/archive/2012/03/31/2427170.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值