Constant Variables and Data Types: In the C programming language, variables, constants, and data types are fundamental concepts used for storing and manipulating data within a program.
In the C programming language, a constant is a value that cannot be altered or modified during the execution of a program. Constants are used to represent fixed, unchanging values that remain the same throughout the program’s execution. They are typically used in place of literal values to improve code readability, maintenance, and to avoid accidental modifications.
Integer constants take one of the following forms:
- A decimal number, eg, 1, 123, 100002.
- An octal number, eg. 01, 0173, 0303242. An octal constant is introduced by a leading 0 and the digits are 0 through 7.
- A hexadecimal number, e.g., 0x1, 0X7b, 0x186A2. A hex constant is introduced by a leading 0x or 0X and the digits are 0 through 9 followed by A through F (note that the letters can be upper or lower case).
- A character constant (see below).
Integer constants can be preceded by a sign: either + or -. E.g., +0x1, -123.
Integer constants can also be suffixed with a U (or u) or L (or I), which is used to indicate that the constant is unsigned or long, respectively. These suffixes may be combined. E.g., 1L is a long integer constant with the value 1, 0xffffffffUL and 123lu are unsigned long integer constants, and 123u is an unsigned integer constant.
Floating Point Constants
Floating point constants take one of the following forms:
- A decimal number followed by a decimal point followed by another decimal number, e.g., 1.0, 1.23, 100.002.
- A number like the previous, but with an exponent part, i.e., an e or E followed by a (possibly signed) decimal number. E.g., 1.0e10, 1.23e-2, 1.002e+2. Note that if an exponent is specified, a decimal is not needed, e.g., 12e10. Floating point constants can also be preceded by a sign: either + or -. E.g., +1.0, -3.182e+4.
A character constant is a character enclosed in single quotes, e.g., ‘a’, ‘2’, ‘.’. Remember that in C, characters are small integers, so you can use a character constant anywhere you can use an integer constant and vise versa (see the ascii table for the character/number mapping). In a character constant (and a string constant), a backslash character is treated specially: it introduces an escape sequence. These escape sequences are usually used to represent characters that are not easy to see when represented by themselves (e.g., newlines, tabs, carriage returns, etc.), or need to be escaped for syntax reasons (e.g., single quote, double quote, backslash, etc.), There are three kinds of escape sequences:
- a backslash followed by up 3 octal numbers (leading 0 not needed), e.g., ‘\1012’ and ‘\176’ are the newline and tilde (~) characters respectively .
- a backslash followed by an x (or a X) and a hexadecimal number,
- a backslash followed by one of the following characters:
|\||the backslash character (octal 134)|
|‘||the single quote character (octal 047)|
|“||the double quote character (octal 042)|
|?||the question mark character (octal 077)|
|a||the alarm or bell character (octal 007)|
|b||the backspace character (octal 010)|
|f||the formfeed character (octal 006)|
|n||the newline character (octal 012)|
|r||the carriage-return character (octal 015)|
|t||the tab character (octal 011)|
String constants are sequences of characters enclosed in double quotes. The same backslash sequences that are used in character constants can be used in string constants. E.g., “hello there”, “a newline: \n”, “a string\nwith multiple\nlines”. If a string is long and you want to break it up across several lines of code, you can put a backslash just before a (real) newline and the C compiler will throw away both the backslash and the newline, e.g.,
printf(“A very long help message\that is more readable if it is broken up over\n\several lines.\n”);
this string contains one real newline (the \n).
Every C program contains a number of building blocks known as functions. Each function of it performs a task independently. A function is a subroutine that may consist of one or more statements. A C program comprises different sections.
When a program is executed, many operations are carried on the data. These data are stored in the memory and at the time of execution different operations are performed on them. A variable is a data named used for storing a data value. Its value may be changed during the program execution. A variable name may be declared based on the meaning of the operation. Ex:- height, average, sum etc.
Rules for defining Variables:-
- They must begin with a character without spaces but underscore is permitted.
- The length of the variable varies from compiler to compiler. Generally most of the compilers support 8 characters excluding extension.
- The variable should not be a keyword.
- The variable name may be a combination of upper and lower characters.
- The variable name should not start with a digit.
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called program. A program usually contains different types of data types (integer, float, character etc.) and need to store the values being used in the program. C language is rich of data types. A C programmer has to employ proper data type as per his requirement. C has different data types for different types of data and can be broadly classified as:
- Primary data types
- Secondary data types
Primary Data Types
Secondary Data Types
The C programming language has an extensive system for declaring variables of different types. The rules for the more complex types can be confusing at times due to the decisions taken over their design. The principal decision is that the declaration of a variable should be similar syntactically to its use (declaration reflects use). This article presents a collection of variable declarations, starting at simple types, and proceeding to more complex types.
There are four basic types of variable in C; they are: char, int, double, and float.
|char||The most basic unit addressable by the machine; typically a single octet(one byte). This is an integral type.|
|int||The most natural size of integer for the machine; typically a whole 16, 32 or 64-bit(2, 4, or 8 bytes respectively) addressable word.|
|float||A single-precision floating point value.|
|double||A double-precision floating point value.|
To declare a variable of any of these basic types, the name of the type is given first, then the name of the new variable second.
Various qualifiers can be placed on these basic types, in order to further describe their type.
unsigned int x;
signed int y;
int z; /* Same as “signed int” */
unsigned char grey;
signed char white;
Unsigned integers can only take non-negative values (positive or zero), while signed integers (default) can take both positive and negative values. The advantage of unsigned integers is to allow a greater range of positive values (e.g., 0 to +65535 depending on the size of the integer), whereas signed integers allow only up to half the same number as positive integers and the other half as negative integers (e.g., -32768 to+32767)
An unsigned character, depending on the code page, might access an extended range of characters from 0 to +255, instead of that accessible by a signed char from -128 to +127, or it might simply be used as a small integer. The standard requires char, signed char, unsigned char to be different types. Since most standardized string functions take pointers to plain char, many C compilers correctly complain if one of the other character types is used for strings passed to these functions.
The int type can also be given a size qualifier, to specify more precisely the range of values (and memory size requirements) of the value stored.
short int yellow.
long int orange;
long long int red;
When declaring a short int, long int or long long int, it is permissible to omit the int, as this is implied. The following two declarations are equivalent.
long int brown,
The standard is deliberately vague about the sizes of common types. The minimum ranges for common types are given: (Note: On most implementations the maximum negative value for signed types is one higher than the maximum positive, lacking a negative 0)
|Type||Minimum allowed range||Typical allowed range||Typical size in bytes|
|char||-127 to +127 or 0 to +255||-128 to +127 or 0 to +255||1|
|signed char||-127 to +127||-128 to +127||1|
|unsigned char||0 to +255||0 to +255||1|
|signed short int||-32767 to +32767||-32768 to +32767||2|
|unsigned short int||0 to +65535||0 to +65535||2|
|signed int||-32767 to +32767||-32768 to +32767 (antique systems) -2147483648 to +2147483647||2 or 4|
|unsigned int||0 to +65535||0 to +65535 (antique systems) 0 to +4294967295||2 or 4|
|signed long int||-2147483647 to +2147483647||-2147483648 to +2147483647 -9223372036854775808 to +9223372036854775807 (64-bit systems)||4 or 8|
|unsigned long int||0 to +4294967295||0 to +4294967295 |
0 to +18446744073709551615 (64-bit systems)
|4 or 8|
|signed long long int||-9223372036854775807 to +9223372036854775807||-9223372036854775808 to +9223372036854775807||8|
|unsigned long long int||0 to +18446744073709551615||0 to +18446744073709551615||8|
|float||1×10-37 to 1×1037||1×10-37 to 1×1037||4|
|double||1×10-37 to 1×1037||1×10-308 to 1×10308||8|
|long double||1×10-37 to 1×1037||1×10-308 to 1×10308 1×10-4932 to 1×104932 (x87 FPU systems)||8 or 12|