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:
- Preprocessor directives
- Function
- Variable
- Statements & Expressions
- 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:
- 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.
- The next line int main () is the main function from which the program starts.
- 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.
- The next line of printf (...) is another available function in C that displays the message "Hello, world!" on the screen.
- 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:
- Open a text editor to add the above code.
- Save file as hello.c.
- Open a command prompt and go to the directory where you saved the file.
- Type gcc hello.c, enter the carriage return, and compile the code.
- If there are no errors in the code, the command prompt jumps to the next line and generates the A.out executable.
- Now type A.out to execute the program.
- 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.