【C语言】C语言基础语法速览

C语言基础语法速览

1. 变量类型

基本类型

  • 整型 int、short、long
  • 字符型 char
  • 浮点型 float 、 double

构造类型

  • 数组类型
  • 结构类型 struct
  • 联合类型 union
  • 枚举类型 enum

指针类型

  • char * 、int * 、int **

1.1 类型字节数

类型字节大小
char1
short2
int4
long4
long long8
float4
double8

可以用 sizeof 函数进行验证:

#include<stdio.h>
int main()
{
        short a;
        unsigned short ua;
        int b;
        unsigned int ub;
        long c;
        long long d;
        char e;
        float f;
        double g;
        printf("short size=%d\n",sizeof(a));
        printf("unsigned short size=%d\n",sizeof(ua));
        printf("int size=%d\n",sizeof(b));
        printf("unsigned int size=%d\n",sizeof(ub));
        printf("long size=%d\n",sizeof(c));
        printf("long long size=%d\n",sizeof(d));
        printf("char size=%d\n",sizeof(e));
        printf("float size=%d\n",sizeof(f));
        printf("double size=%d\n",sizeof(g));
        return 0;
}

//输出
short size=2
unsigned short size=2
int size=4
unsigned int size=4
long size=8
long long size=8
char size=1
float size=4
double size=8

1.1 变量输出格式

整型部分

输出格式作用
%d输出有符号10进制数
%hd可用于接收short类型,2个字节 变量值
%ld可用于接收long类型,4个字节 变量值
%lld可用于接收long long 类型,8个字节 变量值
%nd输出有符号10进制数,左边填充空格,总长为8
%0nd输出有符号10进制数,左边填充0,总长为8
%o输出8进制数
%x输出16进制数,输出字母以小写输出
%X输出16进制数,输出字母以大写输出
%u输出一个10进制的无符号数,可用于接收 sizeof(类型名),以及unsigned int
%hu可用于接收unsigned short类型,2个字节 变量值
%lu可用于接收unsigned long类型,4个字节 变量值
%llu可用于接收unsigned long类型,8个字节 变量值

浮点型部分

输出格式作用
%f接收float、double类型 ,默认保留6位小数
%08.4f显示8位数(包含小数点),不足8位用0填充。并且保留4位小数,对第5位做四舍五入到第4位

字符部分

输出格式作用
%c输出单个字符
%s输出字符串或者字符数组
%%输出一个百分号%
#include<stdio.h>
int main()
{
    int a = 10;
    int b = 0123;
    int c = 0x1abc;
    unsigned int d = 1;
    
    printf("a=%d\n",a);
    printf("a=%08d\n",a);
    printf("a=%8d\n",a);
    printf("b=%o\n",b);
    printf("small c=%x\n",c);
    printf("big c=%X\n",c);
    printf("d=%u\n",d);
    return 0;
}

//输出
a=10
a=00000010
a=      10
b=123
small c=1abc
big c=1ABC
d=1

1.2 变量输入格式

值得注意的是,double类型的输入格式是 %lf ,和输出格式有区别。当然输出格式也可以是%lf,但是根据C语言标准通常使用 %f 用于输出格式。

#include<stdio.h>
int main()
{
        int a;
    	//格式如下
        scanf("%d",&a);
        printf("a=%d\n",a);
    	return 0;
}

2. 分支循环语句

2.1 if分支语句

if(判断表达式)
{
    
}
else if(判断表达式)
{
    
}
else
{
    
}

2.2 switch 分支语句

switch(判断表达式)
{
    case 结果1:
        执行语句;
        break;
    case 结果2:
        执行语句;
        break;
    case 结果3:
        执行语句;
        break;
    default:
        执行语句;
        break;
}

2.3 while循环语句

while(判断表达式)
{
    循环体
}

2.4 do…while循环语句

do{
	循环体
} while (判断表达式);

2.5 for循环语句

for(int i=0;i<length;i++)
{
    循环体
}

注意事项: break 语句在多层循环中,只能跳出一层循环。continue 语句只能跳过当次循环。

3. 数组

3.1 一维数组

声明规则: 类型 数组名[常量] 例如: int a[10];

数组大小 = 数组类型占用字节数 * 元素个数

注意这里的数组大小,不管有没有初始化,只要正常声明了,就会给数组分配所有空间

指针大小 = 64位机器是8个字节,32位是4个字节,不管是什么类型的指针都是这个数。

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int a[10]= { 1, 2, 3 };
	//int a[] = { 1, 2, 3, 4, 5 };
	char cs[10]={0};
	char * cp[10];
	cp[0]=cs;
	cp[1]=cs[1];
	//数组地址就是第一个元素的地址,数组地址可以直接用数组名代替 
    //查看数组地址
	printf("a=\t%p\n",a);
    //查看数组第一个元素地址
	printf("&a[0]=\t%p\n",&a[0]);
	printf("cp[0] size is:%u\n",sizeof(cp[0]));
	printf("===============\n");
	//指针大小,8个字节,因为这是64位机器
	printf("&a[0] size=\t%u\n",sizeof(&a[0]));
	printf("&cs[0] size=\t%u\n",sizeof(&cs[0]));
	printf("&cp[0] size is:%u\n",sizeof(&cp[0]));
	
	printf("int arr size is: %u\n",sizeof(a));
	printf("char arr  size is: %u\n",sizeof(cs));
	return 0;
}

//输出
a=      000000000062FDF0
&a[0]=  000000000062FDF0
cp[0] size is:8
===============
&a[0] size=     8
&cs[0] size=    8
&cp[0] size is:8
int arr size is: 40
char arr  size is: 10

3.2 二维数组

声明规则: 类型 数组名[常量][常量] 例如: int a[10][10];

二维数组默认行优先存储,也就是说存储时存储器中的数组还是一维的,存完第一行再存第二行,元素总数=行数*列数。

int main(int argc, char *argv[]) {
	//二维数组初始化方式
	int aa[3][4] = 
	{ 
		{ 1, 2, 3, 4 },
		{ 5, 6, 7, 8 },
		{ 9, 10, 11, 12 }
	};
	
	//int a[3][4] = { 1, 2, 3, 4 , 5, 6, 7, 8, 9, 10, 11, 12  };
	
	//未初始化的默认是0 
	//int a[3][4] = { 1, 2, 3, 4  };
	
    //一维长度会自动分配
	//int a[][4] = { 1, 2, 3, 4, 5, 6, 7, 8};

	return 0;
}

4. 结构体

结构体就是把一系列的变量组合成一个整体。

4.1 结构体类型定义

//定义一,其中stu是结构体类型名,tom,Jerry是这个类型的变量
struct stu
{
    char name[20];
    int age;
    char sex;
} tom,jerrys;

//定义二,等同于上面
struct stu
{
    char name[20];
    int age;
    char sex;
};
struct stu tom,jerrys;

//定义三,匿名结构体类型
struct
{
    char name[20];
    int age;
    char sex;
} tom,jerrys;

//定义四,引入 typedef 给结构体类型起一个别名
typedef struct stu
{
    char name[20];
    int age;
    char sex;
} student;

student stu1;
//等价于
struct stu stu1;

4.2 结构体类型初始化

//方式一  结构体初始化
typedef struct stu
{
    char name[20];
    int age;
    char sex;
}student;

student stu1;
strcpy(stu1.name, "lisi");
stu1.age =10;
stu1.sex = 'f';
printf("stu1 name=%s age=%d sex=%c\n",stu1.name,stu1.age,stu1.sex);
//输出
stu1 name=lisi age=10 sex=f
    
//方式二  结构体初始化
student stu2={"tom",20,'M'};
//输出
stu2 name=tom age=20 sex=M

    
//方式三  结构体数组初始化
student stu3[2]=
{
    {"jerry",30,'F'},
    {"sandy",25,'M'}
};
int len = sizeof(stu3)/sizeof(student);
int i;
for(i=0;i<len;i++){
    printf("stu3[%d] name=%s age=%d sex=%c\n",i,stu3[i].name,stu3[i].age,stu3[i].sex);
}
//输出
stu3[0] name=jerry age=30 sex=F
stu3[1] name=sandy age=25 sex=M
    
//方式四  结构体指针初始化
student * stu4= (student *)malloc(sizeof(student));
	strcpy(stu4->name,"xiaoli");
	stu4->age=26;
	stu4->sex='f';
	printf("stu4 name=%s age=%d sex=%c\n",stu4->name,stu4->age,stu4->sex);
//输出
stu4 name=xiaoli age=26 sex=f

5. 共用体

  union是一个能在同一个存储空间存储不同类型数据的类型。共用体变量中起作用的成员是最后一次存放的成员。共用体变量的地址和它的各成员的地址都是同一地址。联合体所占的内存长度等于其最长成员的长度。

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef union TempVar 
{
	int a;
	short b;
	char c;
	double d;
	float f;
} tempVar;

int main(int argc, char *argv[]) {
	tempVar temp;
	temp.f = 1.5;
	temp.b=15;
	temp.c='c';
	temp.a=20;
	temp.d = 2.3;
	printf("tempVar size is:%ld\n",sizeof(temp));
	printf("tempVar value is:%f\n",temp.d);
	
	return 0;
}

//输出
tempVar size is:8
tempVar value is:2.300000

6. 枚举

给第一个枚举属性赋值,后面的属性会自动加1个赋值

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef enum weekday
{
	mon=1, tue, wed, thu, fri, sat,sun
}week;


int main(int argc, char *argv[]) {
	week temp;
	temp=tue;
	printf("week is:%u\n",temp);	 
	return 0;
}
//输出2

7. 其他

防止头文件重复包含

使用一些预处理指令来避免重复包含文件

//假如文件名为hello.h,可以给头文件以下定义
//方法一:
#ifndef __HELLO_H__
#define __HELLO_H__
/*
	头文件中方法的声明,或者变量的声明语句
*/
#endif

//方法二:
#pragma once

/* 
	文件中方法的声明,或者变量的声明
*/
  • 31
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值