Are there any problems with performing mathematical operations on different variable types?


C has three categories of built-in data types: pointer types, integral types, and floating-point types. Pointer types are the most restrictive in terms of the operations that can be performed on them. They are limited to

- subtraction of two pointers, valid only when both pointers point to elements in the same array. The result is the same as subtracting the integer subscripts corresponding to the two pointers.

+ addition of a pointer and an integral type. The result is a pointer that points to the element which would be selected by that integer.

Floating-point types consist of the built-in types float, double, and long double. Integral types consist of char, unsigned char, short, unsigned short, int, unsigned int, long, and unsigned long. All of these types can have the following arithmetic operations performed on them:

+ Addition

- Subtraction

* Multiplication

/ Division

Integral types also can have those four operations performed on them, as well as the following operations: % Modulo or remainder of division

<< Shift left

>> Shift right

& Bitwise AND operation

| Bitwise OR operation

^ Bitwise exclusive OR operation

! Logical negative operation

~ Bitwise "one's complement" operation

Although C permits "mixed mode" expressions (an arithmetic expression involving different types), it actually converts the types to be the same type before performing the operations (except for the case of pointer arithmetic described previously). The process of automatic type conversion is called "operator promotion."


© 2017