So what is a variable ?
In computer science (not only in C++) a variable is a storage location. In C++, every variable has a data type, a name and a value.
Exemple :
int val = 100;
double a;
Here we have :
- data type : int ; double
- name : val ; a
- value : 100 ; 0
By default values are set to 0.
What can the name of the variable be ?
First of all, the name need to respect some conventions or else your program will recieve a Compilation Error verdict.
Here are the conventions :
- Your variables must begin with a lowercase or uppercase letter.
- Each character except the first character must be either a number, a character (lowercase or uppercase) or a underscore.
The name of your variables should be meaningful because you need to know what is assigned to the variable.
It is recommended to put a uppercase letter in front of each word except the first one, for exemple :
int maxWeight = 42;
Your variables shouldn't be to long and shouldn't be too short. (about 3 - 11 characters)
int maxWeight = 42;
Your variables shouldn't be to long and shouldn't be too short. (about 3 - 11 characters)
Why do we need variables ?
Variables are extremely important because you can store a value and reuse this value afterwards. Without variables, you can basically do almost nothing.
What are the possible data types ?
The possible data types for decimal numbers are : bool, char, unsigned char, short, int, unsigned int, long long, unsigned long long, __int128_t, __m128d.
Data types for floating numbers are : float, double, long double and __float128.
Please note that unsigned integers do not support negative numbers (you'll get a random number if you use unsigned integers with negative numbers) ! So, be careful when you use them !
Here you can find the limits of these data types for decimal numbers except the last two :
These values can vary depending on your compiler and its version.
The main differences between these data types are precision and overflow limits.
Data types for floating numbers are : float, double, long double and __float128.
Please note that unsigned integers do not support negative numbers (you'll get a random number if you use unsigned integers with negative numbers) ! So, be careful when you use them !
Here you can find the limits of these data types for decimal numbers except the last two :
name | expresses | value* |
---|---|---|
CHAR_BIT | Number of bits in a char object (byte) | 8 or greater* |
SCHAR_MIN | Minimum value for an object of type signed char | -127 (-27+1 ) or less* |
SCHAR_MAX | Maximum value for an object of type signed char | 127 (27-1 ) or greater* |
UCHAR_MAX | Maximum value for an object of type unsigned char | 255 (28-1 ) or greater* |
CHAR_MIN | Minimum value for an object of type char | either SCHAR_MIN or 0 |
CHAR_MAX | Maximum value for an object of type char | either SCHAR_MAX or UCHAR_MAX |
MB_LEN_MAX | Maximum number of bytes in a multibyte character, for any locale | 1 or greater* |
SHRT_MIN | Minimum value for an object of type short int | -32767 (-215+1 ) or less* |
SHRT_MAX | Maximum value for an object of type short int | 32767 (215-1 ) or greater* |
USHRT_MAX | Maximum value for an object of type unsigned short int | 65535 (216-1 ) or greater* |
INT_MIN | Minimum value for an object of type int | -32767 (-215+1 ) or less* |
INT_MAX | Maximum value for an object of type int | 32767 (215-1 ) or greater* |
UINT_MAX | Maximum value for an object of type unsigned int | 65535 (216-1 ) or greater* |
LONG_MIN | Minimum value for an object of type long int | -2147483647 (-231+1 ) or less* |
LONG_MAX | Maximum value for an object of type long int | 2147483647 (231-1 ) or greater* |
ULONG_MAX | Maximum value for an object of type unsigned long int | 4294967295 (232-1 ) or greater* |
LLONG_MIN | Minimum value for an object of type long long int | -9223372036854775807 (-263+1 ) or less* |
LLONG_MAX | Maximum value for an object of type long long int | 9223372036854775807 (263-1 ) or greater* |
ULLONG_MAX | Maximum value for an object of type unsigned long long int | 18446744073709551615 (264-1 ) or greater* |
Source : cplusplus.com
These values can vary depending on your compiler and its version.
The main differences between these data types are precision and overflow limits.
Note that __int128_t, __float128 and __m128d are somehow a little special because they are GNU extensions. If you want to know more about their up and downs please consider looking at this article.
What is a global variable and a local variable ?
No comments:
Post a Comment