博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言变量类型和范围_C变量和类型
阅读量:2506 次
发布时间:2019-05-11

本文共 8687 字,大约阅读时间需要 28 分钟。

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 
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 built-in data types are int, char, short, long, float, double, long double. Let’s find out more about those.

内置数据类型为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 
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 
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 
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 
int main(void) { char j; j = 1000;}

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

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

#include 
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 
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 bytesint size: 4 bytesshort size: 2 byteslong size: 8 bytesfloat size: 4 bytesdouble size: 8 byteslong double size: 16 bytes

翻译自:

c语言变量类型和范围

转载地址:http://hvqgb.baihongyu.com/

你可能感兴趣的文章
2020-11-18
查看>>
Docker面试题(二)
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
注册用户
查看>>
TZC Intercommunication System
查看>>
HDU 4571 SPFA+DP
查看>>
centos 创建以日期为名的文件夹
查看>>
Java Timer触发定时器
查看>>
Page Object设计模式
查看>>
程序的基础知识
查看>>
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
POJ2236(KB5-A)
查看>>
Centos MySQL数据库迁移详细步骤
查看>>
2初出茅庐--初级篇2.1
查看>>
新建 WinCE7.0 下的 Silverlight 工程
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>