🟢 Foundation  ·  Lesson 10

Type Conversion and Type Casting

Type Conversion and Type Casting

What is Type Conversion and Type Casting?

Type Conversion and Type Casting
Type conversion changes a value from one data type to another. It may be automatic or explicit. Correct casting is important in division, average and percentage calculations.
Level
🟢 Beginner – C++ Foundation
Example File
type-casting.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
implicit conversionType Conversion and Type Casting programming में Implicit conversion use होता है।
explicit castingType Conversion and Type Casting programming में Explicit casting use होता है।
static_castType Conversion and Type Casting programming में Static_cast use होता है।
promotionType Conversion and Type Casting programming में Promotion use होता है।

Syntax / Pattern

C++
static_cast<type>(value)

Example Program

main.cpp
#include <iostream>
using namespace std;

int main() {
    int total = 455;
    int subjects = 5;
    double percentage = static_cast<double>(total) / subjects;
    cout << percentage;
    return 0;
}

Expected Output

91

Program Explanation

  • Without casting, integer division may remove decimal part.
  • static_cast<double> converts total into decimal type before division.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • percentage calculation
  • average marks
  • currency conversion

Common Mistakes

  • Expecting 5/2 to give 2.5 in integer division.
  • Using unsafe casts unnecessarily.

Practice Tasks

  1. Find average of 3 integers as decimal.
  2. Explain implicit and explicit conversion.

Summary

Type Conversion and Type Casting एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

Type Conversion and Type Casting क्या है?

Type Conversion and Type Casting
Type conversion changes a value from one data type to another. It may be automatic or explicit. Correct casting is important in division, average and percentage calculations.
Level
🟢 Beginner – C++ Foundation
Example File
type-casting.cpp
मुख्य फोकस
Concept + syntax + practical C++ program

इसे क्यों सीखें?

  • यह आपको सही और पढ़ने-योग्य C++ programs लिखने में मदद करता है।
  • यह school practicals, projects और competitive programming में बार-बार use होता है।
  • यह OOP, STL और data structures जैसे advanced topics की नींव बनाता है।

ज़रूरी Terms

Termअर्थ / उपयोग
implicit conversionImplicit conversion Type Conversion and Type Casting programming में use होता है।
explicit castingExplicit casting Type Conversion and Type Casting programming में use होता है।
static_castStatic_cast Type Conversion and Type Casting programming में use होता है।
promotionPromotion Type Conversion and Type Casting programming में use होता है।

Syntax / Pattern

C++
static_cast<type>(value)

Example Program

main.cpp
#include <iostream>
using namespace std;

int main() {
    int total = 455;
    int subjects = 5;
    double percentage = static_cast<double>(total) / subjects;
    cout << percentage;
    return 0;
}

Expected Output

91

Program Explanation

  • Without casting, integer division may remove decimal part.
  • static_cast<double> converts total into decimal type before division.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • percentage calculation
  • average marks
  • currency conversion

आम गलतियाँ (Common Mistakes)

  • Expecting 5/2 to give 2.5 in integer division.
  • Using unsafe casts unnecessarily.

अभ्यास (Practice Tasks)

  1. Find average of 3 integers as decimal.
  2. Explain implicit and explicit conversion.

सारांश

Type Conversion and Type Casting एक ज़रूरी C++ topic है। परिभाषा सीखें, syntax समझें, example program चलाएं और फिर practice tasks हल करके concept मज़बूत करें।

← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n

💻 Live Code Editor

Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें. Agar load na ho to naye tab mein kholें.