Home Show/Hide Menu
C Tutorials
➜ C Language
  - Features of C
  - First C Program
  - Compilation in C
  - C Character Set
  - Tokens
  - Keywords
  - Identifiers
  - Constants
  - Operators
  - Data Types
  - Variables
  - Type Casting
  - Comments
  - Input/Output
  - Escape Sequence
  - Programming Errors
➜ Control Statements
  - if Statement
  - if-else Statement
  - Nested if-else
  - else-if
  - switch Statement
  - Loop
  - while loop
  - do while loop
  - for loop
  - break
  - continue & goto
  - Nested Loop
  - Infinite Loop
➜ Functions
  - What is function
  - Call by Value & Reference
  - Recursive function
  - Storage Classes
➜ Array
  - 1-D Array
  - 2-D Array
  - Return an Array
  - Array to Function
➜ C Pointers
  - Pointers
  - Pointer to Pointer
  - Pointer Arithmetic
  - Dangling Pointers
  - sizeof() operator
  - const Pointer
  - void pointer
  - Dereference Pointer
  - Null Pointer
  - Function Pointer
  - Function pointer as argument
➜ Memory Management
  - Memory Layout of C
  - Dynamic memory
  - calloc()
  - malloc()
  - realloc()
  - free()
➜ Strings
  - gets() & puts()
  - String Functions
  - strlen()
  - strcpy()
  - strcat()
  - strcmp()
  - strrev()
  - strlwr()
  - strupr()
  - strstr()
➜ C Math
  - Math Functions
➜ Enum, Structure & Union
  - Enum
  - Structure
  - typedef
  - Array of Structures
  - Nested Structure
  - Structure Padding
  - Union
➜ File Handling
  - fprintf() fscanf()
  - fputc() fgetc()
  - fputs() fgets()
  - fseek()
  - EOF and feof()
  - fsetpos()
  - tmpfile()
  - rewind()
  - ftell()
  - lseek()
➜ Preprocessor
  - Preprocessor
  - Macros
  - #include
  - #define
  - #undef
  - #ifdef
  - #ifndef
  - #if
  - #else
  - #error
  - #pragma
➜ Command Line
  - Arguments

Type Casting in C Language

In C, type casting refers to convert the data type of one variable to another data type. In some cases, compiler will automatically convert one data type to another without the programmer's intervention if it makes sense. For example, if we assign an integer value to a float variable, the compiler will convert the int to float. The cast operator allows us to make this type of conversion explicit when type casting wouldn't normally happen.


Type casting in the C language can be classified into the following two types:
  • Implicit Type Casting
  • Explicit Type Casting


1. Implicit Type Casting - Implicit Type Casting happens when compiler automatically convert one data type to another not losing its actual meaning without the programmer's intervention. If we are doing a conversion on two different data types then the lower data type is automatically converted into a higher data type.


Example 1 : Implicit Type Conversion
#include<stdio.h>
void main()
{
   int count = 4;
   float total = 12.4, avg; 
   avg = total / count;
   printf("Avg. : %f\n", avg );
}

Output:
Avg. : 3.1

Example 2 : Char to Int Conversion
#include<stdio.h>
void main() {
   int num = 7;
   char c = 'U';
   int add;
   add = num + c;
   printf("Value of add : %d\n", add);
}

Output:
Value of add : 90
In the above example, the variables 'a' and 'b' are declared inside the function main(). So, they can be used only inside main() function and not in add() function.

2. Explicit Type Casting - Explicit Type Casting is a process of converting a higher data type value into a lower data type using cast operator by the programmer.
Example 1 : Explicit type casting from double to int

#include<stdio.h>
void main()
{
   double d = 12345.6789;
    int i;
    i = (int)d; //Explicit casting from double to int
    printf("i = %d, d = %lf", i, d);
}

Output:
i = 12345, d = 12345.678900

Example 2 : Explicit type casting from int to char

#include<stdio.h>
void main()
{
	int x;
	for(x=97; x<=122; x++)
	{
	    printf("%c ", (char)x);   
	}
}

Output:
a b c d e f g h i j k l m n o p q r s t u v w x y z


Inbuilt Type Casting Functions

There are many inbuilt type casting functions available in C programming language, which performs type conversion from one data type to another.

 Functions  Description Syntax
atof()  Converts string to float  double atof(const char* string)
atoi()  Converts string to int int atoi(const char * str)
atol()  Converts string to long long int atol(const char * str)
itoa()  Converts int to string  char * itoa(int value, char * str, int base) 
ltoa()  Converts long to string char *ltoa(long n, char *str, int base)


ADVERTISEMENT