The compilation process and the data type (a)

The compilation process and the data type (a)


1. g ++ compilation process

Here Insert Picture Description

1.1 Pretreatment

  • Generated files: xxx.c generated by the (source) xxx.i (preprocessed file).

  • Tools: preprocessor (gcc compiler included in the tool set).

  • Generating command: gcc -E xxx.c -o xxx.i preprocessing parameters is wherein -E

  • effect:

    • Expand the header file

      • Expand stdio.h, stdlib.h header files and other content, and the specific content and expand together other source, is stored in the file xxx.i. (#Include <stdio.h> These commands pretreatment pretreatment does not exist in the file xxx.i, it has been replaced by a specific content)
      • This process is not responsible for checking the syntax error, a syntax error is in the compilation process. So, whether the first document written anything can be, no matter what format the header file suffix can be, and can be deployed in other source on xxx.i.

      You can test: Write a name for 111.txt header file, just write what is inside. Write a file name for the source text.c, wherein the header using 111.txt, namely: #include <111.txt>. The header and source files in a directory, and then execute gcc -E text.c -o text.i -I. This command can be seen in the generated text.i file, this header file random write , it has also been launched and the other remaining codes together anyway. (This last command adds a == - I ==, respectively, dashes, capital letters i, periods, the role is a source header files used in the same directory, otherwise without this -I.. if you can not find the files).

    • Replace macro definition

      • Will replace the macro name macro value.
      • Such as: the source file has #define PI 3.14, and uses the PI codes below, then transformed into a file which xxx.i pretreatment, #define PI 3.14disappear simultaneously, all the lower codes have become PI 3.14.
    • Replace comments

      • Single-line, multi-line comment will be replaced by a blank line in the xxx.i.
    • Expand the conditional compilation

      • The conditions to expand the code.
      • Such as: the presence of the following code, #ifdef PIthe Code is a condition, if you define PI, will put the code printf("PI has been defined.");into xxx.i which, at the same time #ifdef PIand #endifdisappear. If you do not define PI before then #ifdef PIand printf("PI has been defined.");, and #endifthis piece will no longer xxx.i display.
      #include<stdio.h>
      #define PI 3.14
      
      int main(void)
      {
          #ifdef PI
              printf("PI has been defined.");
          #endif
          return 0;
      }
      

1.2 compiler

  • Generated files: generating xxx.s (compiled file) by a xxx.i (preprocessed file).
  • Tools: Compiler (gcc compiler included in the tool set).
  • Generating command: gcc -S xxx.i -o xxx.s which is compiled parameter -S
  • effect:
    • Progressive check syntax errors, which is among the gcc compiler four steps the most important and time-consuming.
    • The C program translated into assembly instructions, get .s assembly files.

1.3 Compilation

  • The resulting file: generated by xxx.s (assembly file) xxx.o (the target file). Object files are binary machine language, humans can not read. (Note, though, is a binary file, but not open after these 101010, 101010 because we see is a series of strings, not recognized by the computer 101010).
  • Tools: Assembler (gcc compiler included in the tool set).
  • Generating command: gcc -c xxx.s -o xxx.o wherein the parameter is a compilation -c
  • effect:
    • Translation: The assembler instructions translated into a corresponding binary instructions.

1.4 Links

  • Generated files: generate xxx.exe (executable file) by the xxx.o (the target file). Executable files are binary machine language, humans can not read.
  • Tools: linker (gcc compiler is included in the set of tools).
  • Generating command: gcc xxx.o -o xxx.exe no link parameters
  • effect:
    • Library is introduced: the introduction of libraries, such as: library printf, system, scanf like function.
    • Combine multiple object files
    • The combined startup routine (routine is to call the main function)

1.5 Summary

  • The above process can refer to the following:

Here Insert Picture Description

  • The above process does not have to go step by step, it can be transformed directly from a front side by any instructions into any one behind, such as:
    • Can be changed from xxx.c xxx.i (instruction gcc -E xxx.c -o xxx.i),
    • Can become xxx.s from xxx.c (instruction gcc -S xxx.c -o xxx.s),
    • Xxx.c may become xxx.o (instruction gcc -c xxx.c -o xxx.o),
    • Xxx.c may become xxx.exe (instruction gcc xxx.c -o xxx.exe),
    • Can become xxx.s from xxx.i (instruction gcc -S xxx.i -o xxx.s),
    • Xxx.i may become xxx.o (instruction gcc -c xxx.i -o xxx.o),
    • Xxx.i may become xxx.exe (instruction gcc xxx.i -o xxx.exe),
    • And so on (exemplified in FIG lower part) ...
      Here Insert Picture Description

2. main function

2.1 main function in two standard forms

  • No arguments:int main(void) {return 0;}

  • There are arguments:int main (int argc, char *argv[]) {return 0;}

    (其中*argv[]可以写成**argv[])

2.2 main function other forms

  • int main()

  • void main()

  • int main(int c)

  • main()

  • main(int c)

  • Etc. Other ... although these are not standard form, but the compiler can accept.


3. Debugging

  • Debugging is used to find logic error occurred in the program, not for grammatical errors! The editor is not wrong thing.

  • The core idea: let the program line by line execution.

  • Line Number: Tools - Options - Text Editor - c / c ++ - line number (check).

  • Debugging process :

    • Adding breakpoints: one or more

      1. Method One: Left-click the breakpoint you want to add this line to the far left of the gray area, click again to cancel.

      2. Method Two: the mouse cursor stops at a breakpoint anywhere you want to add this line, press F9 to add, press F9 again disappear.

    • Debug: debug must be in Debug mode, Release mode can not be.

    • F5 to start debugging (different software shortcuts may be different, can be viewed in the toolbar above).

    • Program can be executed before the break, the first break point after the program is not executed.

    • Debug mode:

      1 **-by-statement is executed. ** shortcut keys F11, one by one statement to the next line, which meets function into the function (refer to custom function), the function continues to execute statements one by one, after the function is finished out of the tracking function to continue execution one by one.

      2 ** by the implementation process. ** shortcut keys F10, one by one process to the next line, meet function, do not enter the internal function, continue to track one by one execution.

      3. execution by breakpoints. When there are multiple breakpoints in the code, click on the toolbar above the "Continue" jump directly to the next breakpoint, no shortcuts.

      4. Bounce function: shift + 10, may be out of a function of the current breakpoint. (Unexecuted portion of the disposable performs the function)

Here Insert Picture Description


4. Variable

Three elements of variable 4.1

  • Variable types: integer, real, character, etc., to open the variable size of the memory space.
  • Variable names: for use in the program.
  • Variable values: the actual data stored in the variable.

4.2 Variable Definition

  • Defines the syntax: type name variable name = variable value. Such as:int a = 10;
  • Defined variable, the memory will open up a space corresponding to the variable, but the variable declaration does not open up memory space.

4.3 Variable declaration

  • grammar:
    • Method One: int a;variable does not define the variable value, that is, variable declarations.
    • Method Two: extern int a;Use the keyword extern declared.
  • characteristic:
    • If you want to use a variable, you must first define him.
    • The compiler before using variable looks forward the definition, if not defined, the compiler will automatically find the variable declaration, he was promoted to the definition.
    • On one, if is declared with extern, can not ascend.

5. Constant

Constants are not changed and will not be modified data. There are roughly three ways:

  • "Hello" is a string constant, 'a' is a character constant, 25 is a constant integer, real constant is 63.335.
  • Using define macro definitions (the most common), syntax: #define 宏名 宏值. such as:#define PI 3.14
    • Note that the macro name and the macro intermediate values ​​not equal sign, there is no end of a sentence symbols
  • Use the keyword const, syntax: const type name variable name = variable value, such as:const int a = 1;
    • a variable could have been, but after const keyword modification, it becomes a read-only variable.

6. Identifier

  • Variable name and generic name of the constant name.

6.1 mandatory requirement

  • Identified by letters, numbers, underscore (_) composition.
  • Keywords and identifiers can not be a function name, such as main, system, return, float etc. can not.
  • Identifier can not start with the numbers, letters and may begin with an underscore.
  • Case sensitivity:
    • Generally use capital letters to represent constants, such as:#define MAX 200
    • Typically use lower case letters for the variable, such as:int a = 10;

6.2 naming convention

  • Large hump Method: int HelloWorldHahaHohoHehe = 10;

    • Multiple-word variable names, capitalize the first letter of each word.
  • Small hump Method: int helloWorldHahaHohoHehe = 10;

    • Multiple-word variable names, the first letter of the first word lowercase, and the rest of each word capitalized.
  • + Lowercase underlined: int hello_world_haha_hoho_hehe = 10;

    • C language special!

7. sizeof keyword

  • sizeof is not a function, he is the keyword.
  • Used for obtaining the data type, variable with the size of the memory space.
  • Instructions:
    • sizeof (variable name) Returns a variable size, in bytes.
    • sizeof (data type) Returns the size of the memory space occupied by the type of data, in bytes.
    • Syntax sizeof variable name C language support for the wording, not commonly used.

8. printf function

  • printf function is formatted output.
  • The general form is:printf("格式匹配符", 输出表列);

8.1 format matcher d

Common situation is %d %md %-md %ld %lld %hd

  • %dIt represents the actual number of bits output in integer data.
  • %mdIndicates output data bits, if m is less than the actual number of bits of data, the actual number of bits according to the data output, if m is greater than the actual number of bits of data, the output m-bit result right-justified, padded with spaces left.
  • %-mdWhen m is greater than when the actual number of bits represented by the data, the m-bit output, the result left-justified, right padded with spaces.
  • %ldLetter l represents long integer. Modifications can be added m, n, minus the like, the rules above.
  • %lldLetters ll for long long integers. Modifications can be added m, n, minus the like, the rules above.
  • %hdIn the letter h represents a short integer. Modifications can be added m, n, minus the like, the rules above.
#include <stdio.h>
int main(void)
{
    int a = 222, m = 555;
    short int b = 3;
    long int c = 4;
    long long int d = 5;
    
    printf("%d\n",a);
    printf("%4d,%2d\n",a,a);
    printf("%4d,%-4d\n",a,a);
    printf("%04d,%4d\n",a,a);
    printf("%d + %d = %d\n",a, m, a+m);
    
    printf("%hd",b);
    printf("%ld",c);
    printf("%lld",d);
    
    return 0;
}

Here Insert Picture Description

8.2 format matcher f

  • Common form of:%f %m.nf %-m.nf %mf %.nf
  • %fIt is the real actual length of the output data.
  • %-m.nfM is a positive integer representing data width, comprising a fractional part, the integer part and a decimal point; n is a positive integer representing the number of decimal places.
#include <stdio.h>
int main(void)
{
    float a = 3.1425;
    printf("%f\n",a);
    printf("%5.2f\n",a);
    printf("%.2f\n",a);
    printf("%-5.2f\n",a);
    printf("%7.3f\n",a);
    printf("%f\n",a);
    printf("%4.8f\n",a);
    
    return 0;
}

Here Insert Picture Description

8.3 Character format matcher

  • Common forms% c, outputs a character.
#include<stdio.h>
int main(void)
{
    char c = 'A';
    printf("%c",c);
    return 0;
}

String format matcher 8.4

  • Common forms %s和%m.ns. m represents an output string length allows, n is the number of characters of the character string represents taken.
#include<stdio.h>
int main(void)
{
    printf("%s\n","ABCDEFGHI");
    printf("%3.2s\n","ABCDEFGHI");
    printf("%-3s,%3s\n","ABCDEFGHI","ABCDEFGHI");
    printf("%-.2s\n","ABCDEFGHI");
    printf("%12.2s,%-12.2s\n","ABCDEFGHI","ABCDEFGHI"); 
    printf("%12s,%-12s\n","ABCDEFGHI","ABCDEFGHI");
    
    return 0;
}

Here Insert Picture Description


9. integer

Obtaining the minimum value of the maximum value when the data type, requires the use of header files#include<limits.h>

9.1 Signed integer

name form Format matcher Footprint size Minimum Maximum
Short integer short (int) %hd 2 bytes -32768 32767
Integer int %d 4 bytes -2147483648 2147483647
Long integer long (int) %ld windows system 32 and 64 is 4 bytes; the Linux system, 32 is 4 bytes, 8 bytes 64 -2147483648 2147483647
Long integer long long (int) % LLD 8 bytes -9223372036854775808 9223372036854775807
#include <stdio.h>
#include <limits.h>

int main(void)
{
    printf("short的大小为:%d个字节\n",sizeof(short));
    printf("short的最小值是:%hd,最大值是:%hd\n",SHRT_MIN,SHRT_MAX);
    
    printf("int的大小为:%d个字\n",sizeof(int));
    printf("int的最小值是:%d,最大值是:%d\n",INT_MIX,INT_MAX);
    
    printf("long的大小为:%d个字节\n",sizeof(long));
    printf("long的最小值是:%ld,最大值是:%ld\n",LONG_MIN,LONG_MAX);
    
    printf("long long的大小为:%d个字节\n",sizeof(long long));
    printf("long long的最小值是:%lld,最大值是:%lld",LLONG_MIN,LLONG_MAX);
    
    return 0;
}

Here Insert Picture Description

9.2 unsigned integer

name form Format matcher Footprint size Minimum Maximum
Short integer unsigned short (int) % Hu 2 bytes 0 65535
Integer unsigned int in% 4 bytes 0 4294967295
Long integer unsigned long (int) % Lu windows system 32 and 64 is 4 bytes; the Linux system, 32 is 4 bytes, 8 bytes 64 0 4294967295
Long integer unsigned long long (int) % host 8 bytes 0 18446744073709551615
#include <stdio.h>
#include <limits.h>

int main(void)
{
    printf("unsigned short的大小为:%d个字节\n",sizeof(unsigned short));
    printf("unsigned short的最大值是:%hu\n",USHRT_MAX);
    
    printf("unsigned int的大小为:%d个字\n",sizeof(unsigned int));
    printf("unsigned int的最大值是:%u\n",UINT_MAX);
    
    printf("unsigned long的大小为:%d个字节\n",sizeof(unsigned long));
    printf("long的最大值是:%lu\n",ULONG_MAX);
    
    printf("unsigned long long的大小为:%d个字节\n",sizeof(unsigned long long));
    printf("long long的最大值是:%llu",ULLONG_MAX);
    
    return 0;
}

Here Insert Picture Description

Sincere sense of Wang Fei Xie Chuanzhi broadcast elaborate guest teachers and materials provided.

Released four original articles · won praise 0 · Views 79

Guess you like

Origin blog.csdn.net/Dylan_Cai/article/details/105034879