×

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

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

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.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

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

5 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.