Basics of C Programming for Beginners- Data types in C (Part-III)

Vijay Aneraye
6 min readDec 26, 2023

--

This tutorial is meant for beginners. You get an overview of the basic concepts of data types in C programming language

Before moving on to this section, please read the two parts of the preceding post if you haven’t already.

  1. C Programming language Introduction
  2. Variables in C Programming language

The C programming language is a strongly statically typed language.

In statically typed languages, you need to explicitly declare your variables to be of a certain data type. That way the compiler knows during compilation time if the variable is able to perform the actions it was set out and requested to do.

Computer memory in brief (bits vs. bytes):

  • All data in a computer is stored in memory as a number. Bits are stored in computer memory using electronic circuits that representing the binary values 0 and 1.
  • Bits and Bytes are both units of computer memory.
  • A bit is the smallest unit of memory, while a byte is larger.
  • A bit is short for binary digit.
  • 1 byte = 8 bits

What are Data Types in C Language?

In C programming language, data types are used to define the type of data that a variable can hold as each variable in C needs to declare what data type it represents.

The following data type groups are supported in C:

  1. Primitive data types ( Basic Data types Or Built in or Fundamental data type)
  2. User-defined data types
  3. Derived data types

1. Primitive Types:

  • These data types are built-in or predefined data types and can be used directly by the user to declare variables.
  • These data types are mostly integer, floating point and char. (integers, doubles, floating-point,char etc.)
  • Four main types of primary/basic data types are:
    a. Integer
    b. Float
    c. Char
    d. Void
  • These are further classified short, long, signed and unsigned data types in C.
  • SHORT AND LONG : These are used to define the amount of memory space that the compiler will allocate. In case of short int, it is typically 2 bytes and in long int, it is 4 bytes.
  • SIGNED AND UNSIGNED: This also deals with memory allocation but in a different way. In case of signed int, it takes into account both negative and positive numbers. But in unsigned int, we can only represent positive numbers.
  • LONG AND LONG DOUBLE: These are mostly used with decimal numbers. By using these prefixes, we can increase the range of values represented in float. Float is of 4 bytes, double is of 8 bytes and long double is of 10 bytes.
  • Here is a list of all the primary data types:
  • Examples
#include <stdio.h>
int main() {
char c = 'a' ;
int num = 10;
float f = 1.20;
float f1 = 1;
double d = 3.2325467;
printf("%c",c);
printf("%d",num);
printf("%f",f);
printf("%f",f1);
printf("%lf",d);
return 0;
}
output:
a
10
1.200000
1.000000
3.232547
  • Examples of data type declaration with warning
#include <stdio.h>
int main() {
int num = 10.1; // compilation warning implicit conversion from 'double' to 'int' changes value from 10.2 to 10
float f = 1;
printf("value of f %d \n", f); // compilation warning format specifies type 'int' but the argument has type 'float'
return 0;
}

VOID data type

It is a special type known as empty data type that is used to state that a given variable does not have any type. This is mainly used in defining functions where we do not want to return any value.

2. User-defined data types:

These data types are defined by the user as per their convenience. If a user feels a need of having a data type which is not predefined in C library, then they make their own..

  1. Structure:- To declare a structure variable, use the struct keyword. It stores different data types. A way to combine data of different types.

2. Enum:- To define enums, use the enum keyword. It consists of integer values.

3. Union:- Union is like Structure. But union uses less memory. It stores a package of different types of data.

3. Derived Data Types in C:

The derived data types are those data types that are created by combining primitive data types and other derived data types.

There are three derived data types available in C. They are as follows:

  1. Function
  2. String
  3. Array
  4. Pointer
  5. ARRAY: Array in C is a fixed-size collection of similar data items stored in contiguous memory locations. An array is capable of storing the collection of data of primitive, derived, and user-defined data types. Syntax:
data_type array_name [size];

Example

// C program to illustrate derived data type - Array 
#include <stdio.h>

int main()
{
// declaring the size of the array
int N = 3;

// declaring array of type int of size N
int arrI[] = { 18, 36, 54 };
for (int i = 0; i < N; i++)
printf("%d ", arrI[i]);

printf("\n");

// declaring array of type double of size N
double arrD[] = { 3.14, 6.28, 9.42 };
for (int i = 0; i < N; i++)
printf("%lf ", arrD[i]);

return 0;
}

2. STRING: String is an array of characters. The difference between a normal character array and a string is that string contains ‘\0’ value at the end, indicating that this is the end of the string, while a simple character array does not need to have this.

‘\0’ is a special character, which is known as the NULL character and it is used to indicate the termination of a string.

#include <stdio.h>
int main() {
char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};\
char str2[] = "Hello";
printf("Say: %s\n", str1 );
printf("Say: %s\n", str2 );
return 0;
}
Output:
Say: Hello
Say: Hello

3. Pointer: A pointer is a special type of data type. It is unique because, unlike other data types, it does not store normal values in variables. Instead, a pointer variable stores the memory address of a specific location in the computer’s memory.

Syntax:

data_type * ptr_name;

where,

  • data_type: type of data that a pointer is pointing to.
  • ptr_name: name of the pointer.
  • *: dereferencing operator.

Example:

// C program to illustrate derived datatype - pointers 
#include <stdio.h>

int main()
{
int var = 20;

// declare pointer variable
int* myPtr;

// note that data type of pointer and variable must be same

// assign the address of a variable to a pointer
myPtr = &var;

// printing myPtr to show value stored in myPtr which is
// address of var
printf("Value at myPtr = %p \n", myPtr);

// printing var variable directly
printf("Value at var = %d \n", var);

// using dereferencing operator to get the value myPtr
// is pointing at
printf("Value at *myPtr = %d \n", *myPtr);

return 0;
}

Output

Value at myPtr = 0x7ffd0850df9c 
Value at var = 20
Value at *myPtr = 20

4. Function: Functions in C or in any programming language refers to a set of instructions that, when executed in a given order, performs a specific task. The task could be like, finding maximum element, sorting the elements, reversing the elements etc.

Function Declaration

return_type function_name(data_type param1Name, data_type param2Name, ...);

Let us consider a scenario where you want to calculate the multiplication of two numbers

// C program to illustrate derived data type - function 
#include <stdio.h>

int multiply(int param1, int param2)
{
// return statement which return type int
return (param1 * param2);
}

int main()
{
// declaring parameters to be passed to the function
int param1 = 5, param2 = 3;

// calling the function and storing the return value in
// result
int result = multiply(param1, param2);

// printing the result to the console
printf("%d", result);
return 0;
}
output:
15

There is a main function present in every C program, all the other functions are declared there

Components of a Function in C

  • Return Type: Specifies the type of the value that the function will return after its execution.
  • Function Name: It is a unique name that identifies a function. Using this unique name, a function is called from various parts of a program.
  • Function Body: A function’s body consists of the statements that define what the function actually does. All the operations including the return of the result are done inside the body of a function.
  • Parameters: Parameters are the input values passed to the function by the caller. A function can have none or multiple parameters.
  • Return Statement: A function returns a value depending on its return type.

Conclusion:

In this article, we learnt:

  1. Based on the data type,
  2. Different ranges and sizes for each data type.
  3. Different types of data types in brief

Next Part : Operators in C Programming Language

Thanks for reading and happy reading!

--

--