博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言中浮点数和整数转换_C中的数据类型-整数,浮点数和空隙说明
阅读量:2523 次
发布时间:2019-05-11

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

c语言中浮点数和整数转换

C中的数据类型 (Data Types in C)

There are several different ways to store data in C, and they are all unique from each other. The types of data that information can be stored as are called data types. C is much less forgiving about data types than other languages. As a result, it’s important to make sure that you understand the existing data types, their abilities, and their limitations.

有几种不同的方法可以将数据存储在C中,并且它们彼此之间是唯一的。 可以存储信息的数据类型称为数据类型。 与其他语言相比,C对于数据类型的宽容程度要低得多。 因此,重要的是要确保您了解现有的数据类型,它们的功能及其局限性。

One quirk of C’s data types is that they depend entirely on the hardware that you’re running your code on. An int on your laptop will be smaller than an int on a supercomputer, so knowing the limitations of the hardware you’re working on is important. This is also why the data types are defined as being minimums- an int value, as you will learn, is at minimum -32767 to 32767: on certain machines, it will be able to store even more values that this.

C的数据类型的一个怪癖是它们完全取决于您在其上运行代码的硬件。 笔记本电脑上的int会比超级计算机上的int小,因此了解所用硬件的局限性很重要。 这也是为什么将数据类型定义为最小值的原因-正如您将学到的那样,一个int值至少为-32767到32767:在某些机器上,它将能够存储更多的值。

There are two categories that we can break this into: integers, and floating point numbers. Integers are whole numbers. They can be positive, negative, or zero. Numbers like -321, 497, 19345, and -976812 are all perfectly valid integers, but 4.5 is not because 4.5 is not a whole number.

我们可以将其分为两类:整数和浮点数。 整数是整数。 它们可以是正数,负数或零。 像-321、497、19345和-976812之类的数字都是完全有效的整数,但是4.5并不是因为4.5不是整数。

Floating point numbers are numbers with a decimal. Like integers, -321, 497, 19345, and -976812 are all valid, but now 4.5, 0.0004, -324.984, and other non-whole numbers are valid too.

浮点数是带小数的数字。 像整数一样,-321、497、19345和-976812都有效,但是现在4.5、0.0004,-324.984和其他非整数也都有效。

C allows us to choose between several different options with our data types because they are all stored in different ways on the computer. As a result, it is important to be aware of the abilities and limitations of each data type to choose the most appropriate one.

C允许我们在数据类型的几个不同选项之间进行选择,因为它们都以不同的方式存储在计算机上。 因此,重要的是要知道每种数据类型选择最合适的能力和局限性。

整数数据类型 (Integer data types)

字符: char (Characters: char)

char holds characters- things like letters, punctuation, and spaces. In a computer, characters are stored as numbers, so char holds integer values that represent characters. The actual translation is described by the ASCII standard. a handy table for looking up that.

char包含字符,例如字母,标点和空格。 在计算机中,字符存储为数字,因此char保留代表字符的整数值。 实际翻译由ASCII标准描述。 一个方便查找的桌子。

The actual size, like all other data types in C, depends on the hardware you’re working on. By minimum, it is at least 8 bits, so you will have at least 0 to 127. Alternatively, you can use signed char to get at least -128 to 127.

像C中的所有其他数据类型一样,实际大小取决于您正在使用的硬件。 至少,它至少是8位,因此您至少要有0到127。或者,您可以使用带signed char来获得至少-128到127。

标准整数: int (Standard Integers: int)

The amount of memory that a single int takes depends on the hardware. However, you can expect an int to be at least 16 bits in size. This means that it can store values from -32,768 to 32,767, or more depending on hardware.

一个int占用的内存量取决于硬件。 但是,您可以预期int大小至少为16位。 这意味着它可以存储-32,768到32,767或更多的值,具体取决于硬件。

Like all of these other data types, there is an unsigned variant that can be used. The unsigned int can be positive and zero but not negative, so it can store values from 0 to 65,535, or more depending on hardware.

像所有其他所有数据类型一样,可以使用unsigned变体。 unsigned int可以是正数,可以是零,但不能为负数,因此它可以存储0到65,535之间的值,或者取决于硬件而更多。

短整数: short (Short integers: short)

This doesn’t get used often, but it’s good to know that it exists. Like int, it can store -32768 to 32767. Unlike int, however, this is the extent of its ability. Anywhere you can use short, you can use int.

这并不经常使用,但是很高兴知道它的存在。 与int一样,它可以存储-32768到32767。但是,与int不同,这是其功能的程度。 只要可以使用short ,就可以使用int

长整数: long (Longer integers: long)

The long data type stores integers like int, but gives a wider range of values at the cost of taking more memory. Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647. Alternatively, use unsigned long for a range of 0 to 4,294,967,295.

long数据类型存储整数,如int ,但以占用更多内存为代价,提供了更大范围的值。 Long存储至少32位,范围为-2,147,483,648至2,147,483,647。 或者,使用unsigned long ,范围为0到4,294,967,295。

甚至更长的整数: long long (Even longer integers: long long)

The long long data type is overkill for just about every application, but C will let you use it anyway. It’s capable of storing at least −9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. Alternatively, get even more overkill with unsigned long long, which will give you at least 0 to 18,446,744,073,709,551,615.

long long数据类型对于几乎每个应用程序来说都是多余的,但是C仍然可以让您使用它。 它能够存储至少-9,223,372,036,854,775,807至9,223,372,036,854,775,807。 或者,使用unsigned long long获得更多的过大杀伤力,这将使您至少有0到18,446,744,073,709,551,615。

浮点数数据类型 (Floating point number data types)

基本浮点数: float (Basic Floating point numbers: float)

float takes at least 32 bits to store, but gives us 6 decimal places from 1.2E-38 to 3.4E+38.

float至少需要32位来存储,但是从1.2E-38到3.4E + 38,我们给了6个小数位。

双打: double (Doubles: double)

double takes double the memory of float (so at least 64 bits). In return, double can provide 15 decimal place from 2.3E-308 to 1.7E+308.

double占用float内存的两倍(因此至少需要64位)。 作为回报,double可以提供从2.3E-308到1.7E + 308的小数点后15位。

获得更大范围的双打: long double (Getting a wider range of doubles: long double)

long double takes at least 80 bits. As a result, we can get 19 decimal places from 3.4E-4932 to 1.1E+4932.

long double至少需要80位。 结果,我们可以从3.4E-4932到1.1E + 4932获得19位小数。

选择正确的数据类型 (Picking the right data type)

C makes pick the data type, and makes us be very specific and intentional about the way that we do this. This gives you a lot of power over your code, but it’s important to pick the right one.

C会选择数据类型,并使我们对执行此操作的方式非常明确和有目的地。 这为您提供了很多控制代码的能力,但是选择正确的代码很重要。

In general, you should pick the minimum for your task. If you know you’ll be counting from integer 1 to 10, you don’t need a long and you don’t need a double. If you know that you will never have negative values, look into using the unsigned variants of the data types. By providing this functionality rather than doing it automatically, C is able to produce very light and efficient code. However, it’s up to you as the programmer to understand the abilities and limitations, and choose accordingly.

通常,您应该为任务选择最低要求。 如果您知道要从整数1到10进行计数,则不需要很长,也不需要加倍。 如果您知道永远不会有负值,请研究使用数据类型的unsigned变体。 通过提供此功能而不是自动执行,C可以生成非常轻便且高效的代码。 但是,作为程序员,您需要了解功能和限制并做出相应选择。

We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:

我们可以使用sizeof()运算符来检查变量的大小。 有关各种数据类型的用法,请参见以下C程序:

#include 
int main(){ int a = 1; char b ='G'; double c = 3.14; printf("Hello World!\n"); //printing the variables defined above along with their sizes printf("Hello! I am a character. My value is %c and " "my size is %lu byte.\n", b,sizeof(char)); //can use sizeof(b) above as well printf("Hello! I am an integer. My value is %d and " "my size is %lu bytes.\n", a,sizeof(int)); //can use sizeof(a) above as well printf("Hello! I am a double floating point variable." " My value is %lf and my size is %lu bytes.\n",c,sizeof(double)); //can use sizeof(c) above as well printf("Bye! See you soon. :)\n"); return 0;}

输出: (Output:)

Hello World!Hello! I am a character. My value is G and my size is 1 byte.Hello! I am an integer. My value is 1 and my size is 4 bytes.Hello! I am a double floating point variable. My value is 3.140000 and my size is 8 bytes.Bye! See you soon. :)

虚空类型 (The Void type)

The void type specifies that no value is available. It is used in three kinds of situations:

void类型指定没有可用值。 它在三种情况下使用:

1.函数返回空值 (1. Function returns as void)

There are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);

C中有各种函数不返回任何值,或者可以说它们返回void。 没有返回值的函数的返回类型为void。 例如, void exit (int status);

2.函数参数为void (2. Function arguments as void)

There are various functions in C which do not accept any parameter. A function with no parameter can accept a void. For example, int rand(void);

C中有许多不接受任何参数的函数。 没有参数的函数可以接受空白。 例如, int rand(void);

3.指向无效的指针 (3. Pointers to void)

A pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size); returns a pointer to void which can be casted to any data type.

类型为void *的指针表示对象的地址,但不表示其类型。 例如,一个内存分配函数void *malloc( size_t size); 返回指向void的指针,该指针可以转换为任何数据类型。

翻译自:

c语言中浮点数和整数转换

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

你可能感兴趣的文章
凸包1——卷包裹算法
查看>>
Centos 安装SVN并配置多个版本库
查看>>
Eos持久化实体
查看>>
Shell基础-通配符
查看>>
static类型的变量
查看>>
SpringMVC之文件上传异常处理
查看>>
优先级
查看>>
rest_framework组件
查看>>
css position relative obsolution
查看>>
矩阵重叠面积计算 线段树hdu1542
查看>>
HTML命令
查看>>
SqlServer动态变换库名
查看>>
抓虫记之五:Webservice总是调用不了
查看>>
JS获取父框架的内容:获取子框架的内容:js框架应用
查看>>
关于手码编写autolayout约束
查看>>
Java参考资料
查看>>
goto语句
查看>>
简单的车票管理系统
查看>>
2016年10月25 草堂随笔1 ModelState.IsValid
查看>>
Jenkins Pipelines Summary
查看>>