[C Language] advanced | data type: integer, floating point, logic, type conversion and conditional operation, Data Type Floating Point

Source: Internet
Author: User

[C Language] advanced | data type: integer, floating point, logic, type conversion and conditional operation, Data Type Floating Point

---------------------------------------------------------------------------------

[C language type]

Integer:

Char, short, int, long, long

Floating point:

Float, double, long double

Logic:

Bool

Pointer

 

Custom type

 

[What are the differences between types]

Type name: int, long, double

Formatting for input and output: % d, % ld, % lf

The value range of the expressed number: char <short <int <float <double

Memory size: 1 byte to 16 bytes. the sizeof () is used to obtain the size. sizeof is a static operator, and its result is determined at the compile time, the sizeof operation is not calculated.

Expression in memory: binary (complement), encoding

 

[Integer type]

// main.c
// Created by weichen on 15/3/25.
// Copyright (c) 2015 weichen. All rights reserved.
#include <stdio.h>

int main (int argc, const char * argv []) {
    printf ("sizeof (char) =% ld \ n", sizeof (char)); // 1 byte (8bite)
    printf ("sizeof (short) =% ld \ n", sizeof (short)); // 2 bytes
    printf ("sizeof (int) =% ld \ n", sizeof (int)); // 4 depends on the compiler (cpu), usually meaning "1 word"
    printf ("sizeof (long) =% ld \ n", sizeof (long)); // 8 depends on the compiler (cpu), usually meaning "1 word"
    printf ("sizeof (long long) =% ld \ n", sizeof (long long)); // 8 8 bytes

    // gcc ./test.c -o -m32 #Compile and display with 32 platform architecture
    
    
    char c = 255;
    int i = 255;
    printf ("c =% d, i =% d \ n", c, i); // c = -1, d = 255
    
    unsigned char d = 255; // Used to indicate that integers are not represented in two's complement form, as pure binary
    printf ("d =% d \ n", d); // d = 255
    
    char e = 127;
    e = e + 1;
    printf ("e =% d \ n", e); //-128
    
    unsigned char f = 127;
    f = f + 1;
    printf ("f =% d \ n", f); // 128
    
    return 0;
}

// int is used to express registers
/ *
 The computer expresses positive integers in binary, and complements negative numbers:
 
    0-> 00000000
    1-> 00000001
 
    11111111 + 00000001-> 1000000000 \
                                      |
    00000000-00000001-> (negative 1) |
                                      |
 (1) 00000000-00000001-> 11111111 /

   ① 11111111 is 255 when it is treated as pure binary, and -1 when it is treated as complement.
   ② Similarly, for -a, it is actually (2 ^ n-1), n is the number of bits of this type; the complement is 0-a
   ③ Two's complement means that taking the two's complement and the original code can add an overflowing "zero"
* /


// range of numbers
/ *
 One byte (8 bits), which expresses:
 00000000-11111111

 Where 00000000-> 0
     11111111 ~ 10000000-> -1 ~ -128 // Expressed in two's complement
00000001 ~ 01111111-> 1 ~ 127 // Pure binary representation
* /

// unsigned
/ *
 If a literal constant wants to express itself as unsigned, you can add u or U to the end
    255U
 Use l or L for long (long)
 The original purpose of unsigned is not to extend the range that numbers can express, but to do pure binary operations, mainly to shift.
 * /

// Integer out of bounds
/ *
 Integers are calculated in pure binary, so:
 11111111 + 1-> 100000000-> 0
 01111111 + 1-> 10000000-> -128
 10000000-1-> 01111111-> 127
 * /

// Integer input and output
/ *
 Only two forms: int or long long
 % d: int
 % u: unsigned
 % ld: long long
 % lu: unsigned long long
 * /

// octal and hexadecimal
/ *
 A numeric literal starting with 0 is octal
 A numeric literal starting with 0x is hexadecimal
 % o for octal and% x for hexadecimal
 Octal and hexadecimal are just how to express numbers as strings, and have nothing to do with how numbers are expressed internally
 
 Hexadecimal is very suitable for expressing binary data, because 4-bit binary is exactly a hexadecimal digit
 One digit in octal represents exactly three digits in binary
    Because the word length of early computers was a multiple of 12, not 8
 * /

// Select integer type
/ *
 Why are there so many integers?
    In order to accurately express memory, the need to do low-level programs
 No special needs, choose int
    The word length of current CPUs is generally 32-bit or 64-bit. A memory read and write is an int, and a calculation is also an int. Choosing a shorter type will not be faster or may be slower
    Modern compilers generally involve memory alignment, so shorter types may actually occupy the size of an int in memory (though sizeof tells you it is smaller)
 Unsigned is not the same as the output, the internal calculation is the same, so most modern high-level languages do not distinguish non-negative.
 * /

 

[Floating point type]

// main.c
// Created by weichen on 15/4/6.
// Copyright (c) 2015 weichen. All rights reserved.
#include <stdio.h>

int main (int argc, const char * argv []) {
    // floating point type
    / *
     Type Word Length Range Significant Number
     float 32 ± (1.20 * 10 ^ -38 ~ 3.40 * 10 ^ 38), 0, ± inf (infinity), nan (not a number) 7 (accurate digits)
     double 64 ± (2.2 * 10 ^ -308 ~ 1.79 * 10 ^ 308), 0, ± inf, nan 15
     
     Type scanf printf
     float% f% f,% e (scientific notation)
     double% lf% f,% e
     * /
    
    double ff = 12.345;
    printf ("% e,% f \ n", ff, ff); //1.234500e+01, 12.34500
    printf ("% E \ n", ff); //1.234500E+01
    
    // Add .n between% and f to specify the output digits after the decimal point, such output is rounded
    printf ("%. 3f \ n", -0.0049); //-0,005 three digits after the decimal point
    printf ("%. 30f \ n", -0.0049); //-0.004899999999999999841793218991
    printf ("%. 3f \ n", -0.00049); //0.000
    
    printf ("% f \ n", 1.0 / 0.0); // inf
    printf ("% f \ n", -1.0 / 0.0); //-inf
    printf ("% f \ n", 0.0 / 0/0); // nan
    // printf ("% d \ n", 12/0); // Unable to compile, infinity cannot be expressed as an integer, it can be expressed as a floating point number
    
    float a, b, c;
    a = 1.34f;
    b = 1.22f;
    c = a + b;
    if (c == 2.56) {
        printf ("a = b \ n");
    } else {
        printf ("a! = b, c =%. 10f or% f \ n", c, c); // a! = b, c = 2.5599999428 or 2.560000 (rounded up to seven significant digits)
    }
    
    // The precision of floating-point operations
    / *
     Literals with decimal point are double instead of float
     float requires f or F suffix to indicate identity
     float1 == float2 may fail
     fabs (f1-f2) <1e ^ -12 #Compare the absolute value and the minimum error to determine whether they are equal
     * /
     
    // Internal expression of floating point number
    / *
     Floating point numbers are implemented by dedicated hardware components when calculating
     The components used to calculate double and float are the same
     * /
    
    // Select floating point type
    / *
     If there is no special need, just use double
     Modern CPUs can directly perform hardware calculations on doubles, and the performance will not be worse than floats. On 64-bit machines, the data storage speed is not slower than floats.
     * /
    
    return 0;
}

 

[Logical type]

// main.c
// Created by weichen on 15/4/8.
// Copyright (c) 2015 weichen. All rights reserved.
#include <stdio.h>
#include <stdbool.h>

int main (int argc, const char * argv []) {
    // bool
    / *
     C language does not have a logical type. In internal calculations, integers are used to express the results of relational and logical operations. 0 means false, and values other than 0 represent true.
     In C99, there is no fixed logical type, but a header file defines the two values of true and false that can be used directly, and the type bool.
     Logical operations are an inherent component of the C language.
     
     #include <stdbool.h>
     Then you can use bool and true and false
    * /
    bool a = 6 <5;
    bool b = true;
    
    printf ("% d \ n", a); // 0
    printf ("% d \ n", b); // 1
    
    //logic operation
    / *
     Logic operations are operations on logical quantities, and the result is only 0 or 1
     Logical quantities are the result of relational or logical operations
     !! Logical negation
     && Logical AND
     || logical OR
     
     If you want to express intervals in mathematics, such as: x∈ (4,6) or x∈ [4,6] How should you write an expression for C?
     Formulas like 4 <x <6 are not formulas that C can calculate correctly, because the result of 4 <x is a logical value (0 or 1),
     Should be written as (x> 4 && x <6) or (x> = 4 && x <= 6)
     Determine if a character a is an uppercase letter? a> = 'A' && a <= 'Z'
     
     Precedence operator associativity
     1 () from left to right
     2! +-++-right to left (monocular + and-)
     3 * /% from left to right
     4 +-from left to right
     5 <<=>> = left to right
     6 ==! = From left to right
     7 && from left to right
     8 || from left to right
     9 = + =-= * = / =% = right to left
     
     * /
    
    return 0;
}

 

[Type conversion and conditional operation]

// main.c
// Created by weichen on 15/4/8.
// Copyright (c) 2015 weichen. All rights reserved.
#include <stdio.h>

int main (int argc, const char * argv []) {

    // Automatic type conversion
    // When there are inconsistent types on both sides of the operator, it will be automatically converted to a larger type, which can express a larger range of numbers
    
    // char-> short-> int-> long-> long long
    // int-> float-> double
    
    // For printf, any type less than int will be converted to int; float will be converted to double
    // But sachf doesn't. To input short, you need% hd, and to enter long long, you need% ld.
    
    
    // cast has higher priority than four operations
    // 1. (Type) value
    double a = 1.0;
    double b = 2.0;
    int i = (int) a / b;
    int j = (int) (a / b);
    
    int c = 5;
    int d = 6;
    double e = (double) (c / d);
    
    printf ("% d \ n", i);
    printf ("% d \ n", j);
    printf ("% lf \ n", e);
    
    // 2. Small variables do not always express large quantities
    printf ("% d \ n", (short) 32768); //-32768, short can represent 32767
    printf ("% d \ n", (char) 32768); // 0
    
    // 3. Casting just calculates a new type value from that variable, it does not change that variable, neither the value nor the type
    int k = 32768;
    short kk = (short) k;
    printf ("% d \ n", k); // 32768
    
    // conditional operator
    // c = (count> 20)? count-10: count + 10;
    // The precedence of the conditional operator is higher than the assignment operator, but lower than other operators
    
    
    // comma operator
    // Comma is used to concatenate two expressions and use the value of the expression to its right as its result. The precedence of a comma is the lowest of all operators, so the expressions on both sides of it are evaluated first; the combination of commas is from left to right, so the expression on the left is evaluated first, and the value of the expression on the right It is left as the result of the comma operation.
    // Use in for, for (i = 0, j = 0; i <j; i ++, j-) {}, where the comma divides the two expressions.
    int n;
    int m;
    n = 1 + 2,3 + 4; // n = 1 + 2 is an expression, 3 + 4 is not used, the final result is 3
    m = (1 + 2,3 + 4); // combination relationship, the value of the expression on the right is the result
    printf ("% d \ n", n); // 3
    printf ("% d \ n", m); // 7
    
    return 0;
} 

 

Link: http://www.cnblogs.com/farwish/p/4382543.html

@ Blackeye poet <www.farwish.com>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.