0% found this document useful (0 votes)
23 views23 pages

Data Types and Type Conversions.13103

This document discusses data types and type conversion in the C language. It describes the basic and derived data types in C like integer, floating point, enumerated, pointer, array, structure, union, and function types. It also explains implicit and explicit type conversion. Implicit conversion is performed automatically by the compiler based on type compatibility to avoid data loss. Explicit conversion allows forcing a conversion using a cast operator. The document provides examples of common data types like char, int, float, and double. It discusses the advantages of type conversion and includes some practice quiz questions.

Uploaded by

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

Data Types and Type Conversions.13103

This document discusses data types and type conversion in the C language. It describes the basic and derived data types in C like integer, floating point, enumerated, pointer, array, structure, union, and function types. It also explains implicit and explicit type conversion. Implicit conversion is performed automatically by the compiler based on type compatibility to avoid data loss. Explicit conversion allows forcing a conversion using a cast operator. The document provides examples of common data types like char, int, float, and double. It discusses the advantages of type conversion and includes some practice quiz questions.

Uploaded by

rohan Kottawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

DATA TYPES AND TYPE

CONVERSION IN C
LANGUAGE
Introduction
• Data types in c refer to an extensive system used for declaring variables or functions of different types.
• The type of a variable determines how much space it occupies in storage and how the bit pattern stored
is interpreted.

The types in C can be classified as follows −

1. Basic Types
They are arithmetic types and are further classified into:
(a) integer types
(b) floating-point types.
Enumerated types
They are again arithmetic types and they are used to define variables that can only assign certain discrete
integer values throughout the program.

The type void


The type specifier void indicates that no value is available.

Derived types
They include :
(a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.
Following are the examples of some very common data types used in C:

• char: The most basic data type in C. It stores a single character and requires a single byte of memory in
almost all compilers.
• int: As the name suggests, an int variable is used to store an integer.
• float: It is used to store decimal numbers (numbers with floating point value) with single precision.
• double: It is used to store decimal numbers (numbers with floating point value) with double precision.

Different data types also have different ranges up to which they can store numbers. These ranges may
vary from compiler to compiler.
Type conversion in C Language
• It is a concept of converting one data type values into another data type. Typecasting is performing by
using the cast operator. The compiler will automatically change one type of data into another if it
makes sense.
• For instance, if you assign an integer value to a floating-point variable, the compiler will convert the
int to float. Casting allows you to make this type of conversion explicit, or to force it when it wouldn’t
normally happen

Why do we need Type Casting in C?


• If both data types are the same type then the return value will be the same.
• If both data types are different then among those two bigger ones will be the return type.
• When we are working with integral value with the combination of float then always return value
will be float type.
There are two types of type conversion:
Implicit Type Conversion
• Done by the compiler on its own, without any external trigger
from the user.
• Generally takes place when in an expression more than one data
type is present. In such condition type conversion (type
promotion) takes place to avoid lose of data.
• All the data types of the variables are upgraded to the data type
of the variable with largest data type.
• It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly
converted to unsigned), and overflow can occur (when long is implicitly converted to float).

Output
Explicit Type Conversion

The syntax in C: (type) expression


Output
Some Important Languages
C Language
C Language is a structure-oriented, middle-level programming language mostly used to develop low-level
applications.

C++
C++ is a general purpose, object-oriented, middle-level programming language and is an extension of C
language, which makes it possible to code C++ in a “C style”. In some situations, coding can be done in
either format, making C++ an example of a hybrid language.

C#
Pronounced C-sharp (not C-hashtag), C# is a multi-paradigm programming language that features strong
typing, imperative, declarative, functional, generic, object-oriented and component-oriented disciplines.
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.
Quiz

The subset of ‘int’ data type is __________

a) long, ulong, ushort


b) long, ulong, uint
c) long, float, double
d) long, float, ushort
Quiz

Answer - C
Quiz

Disadvantages of Explicit Conversion are?

a) Makes program memory heavier


b) Results in loss of data
c) Potentially Unsafe
d) None of the mentioned
Quiz

Answer - B
Quiz

‘Implicit Conversion’ follows the order of conversion as


per compatibility of data type as:

a) float < char < int


b) char < int < float
c) int < char < float
d) float < int < char
Quiz

Answer - B
Quiz
Quiz

Answer - B
Quiz
Quiz

Answer - A
THANK YOU!

You might also like