C - TypeCasting

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

Type casting in C is a fundamental concept that allows to convert one data type into another. This process ensures that data is appropriately interpreted by the compiler, and it's a crucial tool for handling various data types efficiently. In C, type casting is achieved using keywords like int, float, char, and others, which indicate the desired data type for the value being converted.

Syntax

In C, type casting is the process of converting a value from one data type to another. The general syntax for type casting in C is as follows:

Here:

new_data_type represents the desired data type to which you want to cast the expression. expression is the value or variable that you want to cast.

For example:

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Types of Type Casting in C:

1. Implicit Type Casting (Type Coercion): Implicit type casting in C, also known as type coercion, occurs automatically by the compiler when a value of one data type is assigned to a variable of another data type. This typically happens when a "smaller" data type is assigned to a "larger" data type, preserving data integrity.

For example:

2. Explicit Type Casting: Explicit type casting in C involves manually converting a value from one data type to another using the casting operator (new_data_type).

For example:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Explicit Type Casting

Let's take an example to demonstrate explicit type casting in c:

Program 1:

Output:

Explanation:

In this program, we explicitly cast the pi variable (which is a double) to an integer using (int) to remove the decimal part. The original value of pi is printed with %lf (long float) format specifier, and the rounded value is printed with %d (integer) format specifier.

Program 2:

Output:

Explanation:

Here, we explicitly cast the num variable (which is an int) to a char using (char). The original value of num is printed as an integer, and the character representation of the casted value is printed with %c.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

5 Built-in Type Casting Functions in C Programming

C provides several built-in type casting functions to perform type casting in C. Here are five commonly used ones:

  1. atof(): Converts a string to a double.
  2. atoi(): Converts a string to an integer.
  3. itoa(): Converts an integer to a string.
  4. itoba(): Converts an integer to a binary string.
  5. atoi(): Converts a binary string to an integer. These functions are particularly useful when dealing with string-to-number or number-to-string conversions.

Advantages of Type Casting:

Data Integrity: Type casting allows you to ensure that data is properly converted when moving between different data types, preventing data loss or unexpected behavior.

Compatibility: It helps in making different data types compatible with each other, allowing operations and assignments that might otherwise be incompatible.

Flexibility: Type casting gives programmers flexibility in how they use and manipulate data, enabling them to adapt to various situations and requirements.

Precision Control: It allows you to control the precision of numeric values, such as truncating decimal places when converting from a float to an integer.

String Manipulation: Type casting functions (e.g., atoi() and itoa()) facilitate easy conversion between strings and numeric types, making string manipulation more straightforward.

Conclusion

  • Type casting in C involves converting a value from one data type to another using the syntax (new_data_type) expression.
  • Implicit Type Casting in C(Type Coercion) is automatically performed by the compiler when assigning values between compatible data types.
  • Explicit Type Casting in C manually performed using the casting operator (new_data_type) to convert values between different data types.
  • C offers built-in functions like atof(), atoi(), itoa(), itoba(), and atoi() for various type casting operations, facilitating string-to-number and number-to-string conversions.