Basic C program structure

Before we study the blocks that make up a C program, first look at a sample C program.

Before we study the blocks that make up a C program, first look at a sample C program.

Program C: Hello World

A C program includes the following:

Preprocessing orders

Functions

Variables

Commands and expressions

Comments

First look at the simple code that will print the screen two words "Hello World":

 #include int main () { /* Day la chuong trinh C dau tien */ printf ( "Hello, World! n" ); return 0 ; } 

Please see the parts of the program above:

  1. The first line of the #include program is a preprocessor command, prompting the C compiler to add the stdio.h file before compiling.
  2. The next line int main () is the main function, where the program starts.
  3. The next line /*.*/ is the comment line that is ignored by the compiler and is used to add comments to the program. This is called the comment section of the program.
  4. The next line printf (.) is another function of the C language, which prints the message "Hello, World!" displayed on the screen.
  5. The next line return 0 ; end the main function and return the value 0.

Compile & Implement Program C

1. If you use Dev-C ++, Microsoft Visual Studio, . then open the application, go to File, select New to create a new source file. Then copy the above code into this new source file. With Dev-C ++, you use Complile & Run to compile and run the program. With Microsoft Visual Studio, you do the same and then use Build to compile and execute.

2. If you use the command prompt to compile, follow these steps:

  1. Open an editor and add the above line of code.
  2. Save the file as hello.c
  3. Open the command prompt and go to the file storage directory.
  4. Prepare gcc hello.c and press Enter to compile the above line of code.
  5. If there is no error on the above code, the command prompt will take you to the next line and create an executable a.out file.
  6. Now, edit a.out to implement this program.
  7. You should now see the words " Hello, World " printed on the screen.
 $ gcc hello . c $ ./ a . out Hello , World ! 

Make sure the gcc compiler is installed on your computer and that you are running it in the directory containing the source file hello.c.

According to Tutorialspoint

Previous article: Instructions for installing C

Next lesson: Basic syntax of C programming

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile