0% found this document useful (0 votes)
14 views7 pages

C 11

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

C 11

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
06/12/2028 06:37 Data Type Conversion 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools C Type Conversion [crv] Looe | Type Conversion Sometimes, you have to convert the value of one data type to another type. This is known as type conversion. For example, if you try to divide two integers, 5 by 2, you would expect the result to be 2.5. But since we are working with integers (and not floating-point values), the following example will just output 2 Example int x = 55 int y = 23 int sum = 5 / 25 printf("%d", sum); // Outputs 2 To get the right result, you need to know how type conversion works, There are two types of conversion in C: + Implicit Conversion (automatically) + Explicit Conversion (manually) nitpsdlmww:wdschools.comile_type_conversion.ohp we 06/12/2028 06:37 Data Type Conversion 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools one type to another. For example, if you assign an int value toa float type: Example // Rutomatic conversion: int to float float myFloat = 9; printf("%F", myFloat); // 9.000000 As you can see, the compiler automatically converts the int value 9 to a float value of 9,0200000 . This can be risky, as you might lose control over specific values in certain situations. Especially if it was the other way around - the following example automatically converts the float value 9.99 to an int value of 9: Example // automatic conversion: float to int int myInt = 9.99; print#("%d", myInt); // 9 What happened to .99 ? We might want that data in our program! So be careful. It is important that you know how the compiler work in these situations, to avoid unexpected results. nitpsdlmww:wdschools.comile_type_conversion.ohp ar 06/12/2028 06:37 Data Type Conversion 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools Example float sum = 5 / 2; printf("%f", sum); // 2.000000 Why is the result 2.00000 and not 2.5 ? Well, it is because 5 and 2 are still integers in the division. In this case, you need to manually convert the integer values to floating-point values. (see below). Explicit Conversion Explicit conversion is done manually by placing the type in parentheses () in front of the value. Considering our problem from the example above, we can now get the right result: Example // Manual conversion: int to float float sum = (float) 5 / 25 printf("%F", sum); // 2.500000 You can also place the type in front of a variable: Example nitpsdlmww:wdschools.comile_type_conversion.ohp an 06/12/2028 06:37 Data Type Conversion 3 WW tutoriaisy Exercises» Servicessy§ Q 0 schools Log in printf("%#", sum); // 2.500000 And since you learned about "decimal precision" in the previous chapter, you could make the output even cleaner by removing the extra zeros (if you like) Example int numl = 53 int num2 = 25 float sum = (float) num / num2; printf("%.1f", sum); // 2.5 nitpsdlmww:wdschools.comile_type_conversion.ohp an 06/12/2028 06:37 w schools Tutorialse Exercises Services Data Type Conversion Q 0 Log in Just landed eres COLOR PICKER w schools SPACES GET CERTIFIED Top Tutorials HTML Tutorial CSS Tutorial Tutorial How To Tutorial SQL Tutorial Python Tutorial W23.CSS Tutorial nitpsdlmww:wdschools.comile_type_conversion.ohp UPGRADE NEWSLETTER REPORT ERROR sr 06/12/2028 06:37 3 WW tutoriaise exercises schools = CSS JAVASCRIPT lop References HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference Wa.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference ‘Angular Reference jQuery Reference Top Examples HTML Examples SS Examples JavaScript Examples How To Examples ‘SQL Examples Python Examples W23.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples G@ @& B® © Forum asour © Data Type Conversion Q 0 SignUp Login JAVA PHP = HOWTO —W3.CSS Get Certified HTML Certificate css Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate Query Certificate Java Certificate c+ Certificate i Certificate XML Certificate W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by hntipsifinww.w3schoots.comicl_type_conversion.ohp 06/12/2028 06:37 3 WV tutoriatse Exercises schools = CSS JAVASCR sQu htipsumaew.wdechools.comicle_type_conversion.ahp C Data Type Conversion Services PYTHON Q 0 JAVA PHP. How TO Login W3.css W

You might also like