Home » C Programming » Tutorial » Variables & Datatypes
C Programming variables, Constants & Data Types
Variables - Variables are simply names used to refer to some location in memory – a location that holds a value with which we are working. It may help to think of variables as a placeholder for a value.
Constants - Constants refer to fixed values that the program may not alter(change) during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant.
Data Types - It defines the type of data any variable is going to store. Data types are Predefined & User Defined. Detailed list of Data Type -
Predefined Data Types
Data Type | Storage Size | Range |
---|---|---|
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 8 bytes | -9223372036854775808 to 9223372036854775807 |
unsigned long | 8 bytes | 0 to 18446744073709551615 |
float | 4 byte | 1.2E-38 to 3.4E+38 [Precision - 6 decimal places] |
double | 8 byte | 2.3E-308 to 1.7E+308 [Precision - 15 decimal places] |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 [Precision - 19 decimal places] |
User Defined Data Types
Use defined data types are Structure, Union, and Enumeration. Size of each user defined data types depends on the size of their memebers. We will discuss size of each user defined data type in details here -
Size of Structure
To calculate size of structure let create one basic example here
#include struct student{ int age; int std; }; int main() { printf("\n Size of student is : %d bytes ", sizeof(struct student)); return 0; }
Size of union
To calculate size of union let create one basic example here
#include union student{ int age; int std; }; int main() { printf("\n Size of student is : %d bytes ", sizeof(union student)); return 0; }
Size of enum
To calculate size of enum let create one basic example here
#include enum BMWcolor{ RED,BLUE, BLACK }; void main() { printf("\n Size of student is : %d bytes ", sizeof(enum BMWcolor)); }
NOTE - sizeof() is open operator in C programming, which can be used to calculate size of predefined & user defined data types.
C Tutorial
- History of C Language
- The First Program in C
- Compilation & Execution
- Compile C on Dev-C++
- Compile C on GCC Linux
- Variable & Data types
- Comments
- Storage Classes
- Conidtional Statements
- Switch Cases
- Loops (for, while, do-while)
- Arrays
- Pointers
- Function Pointer
- Strings & Library func
- Formated I/O
- Structure
- Enum
- Union
- File I/O
- Memory Management
- Error Handling
- Type Casting