How to create different data types in C

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:

  1. int: int is used to store integer values (whole numbers). It typically occupies 4 byte of memory.
  2. 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.
  3. 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.
  4. char: char is used to store single characters (like letters, digits, symbols). It typically occupies 1 byte of memory.
  5. _Bool: _Bool is used for 'True' or 'False' (0 or 1). 0 represents false and non-zero represents true.

Derived data types are:

  1. Arrays: An array is a collection of items of the same data type stored under a single name.
  2. Pointers: Pointers store memory addresses of variables. They are used for advanced memory management.
  3. Structures: Structures allow you to group variables of different data types under a single name, creating user-defined data types.
  4. 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:

  1. 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

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

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

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
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

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

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

  1. Specify the data type of the pointer.
  2. 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.
  3. 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

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

  1. Use 'struct' keyward before structure name like struct Person. Person is the user defined name of structure.
  2. 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

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.

Live Code Playground

In the following C code editor, you can practice by declaring more variables of different data types. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output