×

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

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

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

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.