Getting Started with C language - data types

First, type C language

  1. Integer: char, short, int, long, longlong
  2. Float: float, double, long double
  3. Logic: bool
  4. pointer
  5. Custom Types

What different types

  1. Type name: int, long, double
  2. Input and output formats:% d,% ld,% lf
  3. Range expressed by number: char <short <int <float <double
  4. Occupied by memory size: 1 byte to 16 bytes
  5. Expression memory form: binary (complement) encoding

sizeof

Is an operator, or the type of a given variable number of bytes in the memory occupied,

Is a static operator that results in compile time decided not to do the operation in parentheses sizeof

  1. sizeof(int)
  2. sizeof(i)
#include <stdio.h>

int main()
{
    int a;
    a = 6;

    printf("sizeof(int)=%ld\n", sizeof(int));
    printf("sizeof(double)=%ld\n", sizeof(double));
    printf("sizeof(a)=%ld\n", sizeof(a));

    return 0;
}

//sizeof(int)=4
//sizeof(double)=8
//sizeof(a)=4

Second, the integer type

  1. char: 1 byte (8 bits)
  2. short: 2 bytes
  3. int: depending on the compiler (cpu), usually a word in the sense of
  4. long: depending on the compiler (cpu), usually a word in the sense of
  5. longlong: 8 bytes

Integer internal representation

Everything inside the computer binary

  1. 18 ------> 00010010
  2. 0 ------> 000000
  3. -18 ------> ?
  4. Negative expressed in two's complement form of the original code

Anti-code:

Positive anti same code and the original code, negative inverse of the number of code symbols in addition to the original sign bit inverted Members [negated each (except the sign bit)]. The inversion means: original 1, to obtain 0; 0 is the original to give 1. (1 to 0; 0 to 1)

Complement:

Anti-complement code = 1 +

Here for example with a sub--1

  1. Binary: 00000001 (1)
  2. Anti-code: 11111110
  3. Complement: 11111111

1 expression is so full binary 1 in the computer

to sum up:

  1. Positive anti-code and complement are the same as the original code
  2. Negative inverse of the code number of the original code except the sign bit inverted you
  3. Negative complement to everybody except the sign bit inverted the original code number, and then in the last plus 1

Range of integers

  1. For a byte (8 bits), may indicate: 00000000-11111111
  2. among them:
    • 00000000 ------> 0
    • 11111111 ~~~ 10000000 ------> -1 ~~ -128
    • 00000001 ~~~ 01111111 ------> 1 ~ 127
  3. char: 1 byte: -128 to 127
  4. short: 2 bytes: -32768 to 32767
  5. int: depending on the compiler (cpu), the usual meaning of "one word"
  6. long: 4 bytes
  7. longlong: 8 bytes

unsigned

  1. If you want to express yourself is a constant unsigned, you can add a U or u later
  2. L or L represents a long
  3. unsigned original intention was not able to extend the range of the number of expression, but to do pure binary operations, primarily shift
#include <stdio.h>

int main()
{
    unsigned char c = 255;
    int i = 255;

    printf("c=%d,i=%d\n", c , i);
    // c=-1,i=255

    // 当使用了unsigned之后 c=255,i=255


    return 0;
}

Integer cross-border

Pure binary integer is calculated, so

  1. 11111111 + 1 ------> 100000000 ------> 0
  2. 01111111 + 1 ------> 10000000 ------> -128
  3. 10000000 - 1 ------> 01111111 ------> 127

I.e. in the computer 127 + 1 = -128, -128 -1 = 127

    char a = 127;
    char b = -128;
    char c = a + 1;
    char d = b - 1;
    printf("%d, %d\n", c , d );
    //  -128, 127

As shown in FIG: Save clockwise, counterclockwise plus

Input and output integers

Only two forms: int or long long

  1. %d:int
  2. %u:unsigned
  3. %ld:long long
  4. %lu:unsigned long long

Octal and hexadecimal

  1. A number literal beginning with 0 are octal
  2. A number literal begins with 0x are hexadecimal
  3. % O for octal,% x for hexadecimal
  4. Octal and hexadecimal numbers just how a string expression, how to express numbers has nothing to do with the internal
    char c = 012;
    int i = 0x12;

    printf("c=%d, i=%d\n", c,i);
    printf("c=0%o, i=0x%x\n", c,i);
    // c=10, i=18
    // c=012, i=0x12

Select the type integer

There is no particular need to choose int

Third, the floating-point types

Types of Word length range effective number
float 32 7
double 64 15

Input and output

Types of scanf printf
float %f %f、%e
double %lf %f、%e

Wherein% e scientific notation

#include <stdio.h>

int main()
{
    double a = 12.321;
    printf("%e\n", a );
    //1.232100e+001
    return 0;
}

Output Accuracy

After adding .n you can specify the output f and a decimal point between several%, so the output is made round 4 of the 5

Out of range of floating-point numbers

  1. printf output out of range of floating-point represented inf: + ∞, -∞
  2. printf output nan floating-point representation does not exist

Precision floating-point operations

    float a , b , c ;

    a = 1.345f;
    b = 1.123f;

    if (c == 2.468)
    {
        printf("相等\n");
    }else
    {
        printf("不相等\n");
    }
//不相等

Literal with a decimal point is double instead of float

float need f or F suffix to indicate identity

Select the type of floating-point

No special need for direct use double

Character Types

  1. char is an integer, is a special type: Character
  2. Literal character represented by single quotes: 'a', '1'
  3. printf and scanf with% c in output to input characters
    char c;
    c = '1';
    printf("%d\n", c);  // 49  ascll
    printf("%c\n", c);  // 1
    

Escape character

Expression can not be used to print a special character or a control character, it is the beginning of a backslash back to keep the other characters, and a character composed of

printf("请输入身高和体重,如果输入\"170 80\"表示170cm和80kg\n");
  1. \ B: a fallback grid, \ t: on to the next table position
  2. \ N: line feed, \ r: Enter
  3. ": Double quotation marks, ': single quote
  4. \: The backslash itself

Automatic type conversion

  1. When both sides of the operation appears inconsistent with the type, the type of conversion occurs automatically, automatically converted to the larger type
  2. For printf, less than any type are converted to an int int, float will be converted into double
  3. But scanf not to enter short, needs% hd

Cast

(Type) value

For example: (int) 10.2

Cast precedence over four operators

bool

  1. include <stdbool.h>

  2. Then you can use bool and true, false
#include <stdio.h>
#include <stdbool.h>

int main()
{
    bool b = 6 > 5;
    bool t = true;

    printf("%d\n", b);
    // 1
    return 0;
}

Fourth, the logical operators

Logical operators are arithmetic logic, the result is only 0 or 1

Operators description Examples result
Logical NOT !a
&& Logic and a && b
|| Logical or a||b

priority

as the picture shows:

Short circuit

Logical operators are carried out from left to right, if the result of the left has been able to determine the outcome, it will not do the calculation on the right

  1. For && left is not doing the right when false
  2. For ||, the left is not doing the right when true

Conditional operator

  1. count = (count > 20) ? count - 10 : count + 10;
  2. When the value of the condition, condition is satisfied, the value of the condition is not satisfied

Comma operator

    int i,j;

    i = 3+4,5+6;
    j = (3+4,5+6);
    printf("%d\n", i);
    printf("%d\n", j);
    // 7
    // 11

When parentheses, is the second calculation expression

Guess you like

Origin www.cnblogs.com/mengd/p/11599875.html