Structure of C program
C program structure
Before we learn the basic building blocks of the C language, let's take a look at a minimal C program structure, which can be used as a reference in the following chapters.
|Structure of c program|
C Hello World instance
The C program mainly includes the following parts:
- Preprocessor instruction
- Statement & expression
Let's take a look at a simple code that outputs the word "Hello World":
/* Prints Hello world! */
Next, we explain the above procedure:
- 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 actually compiling.
- The next line int main() is the main function, and the program starts executing from here.
- The next line /*...*/ will be ignored by the compiler, where the comment content of the program is placed. They are called comments for the program.
- The next line printf(...) is another available function in C that will display the message "Hello, World!" on the screen.
- The next line returns 0; terminates the main() function and returns a value of 0.
Compile & Execute C Program
Let's see how to save the source code in a file and how to compile and run it. Here are the simple steps:
- Open a text editor and add the above code.
- Save the file as hello.c.
- Open a command prompt and go to the directory where you saved the file.
- Type gcc hello.c and type Enter to compile the code.
- If there are no errors in the code, the command prompt will jump to the next line and generate the a.out executable.
- Now type a.out to execute the program.
- You can see "Hello World" on the screen.
If you are using Turbo c then click ctr+f9 to run c program