Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C 数值数据类型


数值类型

当需要存储没有小数的整数时,使用 int,例如 35 或 1000;当需要存储浮点数(带小数)时,使用 floatdouble,例如 9.99 或 3.14515。

int

int myNum = 1000;
printf("%d", myNum);
亲自尝试 »

float

float myNum = 5.75;
printf("%f", myNum);
亲自尝试 »

double

double myNum = 19.99;
printf("%lf", myNum);
亲自尝试 »

float vs. double

浮点值的精度表示值在小数点后可以有多少位数字。 float 的精度为六到七位小数,而 double 变量的精度约为 15 位。 因此,在大多数计算中使用 double 通常更安全 - 但请注意,它占用的 内存float 的两倍(8 字节 vs. 4 字节)。

科学计数法

浮点数也可以是科学计数法,用 "e" 表示 10 的幂

示例

float f1 = 35e3;
double d1 = 12E4;

printf("%f\n", f1);
printf("%lf", d1);
亲自尝试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.