C Language Program Structure example parsing _c language

Source: Internet
Author: User
Tags save file c hello world

C Program Structure

Before we learn the basic building blocks of C, let's take a look at a minimal C program structure, which can be used as a reference in the next chapters.

C Hello World Example

The C procedure mainly includes the following sections:

    1. Preprocessor directives
    2. Function
    3. Variable
    4. Statements & Expressions
    5. Comments

Let's look at a simple code that can output the word "Hello World":

#include <stdio.h>

int main ()
{/
 * My first C program *
 /printf ("Hello, world! \ n ");
 
 return 0;
}

Next we'll talk about the above procedure:

    1. The first line of the program #include <stdio.h> is the preprocessor directive, telling the C compiler to include the stdio.h file before it actually compiles.
    2. The next line int main () is the main function from which the program starts.
    3. The next line of/*...*/will be ignored by the compiler, where the annotation content of the program is placed. They are called annotations of the program.
    4. The next line of printf (...) is another available function in C that displays the message "Hello, world!" on the screen.
    5. Next line return 0; Terminates the main () function and returns a value of 0.

Compile & Execute C program

Next, let's look at how to save the source code in a file and how to compile and run it. The following are simple steps:

    1. Open a text editor to add the above code.
    2. Save file as hello.c.
    3. Open a command prompt and go to the directory where you saved the file.
    4. Type gcc hello.c, enter the carriage return, and compile the code.
    5. If there are no errors in the code, the command prompt jumps to the next line and generates the A.out executable.
    6. Now type A.out to execute the program.
    7. You can see "Hello World" on the screen.
$ gcc hello.c
$/a.out
Hello, world!

Make sure your path contains the GCC compiler and make sure that it is running in the directory that contains the source file hello.c.

The above is the C language program structure of the data collation, follow-up continue to supplement the relevant information, hoping to help students learn C language.

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.