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. Here’s 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 <stdio.h>int main()
{int a = 1;char b ='G';double c = 3.14;printf("Hello World!\n");//printing the variables defined above along with their sizesprintf("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 wellprintf("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 wellprintf("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 wellprintf("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的指针,该指针可以转换为任何数据类型。

翻译自: https://www.freecodecamp.org/news/data-types-in-c-integer-floating-point-and-void-explained/

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

c语言中浮点数和整数转换_C中的数据类型-整数,浮点数和空隙说明相关推荐

  1. python中如何进行温度转换_python中温度单位转换的实例方法

    温度有摄氏度和华氏度两个单位,我们通常使用的是摄氏度,对于转换成华氏度,很多小伙伴记不住公式.作为万能的计算机,它是可以帮助我们解决温度单位转换的问题.本文主要演示python中进行温度单位转换的代码 ...

  2. java中日期格式的转换_java中定义日期格式的转换符

    Hi,大家好久不见,今天我们在这里给大家介绍一下关于Java的小知识,在Java中定义日期格式的转换符,至于运用呢就不和大家做介绍了:接下来就给大家详细介绍一下如何实现. 那我们该如何创建使用呢?首先 ...

  3. [转载] c语言中检查命令行参数_C中的命令行参数

    参考链接: Java中的命令行参数 c语言中检查命令行参数 Command line argument is a parameter supplied to the program when it i ...

  4. c# 中代替指针的功能_C中的功能指针

    c# 中代替指针的功能 A function pointer in C is a pointer that points to a function. C语言中的函数指针是指向函数的指针 . The ...

  5. python中data是什么意思_C++中cv::Mat中的data属性对应在python中是什么

    1, 因为我要使用一个dll,看C++的代码,是这样调用的 using namespace cv; m_image_mat = imread ( full_file_name ); data = m_ ...

  6. c++成员声明中的非法限定名_C++中作用域限定符

    在不同作用域内声明的变量可以同名,但如果局部变量和全局变量同名,在局部变量作用域内如何访问全局变量?C语言没有提供同名情况下访问全局变量的方法.在C++中,可以通过使用作用域限定符(::)(scope ...

  7. 将10进制整数转换成16进制整数输出

    题意: 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. Input 每行一个整数x,0<= x <= 2^31. Output 每行输出对应的八位十六进制整数 ...

  8. 6. Python数据类型之浮点数

    6. Python数据类型之浮点数 文章目录 6. Python数据类型之浮点数 1. Python的数据类型 2. 浮点数的定义 3. 浮点数示例 3.1 正浮点数 3.2 负浮点数 4. 浮点数的 ...

  9. python整数和浮点数相乘_python中整数除法和浮点数到整数转换之间的区别是什么原因?...

    我要说的是,您的观察表明这两个操作应该在直观上相似,因为在正数上它们的行为相同.但是,如果您查看它们的起源(一个来自数学,另一个来自计算机科学),那么它们的不同行为更有意义. 您可以在其中查看概念: ...

最新文章

  1. 20172324 2018-2019-1《程序设计与数据结构》实验2报告
  2. css怎么设置列表颜色,css怎么设置table颜色
  3. Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey
  4. 程序员必备:Java 日期处理的十个坑
  5. Hadoop集群完全分布式模式环境部署
  6. 论文推荐 | 2018中国卫星导航年会论文集
  7. python1到n的所有排列_非递归输出1-N的全排列的方法详解
  8. 迅速上手:使用taro构建微信小程序基础教程
  9. IPv6 带来的反欺诈难题,程序员该如何破解?
  10. 第 5 章 会修电脑不会修收音机 —— 依赖倒转原则
  11. libtorch下载
  12. numpy-np.concatenate
  13. hadoop集群全纪录
  14. eureka集群 ha_SpringCloud如何实现Eureka集群、HA机制-百度经验
  15. 武汉加油——传染病模型拟合
  16. saltstack(九)returner
  17. (干货)备战2021年软考中级网络工程师-03-计算机系统开发基础
  18. ValidateCode源码
  19. iOS获取当前网络连接状态WiFi、5G、4G、3G、2G
  20. 学生信息管理系统(面向对象版本)V3.0

热门文章

  1. 微信小程序如何搭建本地环境开发
  2. qt4的quick程序升级到qt5_最新8月书单出炉!送给你程序员
  3. Spring Cloud整合Redis
  4. Node.js的url模块简介
  5. 3.7Python数据处理篇之Numpy系列(七)---Numpy的统计函数
  6. 运维开发笔记整理-前后端分离
  7. Error creating bean with name 'defaultHandlerMapping' defined in ServletContext resource
  8. java maven项目使用sonar审核代码
  9. Kafka 消息监控 - Kafka Eagle
  10. 使用HTML5监測站点性能