Skip to main content

Cast and Sizeof Operators in C

Typecasting in C

In C, typecasting is the process of converting a variable from one data type to another data type.

This is done using the typecasting operator, which is the word "cast" in parentheses, followed by the data type to which the variable is being converted, and the variable itself.

For example, to convert a float variable to an int variable, you would use the following syntax:

int myInt = (int) myFloat;

This would convert the value stored in myFloat to an int and store it in myInt.

danger

Casting can cause unexpected results or truncation of data if the value of the original variable is not within the range of the new data type.

Various types of typecasting in C

There are several types of typecasting in C programming language, including:

Implicit Typecasting

  • When more than one data type is present in an arithmetic expression then all the data types are converted automatically by the compiler to the data type of the highest rank to avoid data loss in that expression.

  • This automatic conversion of one data type into another datatype by the compiler during the execution of the program is regarded as Implicit Typecasting.

  • Also known as ‘automatic type conversion’.

For example, an int variable can be implicitly cast to a float variable.

#include <stdio.h>

int main() {
float myFloat = 3.14;
int myInt;

myInt = myFloat; // Implicit typecasting from float to int

printf("Value of myFloat: %f\n", myFloat);
printf("Value of myInt: %d\n", myInt);

return 0;
}
Output:

Explanation

  • In this program, a float variable myFloat is assigned the value 3.14.
  • Then, the value of myFloat is assigned to an int variable myInt without any explicit casting.
  • The compiler automatically performs implicit typecasting in this case, converting the float value to an int value.

Explicit Typecasting

  • When the user explicitly typecast one data type into anothor using typecasting operator. This is also called "casting"

  • A runtime check is done to see if the destination type can hold the source value prior to the conversion being carried out.

For example, a float variable can be explicitly cast to an int variable using the (int) operator.

#include <stdio.h>

int main() {
float myFloat = 3.14;
int myInt;

myInt = (int)myFloat; // Expicit typecasting from float to int using (int)

printf("Value of myFloat: %f\n", myFloat);
printf("Value of myInt: %d\n", myInt);

return 0;
}
Output:

Explanation

  • In this program, a float variable myFloat is assigned the value 3.14.
  • Then, the value of myFloat is assigned to an int variable myInt using explicit casting (int).

"sizeof" Operator

  • sizeof a compile-time unary operator and used to determine the size (in bytes) of its operand.
  • It simply returns the amount of memory allocated to that data type.

For example:

sizeof(int) will return the number of bytes required to store an int variable.

  • Sizeof can be used on expressions or variable names.
  • Sizeof is a compile-time operator and it's evaluated by the compiler.

Example

Here is an example program that shows the use of the sizeof operator to determine the size of various variable types in C:

#include <stdio.h>

int main() {
printf("Size of int: %ld bytes\n", sizeof(int));
printf("Size of short int: %ld bytes\n", sizeof(short int));
printf("Size of long int: %ld bytes\n", sizeof(long int));
printf("Size of float: %ld bytes\n", sizeof(float));
printf("Size of double: %ld bytes\n", sizeof(double));
printf("Size of char: %ld bytes\n", sizeof(char));
printf("Size of long double: %ld bytes\n", sizeof(long double));
return 0;
}
Output:

Explanation

  • This program uses the printf function to print the size of various variable types, such as int, short int, long int, float, double, char, and long double, in bytes.

  • The sizeof operator is used to determine the size of each variable type, and the result is passed as an argument to the printf function using the %ld format specifier, which is used to print long integers.

Output:

Size of int: 4 bytes
Size of short int: 2 bytes
Size of long int: 8 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 bytes
Size of long double: 16 bytes
note

Please note, the sizes of the data types can vary depending on the architecture, operating system, and compiler used.