Oct 24, 2019

Structure of C programme

Structure of C program

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
Structure of c program

C Hello World instance

The C program mainly includes the following parts:
  • Preprocessor instruction
  • function
  • variable
  • Statement & expression
  • Comment
Let's take a look at a simple code that outputs the word "Hello World":

#include<stdio.h>
int main()
{
/* Prints Hello world! */
printf("Hello World!")
return 0;
}

Next, we explain the above procedure:
  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 actually compiling.
  2. The next line int main() is the main function, and the program starts executing from here.
  3. 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.
  4. The next line printf(...) is another available function in C that will display the message "Hello, World!" on the screen.
  5. 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:
  1. Open a text editor and add the above code. 
  2. Save the file as hello.c.
  3. Open a command prompt and go to the directory where you saved the file.
  4. Type gcc hello.c and type Enter to compile the code.
  5. If there are no errors in the code, the command prompt will jump to the next line and generate the a.out executable.
  6. Now type a.out to execute the program.
  7. You can see "Hello World" on the screen.
 If you are using Turbo c then click ctr+f9 to run c program
Disqus Comments