Skip to content

Latest commit

 

History

History

007_Dec13

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

December 13

Image

Type of type conversion

1. Implicit (Automatic Type Conversion)

Narrowing/ Widening

2. Explicit (Type Casting)

Widening

int int_value = 5;
double double_value = int_value;
cout << double_value;

Narrowing

double double_value = 27.37;
int int_value = double_value;
cout << int_value;

A) C - Style

int val = (int)double_value;

B) Function - Style

int val = int(double_value);

Examples

Implicit

A.

#include <iostream>
using namespace std;

int main() {
    double num_double = 56.76;
    int num_int = num_double;

    cout << "The value of int is " << num_int<< endl;

    return 0;
}

B.

#include <iostream>
using namespace std;

int main() {
    int num_int = 45;
    double num_double = num_int;

    cout << "The number of double is " << num_double;

    return 0;
}

C.

#include <iostream>
using namespace std;

int main() {
    double num_double = 56.76;
    
    //c style
    int num_int = (int)num_double;

    //function notation
    num_int = int(num_double);

    cout << "The given double value is " << num_double << endl;
    cout << "C Style integer type casting = " << num_int << endl;
    cout << "Integer Type Casting using function notation is = " << num_int;

    return 0;
}