DEV Community

221910302043
221910302043

Posted on

Constants, Variables and Data types in C

CONSTANTS

A data item whose value cannot change during the program’s execution.
There are 4 types of constants in C.
1.Integer constants:
An integer constant is an integer quantity which contains a sequence of digits. It should not have a decimal point. Blanks and commas are not allowed within an integer constant. An integer constant can be either +ve or -ve. The constant must lie within the range of the declared data type (including qualifiers long, short etc.).

Example of valid integer constants:- 976 8987 5 -25 etc.
Example of invalid integer constants:- 78.43 7-8 89,76 etc.
2.Character constants:
A character constant is a character which is enclosed in single quotes. A character constant is of size 1byte and can contain only 1 character. A character can be an alphabet like a,b,A,C etc or a special character like &,^, $, #,@ etc or a single digit from 0 through 9. It can also be an escape sequence character like space ‘ ‘ or a null character ‘\o’ or a new line ‘\n’ etc.
Example: ‘A’ ‘a’ ‘ b’ ‘8’ ‘#’ etc.
3.Real/Floating point constants
They are also known as real constants. A real constant contains a decimal point or an exponent. It can be either +ve or -ve. Commas and blank space are not allowed within a real constant.
Examples:– 254.175, -16.47 -0.5e-7 +4.1e8
4.String constants:
A string constant is a collection of characters enclosed in double quotations “”.It may contain alphabets, digits, special characters and blank space.
Example:- “Circuits Today123”

VARIABLES

A variable is a data name that may be used to store a data value. >Eg: name, roll no.
Basically, a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations which can be applied to them.
Variable Declaration:
A typical variable declaration is of the form:
type variable_name;
or
for multiple variables:
type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. However, the name must not start with a number.

DATA TYPES

There are 4 data types in C language.
They are:-int
char
float
double
-->int – This data type is used to define an integer number. A single integer occupies 2 bytes.
Format Specifier:- %d
Date Range : 0 to 255
-->char – Used to define characters. A single character occupy 1 byte.
Format Specifier:- %c
Date Range : -128 to +127
-->float – Used to define floating point numbers (single precision). Occupies 4 bytes.
Format Specifier:- %f
Date Range: -3.4e38 to +3.4e38
-->double – Used for double precision floating point numbers(double precision).Occupies 8 bytes.
Format Specifier:-%lf
Date Range : -1.7e38 to +1.7e38
*Primitive data types are the first form – the basic data types (int, char, float, double).
*Derived data types are a derivative of primitive data types known as arrays, pointer and function.
*User defined data types are those data types which are defined by the user/programmer himself.

Top comments (0)