×

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:

  1. 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

Typecast vs. typedef in C

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

Typecast vs. typedef in C

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

Typecast vs. typedef in C

Difference between typecast and typedef

S.No.TypecastTypedef
 1In 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.  
 2Type cast applies to all the data types, whether they may be compatible or incompatible.Typedef is only applicable to compatible data types.
 3Type casting is more reliable, readable, efficient.Typedef is less reliable, less readable and efficiency is less.
 4Typecast 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.
 5Typecast happens during the program design process.Typedef takes place during compile time process.
 6The 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.

Related Topics

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Attributes in C

Attributes are one of modern C++ key features. It is the process by which initiator can add more information to the language instead of introducing new keywords for every attribute....

4 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.