Press type in C
Type is a way to convert a variable from one data type to another. For example, when you want to store a long value for an integer, you must cast long to int. You can convert values from one type to another using the cast operator as follows:
( ten - kieu ) bieu_thuc
Consider the following example that the cast operator allows to split an integer variable to be executed as a floating-point operation:
#include main () { int sochia = 32 , sobichia = 6 ; double kq ; kq = ( double ) sochia / sobichia ; printf ( "Gia tri cua kq la: %fn" , kq ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); }
When executing the code, the following result is printed, the resulting variable has type double:
Compile and execute the above C program to see the results:
It should be kept in mind that the cast operator has a precedence over division, so the first sochia value is converted to a double and eventually it is divided by calculation in the double value field.
Type transforms can be hidden ie done automatically by the compiler, or it can be explicitly defined using a cast operator. It is good for you to use a cast operator anywhere that needs type conversion.
Integer upgrade in C
Integer upgrade is the process by which integer values smaller than int or unsigned int convert to type int or unsigned int . Suppose you have an example of adding a character to an int:
#include main () { int i = 21 ; char c = 'c' ; /* Gia tri ASCII la 99 */ int tong ; tong = i + c ; printf ( "Gia tri cua tong la: %dn" , tong ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); }
Compile and execute the above C program to see the results:
Here, the value of variable tong is 120 because the compiler performs an integer upgrade and converts the 'c' value to ACII before performing further operations.
Normal arithmetic transformation
The usual arithmetic transformation is the way to cast its value into a commonly used type. The first compiler will perform an integer upgrade, which converts from low to high, below the hierarchy:
Normal arithmetic conversion is not performed for assignment operators, for logical operators: && and ||. We follow the following example to understand this concept:
#include main () { int i = 21 ; char c = 'c' ; /* Gia tri ASCII la 99 */ float tong ; tong = i + c ; printf ( "Gia tri cua tong la: %fn" , tong ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); }
Compile and execute the above C program to see the results:
Here, the simple way to understand is that first value c turns into an integer, but because the final value is double, so the usual arithmetic transformation applies and the compiler transforms i and c into type float and get the result of adding float type.
According to Tutorialspoint
Previous post: Header File in C
Next lesson: Handling errors in C
You should read it
- Operator in programming C
- CAST function in SQL Server
- Operator in JavaScript
- Operator in C ++
- Operator in PHP
- Tips 'mental arithmetic' you can't find in textbooks
- The '+' operator in SQL Server
- FISHER function - The function returns the Fissher transformation at x in Excel
- How to Free Up Hard Disk Space on OS X with Squeeze
- Quick and accurate mental arithmetic tips make you surprised
- Overload the one-seat operator in C ++
- Load operator ++ and - in C ++
Maybe you are interested
How to Integrate ChatGPT for Siri on iPhone
How to get data from web into Excel
Top best apps to turn men into women, boys into girls
How to blur background images in PowerPoint - Adjust background image display mode
Instructions for creating PowerPoint background color effects - Create a new background for slides
7 Best Pinterest Alternatives