Basic C program structure

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

5 ★ | 2 Vote

May be interested

  • Graph data structure (Graph)Graph data structure (Graph)
    a graph (graph) is a form of visual representation of a set of objects in which pairs of objects are connected by links. interconnected objects are represented by points called vertices, and links that connect vertices are called edges.
  • What is Silo Structure? Advantages and disadvantages of silos and effective alternativesWhat is Silo Structure? Advantages and disadvantages of silos and effective alternatives
    everyone knows that a properly structured website is good for both users and seo. silo structure is one of many ways that seo experts recommend people to apply for their website.
  • PHP syntaxPHP syntax
    before starting programming in any language, the basic syntax and program structure is very important. this chapter introduces you to the basic php syntax, including: php tags, comments, print commands, echo commands, and two print and echo commands.
  • How to Create a Basic Laptop Design in the Solidworks CAD ProgramHow to Create a Basic Laptop Design in the Solidworks CAD Program
    have you ever wondered how 3d models are designed and wanted to try to model yourself? this article will teach you the basic skills needed to model a basic laptop design as well as many other shapes open the solidworks program.
  • Hash Table data structureHash Table data structure
    the hash table data structure is a data structure that stores data in a federated manner. in hash table, data is stored in array format, in which data values ​​have their own index values. accessing data becomes faster if we know the index of the data to find.
  • Assign Structure to Diagram in Visio 2010 using ContainerAssign Structure to Diagram in Visio 2010 using Container
    in the following article, we will learn more about the concept and usage of the container in the process of adding structure to diagram in the microsoft visio 2010 application. in essence, the container is the most dynamic component. , can surround and contain the remaining relevant components ...
  • How to Implement a Stack Data Structure in C++How to Implement a Stack Data Structure in C++
    a stack is a basic data structure that is commonly used throughout computer science. for example, a stack is utilized by your web browser to remember the last few pages you were on. developers and programmers should be very familiar with...
  • Data structure in C / C ++Data structure in C / C ++
    struct in c / c ++ arrays in c / c ++ allow you to define several types of variables that can hold the values ​​of several members of the same data type. but the structure is another type of data in the c / c ++ programming language, which allows you to combine other types of data.
  • Structure (Struct) in C programmingStructure (Struct) in C programming
    arrays in c allow you to define several types of variables that can hold the values ​​of several components of the same type. but the structure is another type of data in the c programming language, which allows you to combine other types of data.
  • What is URL? Structure of the URLWhat is URL? Structure of the URL
    what exactly is a url, what is its structure and composition? this article will give you an overview of urls and its structure.