Keywords in C
Keywords are reserved words in C that have special meanings and cannot be used as identifiers. In C, all keywords are in lowercase letters.
| auto | break | case | char |
| const | continue | default | do |
| double | else | enum | extern |
| float | for | goto | if |
| int | long | register | return |
| short | signed | sizeof | static |
| struct | switch | typedef | union |
| unsigned | void | volatile | while |
Data Types in C
C provides various data types to store different kinds of data. In C, data types are categorized into three main groups:
- Primary Data Types
- Derived Data Types
- User-Defined Data Types
Primary Data Types
Primary data types are the basic data types provided by C. They include:
| Data Type Keyword | Description | Size (bytes) | Range |
|---|---|---|---|
| int | Integer type | 4 | -2,147,483,648 to 2,147,483,647 |
| float | Floating-point type | 4 | Approximately ±3.4E±38 (7 digits precision) |
| double | Double-precision floating-point type | 8 | Approximately ±1.7E±308 (15 digits precision) |
| char | Character type | 1 | -128 to 127 (or 0 to 255 if unsigned) |
| void | No value type | N/A | N/A |
Derived Data Types
Derived data types are formed by combining primary data types. They include:
- Arrays
- Pointers
- Functions
- Structures
- Unions
User-Defined Data Types
User-defined data types are created by the programmer to suit specific needs. They include:
- enum (Enumerations) - enum data type is used to define a set of named integer constants. For example:
#include <stdio.h>Output:
int main() {
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
enum Day today = Wednesday;
printf("Day number: %d\n", today);
return 0;
}
Day number: 3 - typedef (Type Definitions) - typedef is used to create a new name for an existing data type. For example:
#include <stdio.h>Output:
typedef unsigned int uint;
int main() {
uint age = 25;
printf("Age: %u\n", age);
return 0;
}
Age: 25
Type Conversion
Converting one data type to another is called type conversion or type casting.
Implicit Type Conversion:The compiler automatically converts one data from type to another is called implicit type conversion. Compiler converts data from lower to higher data type.
For Example:int main() {
int a = 5;
float b;
b = a; // Implicit conversion from int to float
printf("Value of b: %f", b);
return 0;
}
When a programmer manually converts one data from type to another, it is called explicit type conversion or type casting.
For Example:int main() {
int numaretor = 5, denominator = 2;
float result;
result = (float)numaretor / denominator; // Explicit conversion from int to float
printf("Value of result: %f", result);
return 0;
}
Variables
A variable is a named location in memory used to store data values. In C, variables must be declared before they can be used. The declaration specifies the type and name of the variable.
Variable Declaration Syntax:Common Interview Questions
What is keyword?
A keyword is a reserved word that have special meaning in compiler.
Difference between type conversion and type casting?
- Type Conversion: The data is converted from one data type to another data type by the compiler at compile time.
- Type Casting: The data is converted from one data type to another data type by the programmer using casting operator ().
What is enum data type?
An enumeration(enum) is an user-defined data type that contains set of named integer constants.
What is an empty data type?
void
Difference between variable declaration, initialization and definition?
- Declaration - Creation of variable.
- Definition - Creation of variable and also provides implementation details.
- Initialization - Assigning value at the time of creating variable.