YouTip LogoYouTip

C Program Structure

C Program Structure

C Program Structure

--

C Tutorial

C Standard Library

  • C Standard Library - Reference Manual
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library - ">C Standard Library -
  • <a href="#" title="C Standard Library -
  • <a href="#" title="C Standard Library ">C Standard Library
  • <a href="#" title="C Standard Library ">C Standard Library
  • <a href="#" title="C Standard Library ">C Standard Library
  • <a href="#" title="C Standard Library ">C Standard Library
  • <a href="#" title="C Standard Library ">C Standard Library
  • <a href="#" title="C Standard Library ">C Standard Library
< Previous: C VScode | Next: C Basic Syntax >

Deep Dive

  • Computer Science
  • Software
  • Web Design & Development
  • Scripting
  • Programming Languages
  • Scripting Languages
  • Web Services
  • Web Service
  • Development Tools
  • Programming

C Program Structure

Before we learn the basic building blocks of the C language, let's first look at the structure of a minimal C program, which can serve as a reference in the following chapters.

C Hello World Example

A C program mainly consists of the following parts:

  • Preprocessor Directives
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

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

Example

#include<stdio.h>
int main()
{
    /* My first C program */
    printf("Hello, World! n");
    return 0;
}

Next, let's explain the above program:

  1. The first line of the program, #include <stdio.h>, is a preprocessor directive that tells the C compiler to include the stdio.h file before actual compilation.
  2. The next line, int main(), is the main function, where the program execution begins.
  3. The next line, /*...*/, will be ignored by the compiler. Here is where the program's comments are placed. They are called the program's comments.
  4. The next line, printf(...), is another function available in C that displays the message "Hello, World!" on the screen.
  5. The next line, return 0;, terminates the main() function and returns the value 0.

Compile & Execute C Program

Next, let's see how to save the source code in a file, and how to compile and run it. Here are the simple steps:

  1. Open a text editor and add the code above.
  2. Save the file as hello.c.
  3. Open a command prompt and go to the directory where the file is saved.
  4. Type gcc hello.c and press Enter to compile the code.
  5. If there are no errors in the code, the command prompt will jump to the next line, and an executable file a.out (or a.exe on Windows) will be generated.
  6. Now, type a.out to execute the program (or a.exe on Windows).
  7. You will see "Hello World" displayed on the screen.
$ gcc hello.c
$ ./a.out
Hello, World!

Please ensure that the gcc compiler is included in your path, and make sure you run it in the directory containing the source file hello.c.

If there are multiple C source code files, the compilation method is as follows:

$ gcc test1.c test2.c -o main.out
$ ./main.out

test1.c and test2.c are two source code files.

< Previous: C VScode | Next: C Basic Syntax >

3 Notes

Write a note

#0 Yuechengming (747***032@qq.com) 531 Reply

If the gcc command does not specify an output filename, the default generated executable file is named a.out (linux) or a.exe (windows).

You can use gcc -o to specify the target file path and filename.

For example, on a Windows system, gcc hello.c -o target/hello will generate a hello.exe file in the target directory (on a Linux system, it generates a hello executable). The target directory must already exist. The order of and -o can be swapped; gcc -o target/hello hello.c is still valid.

Yuechengming (747***032@qq.com) 8 years ago (2018-06-22)

#0 Jun (191***4533@qq.com) 752 Reply

Due to the compiler, the generated .exe file flashes and disappears when opened, making its running result unobservable. This is because the DOS window automatically closes when the main() function ends. To avoid this problem, you can add the system("pause"); statement before return 0;.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    /* My first C program */
    printf("Hello, World! n");
    system("pause"); // Pause function, press any key to continue...
    return 0;
}

When using the gcc hello.c -o hello command, you don't need to add a target path; gcc will automatically generate a hello.exe file in the current working directory.

Jun (191***4533@qq.com) 8 years ago (2018-07-31)

#0 wosion (191***4627@qq.com) 457 Reply

"Quoting @Jun from above: Due to the compiler, the generated *.exe file flashes and disappears when opened, making its running result unobservable. This is because the DOS window automatically closes when the main() function ends." If you don't want to use the system("pause") function, you can directly use cmd to run the compiled executable file:

  • Create a *.bat file in the directory containing the *.exe file (create a text file and force-change its extension to bat).
  • Open it with Notepad (or another editor) and write the command: cmd.exe cd [directory where the compiler-generated *.exe is located].
  • Running this *.bat will automatically navigate to the current exe directory. After that, just type the name of your generated program to see the result without the window closing immediately.
  • Additional note: You can also directly open cmd and cd to the current directory. As long as you run the *.exe using cmd, you can see the result, unless the program you compiled itself cannot run.

wosion (191***4627@qq.com) 8 years ago (2018-08-19)

Click to share notes

Cancel

Write notes...

Image URL

Image description

Image size Γ—β†Ί

Share note

Category Navigation

← Os WalkOs Utime β†’