Skip to content

0x341 C

1. Language

1.1. Types, Operators and Expressions

1.1.1. Data Types and Sizes

There are only a few basic data types in C - char: a single byte - int: an integer, typically reflecting the natural size of integers on the host machine - float: single-precision point - double: double-precision floating point

The sizes of these types are machine-dependent, for instance, int might be 16-bit depending on the machine you are using. To make sure the size of each type, we have a number of qualifiers to apply to these types

  • short: often 16 bits
  • long: ofteen 32 bits

short int can be abbreviated to short, long int can be abbreviated to long.

Note even those qualifiers are not fixing the size, it dependes on the hardware and compiler, although subject to a couple of constraints. Use limits.h and float.h to find the exact size.

1.1.2. Constants

Element (integer constant) If an integer constant begins with 0x or 0X, it is hexadecimal. If it begins with the digit 0, it is octal. Otherwise, it is assumed to be decimal.

Element (string constant, string literal) A string constant, or string literal, is a sequence of zero or more characters surrounded by double quotes The internal representation of string constant is an array of characters ending with a \0 at the end.

The physical storage requires one more byte than the sequence between the double quotes. For example

# this will take 6 bytes
"hello";

Standard library usually assumes the null is existing, for example, strlen returns the length of string by excluding the last \0, %s requires \0 to be exist to print.

However a char[] does not need to be end with 0, it has to be ending with 0 only when it is treated as a string literal

Pointers and Arrays

Quote

There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.

2. Library

3. Reference

[1] K&R

[2] Microsoft C Language Documentation

[3] A interview of Kernighan explaining history of C and K&R