C语言————第二天

本文介绍了C/C++编程中常量(const)、字面常量、枚举常量(enum)以及#define预定义标识符的使用,展示了如何通过这些概念创建和操作不变量。同时涵盖了字符串、转义字符和基本控制结构如if-else和while的实例。
摘要由CSDN通过智能技术生成

常量

const

常属性,修饰变量使其变为常变量。

#include <stdio.h>

int main{
    const int num = 10;
    printf("%d\n",num);
    return 0;
}

字面常量 

#inclode <stdio.h>

int main(){
    1;
    2;
    //其中1,2是字面常量
    return 0;
}

枚举常量 ————列举

枚举常量关键————enum 

#inclode <stdio.h>

enum Color{
    red,
    yellow,
    blue
}

int main(){
    enum Color color = blue;
    return 0;
}

#define

定义的标识符常量 

 

#include <stdio.h>

#define Max = 10;

int main(){
    char a[Max] = {0};
    return 0 ;
}

 字符串类型 

\0是字符串的结束,不算内容。

strlen()是计算字符串长度的 ,使用时要加#include <string.h>

用双引号引起来的字符。

#include <stdio.h>

int main{
    "chshskfhk";
    ""; //空字符串
    return 0;
}

可以用数组来存放字符串。

#include <stdio.h>

int main{
    char a[] = "abc";
    printf("%s\n",a);
    return 0;
}
#include <stdio.h>

int main{
    char a[] = "abc";
    char a1[] = {'a','b','c','0'};
    printf("%s\n",a);
    printf("%s\n",a1);
    //打印出来的a和a1内容是一样的。
    return 0;
}

 转义字符

 

#include <stdio.h>
#include <strig.h>

int main{
	printf("%s\n","\"");打印出“ ,不加不打印
	return 0;
}
int main{
	printf("%d\n",strlen("c:\test\32\test.c"));//打印出13 --\t为一个,\32为一个
	//\32--32是2个8进制数字
	//32作为8进制代表的那个十进制数字,作为ASCII码值对应的字符
	//32 -->10进制 26 ->作为ASCII码值代表的字符
	return 0;
}

 if...else 选择语句

#include <stdio.h>

int main{
    int num;
    printf("要不要好好学习?(1/0)>:")
    scanf("%d",num);
    if(num ==1){
        printf("好");
    }
    else{
        printf("并不好");
    }
}

while 循环语句

#include <stdio.h>

int main(){
    int a = 0;
    while(a<=100){
        printf("加油")
        a++;
    }
}

函数 ————自定义函数,库函数

 

#include <stdio.h>

int Add(int a,int b){
	int z = a+b;
	return z;
}
int main{
	int s =10;
	int d = 20;
	sum = Add(a,d);
	printf("sum=%d\n" ,sum)
	return 0;
}

数组

#include <stdio.h>

int main{
	//int a=1;
	//int b=2;
	//int c = 3;
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};//int arr[10]定义一个存放10个整数数字的数组
	//char arr[10];
	//.......
	printf("%d\n",arr[4])//下标的方式访问元素
	return 0;
}

 arr  :  1  2  3  4  5  6  7  8  9  10
下标:0  1  2...............

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值