# Data Types
The header <stdint.h> provides several fixed-width integer type definitions. These types are optional and only provided if the platform has an integer type of the corresponding width, and if the corresponding signed type has a two’s complement representation of negative values.
See the remarks section for usage hints of fixed width types.
# Interpreting Declarations
A distinctive syntactic peculiarity of C is that declarations mirror the use of the declared object as it would be in a normal expression.
The following set of operators with identical precedence and associativity are reused in declarators, namely:
- the unary * "dereference" operator which denotes a pointer;
- the binary [] "array subscription" operator which denotes an array;
- the (1+n)-ary () "function call" operator which denotes a function;
- the () grouping parentheses which override the precedence and associativity of the rest of the listed operators.
The above three operators have the following precedence and associativity:
| Operator | Relative Precedence | Associativity |
|---|---|---|
| [] (array subscription) | 1 | Left-to-right |
| () (function call) | 1 | Left-to-right |
| * (dereference) | 2 | Right-to-left |
When interpreting declarations, one has to start from the identifier outwards and apply the adjacent operators in the correct order as per the above table. Each application of an operator can be substituted with the following English words:
| Expression | Interpretation |
|---|---|
| thing[X] | an array of size X of. |
| thing(t1, t2, t3) | a function taking t1 , t2 , t3 and returning. |
| *thing | a pointer to. |
It follows that the beginning of the English interpretation will always start with the identifier and will end with the type that stands on the left-hand side of the declaration.
# Examples
[] takes precedence over * , so the interpretation is: names is an array of size 20 of a pointer to char .
In case of using parentheses to override the precedence, the * is applied first: place is a pointer to an array of size 10 of char .
There is no precedence to worry about here: fn is a function taking long , short and returning int .
The () is applied first: fn is a function taking void and returning a pointer to int .
Overriding the precedence of () : fp is a pointer to a function taking void and returning int .
Multidimensional arrays are not an exception to the rule; the [] operators are applied in left-to-right order according to the associativity in the table: arr is an array of size 5 of an array of size 8 of int .
The two dereference operators have equal precedence, so the associativity takes effect. The operators are applied in right-to-left order: ptr is a pointer to a pointer to an int .
# Multiple Declarations
The declared objects in the above example are:
- fn : a function taking void and returning int ;
- ptr : a pointer to an int ;
- fp : a pointer to a function taking int and returning int ;
- arr : an array of size 10 of an array of size 20 of int ;
- num : int .
# Alternative Interpretation
Because declarations mirror use, a declaration can also be interpreted in terms of the operators that could be applied over the object and the final resulting type of that expression. The type that stands on the left-hand side is the final result that is yielded after applying all operators.
# Integer types and constants
Signed integers can be of these types (the int after short , or long is optional):
Each of these signed integer types has an unsigned version.
For all types but char the signed version is assumed if the signed or unsigned part is omitted. The type char constitutes a third character type, different from signed char and unsigned char and the signedness (or not) depends on the platform.
Different types of integer constants (called literals in C jargon) can be written in different bases, and different width, based on their prefix or suffix.
Decimal constants are always signed . Hexadecimal constants start with 0x or 0X and octal constants start just with a 0 . The latter two are signed or unsigned depending on whether the value fits into the signed type or not.
Without a suffix the constant has the first type that fits its value, that is a decimal constant that is larger than INT_MAX is of type long if possible, or long long otherwise.
The header file <limits.h> describes the limits of integers as follows. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown below, with the same sign.
| Macro | Type | Value |
|---|---|---|
| CHAR_BIT | smallest object that is not a bit-field (byte) | 8 |
| SCHAR_MIN | signed char | -127 / -(2 7 — 1) |
| SCHAR_MAX | signed char | +127 / 2 7 — 1 |
| UCHAR_MAX | unsigned char | 255 / 2 8 — 1 |
| CHAR_MIN | char | see below |
| CHAR_MAX | char | see below |
| SHRT_MIN | short int | -32767 / -(2 15 — 1) |
| SHRT_MAX | short int | +32767 / 2 15 — 1 |
| USHRT_MAX | unsigned short int | 65535 / 2 16 — 1 |
| INT_MIN | int | -32767 / -(2 15 — 1) |
| INT_MAX | int | +32767 / 2 15 — 1 |
| UINT_MAX | unsigned int | 65535 / 2 16 — 1 |
| LONG_MIN | long int | -2147483647 / -(2 31 — 1) |
| LONG_MAX | long int | +2147483647 / 2 31 — 1 |
| ULONG_MAX | unsigned long int | 4294967295 / 2 32 — 1 |
| Macro | Type | Value |
|---|---|---|
| LLONG_MIN | long long int | -9223372036854775807 / -(2 63 — 1) |
| LLONG_MAX | long long int | +9223372036854775807 / 2 63 — 1 |
| ULLONG_MAX | unsigned long long int | 18446744073709551615 / 2 64 — 1 |
If the value of an object of type char sign-extends when used in an expression, the value of CHAR_MIN shall be the same as that of SCHAR_MIN and the value of CHAR_MAX shall be the same as that of SCHAR_MAX . If the value of an object of type char does not sign-extend when used in an expression, the value of CHAR_MIN shall be 0 and the value of CHAR_MAX shall be the same as that of UCHAR_MAX .
The C99 standard added a new header, <stdint.h> , which contains definitions for fixed width integers. See the fixed width integer example for a more in-depth explanation.
# Floating Point Constants
The C language has three mandatory real floating point types, float , double , and long double .
The header <float.h> defines various limits for floating point operations.
Floating point arithmetic is implementation defined. However, most modern platforms (arm, x86, x86_64, MIPS) use IEEE 754
(opens new window) floating point operations.
C also has three optional complex floating point types that are derived from the above.
# String Literals
A string literal in C is a sequence of chars, terminated by a literal zero.
String literals are not modifiable (and in fact may be placed in read-only memory such as .rodata). Attempting to alter their values results in undefined behaviour.
Multiple string literals are concatenated at compile time, which means you can write construct like these.
String literals, same as character constants, support different character sets.
Какая разница между int main и void main?
В чем разница между int main()<> и int main(void)<>?
Только недавно начал изучать язык, и появилось несколько вопросов: 1) в чем разница между int.
Какой новые синтаксис: int main() VS. void main()
Долго не занимался программированием , но тут жена решила поучится , да и сам повторить ,так вот.
int main() или int main(void)?
чем отличается : не сообщаю о наличии аргументов функция не принимает никаких аргументов.
int main обязан возвращать целочисленное значение, void ничего не возвращает
Сообщение от valeriy007
Сообщение от ValeryS
а теперь правильный ответ:
по стандарту должент быть int main
void main — расширение компилятора вижал студии.
но такой код не портируем. гсс его уже кушать не будет, например.
при этом сама функция main не обязана возвращать значение:
по стандарту,
если программист явным образом не напишет возвращаемое значение,
программа вернет ноль.
Чем отличается в c++ int main() от void main().
Т. е. точка выхода.
Можно ее пропустить, конечно, тогда компилятор автоматически добавит return 0; в конец программы.
Это значение передается вызывающему процессу (например, операционной системе) чтобы он «понял», что программа завершена корректно.
Второй вариант:
void main()
по завершении программы ничего не возвращает, что может привести к различным проблемам.
Это можно обойти с помощью функции exit(<число>);
Но тем не менее, запись void main() не соответствует стандарту и считается некорректной, и некоторые компиляторы ее не принимают.
Первый вариант может возвращать код ошибки.
Второй вариант не может возвращать код ошибки.
Первый вариант будет компилироваться в любом нормальном компиляторе.
Второй вариант будет редко где компилироваться.
Data Types in C — Integer, Floating Point, and Void Explained

There are several different ways to store data in C, and they are all unique from each other. The types of data that information can be stored as are called data types. C is much less forgiving about data types than other languages. As a result, it’s important to make sure that you understand the existing data types, their abilities, and their limitations.
One quirk of C’s data types is that they depend entirely on the hardware that you’re running your code on. An int on your laptop will be smaller than an int on a supercomputer, so knowing the limitations of the hardware you’re working on is important. This is also why the data types are defined as being minimums- an int value, as you will learn, is at minimum -32767 to 32767: on certain machines, it will be able to store even more values that this.
There are two categories that we can break this into: integers, and floating point numbers. Integers are whole numbers. They can be positive, negative, or zero. Numbers like -321, 497, 19345, and -976812 are all perfectly valid integers, but 4.5 is not because 4.5 is not a whole number.
Floating point numbers are numbers with a decimal. Like integers, -321, 497, 19345, and -976812 are all valid, but now 4.5, 0.0004, -324.984, and other non-whole numbers are valid too.
C allows us to choose between several different options with our data types because they are all stored in different ways on the computer. As a result, it is important to be aware of the abilities and limitations of each data type to choose the most appropriate one.
Integer data types
Characters: char
char holds characters- things like letters, punctuation, and spaces. In a computer, characters are stored as numbers, so char holds integer values that represent characters. The actual translation is described by the ASCII standard. Here’s a handy table for looking up that.
The actual size, like all other data types in C, depends on the hardware you’re working on. By minimum, it is at least 8 bits, so you will have at least 0 to 127. Alternatively, you can use signed char to get at least -128 to 127.
Standard Integers: int
The amount of memory that a single int takes depends on the hardware. However, you can expect an int to be at least 16 bits in size. This means that it can store values from -32,768 to 32,767, or more depending on hardware.
Like all of these other data types, there is an unsigned variant that can be used. The unsigned int can be positive and zero but not negative, so it can store values from 0 to 65,535, or more depending on hardware.
Short integers: short
This doesn’t get used often, but it’s good to know that it exists. Like int, it can store -32768 to 32767. Unlike int, however, this is the extent of its ability. Anywhere you can use short , you can use int .
Longer integers: long
The long data type stores integers like int , but gives a wider range of values at the cost of taking more memory. Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647. Alternatively, use unsigned long for a range of 0 to 4,294,967,295.
Even longer integers: long long
The long long data type is overkill for just about every application, but C will let you use it anyway. It’s capable of storing at least −9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. Alternatively, get even more overkill with unsigned long long , which will give you at least 0 to 18,446,744,073,709,551,615.
Floating point number data types
Basic Floating point numbers: float
float takes at least 32 bits to store, but gives us 6 decimal places from 1.2E-38 to 3.4E+38.
Doubles: double
double takes double the memory of float (so at least 64 bits). In return, double can provide 15 decimal place from 2.3E-308 to 1.7E+308.
Getting a wider range of doubles: long double
long double takes at least 80 bits. As a result, we can get 19 decimal places from 3.4E-4932 to 1.1E+4932.
Picking the right data type
C makes pick the data type, and makes us be very specific and intentional about the way that we do this. This gives you a lot of power over your code, but it’s important to pick the right one.
In general, you should pick the minimum for your task. If you know you’ll be counting from integer 1 to 10, you don’t need a long and you don’t need a double. If you know that you will never have negative values, look into using the unsigned variants of the data types. By providing this functionality rather than doing it automatically, C is able to produce very light and efficient code. However, it’s up to you as the programmer to understand the abilities and limitations, and choose accordingly.
We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:
Output:
The Void type
The void type specifies that no value is available. It is used in three kinds of situations:
1. Function returns as void
There are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);
2. Function arguments as void
There are various functions in C which do not accept any parameter. A function with no parameter can accept a void. For example, int rand(void);
3. Pointers to void
A pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size); returns a pointer to void which can be casted to any data type.