Detailed Explanation of Different Data Types with Examples & Errors of C Programming
DATA TYPES
Data types in C programming language enables the programmers to appropriately select the data as per requirements of the program and the associated operations of handling it.
C provides 5 fundamental data types- int, float, double, char, and void.
1. int (Integer data type)
The integer data type is used to declare a variable that can store numbers without a decimal.
Syntax: int variable_name;
e.g.: int a;
int x,y;
2. float (Float data type)
Float data type declares a variable that can store numbers containing a decimal number.
Syntax: float variable_name;
e.g.: float c;
3. double (Double data type)
Double data type also declares a variable that can store floating-point numbers. But it is treated as a distinct data type because it occupies twice as much memory as float and can store floating-point numbers with a much larger range and double precision. Thus, a double data type is also referred to as a double-precision data type.
Syntax: double variable_name;
e.g.: double z;
4. char (Character data type)
Character data type allows a variable to store only one character. Character is enclosed by single quotes in C.
Syntax: char variable_name;
e.g.: char c= ‘Y’;
5. void
Unlike other primitive data types in c, void data type does not create any variable but returns an empty set of values. Thus, we can say that it stores null.
Syntax: void variable_name;
VARIABLES
A variable is a name given to the memory location which holds the value of the variable for later use. Each variable in C has a specific data type, which determines the size of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. The value of a variable can be changed later in the program.
Rules for naming C variable:
1. A variable name must begin with a letter or underscore.
2. Variables are case sensitive
3. They can be constructed with digits, letters.
4. No special symbols are allowed other than underscores.
total, sum, address1, age_value are some examples for the variable name.
VARIABLE DECLARATION & INITIALIZATION:
All variables must be declared before they are used. More than one variable of the same data type can be declared in a single statement.
Syntax: data_type variable _name;
Example: int x; float y; char a, b;
Variable initialization means assigning a value to the variable.
Syntax: variable _name=value;
Example: x=5; y=3.14; a=‘y’, b=‘n’;
Both declaration and initialization can be done in a single statement also.
Syntax: data _ type variable _name=value;
Example: int x=5; float y=3.14; char a=‘y’, b=‘n’;
TYPES OF ERRORS
Syntax errors:
Like the English language having grammar, every programming language has its own set of rules or syntax. Syntax error occurs if the rules of a programming language are misused. Syntax errors are detected at compile - time.
Run-time errors:
Run-time errors occur during the execution of a program. It is mainly caused because of some illegal operation or unavailability of desired conditions for the execution of the program.
e.g.: Trying to divide a number by zero, insufficient memory, infinitive loop, etc.
Related Topic: List of C Header File with Detailed Explanations