Skip to content

Latest commit

 

History

History
224 lines (158 loc) · 2.18 KB

7-Type-Conversion.md

File metadata and controls

224 lines (158 loc) · 2.18 KB

Type Conversion in C Programming

Video Link: https://youtu.be/xi2wf0Zy2Y4 Tutorial Link: https://www.programiz.com/c-programming

Add two integers

#include <stdio.h>

int main() {

  int a = 5;
  int b = 9;

  int result = a + b;

  printf("%d", result);
  return 0;
}

Output

14

char to int Conversion

#include <stdio.h>

int main() {

  char a = '5';
  int b = 9;

  int result = a + b;

  printf("%d", result);
  return 0;
}

Output

62

double to int Conversion

#include <stdio.h>

int main() {

  double a = 5.67;
  int b = 9;

  int result = a + b;

  printf("%d", result);
  return 0;
}

Output

14

int to double Conversion

#include <stdio.h>

int main() {

  double a = 5.67;
  int b = 9;

  double result = a + b;

  printf("%lf", result);
  return 0;
}

Output

14.670000

Data Type Hierarchy

  • long double
  • double
  • float
  • long
  • int
  • short
  • char

Higher data type demoted to lower data type

#include <stdio.h>

int main() {

  int a = 5.67;

  printf("%d", a);
  return 0;
}

Output

5

Explicit Type Conversion

Convert double data type to int

#include <stdio.h>

int main() {

  double a = 5.67;
  int b = 9;

  double result = (int)a + b;

  printf("%lf", result);
  return 0;
}

Output

14.000000

Change data type of result variable to int

#include <stdio.h>

int main() {

  double a = 5.67;
  int b = 9;

  int result = (int)a + b;

  printf("%d", result);
  return 0;
}

Output

14

Change data type of result variable to double

#include <stdio.h>

int main() {

  int a = 9;
  int b = 2;

  double result = a / b;

  printf("%lf", result);
  return 0;
}

Output

4.000000

Division by converting int to double

#include <stdio.h>

int main() {

  int a = 9;
  int b = 2;

  double result = (double)a / b;

  printf("%lf", result);
  return 0;
}

Output

4.500000

Programiz Quiz

Q. What is the value of result in the following statement?

int result = '8' + 12;

Options

  1. 20
  2. 68
  3. Error
  4. 12650

Answer: 2