C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program Swap Numbers in cyclic order Using Call by Reference C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++ C Function Argument and Return Values Keywords in C Bank management system in C Calendar application in C Floor() Function in C Free() Function in C How to delete a file in C How to move a text in C Remove an element from an array in C Unformatted input() and output() function in C What are linker and loader in C SJF Scheduling Program in C Socket Programming in C Structure in C Tower of Hanoi in C Union Program in C Variable Declaration in C What is Linked List in C While Loop Syntax in C fork() in C GCD program in C Branching Statements in C Comma Operator in C Control statement in C Double Specifier in C How to create a binary file in C Long int in C Palindrome Number in C Pure Virtual Function in C Run Time Polymorphism in C Types of Array in C Types of Function in C What is a buffer in C What is required in each C Program Associativity of Operators in C Bit Stuffing Program in C Actual and Formal Parameters Addition of two Numbers in C Advantages of function in C Arithmetic Progression Program in C Binomial Coefficient Program in C Difference between Array and List in C Diffie-Hellman Algorithm in C How to convert a number to words in C How to convert a string to hexadecimal in C Difference between If and Switch Statement in C C and C++ Binary Files C program that does not Suspend when Ctrl+Z is Pressed Different ways to Declare the Variable as Constant in C Range of Int in C C Program to find the size of a File FIFO Example in the C Language For loop in C Programming GCD program of two numbers in C GPA Calculator in C How to Calculate Time Complexity in C How to include graphics.h in C How to measure time taken by a function in C How to return a Pointer from a Function in C What is the main in C Addition of Matrix in C Booleans in C C Program for Extended Euclidean algorithms C Program of Fencing the Ground Ceil and Floor in C Compound Interest Program in C Displaying Array in C Distance Vector Routing Protocol Program in c Dos.h Header File in C Language DSA Program in C Explain the two-way selection in C Fee Management System in C File Operations in C Malloc function in C Multiplication Table in C Simple Programs in C Language tolower() Function in C Type Conversion in the C Why does sizeof(x++) not Increment x in C Advantages of Dynamic Memory Allocation in C Armstrong Number in C Assignment Operator Program in C Banker’s Algorithm in C Binary Search in C with Best and Worst Time Complexity Caesar Cipher Program in C Call by Value and Call by Reference in C Conditional Operator in C CRC Program in C Deadlock Detection Program in C Decimal to Binary in C Difference between If Else and Nested If Else in C Difference between Pre-increment and Post-increment in C Difference between Scope and Lifetime in C Evaluation of Arithmetic Expression in C Explain the Increment and Decrement Operators in C Fseek Function in C Functions in C How to Find Square Free Numbers in C Length of an Array Function in C OpenGL in C Projects on C language in 2023 Purpose of a Function Prototype in C Stdio.h in C Two-Dimensional array in C What is String Comparison in C C Compilers for Windows Functions and Recursion in C How to Declare Boolean in C How to Declare Character in C How to Round up a number in C How to use strlen() in C Pointer Declaration in C Algorithm for String Palindrome in C C Program to find ASCII value of a character Constant Pointer in C How to find string length in C using strlen() function Implicit and Explicit in C Indirect Recursion in C Input and Output functions in C isupper() in C Jump Statement in C Lifetime of a Variable in C Linker Error in C Language Numeric Constant in C Size of Pointer in C Square Root in C Language Static and Dynamic Memory allocation String Declaration in C Strong Number in C Symmetric Matrix in C Types of C Tokens What is a Size of Pointer in C What is Increment and Decrement Operator in C 1 2 3 4 Series Program in C Advantages and Disadvantages of C Language C Program for Polynomial Addition C Program to Count the Number of Vowels in a String C Programming Errors and Solutions Compilation Errors in C Complex C Programs Difference between Argument and Parameter in C Difference between char s[] and char *s in C Evaluation of Postfix Expression Using Stack in C Find Leftmost and Rightmost Set Bit of a Number fprintf and fscanf in C Introduction to Dynamic Array in C Print Address in C Realloc function in C Ternary Operators in C Types of Tokens in C with Examples Difference between Static and Dynamic Memory Allocation in C Addition Program in C Array Definition in C Array of Pointers in C Arrow Operator in C Average of Two Numbers in C Binary to Decimal in C Binary to Octal in C BREAK STATEMENT in C C Programming Operators Questions C Programs Asked in Interview Calculator Program in C C Program to Read and Print an Employee's Detail Using Structure Bubble Sort Algorithm in C C Program to Find Area and Perimeter of Circle C Program to Check Whether a Given Number is Even or Odd C in Roman Numerals C Program to Make a Simple Calculator Using Switch Case Insertion Sort Program in C How to take input in string in C GCC Conflicting Types in C Function Definition in C Format Specifier for Hexadecimal in C Flowchart in C Float in C Fizzbuzz Implementation in C Conditional Statement in C Conio.h functions list in C Constants in C Dynamic Array in C Decision Making Statements in C Continue Statement in C Creation of Thread in C DFS Algorithm in C Difference between parameter and arguments in C Dijkstra's Algorithm in C Leap Year Program in C Jump Statements in C Modulus Operator in C Memory Allocation in C Simple Interest Program in C Reverse Array in C Recursive Function in C Queue in C Printing Pascal’s Triangle in C Preprocessor Directives in C Perror() in C Perfect Number in C Programming Language Parameter Passing Techniques in C Pascal Triangle in C Patterm Program in C

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to declare the variable in C along with its data type before we use that variable in the program.

Data type of an entity (variable) describes:

  • The values the entity can hold or store
  • The format in which the value is stored
  • The operations it can perform
  • Determines the meaning of the data it represents

Types of Data Types


















There are several classifications of data types. Basically, they are classified into four different types:

  1. Primary or Scalar Data types
  2. Derived or Secondary Data types
  3. User-defined data types
  4. Empty data type

1. Primary or Scalar Data types:

These are the predefined or in-built data types in the system. These are further classified into the four most used data types in C:

  1. int
  2. char
  3. float
  4. double
  • int data type:

Just like the name suggests, this data type stores only integers. It occupies 2 bytes of memory (16 bits) in a 16-bit operating system, 4 bytes in a 32-bit operating system and 8 bytes in a 64-bit operating system.

Example program:


void main()

       int a = -45;

       signed int b = -78;

       int d = 89.9876;

       unsigned int c = 90;

       printf(“a has %d, b has %d and c has %d”,a,b,c);

       printf(“\nd has %d”,d);



a has -45, b has -78 and c has 90

d has 89

Here, a and b can store any integer but as it is declared unsigned, it can’t store negative integers.

  • We can’t store decimal values in int. Even if we use, the decimal part will be gone and only the whole number part will be stored.


There are some keywords in C which we use with the data type that manipulates or changes some attributes like the memory space occupied by that data type etc…

  1. long
  2. short
  3. unsigned and
  4. signed are such keywords.

For a clear view, the memory required to store int data type is 4 bytes in a 32 bit processor system. We can use “short” keyword to decrease the memory to 2 bytes. By using these keywords, the maximum value the data type can store also changes.

If we use “unsigned”, it restricts the data type to store only positive values. By using signed, both the negative and positive values can be stored. “signed” is the default system data type if we didn’t specify any keyword.

short int <= int <= long int

Here is a table to give a clear version:

Data typeMemory requiredRangeFormat Specifier
int4 bytes-2,147,483,648 to 2,147,483,647%d
short int2 bytes-32,768 to 32,767%hd
long int4 bytes-2,147,483,648 to 2,147,483,647  %ld
signed int4 bytes-2,147,483,648 to 2,147,483,647%d
unsigned int4 bytes0 to 4,294,967,295%u
unsigned short int2 bytes0 to 65,535  %hu
unsigned long int4 bytes0 to 4,294,967,295%lu
long long int8 bytes-(2^63) to (2^63)-1%lld
Unsigned long long int8 bytes0 to 18,446,744,073,709,551,615  %llu
  • If a variable is not given a value in the declaration i.e., not initialized, then by default it takes a garbage value. Some compilers like gcc initializes zero by default.
  • char data type:

The char data type is used to store characters (Individual characters). It needs one byte i.e., 8 bits of memory. It is denoted inside single quotes.

Example: ‘d’,’e’ etc…

It can’t be enlarged to multiple lines. When we declare a character in our program, the compiler turns or translates the character into its ASCII value (American Standard Code for Information technologies).

Some Important ASCII values:

‘A’ to ‘Z’ -> 65 – 90

‘a’ to ‘z’ -> 97 – 122

‘0’ to ‘9’ -> 48 – 57

Null -> 0

Space -> 32


char ch = ‘A’;

Here, char is the data type, ch is the variable and A is the character value stored in ch.

Example program:

Void main()
		char gender1 = “M”;
		printf(“I am a male(%c)”,gender1);
		char gender2 = “F”;
		printf(“\nI am a female(%c)”,gender2);


I am a male(M)

I am a female(F)

Modifiers and Ranges:

-128 to +127 -> signed char

0 to 255 -> unsigned char

-128 to +127 -> normal char

  • Float Data type:

This data type stores the decimal values. It occupies 4 bytes (32 bits) of memory. It is accurate up to a precision of 6 digits after the decimal point.


float num1 = 6.1;

Here float is the data type, num1 is assumed to be the name of the variable and 6.1 is the value given to the variable.

Example program:

void main()
		float num1 = 6.7;
		printf(“The float number num1 is storing is: %f”,num1);
		float num2 = 5;
		printf(“\n5 is int but as we gave float, it stores in the 
			format %f”,num2);


The float number num1 is storing is: 6.7

5 is int but as we gave float, it stores in the format 5.0

Range: 3.4E-38 to 3.4E+38

Here, note that E means 10 to the power of.

Format Specifier: %f

  • Double Data Type:

As mentioned above, float is accurate only up to about 6 decimal places. To increase this accuracy, double data type comes into picture. It has a precision of 14 digits. It occupies 8 bytes of memory.


double num1 = 8.845643345;

Here, double is the data type, num1 is assumed to be the name of the variable, and it stores that given value.

Example Program:

void main()
		double num1 = 6.78567;
		printf(“The value of num1 is %lf\n”,num1);
		double num2 = 7.896544;
		printf(“The value of num2 is %lf\n”,num2);
		double sum = num1 + num2;
		printf(”The sum of num1 and num2 is 


The value of num1 is 6.78567

The value of num2 is 7.896544

The sum of num1 and num2 is 14.682214

Range: 1.7E-308 to 1.7E+308

Format Specifier : %lf

  • Long Double:

Extending the precision even more, long double is introduced. It has more accuracy than that of double but its value varies with every single OS. It allocates 10 bytes( 80 bits) of memory to store the values approximately.

Range: 3.4E-4932 to 1.1E+4932

Format Specifier: %Lf

2. Derived data types:

As the name suggests, these are the data types derived from the fundamental data types. They append more functionality to the existing data types. These are the derived data type in C:

  1. Arrays
  2. Pointers
  3. Functions
  • Arrays:

Assume you are the class representative of your class. You are given a task to collect the marks of all the 40 students and note them. In order to work with all the 40 different values, you have to initialize 40 different int variables and then assign values to all the variables.

It’s complicated. Isn’t it? This is where arrays come into picture.

When we want to store multiple values of the same data type, arrays are the options we’ll be left with.

To define it, Arrays are ordered sequences of data items of the same data type (Homogeneous) stored under the same name. The elements in an array are stored in contiguous memory locations.


data_type name[size] = {value1, value2,…};

data_type name[size];


int arr[10];


As shown in the representations, the elements in the array can be accessed by the indices of the array which starts from 0 to size-1, which means arr[size] or arr[10] is wrong. It ends with arr[size-1] or arr[9].

Storing the values:

Assume arr[5] = {1,2,3,4,5};


Here, arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5

Contiguous memory locations means that suppose in this array, all are of int data type and it needs 2 bytes (16 bit). Suppose, the first element is stored in an address 34567, it takes 2 bytes of space and the second one is stored in 34569 and goes on till the last element.

Example program:

void main()
	int arr[10], i;
	arr[10] = {1,2,3,4,5,6,7,8,9,10};
	printf(“The value at arr[6] is %d\n”,arr[6]);
	int arr1[3];
	printf(“Enter the elements into arr1: “);
	printf(“\narr1 has elements: “);
		printf(“ %d”,arr1[i]);


The value at arr[6] is 7
Enter the elements into arr1:
arr1 has elements: 4 5 6

Arrays also have types based on the sub-script:

  • One dimensional
  • Two dimensional
  • Three dimensional arrays
  • Pointers:

It is a mandatory concept in C. It is in simple sense, a variable which refers to a memory location or the address of another variable or another pointer.


 Here, assume A is a normal variable with the value 45 and its address is 5678. Assume B is a pointer with address 8970. B is pointing towards the address of A. It stores that address.

Two important facts to keep in mind:

*-> value at
&->address of

So, here we give the address of A to B which means

B = &A;

Now, if we access:

*B->45 which means it is showing the value at the address it is pointing to. If we print B we get the value of B which is the address of A.



void main()


                                      int *p1,a;

                                      p1 = &a;

                                      a = 4;

                                      printf(“The value of a is %d”,a);

                                      printf(“\nAddress of a is %d”,&a);

                                      printf(“\nThe value of p1 is %d”,p1);

                                      printf(“\np1 is pointing to %d”,*p1);


Assuming the value &a = 5678,


The value of a is 4

Address of a is 5678

The value of p1 is 5678

p1 is pointing to 4


There are two types of functions:

  1. Built-in functions
  2. User-defined functions

Built-in functions are predefined in the system that we keep using like main(), printf() and scanf() for reading and writing, there are a lot of functions available in different libraries in C.

User-defined functions:

Functions are considered a data type but it’s a different concept. Precisely, Functions are used to reuse the code that is needed frequently, other than typing the code again and again.

There are three parts in every function:

  • Function definition
  • Function call
  • Function declaration

First, we tell the compiler, there is a function in the code and then we declare it and write the code in the body and we call it. The control from the main function goes to the function call and then the function is executed, it gives the control back to the main function.

3. User-defined Data types:

These are the data types derived from already existing data types with some additional updates and customizations according to the need of the programmer.

  • Structures
  • Unions
  • Enum
  • Structures:

As mentioned above, Arrays only store data of the same data type. Structures, on the other hand, create its own data type, that is used to group different items of different data types into a single type.

Creating a Structure:

  • “struct” keyword is used.
struct employee
	char name[50];
	int ID;
	char gender; 
	int age;

Here, different details of different data types are stored under the so-called data type employee. emp is the variable used to access the members of the structure in the body of the program. It is called the “Structure Variable”.

Memory allocation:

In the structure, memory is allocated to all the items in order. For example, from the above program,

char name[50] -> 1*50 = 50 bytes

int ID -> 2 bytes

char gender -> 1 byte

age -> 2 bytes

name (50 bytes)ID (2 bytes)gender (1 byte)age (2 bytes)

Total of 50+2+1+2 = 55 bytes is allocated the structure employee.

  • Unions:

Unions are similar to Structures. The difference is that in structure, memory is allocated to all the data items inside, but in a union, the memory required to store the largest data item is given and all the items are placed in that space.

But, how are all the items stored in that space?

Taking the same example in Structures,

union employee


                                      char name[50];

                                      int ID;

                                      char gender;

                                      int age;


Here, the data item that takes more memory space is name[50]. So, the total space for the union is 50 bytes. Then, the ID is stored upon this stored name and over it, gender and then age are stored.


Arrangement varies with respect to the situation.

So, all the items are stored upon each other, giving us access to only one data item at a time. This is the other difference, in structure, we can access multiple data items at the same time, while in union, we can access only one data type at a time.

  • Enumeration (enum):

It is used to assign name to constants, to help make the program easy to maintain and read. The keyword “enum” is used for the declaration.


enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
void main()
	enum year month;
	month = Feb;
	printf(“%d”, month);



Here, we gave a set of names in order. These names are stores in order of integer constants from 0 to n-1. Now, we can access the names by integer constants.