- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Type Casting in C
The term "type casting" refers to converting one datatype into another. It is also known as "type conversion". There are certain times when the compiler does the conversion on its own (implicit type conversion), so that the data types are compatible with each other.
On other occasions, the C compiler forcefully performs the typecasting (explicit type conversion), which is caused by the typecasting operator. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'.
You can use the typecasting operator to explicitly convert the values from one type to another −
(type_name) expression
Example 1
Consider the following example −
#include <stdio.h> int main() { int sum = 17, count = 5; double mean; mean = sum / count; printf("Value of mean: %f\n", mean); }
Output
Run the code and check its output −
Value of mean: 3.000000
While we expect the result to be 17/5, that is 3.4, it shows 3.000000, because both the operands in the division expression are of int type.
Example 2
In C, the result of a division operation is always in the data type with a larger byte length. Hence, we have to typecast one of the integer operands to float.
The cast operator causes the division of one integer variable by another to be performed as a floating-point operation −
#include <stdio.h> int main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean: %f\n", mean); }
Output
When the above code is compiled and executed, it produces the following result −
Value of mean: 3.400000
It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value.
Type conversions can be implicit which is performed by the compiler automatically, or it can be specified explicitly through the use of the cast operator. It is considered good programming practice to use the cast operator whenever type conversions are necessary.
Rules of Type Promotions
While performing the implicit or automatic type conversions, the C compiler follows the rules of type promotions. Generally, the principle followed is as follows −
- Byte and short values − They are promoted to int.
- If one operand is a long − The entire expression is promoted to long.
- If one operand is a float − The entire expression is promoted to float.
- If any of the operands is double − The result is promoted to double.
Integer Promotion in C
Integer promotion is the process by which values of integer type "smaller" than int or unsigned int are converted either to int or unsigned int.
Example
Consider an example of adding a character with an integer −
#include <stdio.h> int main() { int i = 17; char c = 'c'; /* ascii value is 99 */ int sum; sum = i + c; printf("Value of sum : %d\n", sum); }
Output
When the above code is compiled and executed, it produces the following result −
Value of sum: 116
Here, the value of sum is 116 because the compiler is doing integer promotion and converting the value of 'c' to ASCII before performing the actual addition operation.
Usual Arithmetic Conversion
The usual arithmetic conversions are not performed for the assignment operators nor for the logical operators && and ||.
Example
Let us take the following example to understand the concept −
#include <stdio.h> int main() { int i = 17; char c = 'c'; /* ascii value is 99 */ float sum; sum = i + c; printf("Value of sum : %f\n", sum); }
Output
When the above code is compiled and executed, it produces the following result −
Value of sum: 116.000000
Here, it is simple to understand that first c gets converted to an integer, but as the final value is double, usual arithmetic conversion applies and the compiler converts i and c into 'float' and adds them yielding a 'float' result.