c++数据处理
1.简单的变量
1.1变量名的命名规则
- 在名称只能使用字母字符、数字和下划线表示
- 名称的第一个字符不能是数字
- 区分大小写字符
- 不能将c++关键字作为名称
- 以两个下划线打头或以下划线和大写字母打头的名称被保留给实现使用。以一个下划线开头的名称被保留给实现,用作全局标识。(当有两个下划线时就成了注释了)
- c++命名没有长度限制,命名的所有字母都有意义。
命名举例
int poodle;// true
int Poodle; // true
数据类型整型:
- short 8 位单元
- int 16 位单元
- long 32 位单元
- long long 64 位单元
每种类型的整形都是有取值范围的,更具在计算机储存分配的内存大小而决定。
详细见如下代码。
代码输出
#include<iostream>
#include<climits>
int main()
{
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
cout << "int is " << sizeof(int) << " bytes" << endl;
cout << "short is " << sizeof(short) << " bytes" << endl;
cout << "long is " << sizeof(long) << " bytes" << endl;
cout << "long long is " << sizeof(long long) << " bytes" << endl;
cout << "Maximum values: "<<endl;
cout << "int : " << n_int << endl;
cout << "short: " << n_short << endl;
cout << "long: " << n_long << endl;
cout << "long long : " << n_llong << endl;
return 0;
}
/* 输出结果:
int is 4 bytes
short is 2 bytes
long is 4 bytes
long long is 8 bytes
Maximum values:
int : 2147483647
short: 32767
long: 2147483647
long long : 9223372036854775807
*/
当然还有最小值,感兴趣的小伙伴可以自己试试
climits中符号的常量
翻看climits.h 文件的源码。
// climits.h 文件
// climits standard header (core)
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _CLIMITS_
#define _CLIMITS_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <limits.h>
#endif // _STL_COMPILER_PREPROCESSOR
#endif // _CLIMITS_
// 看了一眼发现啥都没有。嘿嘿
// 再打开 limits.h 文件翻看如下
//
// limits.h
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The C Standard Library <limits.h> header.
//
#pragma once
#define _INC_LIMITS
#include <vcruntime.h>
#pragma warning(push)
#pragma warning(disable: _VCRUNTIME_DISABLED_WARNINGS)
_CRT_BEGIN_C_HEADER
#define CHAR_BIT 8
#define SCHAR_MIN (-128)
#define SCHAR_MAX 127
#define UCHAR_MAX 0xff
#ifndef _CHAR_UNSIGNED
#define CHAR_MIN SCHAR_MIN
#define CHAR_MAX SCHAR_MAX
#else
#define CHAR_MIN 0
#define CHAR_MAX UCHAR_MAX
#endif
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffff
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffff
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807i64
#define LLONG_MIN (-9223372036854775807i64 - 1)
#define ULLONG_MAX 0xffffffffffffffffui64
#define _I8_MIN (-127i8 - 1)
#define _I8_MAX 127i8
#define _UI8_MAX 0xffui8
#define _I16_MIN (-32767i16 - 1)
#define _I16_MAX 32767i16
#define _UI16_MAX 0xffffui16
#define _I32_MIN (-2147483647i32 - 1)
#define _I32_MAX 2147483647i32
#define _UI32_MAX 0xffffffffui32
#define _I64_MIN (-9223372036854775807i64 - 1)
#define _I64_MAX 9223372036854775807i64
#define _UI64_MAX 0xffffffffffffffffui64
#ifndef SIZE_MAX
// SIZE_MAX definition must match exactly with stdint.h for modules support.
#ifdef _WIN64
#define SIZE_MAX 0xffffffffffffffffui64
#else
#define SIZE_MAX 0xffffffffui32
#endif
#endif
#if __STDC_WANT_SECURE_LIB__
#ifndef RSIZE_MAX
#define RSIZE_MAX (SIZE_MAX >> 1)
#endif
#endif
_CRT_END_C_HEADER
#pragma warning(pop) // _VCRUNTIME_DISABLED_WARNINGS
再从文件中的代码截取一段
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffff
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffff
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807i64
#define LLONG_MIN (-9223372036854775807i64 - 1)
#define ULLONG_MAX 0xffffffffffffffffui64
这是一段宏定义。
显然这些数据类型的值和范围都是定义好了,当然这也跟计算机储存有一定的相关。