basic_data_types

basic_data_types

Fundamentally c only really accepts integers (signed and unsigned) as well as floats.

short and longs and their use case

long:~ more space to store an integer short:~ less space to save memory

all non-trivial combinations of basic data types, e.g. shorts, ints

Here are all the non-trivial combinations

short int
unsigned short int

int
unsigned int

long int
unsigned long int

note, combinations like long signed int is the same as long int

You may omit the int at the end as it is not mandatory.

By the spec, we need short <= int <= long but that means we can have short = int = long

We can check the <limits.h> to determine the ranges on your machine.

long long ints are a c99 feature

size of shorts, chars

shorts hold between ±32, 768

chars hold 128 characters, and are 7-bit.

constants, with assigned datatypes

L:= The constant is of type long, as well as long doubles. U:= Unsigned, can be used in combination with L, like UL LL:= The constant is of type long long.

floats can be represented in the following ways. 57.0 57. 570e0 57E0 5.7e1 5.7e+1 .57e2 570.e-1 // all are 57.0

F:= float constants. l

The exponent is the power of 10 that the number is scaled.

integer overflow, when is it defined and when is it undefined?

When the variable is asked to store a number larger than it can, an integer overflow happens. It has undefined behaviour with signed integers, but defined behaviour with unsigned. With unsigned we get the correct answer modulo 2n where n is the number of bits used to store the result.

octal and hexadecimal numbers

Decimals must not begin with a 0. Octals must begin with a 0 (this are base 8 numbers). Hexadecimals must begin with a 0x

how do I read and write integers?

for unsigned integers

instead of %d use %u for base 10, %o for octal, %x for hexadecimal.

for short integers, and long integers in decimal octal or hex

for shorts, prepend with h, e.g. %hd, %hu, %ho, %hx

for longs, prepend with l as for shorts, and long longs, prepend with ll

what are the three floating point types?

floa := single precision floating point doubl := double precision floating point. Enough for most programs. long doubl := Extended precision floating points. Rarely needed.

In c99 we also have complex variants of float, double and long double.

Char

Ascii is a 128 character set Latin-1 is a 256 character set

Characters are small integers. Use single quotes.

Look at the following UNPORTABLE code.

if ('a' <= ch && ch <= 'z'){
    ch = ch - 'a' + 'A';
}

for (ch = 'A'; ch <= 'Z'; ch++) //iterate from A to Z

If you are using char to store a 256 bit integer, you may want to specify if it is unsigned or signed.

c

backlinks