返回

C语言学习初识day1

发布时间:2022-12-31 06:46:18 290
  • 全局变量,局部变量:

        放在大括号里的变量就是局部变量;全局变量生命周期是整个程序。当局部变量和全局变量辆重名时,局部变量优先。

  • 常量,变量,常变量:

C语言中的常量分为以下以下几种:

1.字面常量、

0;106;3.14

2.const修饰的常变量。

const-常属性,"const int a"虽然是常变量,但本质依旧是变量。

例:

#include
int main()
{
const int n = 10;
int arr[n] = {0}; //此处会报错
return 0;
}

        可以理解为const的功能是锁定变量的值,即“死变量”。

3.#cefine定义的标识符常量

#include
#define num 10
int main()
{
int arr[num] = {0};
return 0;
}

4.枚举常量

enum

#include
enum Color
{
RED,
BLUE,
YELLOW,
};
int main()
{
printf("%d\n",RED);
printf("%d\n",BLUE);
printf("%d\n",YELLOW);
return 0;

  • 字符串

字符串的结束标志是一个\0的转义字符。在计算字符串长度的时候\0是结束标志,不算作字符串内容。'/0'的值为0(此处的值是指ASCII码值)。

#include
int main()
{
char arr1[] = "abc";
//此处在abc暗藏一个'/0',值为0 --'/0'为字符串的结束标志
char arr2[] = {'a','b','c'};//{'a','b','c','0'}是正确的
printf("%s",arr1);
printf("%s",arr2);
return 0;
}

  • 转义字符

//、/ddd(默认ddd为八进制数)、/xdd

#include 
int main()
{
printf("%d\n" strlen("abcdef"));//\32被解析成一个转义字符
printf("%d\n", strlen("c:\test\327\test.c"));
return 0;
}

  • 关键字
  1. static(修饰局部变量)
#include 
void test ()
{
static int a=1;//
a++;
printf("a = %d\n",a);
}
int main()
{
int i = 0;
while (i<5)
{
test();
i++;
}
return 0;
}

结果:C语言学习初识day1_c

作用:延长生命周期。

2.static(修饰全局变量)

static修饰全局变量,改变了变量的作用域–让静态的全局变量只能在自己所在的源文件内部使用,禁止在其他源文件的使用。

3.static(修饰函数)

作用类似与修饰全局变量——禁止函数在非本源文件的使用,即改变了函数的链接属性,外部链接→内部链接。

  • define定义宏
  • 结构体
struct People
{
char name[20];
short age;
};
int main()
{
struct People the_first = {"张三", 54};
struct People* pb = &the_first;
//利用指针打印结构体变量1
printf("姓名:%s\n", (*pb).name);
printf("姓名:%d\n", (*pb).age);
//利用指针打印结构体变量2(结构体指针->成员)
printf("姓名:%s\n", pb->name);
printf("姓名:%d\n", pb->age);
//结构体变量.成员
printf("姓名:%s\n", the_first.name);
printf("年龄:%d", the_first.age);
return 0;
}


特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线