Data Types in C
In C programming, data types specify how the data is to be stored and manipulated.
The basic C data types are:
-
int:
int
is used to store integer values (whole numbers). It typically occupies 4 byte of memory. -
float:
float
is used to store floating-point numbers (numbers with decimal points), typically providing 6 decimal digits of precision. It typically occupies 4 byte of memory. -
double:
double
is used to store floating-point numbers with double precision, typically providing 15 decimal digits of precision. It typically occupies 8 byte of memory. -
char:
char
is used to store single characters (like letters, digits, symbols). It typically occupies 1 byte of memory. -
_Bool:
_Bool
is used for 'True' or 'False' (0 or 1). 0 represents false and non-zero represents true.
Derived data types are:
- Arrays: An array is a collection of items of the same data type stored under a single name.
- Pointers: Pointers store memory addresses of variables. They are used for advanced memory management.
- Structures: Structures allow you to group variables of different data types under a single name, creating user-defined data types.
- Unions: Unions are similar to structures, but all members share the same memory location. This is useful when you know only one member will have a value at a time.
Other data types:
-
void:
void
indicates the absence of a value. It's often used for functions that don't return any value.
Create Integer Data Type in C
To define an integer data type, use int
keyword before variable name.
Examples of Integer Data Type
xxxxxxxxxx
#include <stdio.h>
int main() {
int num; // Declares an integer variable named 'num'
num = 100; // Assigns the integer value 100 to the variable 'num'
printf("%d", num);
return 0;
}
Example explained
-
int num;
: The code declares an integer variable named 'num'. -
num = 100;
: It assigns the integer value 100 to the variable 'num'. -
printf("%d", num);
: It prints the value of integer variable named 'num'. '%d' is the format specifier for integer.
Create Float Data Type in C
To define a float data type, use float
keyword before variable name.
Examples of Float Data Type
xxxxxxxxxx
#include <stdio.h>
int main() {
float numFloat; // Declares a float variable named 'numFloat'
numFloat = 32.54; // Assigns the float value 32.54 to the variable 'numFloat'
printf("%f", numFloat);
return 0;
}
Example explained
-
float numFloat;
: The code declares a float variable named 'numFloat'. -
numFloat = 32.54;
: It assigns the float value 32.54 to the variable 'numFloat'. -
printf("%f", numFloat);
: It prints the value of float variable named 'numFloat'. '%f' is the format specifier for float.
Create Double Data Type in C
To define a double data type, use double
keyword before variable name.
Examples of Double Data Type
xxxxxxxxxx
#include <stdio.h>
int main() {
double numDouble; // Declares a double variable named 'numDouble'
numDouble = 564.25647; // Assigns the double value 564.25647 to the variable 'numDouble'
printf("%lf", numDouble);
return 0;
}
Example explained
-
double numDouble;
: The code declares a double variable named 'numDouble'. -
numDouble = 564.25647;
: It assigns the double value 564.25647 to the variable 'numDouble'. -
printf("%lf", numDouble);
: It prints the value of double variable named 'numDouble'. '%lf' is the format specifier for double.
Create Char Data Type in C
To define a char data type, use char
keyword before variable name.
Examples of Char Data Type
xxxxxxxxxx
#include <stdio.h>
int main() {
char letter; // Declares a char variable named 'letter'
letter = 'A'; // Assigns the char value 'A' to the variable 'letter'
printf("%c", letter);
return 0;
}
Example explained
-
char letter;
: The code declares a char variable named 'letter'. -
letter = 'A';
: It assigns the char value 'A' to the variable 'letter'. -
printf("%c", letter);
: It prints the value of char variable named 'letter'. '%c' is the format specifier for char.
Create Boolean Data Type in C
To define a boolean data type, use bool
keyword before variable name.
You must include stdbool.h
header to use bool.
Examples of Boolean Data Type
xxxxxxxxxx
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isTrue; // Declares a boolean variable
isTrue = true; // Assigns true value
printf("isTrue: %s\n", (isTrue) ? "true" : "false");
return 0;
}
Example explained
-
bool isTrue;
: The code declares a boolean variable named 'isTrue'. -
isTrue = true;
: It assigns the boolean value 'true' to the variable 'isTrue'. -
printf("isTrue: %s\n", (isTrue) ? "true" : "false"):
It prints the value of boolean variable named 'isTrue'. The ternary operator method is used for better readability.
Create Array in C
To declare an array, specify the data type of the elements before the array name. The array name must be followed by square brackets []. The size of the array is put in the square brackets in integer value greater than zero.
Array elements are placed between curly braces '{}' and are separated by comma.
You can initialize the array with elements at the time of declaration or you can add elements later.
Examples of Array
xxxxxxxxxx
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5}; // Declare and initialize the array
printf("%d\n", numbers[2]);
return 0;
}
Example explained
-
int numbers[5] = {1, 2, 3, 4, 5};
: The code declares and initializes an integer array of 5 numbers. -
printf("%d\n", numbers[2]);
: It prints the third element of integer array. Index of C array is 0 (zero) based.
Create Pointer in C
- Specify the data type of the pointer.
- Use the * operator to declare a pointer variable name. The * operator is conventionally placed at the beginning of the variable name to indicate that it's a pointer.
- Use the address-of operator '&' before a variable to get the memory address of the variable, and assign it to the pointer variable.
Examples of Pointer
xxxxxxxxxx
#include <stdio.h>
int main() {
int num = 20;
int *numptr = # //Declaring pointer and assigning value
printf("The memory address of num variable: %p\n", numptr); // %p is format specifier for pointer
return 0;
}
Example explained
-
int num = 20;
: The code declares and initializes an integer variable 'num'. -
int *numptr = #
: It declares a pointer name '*numptr' and assigns the memory address of 'num' variable using '&num'. Please note that '&' operator is used with 'num' varibale to get the memory address of the variable. -
printf("The memory address of num: %p\n", numptr);
: It prints the memory address of 'num' variable using pointer 'numptr'. '%p' is the format specifier for pointer.
Create Structure in C
-
Use 'struct' keyward before structure name like
struct Person
. Person is the user defined name of structure. -
Define members of the structure inside curly braces '{}' like
{ char name[60]; int age; };
. Declare data type before the member name to define a struct member.
Please take a closer look at the following example.
Examples of Pointer
xxxxxxxxxx
#include <stdio.h>
#include <string.h>
// Define a structure named 'Person'
struct Person {
char name[60];
int age;
};
int main() {
// Declare a variable of type 'Person' structure
struct Person person1;
// copy string to name member
strcpy(person1.name, "John Done");
person1.age = 35;
//print person1
printf( "%s is %d years old.", person1.name, person1.age);
return 0;
}
Example explained
-
struct Person { char name[60]; int age;};
: The line declares Person structure with two members - 'name' and 'age'. -
struct Person person1;
: It declares a variable of type 'Person' structure name 'person1'. -
strcpy(person1.name, "John Done");
: The line copies string value ("John Done") to the name member. -
person1.age = 35;
: It assigns '35' to the 'age' member of Person structure. -
printf( "%s is %d years old.", person1.name, person1.age);
: It prints the member values of Person structure.