c语言变量类型和范围

C is a statically typed language.

C是一种静态类型的语言。

This means that any variable has an associated type, and this type is known at compilation time.

这意味着任何变量都具有关联的类型,并且该类型在编译时是已知的。

This is very different than how you work with variables in Python, JavaScript, PHP and other interpreted languages.

这与处理Python,JavaScript,PHP和其他解释语言中的变量的方式非常不同。

When you create a variable in C, you have to specify the type of a variable at the declaration.

在C语言中创建变量时,必须在声明中指定变量的类型。

In this example we initialize a variable age with type int:

在此示例中,我们使用int类型初始化变量age

int age;

A variable name can contain any uppercase or lowercase letter, can contain digits and the underscore character, but it can’t start with a digit. AGE and Age10 are valid variable names, 1age is not.

变量名可以包含任何大写或小写字母,可以包含数字和下划线字符,但不能以数字开头。 AGEAge10是有效的变量名,不是1age

You can also initialize a variable at declaration, specifying the initial value:

您还可以在声明时初始化变量,并指定初始值:

int age = 37;

Once you declare a variable, you are then able to use it in your program code, and you can change its value at any time, using the = operator for example, like in age = 100;, provided the new value is of the same type.

声明变量后,便可以在程序代码中使用它,并且可以随时使用=运算符来更改其值,例如age = 100; ,前提是新值的类型相同。

In this case:

在这种情况下:

#include <stdio.h>int main(void) {int age = 0;age = 37.2;printf("%u", age);
}

the compiler will raise a warning at compile time, and will convert the decimal number to an integer value.

编译器将在编译时发出警告,并将十进制数转换为整数值。

The C built-in data types are int, char, short, long, float, double, long double. Let’s find out more about those.

C内置数据类型为intcharshortlongfloatdoublelong double 。 让我们进一步了解这些。

整数 (Integer numbers)

C provides us the following types to define integer values:

C为我们提供了以下类型来定义整数值:

  • char

    char

  • int

    int

  • short

    short

  • long

    long

Most of the times, you’ll likely use an int to store an integer. But in some cases, you might want to choose one of the other 3 options.

在大多数情况下,您可能会使用int来存储整数。 但是在某些情况下,您可能要选择其他三个选项之一。

The char type is commonly used to store letters of the ASCII chart, but it can be used to hold small integers from -128 to 127. It takes at least 1 byte.

char类型通常用于存储ASCII图表的字母,但可以用于保存-128127小整数。 至少需要1个字节。

int takes at least 2 bytes. short takes at least 2 bytes. long takes at least 4 bytes.

int至少占用2个字节。 short至少需要2个字节。 long至少需要4个字节。

As you can see, we are not guaranteed the same values for different environments. We only have an indication. The problem is that the exact numbers that can be stored in each data type depends on the implementation and the architecture.

如您所见,我们不能保证在不同环境下使用相同的值。 我们只有一个指示。 问题在于,每种数据类型中可以存储的确切数字取决于实现和体系结构。

We’re guaranteed that short is not longer than int. And we’re guaranteed long is not shorter than int.

我们保证short不长于int 。 并且我们保证long不小于int

The ANSI C spec standard determines the minimum values of each type, and thanks to it we can at least know what’s the minimum value we can expect to have at our disposal.

ANSI C规范标准确定每种类型的最小值,并且由于它,我们至少可以知道我们可以期望的最小值是多少。

If you are programming C on an Arduino, different board will have different limits.

如果您在Arduino上进行C语言编程,则不同的开发板将具有不同的限制。

On an Arduino Uno board, int stores a 2 byte value, ranging from -32,768 to 32,767. On a Arduino MKR 1010, int stores a 4 bytes value, ranging from -2,147,483,648 to 2,147,483,647. Quite a big difference.

在Arduino Uno板上, int存储2字节值,范围-32,76832,767 。 在Arduino MKR 1010上, int存储4个字节的值,范围为-2,147,483,6482,147,483,647 。 差异很大。

On all Arduino boards, short stores a 2 bytes value, ranging from -32,768 to 32,767. long store 4 bytes, ranging from -2,147,483,648 to 2,147,483,647.

在所有Arduino板上, short存储一个2字节的值,范围为-32,76832,767long存储4个字节,范围从-2,147,483,6482,147,483,647

无符号整数 (Unsigned integers)

For all the above data types, we can prepend unsigned to start the range at 0, instead of a negative number. This might make sense in many cases.

对于上述所有数据类型,我们可以在unsigned添加范围以0开头,而不是负数。 在许多情况下,这可能是有道理的。

  • unsigned char will range from 0 to at least 255

    unsigned char范围是0到至少255

  • unsigned int will range from 0 to at least 65,535

    unsigned int范围是0到至少65,535

  • unsigned short will range from 0 to at least 65,535

    unsigned short范围从0到至少65,535

  • unsigned long will range from 0 to at least 4,294,967,295

    unsigned long范围从0到至少4,294,967,295

溢出的问题 (The problem with overflow)

Given all those limits, a question might come up: how can we make sure our numbers do not exceed the limit? And what happens it we do exceed the limit?

考虑到所有这些限制,可能会出现一个问题:我们如何确保我们的人数不超过限制? 而我们超出限制会发生什么呢?

If you have an unsigned int number at 255, and you increment it, you’ll get 256 in return. As expected. If you have a unsigned char number at 255, and you increment it, you’ll get 0 in return. It resets starting from the initial possible value.

如果您在255处有一个unsigned int数,并对其进行递增,则将得到256。 如预期的那样。 如果您的unsigned char数为255,并且对其进行递增,则将得到0。 从可能的初始值开始复位。

If you have a unsigned char number at 255 and you add 10 to it, you’ll get the number 9:

如果您在255处有一个unsigned char数,并将其加10,则将得到数字9

#include <stdio.h>int main(void) {unsigned char j = 255;j = j + 10;printf("%u", j); /* 9 */
}

If you have a signed value, the behavior is undefined. It will basically give you a huge number which can vary, like in this case:

如果您有一个带符号的值,则行为是不确定的。 基本上,它将为您提供很大的数量,并且可能会有所不同,例如在这种情况下:

#include <stdio.h>int main(void) {char j = 127;j = j + 10;printf("%u", j); /* 4294967177 */
}

In other words, C does not protect you from going over the limits of a type. You need to take care of this yourself.

换句话说,C不能保护您避免超出类型的限制。 您需要自己照顾。

声明错误类型时的警告 (Warnings when declaring the wrong type)

When you declare the variable and initialize it with the wrong value, the gcc compiler (the one you’re probably using) should warn you:

当您声明变量并使用错误的值对其进行初始化时, gcc编译器(您可能正在使用的编译器)会警告您:

#include <stdio.h>int main(void) {char j = 1000;
}

hello.c:4:11: warning: implicit conversion from 'int' to'char' changes value from 1000 to -24[-Wconstant-conversion]char j = 1000;~   ^~~~
1 warning generated.

And it also warns you in direct assignments:

它还会在直接任务中警告您:

#include <stdio.h>int main(void) {char j;j = 1000;
}

But not if you increase the number using for example +=:

但是,如果使用+=来增加数字,则不会:

#include <stdio.h>int main(void) {char j = 0;j += 1000;
}

浮点数字 (Floating point numbers)

Floating point types can represent a much larger set of values than integers can, and can also represent fractions, something that integers can’t do.

浮点类型可以表示比整数更大的一组值,并且还可以表示分数,而整数则不能。

Using floating point numbers, we represent numbers as decimal numbers times powers of 10.

使用浮点数,我们将数字表示为十进制数乘以10的幂。

You might see floating point numbers written as

您可能会看到浮点数写为

  • 1.29e-3

    1.29e-3

  • -2.3e+5

    -2.3e+5

and in other seemingly weird ways.

以及其他看似奇怪的方式。

The following types:

以下类型:

  • float

    float

  • double

    double

  • long double

    long double

are used to represent numbers with decimal points (floating point types). All can represent both positive and negative numbers.

用于表示带小数点的数字(浮点类型)。 全部都可以代表正数和负数。

The minimum requirements for any C implementation is that float can represent a range between 10^-37 and 10^+37, and is typically implemented using 32 bits. double can represent a bigger set of numbers. long double can hold even more numbers.

任何C实现的最低要求是float可以表示10 ^ -37到10 ^ + 37之间的范围,并且通常使用32位实现。 double可以代表更大的一组数字。 long double可以容纳更多数字。

The exact figures, as with integer values, depend on the implementation.

与整数值一样,确切的数字取决于实现方式。

On a modern Mac, a float is represented in 32 bits, and has a precision of 24 significant bits, 8 bits are used to encode the exponent. A double number is represented in 64 bits, with a precision of 53 significant bits, 11 bits are used to encode the exponent. The type long double is represented in 80 bits, has a precision of 64 significant bits, 15 bits are used to encode the exponent.

在现代Mac上, float以32位表示,精度为24个有效位,其中8位用于编码指数。 一个double精度数以64位表示,精度为53个有效位,其中11个位用于对指数进行编码。 long double类型用80位表示,精度为64位有效位,其中15位用于编码指数。

On your specific computer, how can you determine the specific size of the types? You can write a program to do that:

在您的特定计算机上,如何确定类型的特定大小? 您可以编写一个程序来做到这一点:

#include <stdio.h>int main(void) {printf("char size: %lu bytes\n", sizeof(char));printf("int size: %lu bytes\n", sizeof(int));printf("short size: %lu bytes\n", sizeof(short));printf("long size: %lu bytes\n", sizeof(long));printf("float size: %lu bytes\n", sizeof(float));printf("double size: %lu bytes\n", sizeof(double));printf("long double size: %lu bytes\n", sizeof(long double));
}

In my system, a modern Mac, it prints:

在我的系统中,现代Mac可以打印:

char size: 1 bytes
int size: 4 bytes
short size: 2 bytes
long size: 8 bytes
float size: 4 bytes
double size: 8 bytes
long double size: 16 bytes

翻译自: https://flaviocopes.com/c-variables-types/

c语言变量类型和范围

c语言变量类型和范围_C变量和类型相关推荐

  1. 返回值类型与函数类型不匹配_C++返回值类型后置(跟踪返回值类型)

    在泛型编程中,可能需要通过参数的运算来得到返回值的类型.考虑下面这个场景: template <typename R, typename T, typename U>R add(T t, ...

  2. c语言里变量列表,嵌入式C语言里的土豪们之变量类型

    嵌入式C语言里的土豪们之变量类型本文引用地址:http://www.eepw.com.cn/article/184332.htm 上一篇我们谈到了运算奢华大户除法(详见<嵌入式C语言里的土豪们之 ...

  3. mysql变量赋值加冒号_C语言变量声明加冒号的用法(占位符)

    有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又提供了一种数据结构 ...

  4. bool类型头文件_C++ Primer Chapter2 变量和基本类型

    第二章 变量和基本类型 声明: 本文为<C++ Primer 中文版(第五版)>学习笔记. 原书更为详细,本文仅作学习交流使用. P30-P71 数据类型是程序的基础.C++语言支持广泛的 ...

  5. 【C】C语言函数中的变量(包括:作用域、存储类型)

    局部变量和全局变量 C语言中变量按其作用域分,可分为局部变量和全局变量,具体的解释为: 局部变量(内部变量):在定义它的函数内有效,但是函数返回后失效: 全局变量(外部变量):在所有源文件内均有效.在 ...

  6. C语言的指针符号到底靠近变量类型还是变量名?

    今天在复习数据结构时,C语言实现线性表用到了指针,对指针符号的位置存疑,遂查询了一下: 在C语言中只要使用指针,必然用到* 运算符.然后经常会看到不同的书中*有靠近变量类型的,有靠近变量名称的,很迷惑 ...

  7. JAVA语言规范 JAVA SE 8 - 类型、值和变量

    JAVA语言规范 JAVA SE 8 - 类型.值和变量 类型和值的种类 简单类型和值 整数类型和值 整数操作 浮点数类型.格式和值 浮点数操作 boolean类型和布尔值 引用类型和值 对象 Obj ...

  8. 动态二维数组外圈元素值的和_C语言 | 用指向元素的指针变量输出二维数组元素的值...

    例33:有一个3*4的二维数组,要求用C语言实现指向元素的指针变量输出二维数组个元素的值. 解题思路:二维数组的元素时整型的,它相当于整型变量,可以用int*型指针变量指向它.二维数组的元素在内存中是 ...

  9. 【C语言】结构体类型和结构体变量(字节对齐)

    目录 一.结构体类型定义 二.结构体变量的定义 ①先定义结构体类型,再定义结构体变量 ②在定义结构体类型的同时,定义结构体变量 ③直接定义结构体变量 三.结构体变量的内存分配 四.结构体变量的初始化 ...

最新文章

  1. MDK调试错误之assert_failed
  2. Chrome开发者工具使用console.trace的一个小技巧
  3. 男人都应该懂的一张图。。 | 今日趣图
  4. 安装oracle需要多少内存,针对大型内存配置的 Oracle VM 安装需要更多步骤 (7195262)...
  5. vue瀑布流demo_面试加分企业级Vue瀑布流
  6. Chrome 浏览器将登陆 Fuchsia OS
  7. 啥是佩奇,Python 告诉你!
  8. java二次方程式答案_二次方程式Java?
  9. opencv中的resize函数实现图像插值缩放
  10. selenium BY定位
  11. 文件管理器之文件操作
  12. 《Java技术》第四次作业
  13. 用Latex beamer做poster经验总结
  14. Visual Studio Code的下载与安装
  15. WebService框架简介
  16. 数学建模系列(三)-评价模型(六)---神经网络评价
  17. Matlab 2019b AudioToolBox设备环境测试
  18. 海风教育荣获第七届中国财经峰会最具成长价值奖
  19. 强化学习中 on-policy与off-policy 的理解;如何区分on-policy 与 off-policy;RL更新策略、policy结构总结
  20. opencv区域提取

热门文章

  1. java编程50_荐非常经典的java编程题全集-共50题(1-10)...
  2. poi3.17版本生成excel文件时,单元格生成求和公式
  3. ESP32开发(1)----Espressif-IDE开发环境配置
  4. 已开源!Flutter 流畅度优化组件 keframe
  5. DC010的精华分享【首发速看】
  6. 11月获得融资的区块链企业汇总
  7. 6-1 类模板Point的定义与使用 (10 分)
  8. java项目如何分工合作,JavaWeb基础 jsp+servlet分工合作的简单示例
  9. 新库上线 | CnOpenData·IFR工业机器人数据
  10. GStreamer连接 IP 网络摄像机的实际例子