c语言中浮点数和整数转换_C中的数据类型-整数,浮点数和空隙说明-程序员宅基地

技术标签: python  c++  java  指针  数据结构  

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 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的指针,该指针可以转换为任何数据类型。

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

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cumichun6193/article/details/108153305

智能推荐

适合入门的8个趣味机器学习项目-程序员宅基地

文章浏览阅读86次。首发地址:https://yq.aliyun.com/articles/221708谈到机器学习,相信很多除学者都是通过斯坦福大学吴恩达老师的公开课《Machine Learning》开始具体的接触机器学习这个领域,但是学完之后又不知道自己的掌握情况,缺少一些实际的项目操作。对于机器学习的相关竞赛挑战,有些项目的门槛有些高,参加后难以具体的实现,因此造..._scrath五子棋下载

oracle 12c avg,Oracle 12c新特性系列专题-安徽Oracle授权认证中心-程序员宅基地

文章浏览阅读83次。原标题:Oracle 12c新特性系列专题-安徽Oracle授权认证中心 随着Oracle database 12c的普及,数据库管理员 (DBA) 的角色也随之发生了转变。 Oracle 12c数据库对 DBA 而言是下一代数据管理。它让 DBA 可以摆脱单调的日常管理任务,能够专注于如何从数据中获取更多价值。未来我们会推出基于Oracle12c的技术文章,帮助DBA尽快掌握新一代数据库的新特性..._ilm add policy row store compress advanced row after

第七周项目三(负数把正数赶出队列)-程序员宅基地

文章浏览阅读150次。问题及代码:*Copyright(c)2016,烟台大学计算机与控制工程学院 *All right reserved. *文件名称:负数把正数赶出队列.cpp *作者:张冰 *完成日期;2016年10月09日 *版本号;v1.0 * *问题描述: 设从键盘输入一整数序列a1,a2,…an,试编程实现: 当ai>0时,ai进队,当ai<0时,将队首元素出队,当ai

Linux命名空间学习教程(二) IPC-程序员宅基地

文章浏览阅读150次。本文讲的是Linux命名空间学习教程(二) IPC,【编者的话】Docker核心解决的问题是利用LXC来实现类似VM的功能,从而利用更加节省的硬件资源提供给用户更多的计算资源。而 LXC所实现的隔离性主要是来自内核的命名空间, 其中pid、net、ipc、mnt、uts 等命名空间将容器的进程、网络、消息、文件系统和hostname 隔离开。本文是Li..._主机的 ipc 命名空间

adb强制安装apk_adb绕过安装程序强制安装app-程序员宅基地

文章浏览阅读2w次,点赞5次,收藏7次。在设备上强制安装apk。在app已有的情况下使用-r参数在app版本低于现有版本使用-d参数命令adb install -r -d xxx.apk_adb绕过安装程序强制安装app

随便推点

STM32F407 越界问题定位_stm32flash地址越界怎么解决-程序员宅基地

文章浏览阅读290次。如果是越界进入硬件错误中断,MSP 或者 PSP 保存错误地址,跳转前会保存上一次执行的地址,lr 寄存器会保存子函数的地址,所以如果在 HardFault_CallBack 中直接调用 C 语言函数接口会间接修改了 lr,为了解决这个问题,直接绕过 lr 的 C 语言代码,用汇编语言提取 lr 寄存器再决定后面的操作。由于 STM32 加入了 FreeRTOS 操作系统,可能导致无法准确定位,仅供参考(日常编程需要考虑程序的健壮性,特别是对数组的访问,非常容易出现越界的情况)。_stm32flash地址越界怎么解决

利用SQL注入上传木马拿webshell-程序员宅基地

文章浏览阅读1.8k次。学到了一种操作,说实话,我从来没想过还能这样正常情况下,为了管理方便,许多管理员都会开放MySQL数据库的secure_file_priv,这时就可以导入或者导出数据当我如图输入时,就会在D盘创建一个名为123456.php,内容为<?php phpinfo();?>的文件我们可以利用这一点运用到SQL注入中,从拿下数据库到拿下目标的服务器比如我们在使用联合查询注入,正常是这样的语句http://xxx?id=-1 union select 1,'你想知道的字段的内容或查询语句',

Html CSS的三种链接方式_html链接css代码-程序员宅基地

文章浏览阅读2.9w次,点赞12次,收藏63次。感谢原文:https://blog.csdn.net/abc5382334/article/details/24260817感谢原文:https://blog.csdn.net/jiaqingge/article/details/52564348Html CSS的三种链接方式css文本的链接方式有三种:分别是内联定义、链入内部css、和链入外部css1.代码为:<html>..._html链接css代码

玩游戏哪款蓝牙耳机好?2021十大高音质游戏蓝牙耳机排名_适合游戏与运动的高音质蓝牙耳机-程序员宅基地

文章浏览阅读625次。近几年,蓝牙耳机市场发展迅速,越来越多的消费者希望抛弃线缆,更自由地听音乐,对于运动人士来说,蓝牙耳机的便携性显得尤为重要。但目前市面上的大多数蓝牙耳机实际上都是“有线”的,运动过程中产生的听诊器效应会严重影响听歌的感受。而在“真无线”耳机领域,除了苹果的AirPods外,可供选择的产品并不多,而AirPods又不是为运动场景打造的,防水能力非常差。那么对于喜欢运动又想要“自由”的朋友来说,有没有一款产品能够满足他们的需求呢?下面这十款小编专门为大家搜罗的蓝牙耳机或许就能找到适合的!网红击音F1_适合游戏与运动的高音质蓝牙耳机

iOS 17 测试版中 SwiftUI 视图首次显示时状态的改变导致动画“副作用”的解决方法-程序员宅基地

文章浏览阅读1k次,点赞6次,收藏7次。在本篇博文中,我们在 iOS 17 beta 4(SwiftUI 5.0)测试版中发现了 SwiftUI 视图首次显示时状态的改变会导致动画“副作用”的问题,并提出多种解决方案。

Flutter 自定义 轮播图的实现_flutter pageview轮播图 site:csdn.net-程序员宅基地

文章浏览阅读1.9k次。  在 上篇文章–Flutter 实现支持上拉加载和下拉刷新的 ListView 中,我们最终实现的效果是在 listView 上面留下了一段空白,本意是用来加载轮播图的,于是今天就开发了一下,希望能给各位灵感。一 、效果如下说一下大体思路   其实图片展示是用的 PageView ,然后,下面的指示器 是用的 TabPageSelector ,当然整体是用 Stack 包裹起来的。1、..._flutter pageview轮播图 site:csdn.net

推荐文章

热门文章

相关标签