Typecast vs. typedef in C
Typecast vs. typedef in C
Typecast
In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a way of making one variable of one data type present in the C standard and making it act like some other data type present within the C language, such as a programmer can initiate a data type of int and make it work like another data type like char or float for one single operation.
To type cast, a variable is to put that particular variable to an actual variable to act as a parenthesis in front of the actual variable. In the type casting method, the compiler will immediately and automatically change the type of data given by a programmer to another if it makes sense.
For example, if a programmer assigns an integer value to a floating-point variable, the compiler automatically converts the integer value to a floating-point value. Type casting allows a developer to make these types of conversions explicit or might force it when it does not usually happen.
Syntax
(type name) expression
Types of typecasting
There are two types of typecasting in the C standard:
- Implicit Type casting
Whenever the type conversion is performed automatically by the compiler you are working on without the programmer’s intervention, such type conversion is known as implicit type conversion or type promotion.
int x; for (x = 97; x <= 150; x++) { printf (“%c”, x); /* it is implicitly converted from int to char }
- Explicit Type casting
This is the type of conversion that happens due to human intervention. A developer will perform it by posing the type of the variable. This type is also referred to as type casting.
int x; for (x = 97; x <= 150; x++) { printf (“%c”, (char)x); /* it is explicitly converted from int to char }
Rules to be followed while doing a type conversion:
- All the character types must be converted to integer types.
- All the integer types to be converted to float.
- All the float types to be converted to a double.
E.g.:
#include <stdio.h> int main() { int sum = 17, count = 5; double mean; mean = (double)sum / count; printf (“Value of mean: %f \n”, mean); }
Output
Typedef
In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to provide meaningful names to already existing variables in the program. It acts similarly as we define initially as a developer defines the alias name for the following commands. This keyword is used to redefine the name of an already present variable in the C standard.
Syntax
typedef <existing name> <alias name> Or typedef type TYPE_NAME; Where;
Existing name: name of an existing variable.
Alias name: name given to the existing variable by a developer.
#include <stdio.h> #include <conio.h> int main() { typedef unsigned int tutorial; tutorial i = 5, j = 8; printf("i = %d\n", i); printf("j = %d\n", j); return 0; }
Output
For instance, we can define a term BYTE for one-byte numbers such as:
typedef unsigned char BYTE;
Once an alias name ‘BYTE’ is given to a pre-defined term ‘unsigned char’, the identifier ‘BYTE’ can be used as an abbreviation for the same.
E.g.:
BYTE b1, b2;
The uppercase letters used in the above example definitions is to remind the programmer that the type name is just a symbolic representation. One can use either uppercase or lowercase or a combination of both uppercase and lowercase letters while defining in the beginning.
E.g.:
typedef unsigned char byte;
Typedef can also be used to give a name to a user defined data type also. Typedef can be used along with the structure to define a new data type and then can be used to define the structure variables as such.
#include <stdio.h> #include <conio.h> #include <string.h> typedef struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } Book; int main( ) { Book book; strcpy( book.title, "C Programming"); strcpy( book.author, "Dennis Ritchie"); strcpy( book.subject, "Tutorials and examples"); book.book_id = 110291; printf( "The title of the book is: %s \n", book.title); printf( "The author of the book is : %s\n", book.author); printf( "The subject of the study is : %s\n", book.subject); printf( "The ID of the book is (book_id) : %d\n", book.book_id); return 0; }
Output
Difference between typecast and typedef
S.No. | Typecast | Typedef |
1 | In typecasting, a programmer converts the data type from one form to another. | In typedef, a compiler converts the data type from one form to another. |
2 | Type cast applies to all the data types, whether they may be compatible or incompatible. | Typedef is only applicable to compatible data types. |
3 | Type casting is more reliable, readable, efficient. | Typedef is less reliable, less readable and efficiency is less. |
4 | Typecast is more often used in competitive programming. | Typedef is less likely to be used in the competitive world as it is more prone to errors. |
5 | Typecast happens during the program design process. | Typedef takes place during compile time process. |
6 | The destination data type in type cast is smaller than the source data type while converting the data types. | While in type conversion, the destination data type cannot be smaller than the source data type. |