且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字符和字符串的基础知识

更新时间:2021-11-30 17:09:18

/*
 ============================================================================
 Name        : TestChar.c
 Author      : lf
 Version     :
 Copyright   : Your copyright notice
 Description : 字符和字符串的基本知识
 ============================================================================
 */

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

int main(void) {
	testChar();
	testArray();
	return EXIT_SUCCESS;
}

/**
 * 将'a'+1的结果用字符表示
 * 即printf("%c \n",'a'+1);输出b
 * 将'a'+1的结果用整数表示
 * 即printf("%d \n",'a'+1);输出98
 * 这些都是由ASCII码得出.
 * 有的转义字符还是值得注意的,比如'\0'在ASCII码表示结束符NUL
 */
void testChar(){
	printf("%c \n",'a'+1);
	printf("%d \n",'a'+1);
}


/**
 * 字符串可以看作一个数组,数组中的每个元素是字符型的.
 * 所以:
 * char c="hello"[0];
 * printf("%c \n",c);
 * 可以打印出字符h
 *
 * 正因为"字符串可以看作一个数组,数组中的每个元素是字符型的"
 * 所以可以直接用一个字符串初始化一个字符数组
 *
 */
void testArray(){
	char c="hello"[0];
	printf("%c \n",c);

	char charArray[]="world";
	//等价于
	//char charArray[]={'w','o','r','l','d','\0'};
	//打印字符数组
	printf("char array=%s \n",charArray);
}