Data Types and Operators

2022. 5. 7. 00:09Coding - C

DATA TYPE SECTION: Whole and Real Numbers

 

For Whole Numbers (Numbers with a sign like + and - but without decimal places), if we add "unsigned" word, we can show the MSB (Most Significant Bit) size. Unsigned number can’t show the negative numbers, but we can present the two times the positive number. Signed number can present the negative and positive numbers, but it is usually not shown. 

http://www.tcpschool.com/c/c_datatype_basic

 

If the data goes over the MSB, then overflow occurs. It is when the number goes over the limited range of MSB, which can result in giving the wrong result since the leftover data runs over the limit. Same thing with underflow, which is when the number is less than the minimum bit that the data can hold.

 

 

 

 

For All Real Numbers, it has more larger range of bits. However, it applies to all computers that there are always errors when showing the numbers. For example, when we use double and float, since float has maximum accurate decimal number of6th, from the 7th decimal number, it is wrongly saved. Same thing with double, maximum accurate decimal number of double is 15th, so from the 16th decimal number, it is wrongly saved..

 

http://www.tcpschool.com/c/c_datatype_basic
http://www.tcpschool.com/c/c_datatype_basic

*When we save decimals, it is not exactly 1, because there is error range, as the decimal places, for example, float can only show until 6 decimal places, it can be 1.00000119 due to the error range. 

 

 

DATA TYPE SECTION: Type Conversion

 

To begin: When the data type goes from short bits to long bits (int --> Double), no data is lost, but if the other way around (Double --> int), then there is data loss.

 

Two types of data conversion: implicit data conversion, explicit type conversion

 

Implicit data conversion is one that happens automatically by the complier, from left to right, in a way that data loss is the least. For example, if we put 3.14 to int, then it only gives 3, which is a data loss of .14. But if we put 5 in double, it gives 5.000000, which isn't a data loss occurring. In solving math problem, if we put 5+3.14, the 5 would be converted to double to make the data loss least. The order is from char --> short --> int --> long --> float --> double --> long double.

 

Explicit data conversion is one that happens intentionally by the user. We would add a parenthesis and the data type we want to convert in it. For exammple, (double)num01 / num02. We call the (double/int/char) as a type cast

 

 

 

 

 

 

Operators

 

The operators are used for executing math problems.

 

Arithmetic operators are +, -, *, /, and %. They are most commonly used. 

http://www.tcpschool.com/c/c_operator_arithmetic

 

Assignment operators are used to assign a value, and they are sometimes used to operate a value after a arithmetic operator had occured. There are =, +=, -=, *=, /=, and %=.

http://www.tcpschool.com/c/c_operator_assignment

 

Increment and decrement operators are used to raise or lower a value by 1. There are x++, ++x, x--, --x. The difference between x++ and ++x is that code is run, then incremented after for x++, and the other way around for ++x. Same thing with x-- and --x. 

http://www.tcpschool.com/c/c_operator_incAndDec

 

Comparison operator is used to determine if the value is bigger, smaller, not equal, or equal. There are ==, !=, >, >=, <, and <=. It is always from the left to right.

http://www.tcpschool.com/c/c_operator_comparison

 

Logical operators are used to determine if the code is false or true. For example, && returns 1(true) if both statements are true and 0(false) if any one of them is false. There are &&, ||, and !.

http://www.tcpschool.com/c/c_operator_logic
http://www.tcpschool.com/c/c_operator_logic, Truth Table

 

Bitwise operator is used when the statement is run in a bit level (very small). There is &, |, ^, ~, <<, and >>.

http://www.tcpschool.com/c/c_operator_bitwise

For &, it will return 1 if both corresponding bits are 1.

http://www.tcpschool.com/c/c_operator_bitwise

For |, it will return 1 if both any one of the bits is 1.

http://www.tcpschool.com/c/c_operator_bitwise

For ^, it will return 1 if both bits are not equal to each other.

http://www.tcpschool.com/c/c_operator_bitwise

For ~, if the value is 0, it will return 1 and if value is 1, then it will return 0.

http://www.tcpschool.com/c/c_operator_bitwise

For <<(left shift) and >>(right shift), it will move the bits for certain amount to left or right.

 

 

Ternary operators are unique operators of C, it will be consisted of three parts. The format will be

Conditional Statement ? Value 1 / Value 2. If the conditional statement is true, it will return value 1 and if false, value 2.

For example, if value 1 is 15, and value 2 is 8, (value 1 > value 2) ? value 1 / value 2. The result will be value 1 since 15 is greater than 8, which is true for the conditonal statement. 

 

Comma operators are very simple, they are just used when either to show two statements into one (int num01 = 15, num 02 = 8), or to show two or more values (printf ("%d, %d", num01, num02). 

 

sizeof operators are used to tell how many bytes the value is. We can get data type, variable, and numbers, either whole or all real numbers. 

'Coding - C' 카테고리의 다른 글

Functions, Variable Scope, and Memory  (0) 2022.05.20
Control Flow Statement  (0) 2022.05.13
Kernel and the C  (0) 2022.04.22
Binary Notation & Conversion, Bits and Bytes  (0) 2022.04.15
C Foundation Review  (0) 2022.04.08