初识C语言

初识C语言

1、第一个C语言程序

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

int main()
{
	printf("Hello,World!\n");
	system("pause");
	return 0;
}

2、数据类型

char //字符数据类型 1字节
short //短整型2字节
int //整型4字节
long //长整型8字节
long long //更长整形8字节
float //单精度浮点数4字节
double //双精度浮点数8字节

#include <stdio.h>

int main()
{
	printf("%d\n", sizeof(char));
	printf("%d\n", sizeof(short));
	printf("%d\n", sizeof(int));
	printf("%d\n", sizeof(long));
	printf("%d\n", sizeof(long long));
	printf("%d\n", sizeof(float));
	printf("%d\n", sizeof(double));
	printf("%d\n", sizeof(long double));
	system("pause");
	return 0;
}

3、全局变量和局部变量及作用域和生命周期

作用域:
作用域是限定程序代码中所用到的名字的可用性的代码范围称之为作用域。

生命周期:
生命周期是变量的创建到变量的销毁之间的一个时间段。

1、当局部变量与全局变量冲突时,优先使用局部变量;
2、局部变量的作用域是变量所在局部范围;
3、全局变量作用域是整个工程;
4、局部变量的生命周期是进入作用域生命周期喀什,出去作用域生命周期结束;
5、全局变量的生命周期是整个程序的生命周期。

#include <stdio.h>

int global = 2020;//全局变量

int main()
{
	//局部变量和全局变量同名时,局部变量优先使用
	int local = 2020;//局部变量
	int global = 2019;//局部变量
	printf("%d\n", global);
	system("pause");
	return 0;
}

4、变量的使用

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	int num1 = 0;
	int num2 = 0;
	int sum = 0;

	printf("输入两个操作数:\n");
	scanf("%d %d", &num1, &num2);

	sum = num1 + num2;
	
	printf("sum = %d\n", sum);
	system("pause");
	return 0;
}

5、常量

常量分为以下几种:
字面值常量、const修饰的常变量 #define定义的标识符常量 枚举常量

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

//枚举类型常量
enum Sex
{
	MALE,
	FEMALE,
	SECrET
};

int main()
{
	3.14;//字面常量
	1000;//字面常量
	const float pai = 3.14f;//const修饰的常量 
	//pai = 3.15; 不可以给常量赋值
	#define MAX 100
	system("pause");
	return 0;
}

6、字符串,转义字符,注释

字符串

由双引号引起来的一串字符串字面值称之为字符串

转义字符

转义字符释义
?在书写连续多个问号时使用,防止他们被解析成三字母词
\’用于表示字符常量’
\“用于表示一个字符串内部的双引号
\用于表示一个反斜杠,防止它被解释为一个转义序列符。
\a警告字符,蜂鸣
\b退格符
\n换行
\r回车
\t水平制表符
\v垂直制表符
\dddddd表示1~3个八进制的数字。 如: \130 X
\xdddddd表示3个十六进制数字。 如: \x030

注释

1、代码中不需要的代码可以删除或者注释掉
2、代码中有些代码比较难,可以加文字注释
3、C语言注释风格/XXXXXXXXXXXXXX/
4、C++语言注释风格//XXXXXXXXXX

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

int Add(int x, int y)
{
	return x + y;
}
/*C语言风格注释
int Sub(int x, int y)
{
	return x-y;
}
*/

int main()
{
	"Hello,World!";//字符串字面值(字符串)
	//字符串的结束标志是\0是结束标志,不算做字符串内容。

	//在屏幕上打印一个目录C:\code\test.c
	printf("C:\\code\\test.c\n");

	//在屏幕上打印一个单引号'
	printf("%c\n", '\'');
	//在屏幕上打印一个字符串,字符串的内容是一个双引号"
	printf("%s\n", "\"");

	printf("%d\n", strlen("abcdef"));
	printf("%d\n", strlen("C:\test\328\test.c"));

	//C++注释风格
	//int a = 10;
	//调用Add函数,完成加法
	printf("%d\n", Add(1, 2));
	system("pause");
	return 0;
}

7、选择语句

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

int main()
{
	int flag = 0;
	if (flag == 0)
	{
		printf("Hello,World!\n");
	}
	else
	{
		printf("Goodbye!\n");
	}
	system("pause");
	return 0;
}

8、循环语句

1、while语句
2、for语句
3、do-while语句

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

int main()
{
	int flag1 = 0;
	int flag2 = 0;
	//while语句
	while (flag1 < 10)
	{
		printf("1Hello,World!\n");
		flag1++;
	}
	//for语句
	for(int i = 0; i < 10; i++)
	{
		printf("2Hello,World!\n");
	}
	//do-while语句
	do 
	{
		printf("3Hello,World!\n");
		flag2++;
	} while (flag2 < 10);
	system("pause");
	return 0;
}

9、函数

实现代码复用,简化代码

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

#if 0
int main()
{
	int num1 = 0;
	int num2 = 0;
	int sum = 0;
	printf("输入两个操作数:\n");
	scanf("%d %d", &num1, &num2);

	sum = num1 + num2;

	printf("sum = %d\n", sum);
	system("pause");
	return 0;
}
#else
int Add(int x, int y)
{
	return x + y;
}

int main()
{
	int x, y;
	int sum = 0;
	printf("输入两个操作数:\n");
	scanf("%d %d", &x, &y);

	sum = Add(x, y);

	printf("sum = %d\n", sum);
	system("pause");
	return 0;
}
#endif

10、数组的使用

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

int main()
{
	int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };//定义一个整形数组,最多存放10个元素
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	system("pause");
	return 0;
}

11、static关键字

static是用来修饰变量和函数的
1、修饰局部变量:局部变量的作用域扩大,生命周期增长;
2、修饰全局变量:全局变量作用域和生命周期被限定在本文件;
3、修饰函数:函数只能在本文件内使用,不能再其他源文件内使用。

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


//static是用来修饰变量和函数的
//1、修饰局部变量
#if 0
void test()
{
	int i = 0;
	i++;
	printf("%d ", i);
}

int main()
{
	for (int i = 0; i < 10; i++)
	{
		test();
	}
	system("pause");
	return 0;
}
#else
void test()
{
	static int i = 0;
	i++;
	printf("%d ", i);
}

int main()
{
	for (int i = 0; i < 10; i++)
	{
		test();
	}
	system("pause");
	return 0;
}
#endif
//2、修饰全局变量
#if 0
int g_val = 2020;

int main()
{
	printf("%d\n", g_val);
	system("pause");
	return 0;
}
#else
static int g_val = 2020;

int main()
{
	printf("%d\n", g_val);
	system("pause");
	return 0;
}
#endif

//3、修饰函数
#if 0
int Add(int x, int y)
{
	return x + y;
}

int main()
{
	printf("%d\n", add(2, 3));
	system("pause");
	return 0;
}
#else
static int Add(int x, int y)
{
	return x + y;
}

int main()
{
	printf("%d\n", add(2, 3));
	system("pause");
	return 0;
}
#endif

12、define定义常量和宏

定义标识符常量
定义宏

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define ADD(x, y) ((x) + (y))

int main()
{
	printf("%d\n", ADD(2, 3));
	printf("%d\n", 10 * ADD(2, 3));
	system("pause");
	return 0;
}

13、指针及指针变量的大小

指针大小在32位平台是4个字节,64位平台是8个字节

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

#if 0
int main()
{
	int num = 10;
	&num;//取出num的地址
	printf("%p\n", &num);
	system("pause");
	return 0;
}
#endif
//指针的使用实例
#if 0
int main()
{
	int num = 10;
	int* p = &num;
	*p = 20;
	printf("%d\n", num);
	system("pause");
	return 0;
}
#endif
//指针的推广
#if 0
int main()
{
	char ch = 'w';
	char* pch = &ch;
	*pch = 'q';
	printf("%c\n", ch);
	system("pause");
	return 0;
}
#endif
//指针变量的大小
//指针大小在32位平台是4个字节,64位平台是8个字节
#if 0
int main()
{
	printf("%d\n", sizeof(char*));
	printf("%d\n", sizeof(short*));
	printf("%d\n", sizeof(int*));
	printf("%d\n", sizeof(long*));
	printf("%d\n", sizeof(float*));
	printf("%d\n", sizeof(double*));
	printf("%d\n", sizeof(long double*));

	printf("%d\n", sizeof(char*));


	system("pause");
	return 0;
}
#endif

14、结构体

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

struct Student
{
	char name[20];//名字
	int age;//年龄
	char sex[5];//性别
	char id[15];//学号
};

int main()
{
	struct Student Stu = { "张三", "20", "男", "20200202" };
	printf("name = %s age = %d sex = %s id = %s", Stu.name, Stu.age, Stu.sex, Stu.id);
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值