C 1.6 Types

1.6 Types

 

A machine has no concept of type, everything is just a sequence of bits, and any operation with those sequences can be done, even if it is not meaningful at all, for example adding two addresses, or multiplying two character strings.

 

机器没有类型的概念,所有的只是位的序列,任何序列都是可行的,即使它是没有任何意义的,例如两个地址相加,或者两个字符串相乘。

 

A high level programming language however, enforces the concept of types of data. Operations are allowed between compatible types and not between any data whatsoever. It is possible to add two integers, or an integer and a floating point number, and even an integer and a complex number. It is not possible to add an integer to a function or to a character string, the operation has no meaning for those types.

 

一种高级编程语言,强制数据类型的概念。操作只允许在兼容的类型中进行,不允许在不兼容的数据中。可以把两个整型相加,或者一个整型和一个浮点型,又或者一个整型和一个复数。不可以把一个整型和一个字符串相加,操作是没有意义的。

 

An operation implies always compatible types between the operands.

 

In C, all data must be associated with a specific type before it can be used. All variables must be declared to be of a known type before any operation with them is attempted since to be able to generate code the compiler must know the type of each operand.

 

在C语言中,所有数据在使用之前,必须与特定的类型联系。所有变量在对他进行操作之前,必须声明为一个已知的类型,因为编译器产生编码必须知道每个运算子类型。

 

C allows the programmer to define new types based on the previously defined ones. This means that the type system in C is static, i.e. known at compile time, but extensible since you can add new types.

This is in contrast to dynamic typing, where no declarations are needed since the language associates types and data during the run time. Dynamic typing is much more flexible, but this flexibility has a price: the run time system must constantly check the types of the operands for each operation to see if they are compatible. This run-time checking slows down the program considerably.

 

C允许程序员在预定义类型的基础上定义新的类型。这意味着在C中,类型是静态的,在编译时是已知的,但是可扩展的,因为可以增加新的类型。这与动态类型对比。运行时检查使程序变慢。

 

In C there is absolutely no run time checking in most operations, since the compiler is able to check everything during the compilation, which accelerates the execution of the program, and allows the compiler to discover a lot of errors during the compilation instead of crashing at run time when an operation with incompatible types is attempted.

 

在C中,许多操作中,绝对没有运行时检查,因为在编译期间编译器能够做检查。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值