C language Hello world

 

Major programming language classic starts out hello world. Origin would not have said (can be found on the Internet). Through this simple procedure, we should know what the message from this is that we need to understand and master.

Take a look at the C language, hello world code to it:

#include <stdio.h>
int main()
{
    printf("Hello world!\n");
    return 0;
}

 

The code itself is relatively simple, only one subject: 

printf("Hello world!\n");

The role of this sentence is to print out a "hello world!" On the screen.

Let's look at the results of running Linux and windows in it:

 

 windows output we expect results in the console, linux is also the output terminal of the string. This, hello world body's function end.

Next, we analyze the progressive hello world code.

Line#1: #include <stdio.h>

This is referred to as a pretreatment, the compilation process referred to as pre-compiled (pre-compile). This action is to copy the sentence stdio.h #include in the code block to the code file. If we do not add this sentence, compile time will printf function undefined error (warning) occurs. The compiler different different results

gcc will automatically find some build-in functions, this program looks normal compiler, but there will be hidden (if you customize the functions of the same name, the results may not we expect, will be explained in subsequent blog)

We look at the results of pre-compiled:

......................
......................
#line 723 "c:\\program files (x86)\\microsoft visual studio 11.0\\vc\\include\\stdio.h"

#pragma pack(pop)

#line 727 "c:\\program files (x86)\\microsoft visual studio 11.0\\vc\\include\\stdio.h"

#line 2 "c:\\kylin\\consoleapplication1\\source.cpp"
int main()
{
    printf("Hello world!\n");
    return 0;
}

Before the main function, insert some code blocks, which are brought by the file stdio.h

In gcc, similar to the code block insert

................
extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;


extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
# 943 "/usr/include/stdio.h" 3 4

# 2 "hello.c" 2
int main()
{
 printf("Hello world!\n");
}
View Code

The second line is a function of the main function entry. C and C ++ are the main application entry function, the code should also have only one main function. Parameters can be defined in the main function.

printf C library functions are provided in stdio.h, used to print to the screen.

hello world followed by the "\ n", a newline, if not, then after printing will not wrap.

 

Simple hello world program that actually contain several compile C / C ++ related questions:

  1. Pretreatment
  2. Compile
  3. compilation
  4. link

  If the code is not interested in a few below, you can not open.

 Follow-up will be more exciting Bowen.

Gcc more flexible because, for example using gcc code block.

When we use gcc -S parameter, the compiler generates a compilation of the results before the following:

        .file   "hello.c"
        .section .rodata
.LC0:
        .string "Hello world!"
        .text
        .globl main
        .type   main, @function
main:
.LFB0:
        .cfi_startproc
        Pushk% Rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movl $ .LC0,% edi
        call    puts
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   main, .-main
        .ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-36)"
        .section        .note.GNU-stack,"",@progbits
View Code

When using gcc -c parameter code before generating link as follows:

^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^A^@>^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@<98>^B^@^@^@^@^@^@^@^@^@^@@^@^@^@^@^@@^@^M^@^L^@UH<89>å¿^@^@^@^@è^@^@^@^@]ÃHello world!^@^@GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-36)^@^@^@^@^@^@^T^@^@^@^@^@^@^@^AzR^@^Ax^P^A^[^L^G^H<90>^A^@^@^\^@^@^@^\^@^@^@^@^@^@^@^P^@^@^@^@A^N^P<86>^BC^M^FK^L^G^H^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^D^@ñÿ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^C^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^D^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^E^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^G^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^H^@^@^@^@^@^@^@^@^@^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