2.8. Data Types, Variables and Constants
DATA TYPE
The data type in C defines the amount of storage allocated to variables, the values that they can accept, and the operation that can be performed on those variables.C is rich in data types. The variety of data type allow the programmer to select appropriate data type to satisfy the need of application as well as the needs of different machine.There are following type of data types supported by C programming:
- Primary Data Type
- Derived Data Type
- User Defined Data Type
Primary (Fundamental) Data type
All C compiler support following fundamental data type:
Integer Type
Integers are the whole numbers, i.e. non-fractional numbers. They are defined in c by keyword int. Generally integers require 16 bit of storage. Integers can be of following types:
-32768 to +32768: range of signed integer 0 to 65535: range of unsigned integer |
Following are the rules for constructing integer constants in C:
- Digits:
An integer constant is a sequence of digits (0-9) without any decimal point. - Sign:
- An integer constant may have an optional sign (+ or -) before the digits.
- Base:
- If an integer constant begins with '0' (zero), it is treated as an octal constant. If it begins with '0x' or '0X', it is treated as a hexadecimal constant.
- Suffixes:
- Integer constants can have optional suffixes to specify the type:
- u or U for unsigned (e.g., 10U)
- l or L for long (e.g., 20L)
- ul, LU, uL, or Ul for unsigned long (e.g., 30UL)
- Integer constants can have optional suffixes to specify the type:
- Range:
- The range of values an integer constant can represent depends on the size of the integer data type used to store it.
- Examples:
- Valid integer constants: 10, -20, 0, 077 (octal), 0x1A (hexadecimal), 123456789L (long), 42U (unsigned), 0xFFLU (hexadecimal and unsigned long).
- No Commas:
- Commas cannot be used in integer constants. For example, 1,000 is not a valid integer constant; use 1000 instead.
- No Spaces:
- Spaces are not allowed within integer constants.
- Overflow:
- Be cautious about potential integer overflow. If an integer constant exceeds the range of the data type it's assigned to, it may lead to unexpected behavior.
Floating Point Types
Floating types are fractional numbers (i.e. real numbers). They are defined in c by keyword float. Generally floating numbers reserve 32 bits of storage.
Following the rules for constructing floating-point constants in C:
- Decimal Point:
- A floating-point constant must have a decimal point to distinguish it from an integer constant.
- Digits:
- It consists of a sequence of digits (0-9) before and after the decimal point.
- Sign:
- An optional sign (+ or -) may precede the entire constant.
- Exponent:
- An optional exponent part, consisting of the letter 'e' or 'E' followed by an integer, can be used to represent numbers in scientific notatio
- An optional exponent part, consisting of the letter 'e' or 'E' followed by an integer, can be used to represent numbers in scientific notatio
- Suffixes:
- Floating-point constants can have optional suffixes to specify the type:
- f or F for float (e.g., 3.14f)
- l or L for long double (e.g., 2.71828L)
- Examples:
- Valid floating-point constants: 3.14, -2.0, 0.001, 1.23e-5, 2.5f (float), 4.0L (long double).
- No Commas:
- Commas cannot be used in floating-point constants. For example, 1,000.0 is not a valid floating-point constant; use 1000.0 instead.
- No Spaces:
- Spaces are not allowed within floating-point constants.
Character Constants
Character constants are single alphabets,digits or special symbols enclosed within the single quote.
Example:
'a' 'b' '1' '$' |
Following are the rules for constructing character constants in C:
- Enclosure:
- Character constants are enclosed in single quotes ('). For example: 'A', '7', '%'.
- Single Character:
- A character constant must represent a single character. For example, 'AB' is not a valid character constant.
- Escape Sequences:
- Special characters and non-printable characters can be represented using escape sequences. For example:
- '\n' represents a newline character.
- '\t' represents a tab character.
- '\'' represents a single quote.
- '\\' represents a backslash.
- Character Code:
- You can represent characters using their ASCII codes by using the \x or \u or \U escape sequences. For example:
- '\x41' represents the character 'A'.
- '\u0031' represents the digit '1'.
- '\U0001F609' represents the Unicode character '????'.
- Empty Character Constant:
- An empty character constant, like '', is not allowed. A character constant must represent an actual character.
- Single Quotes Within Character Constant:
- To include a single quote within a character constant, use the \' escape sequence. For example: 'It\'s'.
- Examples:
- Valid character constants: 'A', '5', '\n', '\x41', '\u0031'.
- Size:
- The size of a character constant is 1 byte
ASCII VALUE
In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself. This integer value is the ASCII code of the character. For example, the ASCII value of 'A' is 65.Following table shows characters and its corresponding ASCII codes:
Write a C program to print the range of signed and unsigned integers.
#include <stdio.h>
#include <limits.h>
int main() {
printf("Range of signed char: %d to %d\n", SCHAR_MIN, SCHAR_MAX);
printf("Range of unsigned char: 0 to %u\n", UCHAR_MAX);
printf("Range of signed short: %d to %d\n", SHRT_MIN, SHRT_MAX);
printf("Range of unsigned short: 0 to %u\n", USHRT_MAX);
printf("Range of signed int: %d to %d\n", INT_MIN, INT_MAX);
printf("Range of unsigned int: 0 to %u\n", UINT_MAX);
printf("Range of signed long: %ld to %ld\n", LONG_MIN, LONG_MAX);
printf("Range of unsigned long: 0 to %lu\n", ULONG_MAX);
return 0;
}
Output:
Range of signed char: -128 to 127
Range of unsigned char: 0 to 255
Range of signed short: -32768 to 32767
Range of unsigned short: 0 to 65535
Range of signed int: -2147483648 to 2147483647
Range of unsigned int: 0 to 4294967295
Range of signed long: -9223372036854775808 to 9223372036854775807
Range of unsigned long: 0 to 18446744073709551615
Write a C program to print the size of basic data types: char, int and float
#include <stdio.h>
int main() {
printf("Size of char: %zu bytes\n", sizeof(char));
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
return 0;
}
Output:
Size of char: 1 bytes
Size of int: 4 bytes
Size of float: 4 bytes
Write a C program to print the ASCII value of a given character.
#include <stdio.h>
int main() {
char ch = 'a';
printf("\nThe given character is: %c\n ", ch);
printf("\nThe ASCII of given character is: %d\n", ch);
return 0;
}
Output:
The given character is: a
The ASCII of given character is: 97