学而时习之: C语言的基础了解

29 阅读7分钟

C程序的结构

c语言结构.png

C语言变量

C语言中每个变量都有其关联的数据类型。数据类型规定了变量所能存储的数据形式,例如整型、字符型、浮点型、双精度型等。 C语言的数据类型.png a.基本数据类型大小范围

类型范围大小格式说明符
int -2,147,483,648 ~ 2,147,483,6474 字节%d
char(-128 ~ 127)或(0 ~ 255)1 字节 %c
float1.2E-38 ~ 3.4E+384 字节 %f
double1.7E-308 ~ 1.7E+3088 字节 %lf
bool在 C 中,bool 数据类型不是内置数据类型。但是,C 语言的 C99 标准支持 bool 变量。布尔值可以将值存储为 true-false、0-1 或可以是 yes-no, 使用头文件 “stdbool.h”

b.基本数据类型的修饰符

修饰符中文简介
short短整型用于整型数据类型。它将 int 的大小减少到 2 个字节,其存储的值范围变为 -32,768 到 32,767。
long长整型用于增加 int 或 double 数据类型的大小(通常是两倍)。例如,一个 int 占 4 字节,使用 long int 后将占 8 字节。
unsigned无符号表示该数据类型只能表示非负整数(正数和零)。例如,unsigned int 的范围是 0 到 4,294,967,295。
signed有符号表示该数据类型可以表示正数、负数和零。这是整型的默认行为,通常可以省略不写。

b1关键点:

  • 它们通常与 intchardouble 等基本数据类型结合使用(例如 short intlong doubleunsigned char)。
  • 在使用时,int 关键字常常可以省略。例如,直接写 short 就等价于 short int
  • 这些修饰符通过改变数据的长度(size)  和表示范围(range) ,为程序员提供了更灵活的内存控制

b2关键点: 对上面的 修饰符+基本数据类型 使用typedef 改个名就有了。

有符号固定宽度类型:

int8_t      // 精确8位有符号整数
int16_t     // 精确16位有符号整数  
int32_t     // 精确32位有符号整数
int64_t     // 精确64位有符号整数

无符号固定宽度类型

uint8_t     // 精确8位无符号整数
uint16_t    // 精确16位无符号整数
uint32_t    // 精确32位无符号整数
uint64_t    // 精确64位无符号整数

类型有很多,代码位于stdint.h中

#ifndef _STDINT_H
#define _STDINT_H

#include <crtdefs.h>

#define __need_wint_t
#define __need_wchar_t
#include <stddef.h>

/* 7.18.1.1  Exact-width integer types */
typedef signed char int8_t;
typedef unsigned char   uint8_t;
typedef short  int16_t;
typedef unsigned short  uint16_t;
typedef int  int32_t;
typedef unsigned   uint32_t;
__MINGW_EXTENSION typedef long long  int64_t;
__MINGW_EXTENSION typedef unsigned long long   uint64_t;

/* 7.18.1.2  Minimum-width integer types */
typedef signed char int_least8_t;
typedef unsigned char   uint_least8_t;
typedef short  int_least16_t;
typedef unsigned short  uint_least16_t;
typedef int  int_least32_t;
typedef unsigned   uint_least32_t;
__MINGW_EXTENSION typedef long long  int_least64_t;
__MINGW_EXTENSION typedef unsigned long long   uint_least64_t;

/*  7.18.1.3  Fastest minimum-width integer types
 *  Not actually guaranteed to be fastest for all purposes
 *  Here we use the exact-width types for 8 and 16-bit ints.
 */
typedef signed char int_fast8_t;
typedef unsigned char uint_fast8_t;
typedef short  int_fast16_t;
typedef unsigned short  uint_fast16_t;
typedef int  int_fast32_t;
typedef unsigned  int  uint_fast32_t;
__MINGW_EXTENSION typedef long long  int_fast64_t;
__MINGW_EXTENSION typedef unsigned long long   uint_fast64_t;

/* 7.18.1.5  Greatest-width integer types */
__MINGW_EXTENSION typedef long long  intmax_t;
__MINGW_EXTENSION typedef unsigned long long   uintmax_t;

/* 7.18.2  Limits of specified-width integer types */
#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) ||	\
    defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L

/* 7.18.2.1  Limits of exact-width integer types */
#define INT8_MIN (-128)
#define INT16_MIN (-32768)
#define INT32_MIN (-2147483647 - 1)
#define INT64_MIN  (-9223372036854775807LL - 1)

#define INT8_MAX 127
#define INT16_MAX 32767
#define INT32_MAX 2147483647
#define INT64_MAX 9223372036854775807LL

#define UINT8_MAX 255
#define UINT16_MAX 65535
#define UINT32_MAX 0xffffffffU  /* 4294967295U */
#define UINT64_MAX 0xffffffffffffffffULL /* 18446744073709551615ULL */

/* 7.18.2.2  Limits of minimum-width integer types */
#define INT_LEAST8_MIN INT8_MIN
#define INT_LEAST16_MIN INT16_MIN
#define INT_LEAST32_MIN INT32_MIN
#define INT_LEAST64_MIN INT64_MIN

#define INT_LEAST8_MAX INT8_MAX
#define INT_LEAST16_MAX INT16_MAX
#define INT_LEAST32_MAX INT32_MAX
#define INT_LEAST64_MAX INT64_MAX

#define UINT_LEAST8_MAX UINT8_MAX
#define UINT_LEAST16_MAX UINT16_MAX
#define UINT_LEAST32_MAX UINT32_MAX
#define UINT_LEAST64_MAX UINT64_MAX

/* 7.18.2.3  Limits of fastest minimum-width integer types */
#define INT_FAST8_MIN INT8_MIN
#define INT_FAST16_MIN INT16_MIN
#define INT_FAST32_MIN INT32_MIN
#define INT_FAST64_MIN INT64_MIN

#define INT_FAST8_MAX INT8_MAX
#define INT_FAST16_MAX INT16_MAX
#define INT_FAST32_MAX INT32_MAX
#define INT_FAST64_MAX INT64_MAX

#define UINT_FAST8_MAX UINT8_MAX
#define UINT_FAST16_MAX UINT16_MAX
#define UINT_FAST32_MAX UINT32_MAX
#define UINT_FAST64_MAX UINT64_MAX

/* 7.18.2.4  Limits of integer types capable of holding
    object pointers */
#ifdef _WIN64
#define INTPTR_MIN INT64_MIN
#define INTPTR_MAX INT64_MAX
#define UINTPTR_MAX UINT64_MAX
#else
#define INTPTR_MIN INT32_MIN
#define INTPTR_MAX INT32_MAX
#define UINTPTR_MAX UINT32_MAX
#endif

/* 7.18.2.5  Limits of greatest-width integer types */
#define INTMAX_MIN INT64_MIN
#define INTMAX_MAX INT64_MAX
#define UINTMAX_MAX UINT64_MAX

/* 7.18.3  Limits of other integer types */
#ifdef _WIN64
#define PTRDIFF_MIN INT64_MIN
#define PTRDIFF_MAX INT64_MAX
#else
#define PTRDIFF_MIN INT32_MIN
#define PTRDIFF_MAX INT32_MAX
#endif

#define SIG_ATOMIC_MIN INT32_MIN
#define SIG_ATOMIC_MAX INT32_MAX

#ifndef SIZE_MAX
#ifdef _WIN64
#define SIZE_MAX UINT64_MAX
#else
#define SIZE_MAX UINT32_MAX
#endif
#endif

#ifndef WCHAR_MIN  /* also in wchar.h */
#define WCHAR_MIN 0U
#define WCHAR_MAX 0xffffU
#endif

/*
 * wint_t is unsigned short for compatibility with MS runtime
 */
#define WINT_MIN 0U
#define WINT_MAX 0xffffU

#endif /* !defined ( __cplusplus) || defined __STDC_LIMIT_MACROS */


/* 7.18.4  Macros for integer constants */
#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) ||	\
    defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L

/* 7.18.4.1  Macros for minimum-width integer constants

    Accoding to Douglas Gwyn <gwyn@arl.mil>:
	"This spec was changed in ISO/IEC 9899:1999 TC1; in ISO/IEC
	9899:1999 as initially published, the expansion was required
	to be an integer constant of precisely matching type, which
	is impossible to accomplish for the shorter types on most
	platforms, because C99 provides no standard way to designate
	an integer constant with width less than that of type int.
	TC1 changed this to require just an integer constant
	*expression* with *promoted* type."

	The trick used here is from Clive D W Feather.
*/

#define INT8_C(val) (INT_LEAST8_MAX-INT_LEAST8_MAX+(val))
#define INT16_C(val) (INT_LEAST16_MAX-INT_LEAST16_MAX+(val))
#define INT32_C(val) (INT_LEAST32_MAX-INT_LEAST32_MAX+(val))
/*  The 'trick' doesn't work in C89 for long long because, without
    suffix, (val) will be evaluated as int, not intmax_t */
#define INT64_C(val) val##LL

#define UINT8_C(val) (val)
#define UINT16_C(val) (val)
#define UINT32_C(val) (val##U)
#define UINT64_C(val) val##ULL

/* 7.18.4.2  Macros for greatest-width integer constants */
#define INTMAX_C(val) val##LL
#define UINTMAX_C(val) val##ULL

#endif  /* !defined ( __cplusplus) || defined __STDC_CONSTANT_MACROS */

#endif  /* _STDINT_H */

C语音中的关键字(列出部分)

分类英文关键词中文翻译
数据类型关键字char, int, float, double, void, short, long, signed, unsigned字符型、整型、浮点型、双精度型、无类型、短整型、长整型、有符号、无符号
运算符与工具关键字sizeof, return, goto, typedef大小、返回、跳转、类型定义
控制流关键字if, else, switch, case, default, for, while, do, break, continue如果、否则、开关、情况、默认、循环、当、做、中断、继续
存储类关键字auto, register, static, extern自动、寄存器、静态、外部
类型限定符const, volatile常量、易变
用户自定义类型struct, union, enum结构体、联合体、枚举

C与C++的区别

C++的诞生是为了将面向对象编程概念引入C语言,因此两者语法高度相似,但存在若干差异。以下是C与C++编程语言的主要区别对照表:

特性C语言C++
编程范式过程化编程过程化编程、面向对象、泛型编程
面向对象不支持支持类、继承、多态和封装等面向对象概念
内存管理手动管理(使用malloc()、calloc()、free()等函数)手动与自动兼顾(支持new/delete操作符进行动态内存分配与释放)
函数重载不支持(函数名称必须唯一)支持函数重载
运算符重载不支持(每个运算符仅执行预设操作)支持运算符重载,允许为+、-、*等运算符定义自定义行为
访问控制无访问控制机制支持使用private、public、protected关键字进行访问控制