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 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
May be interested
- How to press the Sino audio wall wireclicking on the wired network will be different from the usual wiring, because the network cable will follow this source of electricity in the wall to connect to the network.
- What does press any key to restart error mean? How to fix?a computer with a press any key to restart error is an extremely annoying problem. to fix the press any key to restart error on your computer, please read tipsmake's information immediately.
- How to Use Type to Siri to Eliminate Public Embarrassmentyou can use type to siri to skip awkward phone conversations while still getting the answers you need.
- What is USB type A? What is the connectivity of USB type A?what is usb type a? what is the connectivity of usb type a? outstanding advantages of usb type a compared to other types.
- What is USB Type-C, Lightning, which has a superior advantage?you've probably heard of usb type-c and lightning, so you understand what these connection standards are and how they have outstanding advantages? if not, read this article right away.
- Chromebooks can now warn users when connecting to an 'inappropriate' USB Type C cableusb type-c is a general-purpose connector standard, that's undisputed. however, not all usb type-c cables give the same transmission quality and support the same data transmission standards.
- Regular juicer or slow press are easier to cleanone of the most popular criteria when choosing to buy a juicer is cleaning the machine. so which type of juicer is easier to clean?
- Instructions for fixing errors Press Ctrl + Alt + Del to restartwhen i start the computer and see an error message, what should i do? press ctrl + alt + del to restart? please stay calm and find a solution together
- A close-up of the 1-pound 24K gold-molded hydraulic press machinethe close-up video of a bar of 1kg of 24k gold, worth up to 30,000-40,000 usd, is easily shaped or compressed by the hydraulic press many times but its value remains unchanged.
- How to type VNI in Windowsto type vni in windows, users have to change the typing method in vietnamese typing tools.