TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Friday, 8 May 2020

Type Conversion In C Language | Type Conversion In C Language In Hindi

Type Conversion In C Language


A type cast is basically a conversion from one type to another. There are two types of type conversion:
1.Implicit Type Conversion
2.Explicit Type conversion


Advantages of Type Conversion


·     This is done to take advantage of certain features of type hierarchies or type representations.

·         It helps us to compute expressions containing variables of different data types.

1.Implicit Type Conversion

All the data types of the variables are upgraded to the data type of the variable with largest data type.

download  image implicit type conversion

Fig:Implicit Type conversion 


Program

#include<stdio.h>
int main()
{
          int x = 10; // integer x
          char y = 'a'; // character c

          // y implicitly converted to int. ASCII
          // value of 'a' is 97
          x = x + y;
         
          // x is implicitly converted to float
          float z = x + 1.0;

          printf("x = %d, z = %f", x, z);
          return 0;
}

Output:

x=107 z=108.0000

2.Explicit Type Conversion

This process is also called type casting and it is user defined. Here the user can type cast the result to make it of a particular data type.

download image of explicit type of conversion

Fig: Explicit Type Conversion 


The syntax in C:


(type) expression

Program:


#include<stdio.h>
int main()
{
          double x = 1.2;

          // Explicit conversion from double to int
          int sum = (int)x + 1;

          printf("sum = %d", sum);

          return 0;
}

Output:


Sum=2

No comments:

Post a Comment