博客
关于我
Variables and Types 变量和类型
阅读量:192 次
发布时间:2019-02-28

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

 


Data types   数据类型

C has several types of variables, but there are a few(一些) basic types:

  • Integers (整型)- whole numbers which can be either positive(正数) or negative(负数)(要么...要么). Defined using charintshortlong or long long.

  • Unsigned integers(无符号整型) - whole numbers which can only be positive. Defined  using unsigned char,  unsigned int,   unsigned shortunsigned long or unsigned long long.

  • Floating point numbers(浮点数) - real numbers实数 (numbers with fractions). Defined using float and double.

  • Structures(结构体) - will be explained later, in the Structures section.(稍后将在“结构”一节中解释。)
     

The different types of variables define their bounds(界限). A char can range(范围) only from -128 to 127, whereas a long can range from -2,147,483,648 to 2,147,483,647 (long and other numeric data types(数字数据类型) may have another range on different computers, for example - from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit computer).

Note that C does not have a boolean type(布尔类型). Usually, it is defined using the following notation(符号):

#define BOOL char#define FALSE 0#define TRUE 1

C uses arrays of characters(字符数组) to define strings(字符串), and will be explained in the Strings section.

Defining variables  定义变量

For numbers(数字), we will usually use the type int, which an integer in the size of a "word"(字)the default number size of the machine which your program is compiled on. On most computers today, it is a 32-bit number, which means the number can range from -2,147,483,648 to 2,147,483,647.

To define the variables foo and bar, we need to use the following syntax(语法):

int foo;int bar = 1;

The variable foo can be used, but since we did not initialize(初始化) it, we don't know what's in it. The variable bar contains(包含) the number 1.

Now, we can do some math. Assuming(假设) abcd, and e are variables, we can simply use plus, minus and multiplication(乘法) operators in the following notation, and assign(分配,赋值) a new value to a:

int a = 0, b = 1, c = 2, d = 3, e = 4;a = b - c + d * e;printf("%d", a); /* will print 1-2+3*4 = 11 */

Exercise

In the next exercise, you will need to create a program which prints out the sum of the numbers ab, and c.

原:

#include 
int main() { int a = 3; float b = 4.5; double c = 5.25; float sum; /* Your code goes here */ printf("The sum of a, b, and c is %f.", sum); return 0;}

 

改:

#include 
int main() { int a = 3; float b = 4.5; double c = 5.25; float sum; /* Your code goes here */ sum = a + b + c ; printf("The sum of a, b, and c is %f.", sum); return 0;}

 

 

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

你可能感兴趣的文章
MySQL报错ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘
查看>>
Mysql报错Packet for query is too large问题解决
查看>>
mysql报错级别_更改MySQL日志错误级别记录非法登陆(Access denied)
查看>>
Mysql报错:too many connections
查看>>
MySQL报错:无法启动MySQL服务
查看>>
mysql授权用户,创建用户名密码,授权单个数据库,授权多个数据库
查看>>
mysql排序查询
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
Mysql推荐书籍
查看>>
Mysql插入数据从指定选项中随机选择、插入时间从指定范围随机生成、Navicat使用存储过程模拟插入测试数据
查看>>
MYSQL搜索引擎
查看>>
mysql操作数据表的命令_MySQL数据表操作命令
查看>>
mysql操作日志记录查询_如何使用SpringBoot AOP 记录操作日志、异常日志?
查看>>
MySQL支持的事务隔离级别,以及悲观锁和乐观锁的原理和应用场景?
查看>>
mysql支持表情
查看>>
MySQL支撑百万级流量高并发的网站部署详解
查看>>
MySQL改动rootpassword的多种方法
查看>>
mysql数据分组索引_MYSQL之索引配置方法分类
查看>>
mysql数据取差,mysql屏蔽主外键关联关系
查看>>