C Data Types
C provides rich basic types and derived types. This chapter will systematically organize integers, floating-point types, character types, enumerations, arrays, structures, unions, pointers, etc.
1. Basic Types
- Integer types:
short,int,long,long long(signed/unsigned) - Character types:
char(often used to store characters or small integers) - Floating-point types:
float,double,long double - Boolean:
_Boolorboolinstdbool.h
Example:
c
#include <stdbool.h>
short s = 1; unsigned int ui = 10u; long long ll = 1000ll;
char c = 'A'; bool ok = true; double pi = 3.14159;2. Literals and Suffixes
- Integer suffixes:
u,l,ll - Floating-point suffixes:
f,l - Number bases: hexadecimal
0x2A, octal052, binary (not standardized before C23, can use macros/libraries)
3. sizeof and Alignment
c
printf("%zu\n", sizeof(int));sizeof returns the number of bytes occupied by an object or type.
4. Type Conversion
- Implicit conversion: small types are promoted to larger types during arithmetic operations
- Explicit conversion:
(int)3.14
5. Enumerations (enum)
c
enum Color { RED=1, GREEN=2, BLUE=3 };6. Derived Types Overview
- Arrays:
int a[10]; - Pointers:
int *p; - Structures:
struct Point { int x; int y; }; - Unions:
union Data { int i; float f; char c; };
7. Constant Qualifiers and Qualifiers
constread-onlyvolatileprevents optimization from ignoring changes (e.g., hardware registers)restrictpointer aliasing optimization hint (C99)
8. Characters and Strings
- Character constants:
'A'