0% found this document useful (0 votes)
52 views

A First C Program (Mixing Datatypes) : Department of Computer and Information Science, School of Science, IUPUI

This document discusses data type conversion in C programming. It provides two rules for implicit type conversion: 1) char, short convert to int, float converts to double, and 2) if either operand is double, the type converts to double, if either is long it converts to long, if either is unsigned it converts to unsigned, otherwise the type is int. It then gives examples of expressions and their resulting data types. It notes that conversion of int to long preserves sign and fractions may be eliminated in certain conversions. The document also discusses using a cast operator to require a specific type and details conversion between unsigned int and int.

Uploaded by

Anjali Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

A First C Program (Mixing Datatypes) : Department of Computer and Information Science, School of Science, IUPUI

This document discusses data type conversion in C programming. It provides two rules for implicit type conversion: 1) char, short convert to int, float converts to double, and 2) if either operand is double, the type converts to double, if either is long it converts to long, if either is unsigned it converts to unsigned, otherwise the type is int. It then gives examples of expressions and their resulting data types. It notes that conversion of int to long preserves sign and fractions may be eliminated in certain conversions. The document also discusses using a cast operator to require a specific type and details conversion between unsigned int and int.

Uploaded by

Anjali Naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computer and Information Science,

School of Science, IUPUI

A First C Program
(mixing datatypes)
Dale Roberts, Lecturer
Computer Science, IUPUI
E-mail: [email protected]

Dale Roberts

Data Type Conversion


Rule #1
char, short
float

int
double

Rule #2 (double long unsigned int)


If either operand is double, the other is converted to
double, and the result is double
Otherwise, if either operand is long, the other is
converted to long, and the result is long
Otherwise, if either operand is unsigned, the other is
converted to unsigned, and the result is unsigned
Otherwise, the operand must be int
Dale Roberts

Examples
Example: c: char, u: unsigned, i: int, d: double, f:float,
s: short, l: long,
Expression

Final Data Type

c s / i

int
unsigned

u * 3 i

Explanation

shortint, int/int, charint, int-int


int(3)unsigned,
unsigned*unsigned=unsigned,
intunsigned, unsigned-unsigned=unsigned

u * 3.0 i

double

unsigneddouble, double*double,
intdouble, double-double=double

c + i

int
double
long

char int

c + 1.0
3 * s * l

char int (rule 1), intdouble(rule 2)


shortint, int*int, intlong, long*long

Dale Roberts

Data Type Conversion (cont.)


Note:
1.
2.

Conversion of int to long preserves sign, so does short


Var = expr
f = d; /* round off */
i = f;
/* truncates fractions part, if the number is too big to fit, the result is
undetermined */

i = l; s = i; and c = i; /* may eliminate high order


bits */

Dale Roberts

3.

If a specific type is required, the following syntax may be used,


called cast operator.

(type) expr
Example:
float f=2.5;
x = (int)f + 1;
/* the result is 3, Q: will f value be changed? */
4.

Unsigned int to int:


there is not actual conversion between int and unsigned int.
Example:(Assuming 2s complement machine and int is 2 bytes long)
unsigned i = 65535; int j;
j = i;
/* j will be 1 */
j = -2;
unsigned i = 1 + j;
/* i= 65535 */
Dale Roberts

You might also like