C/C++ 基础 - 变量、基本类型、表达式
1. 程序基本概念
变量 和 常量 是程序处理的两种基本数据对象。
声明语句 说明变量的名字及类型,也可以指定变量的初值。
运算符 指定将要进行的操作。
表达式 则把变量与常量组合起来生成新的值。
对象的类型决定该对象可取值的集合以及可以对该对象执行的操作。
1.1 变量名
变量名命令规则:由字母、数字和下划线组成,首字符必须是字母或下划线。
变量命名尽量见名知义
Camel标记法采用首字母小写,接下来的单词都以大写字母开头的方法,如myName。
Pastal标记法采用首字母大写,接下来的单词都以大写字母开头的方法,如MyName。
1.2 数据类型与长度
问题:如何显示当前机器对 char short、int, long 数据类型长度?
采用打印标准头文件中的相应值以及直接计算两种方式实现。
所有类型长度定义符号常量以及其他与机器和编译器有关的属性可在标准头文件
在类Unix系统中,所有头文件都在/usr/include目录中
<limits.h> 与 <float.h> 中找到。
(1)标准头文件实现方式
#include <stdio.h> #include <limits.h> int main() { printf("signed char min = %d\n", SCHAR_MIN); printf("signed char max = %d\n", SCHAR_MAX); printf("signed short min = %d\n", SHRT_MIN); printf("signed short max = %d\n", SHRT_MAX); printf("signed int min = %d\n", INT_MIN); printf("signed int max = %d\n", INT_MAX); printf("signed long min = %ld\n", LONG_MIN); printf("signed long max = %ld\n", LONG_MAX); printf("unsigned char max = %u\n", UCHAR_MAX); printf("unsigned short max = %u\n", USHRT_MAX); printf("unsigned int max = %u\n", UINT_MAX); printf("unsigned long max = %lu\n", ULONG_MAX); return 0; } /* 输出结果 signed char min = -128 signed char max = 127 signed short min = -32768 signed short max = 32767 signed int min = -2147483648 signed int max = 2147483647 signed long min = -9223372036854775808 signed long max = 9223372036854775807 unsigned char max = 255 unsigned short max = 65535 unsigned int max = 4294967295 unsigned long max = 18446744073709551615 */
(2) 直接计算方式
#include <stdio.h> int main() { printf("signed char min = %d\n", -(char)((unsigned char) ~0 >> 1)); printf("signed char max = %d\n", (char)((unsigned char) ~0 >> 1)); printf("signed short min = %d\n", -(short)((unsigned short) ~0 >> 1)); printf("signed short max = %d\n", (short)((unsigned short) ~0 >> 1)); printf("signed int min = %d\n", -(int)((unsigned int) ~0 >> 1)); printf("signed int max = %d\n", (int)((unsigned int) ~0 >> 1)); printf("signed long min = %d\n", -(long)((unsigned long) ~0 >> 1)); printf("signed long max = %d\n", (long)((unsigned long) ~0 >> 1)); printf("unsigned char max = %u\n", (unsigned char) ~0 ); printf("unsigned short max = %u\n", (unsigned short) ~0 ); printf("unsigned int max = %u\n", (unsigned int) ~0 ); printf("unsigned long max = %lu\n", (unsigned long) ~0 ); return 0; } /* 输出结果 signed char min = -127 signed char max = 127 signed short min = -32767 signed short max = 32767 signed int min = -2147483647 signed int max = 2147483647 signed long min = 1 signed long max = -1 unsigned char max = 255 unsigned short max = 65535 unsigned int max = 4294967295 unsigned long max = 18446744073709551615 */
用变量a给出下面的定义:
a) 一个整型数(An integer)
int a;
b)一个指向整型数的指针( A pointer to an integer)
int *a;
c)一个指向指针的的指针,它指向的指针是指向一个整型数( A pointer to a pointer to an intege)
*(int *a) int **a;
d)一个有10个整型数的数组( An array of 10 integers)
int a[10];
e) 一个有10个指针的数组,该指针是指向一个整型数的。(An array of 10 pointers to integers)
int *a[10];
f) 一个指向有10个整型数数组的指针( A pointer to an array of 10 integers)
*(int a[10]) int (*a)[10];
g) 一个指向函数的指针,该函数有一个整型参数并返回一个整型数(A pointer to a function that takes an integer as an argument and returns an integer)
fun(*int a ) int (*max_function)(int a);
h)一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数( An array of ten pointers to functions that take an integer argument and return an integer )
fun(*int a[10]) int (*a[10])(int) ;