Lesson 4 of 31

Type Casting

Type Casting

Type casting converts a value from one type to another. C performs some conversions automatically (implicit) and allows you to request others explicitly.

Implicit Conversions

C automatically converts between compatible types when needed:

int a = 5;
long b = a;      // int -> long (safe, no data loss)
int c = 3.14;    // double -> int (truncates to 3)

Like a Changeling shapeshifting: the same underlying matter, reinterpreted as a different form.

Explicit Casts

Use the cast operator (type) to convert explicitly:

int a = 7;
int b = 2;
double result = (double)a / b;  // 3.5 (not 3)

Without the cast, 7 / 2 would be integer division (3). Casting a to double forces floating-point division.

Integer Truncation

Converting a larger type to a smaller one may lose data:

int big = 300;
char small = (char)big;  // 44 (300 % 256)

Characters and Integers

In C, char is an integer type. Every character has an ASCII value:

char c = 'A';
int ascii = c;      // 65
char next = c + 1;  // 'B' (66)

Common ASCII values:

CharacterValue
'0'48
'A'65
'a'97

Converting Digit Characters

char digit = '7';
int value = digit - '0';  // 7

Your Task

Write a function int to_upper(int c) that converts a lowercase letter to uppercase. If the character is not a lowercase letter, return it unchanged. Use the fact that 'a' is 97 and 'A' is 65 (a difference of 32). Print the results for 'h', 'i', '!', and 'A'.

TCC compiler loading...
Loading...
Click "Run" to execute your code.