Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
14 views
7 pages
C 11
Uploaded by
hlemorvan
AI-enhanced title
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
Download
Save
Save c11 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
14 views
7 pages
C 11
Uploaded by
hlemorvan
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save c11 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 7
Search
Fullscreen
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 we06/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 ar06/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 an06/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 an06/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 sr06/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.ohp06/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
C & C++ Language-3
PDF
100% (1)
C & C++ Language-3
99 pages
C Essentials Mod 2 Notes
PDF
No ratings yet
C Essentials Mod 2 Notes
72 pages
WINSEM2023-24 BCSE102L TH VL2023240501141 2024-01-05 Reference-Material-I
PDF
No ratings yet
WINSEM2023-24 BCSE102L TH VL2023240501141 2024-01-05 Reference-Material-I
65 pages
Example ?
PDF
No ratings yet
Example ?
36 pages
Datatypes in C: ESC101 9 August
PDF
No ratings yet
Datatypes in C: ESC101 9 August
25 pages
CDG C
PDF
No ratings yet
CDG C
20 pages
02a. Data Types-2
PDF
No ratings yet
02a. Data Types-2
35 pages
C-Var Defs, Data Types
PDF
100% (1)
C-Var Defs, Data Types
21 pages
L07
PDF
No ratings yet
L07
29 pages
Data Types and Type Conversions.13103
PDF
No ratings yet
Data Types and Type Conversions.13103
23 pages
Steps in Learning A Natural Language
PDF
No ratings yet
Steps in Learning A Natural Language
70 pages
Lec 05
PDF
No ratings yet
Lec 05
18 pages
Lecture 4 Oct 01 2019
PDF
No ratings yet
Lecture 4 Oct 01 2019
42 pages
Lecture #4
PDF
No ratings yet
Lecture #4
46 pages
ESC101: Introduction To Computing: Operators and Expressions
PDF
No ratings yet
ESC101: Introduction To Computing: Operators and Expressions
24 pages
Subject: C' Programming For Problem Solving Subject Code: 18CPS23
PDF
No ratings yet
Subject: C' Programming For Problem Solving Subject Code: 18CPS23
16 pages
Data Types in C (A Deeper Dive) : ESC101: Fundamentals of Computing Nisheeth
PDF
No ratings yet
Data Types in C (A Deeper Dive) : ESC101: Fundamentals of Computing Nisheeth
21 pages
Automatic Conversion Type Casting Otherwise
PDF
No ratings yet
Automatic Conversion Type Casting Otherwise
19 pages
Basics 2
PDF
No ratings yet
Basics 2
17 pages
3-Elements of C-31-07-2023
PDF
No ratings yet
3-Elements of C-31-07-2023
31 pages
What Is C
PDF
No ratings yet
What Is C
40 pages
Lecture 2
PDF
No ratings yet
Lecture 2
37 pages
Lecture2 - Variables Data-Types Arithmetic-Expressions
PDF
No ratings yet
Lecture2 - Variables Data-Types Arithmetic-Expressions
37 pages
Computing Fundamentals: Dr. Muhammad Yousaf Hamza
PDF
No ratings yet
Computing Fundamentals: Dr. Muhammad Yousaf Hamza
27 pages
Type Conversion in C
PDF
No ratings yet
Type Conversion in C
11 pages
Identifiers - Keywords - Data Types - Data Type Conversions
PDF
No ratings yet
Identifiers - Keywords - Data Types - Data Type Conversions
13 pages
Type Conversion in C
PDF
No ratings yet
Type Conversion in C
8 pages
Lec 4 Programming Fundamentals (1) (1)
PDF
No ratings yet
Lec 4 Programming Fundamentals (1) (1)
23 pages
e102f23Lecture004
PDF
No ratings yet
e102f23Lecture004
26 pages
1.3.docx
PDF
No ratings yet
1.3.docx
12 pages
PART 2
PDF
No ratings yet
PART 2
15 pages
C Data Types (2)
PDF
No ratings yet
C Data Types (2)
24 pages
Basic Types and Formatted Io 170609161349
PDF
No ratings yet
Basic Types and Formatted Io 170609161349
26 pages
Unit 2 Notes
PDF
No ratings yet
Unit 2 Notes
26 pages
C-Programing Note PDF by Nafi
PDF
No ratings yet
C-Programing Note PDF by Nafi
22 pages
16 Type Casting
PDF
No ratings yet
16 Type Casting
9 pages
SCS101 Lecture 5
PDF
No ratings yet
SCS101 Lecture 5
7 pages
Jayashree Type Conversion
PDF
No ratings yet
Jayashree Type Conversion
5 pages
pps type conversion
PDF
No ratings yet
pps type conversion
7 pages
Scanf (Continued) and Data Types in C: ESC101: Fundamentals of Computing Nisheeth
PDF
No ratings yet
Scanf (Continued) and Data Types in C: ESC101: Fundamentals of Computing Nisheeth
26 pages
5_Typecasting - Copy
PDF
No ratings yet
5_Typecasting - Copy
19 pages
C - Type Casting - Tutorialspoint
PDF
No ratings yet
C - Type Casting - Tutorialspoint
3 pages
Variables and Data Types Numeric Data: Sen119 / Sen121 Assoc. Prof. Yakup Çelikbilek, Phd. 27.09.2021
PDF
No ratings yet
Variables and Data Types Numeric Data: Sen119 / Sen121 Assoc. Prof. Yakup Çelikbilek, Phd. 27.09.2021
4 pages
Lecture 11
PDF
No ratings yet
Lecture 11
6 pages
II Year - C Worksheet 2 (1)
PDF
No ratings yet
II Year - C Worksheet 2 (1)
8 pages
KCA102 Unit1 - Datatypes
PDF
No ratings yet
KCA102 Unit1 - Datatypes
12 pages
Explain, Type Conversion in Expressions
PDF
No ratings yet
Explain, Type Conversion in Expressions
2 pages
Type Conversion in C
PDF
No ratings yet
Type Conversion in C
4 pages
Type Conversion in C
PDF
100% (1)
Type Conversion in C
107 pages
Type conversion
PDF
No ratings yet
Type conversion
12 pages
typecasting
PDF
No ratings yet
typecasting
7 pages
Data Types in C-1
PDF
No ratings yet
Data Types in C-1
13 pages
Computer Science Unit-1 - 1 Type Conversion Vs Type Casting Sem1
PDF
No ratings yet
Computer Science Unit-1 - 1 Type Conversion Vs Type Casting Sem1
1 page
Type Conversion in C
PDF
No ratings yet
Type Conversion in C
3 pages
Programming-Arduino (1) - Pages-78
PDF
No ratings yet
Programming-Arduino (1) - Pages-78
1 page
Cse115 Lab 04
PDF
No ratings yet
Cse115 Lab 04
5 pages
8-Type Conversion in C
PDF
No ratings yet
8-Type Conversion in C
2 pages
C 9
PDF
No ratings yet
C 9
13 pages
Ch 3 Type Casting in C
PDF
No ratings yet
Ch 3 Type Casting in C
3 pages
C 29
PDF
No ratings yet
C 29
11 pages
C 17
PDF
No ratings yet
C 17
9 pages
C 14
PDF
No ratings yet
C 14
9 pages
C 31
PDF
No ratings yet
C 31
8 pages
C 30
PDF
No ratings yet
C 30
8 pages
C 45
PDF
No ratings yet
C 45
8 pages
C 25
PDF
No ratings yet
C 25
8 pages
C 26
PDF
No ratings yet
C 26
8 pages
C 1
PDF
No ratings yet
C 1
8 pages
C 52
PDF
No ratings yet
C 52
7 pages
C 8
PDF
No ratings yet
C 8
7 pages
C 32
PDF
No ratings yet
C 32
6 pages
C 43
PDF
No ratings yet
C 43
6 pages
C 33
PDF
No ratings yet
C 33
6 pages
C 36
PDF
No ratings yet
C 36
6 pages
C 22
PDF
No ratings yet
C 22
6 pages
C 12
PDF
No ratings yet
C 12
6 pages
C 21
PDF
No ratings yet
C 21
9 pages
C 39
PDF
No ratings yet
C 39
8 pages
C 55
PDF
No ratings yet
C 55
5 pages
C 50
PDF
No ratings yet
C 50
5 pages
C 41
PDF
No ratings yet
C 41
5 pages
C 27
PDF
No ratings yet
C 27
5 pages
C 5
PDF
No ratings yet
C 5
5 pages
C 6
PDF
No ratings yet
C 6
5 pages
C 2
PDF
No ratings yet
C 2
5 pages
C 44
PDF
No ratings yet
C 44
9 pages
C 3
PDF
No ratings yet
C 3
6 pages
C 20
PDF
No ratings yet
C 20
5 pages