31 Days of C - 3, Constants, Strings

1, literal

3 is 3 and will never be 4.

#include<stdio.h>
int main(){
    
    
	printf("%d\n",3);
	return 0;
}

Effect:

insert image description here

2, const constant

When a variable is const modified, it cannot be assigned again.
The essence is still a variable, but it cannot be assigned a value.

This code throws an error:

#include<stdio.h>
int main(){
    
    
	const int a=3;
	a=4;
	printf("%d\n",a);
	return 0;
}

Error content:

insert image description here

3, #define constants

#define can define a constant.

#include<stdio.h>
#define hello "你好,世界!"
int main(){
    
    
	printf("%s\n",hello);
	return 0;
}

Effect:

insert image description here

4, enumeration constants

An enumeration represents a listable value, such as:
gender: male, female.
Week: 1 to 7.

enum Gender{
    
    
	Male,Female
}

Male and female are essentially 0 and 1, which are represented by words, have semantics, and are readable.
Simple to use:

#include<stdio.h>
enum Gender{
    
    
	Male,Female
};
int main(){
    
    
	enum Gender a=Male;
	enum Gender b=Female;
	
	printf("%d,%d\n",a,b);
	return 0;
}

Effect:

insert image description here

5. String

Strings are surrounded by double quotes.

"你好,世界!"

The essence is a char array, and it ends with \0, which is not in the content.
\0 represents the end of the string.

It can be directly assigned to a char array, and then printed as a %s string:

#include<stdio.h>

int main(){
    
    
	char a[]="你好,世界!\n";
	printf("%s",a);
	return 0;
}

Effect:

insert image description hereIt will end when it encounters \0:

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

Effect:

insert image description here
In the figure, d is not output.

escape character

Some meaningful characters, which are inconvenient to express themselves, are converted:

换行:\n
tab位:\t
字符串结束:\0
单引号:\'
双引号:\"
斜线:\\

Simple to use:

#include<stdio.h>
int main(){
    
    
	printf("%c\n",'\\');
	printf("%c\n",'\'');
	printf("%c\n",'\"');
	return 0;
}

Effect:

insert image description here

How characters are stored

ASC code, which is seen when the char is output as %d:

#include<stdio.h>
int main(){
    
    
	printf("%d\n",'A');
	printf("%d\n",'a');
	printf("%d\n",'0');
	return 0;
}

Effect:

insert image description here

Guess you like

Origin blog.csdn.net/qq_37284843/article/details/124387662