What is The Structure of a C Program?

Published by Prateek Agarwal on

Before starting the journey to learn the C programing., let’s look at a bare minimum C program structure so that we can take it as a reference in the upcoming chapters.

Hello World Example

C program basically consists of the following parts.

  • Preprocessor Commands
  • Functions
  • Variables
  • Statements and Expressions
  • Comments

let us look at a simple code that would print the word “Hello World”-

# include<stdio.h>

int main() {
/* my first program in C */
printf(“Hello, World! \n”);

return 0;
}

let’s take a look at the above program –

  • The first line of the program #include<stdio.h> is a preprocessor command, which tells a C compiler to include stdio.h header file before going to the actual compilation and run the program.
  • The next line int main() is the main function where the program execution begins.
  • The next line /*…*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program. these lines are not executed by the compiler. it is written for memorable our program.
  • The next line printf(…) is another function available in C library which causes the message “Hello, World!” to be displayed on the screen. We will learn it letter.
  • The next line return 0; terminates the main() function and returns the value 0.

Compile and Execute C Program

Let us see how to save the source code in a file, and how to compile and run it. Following are the simple steps −

  • Open VS Code and add the above-mentioned code.
  • Save the file as hello.c
  • then click on new terminal under the terminal menu. Then a terminal opened in the footer area.
  • Type gcc hello.c and press enter to compile your code.
  • If there are no errors in your code, the terminal will take you to the next line and would generate a.out executable file.
  • Now, type a.out to execute your program.
  • You will see the output “Hello World” printed on the screen.

YOU CAN CONNECT WITH THE AUTHOR ON LINKEDIN.

IF YOU WANT TO KNOW ABOUT MORE IN THIS PROGRAMMING LANGUAGE YOU CAN VISIT OUR WEBSITE BRIGHTERBEES.

YOU CAN ACCESS OUR BLOG SERIES OF CLOUD COMPUTING AND ETHICAL HACKING.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US