Categories: C

C Program – Structure

Before we begin with our basic concepts of the C programming language let’s take a look at the C Program -Structure of a simple code for better understanding.

A C program consists of the following components−

  • standard input-output library functions
  • variables
  • comments
  • statements
  • operators
  • operands
  • functions
Hello World Program
#include <stdio.h>    
    1. int main()
    2.  {    
    3. /* first program in C */    4. printf("Hell, World!");    
    5. return 0;   
    6. }  

Description of the above code:

  • here the first line of the code #include <stdio.h> is the standard input-output library. printf() function is defined in the stdio.h library file.
  • int main() is the main function where the execution of the code begins.
  • The opening curly brace { symbolizes the beginning of the block.
  • The statement inside /*…*/ is the comment which is ignored by the compiler.
  • The print() function prints the message “Hello World” on the output window/screen.
  • The return 0 statement is the ending of the code, as well as returns the execution state of the OS.
  • The closing curly brace } symbolizes the end of the block.
Compiling and Executing a C Program

Let us take a look at how to compile and run a program:

  • If you are using a C code editor, you may directly click on the compile and run button resulting in the compilation of the program and an output will be generated on the screen.
  • Otherwise, if you are using the command prompt then follow the below-given methods.
  • open an editor and type your code in it
  • save the file with the “.c” extension.
  • open the command prompt go to the directory where you saved your c file
  • type gcc filename.c -o filename and press enter.
Output
$ gcc filename.c -o filename
$ ./filename
Hello, World!

Make sure the gcc compiler is in your path and that you are running it in the directory containing the source file filename.c.

Now you might have a good understanding of the C Program -Structure.

Note: Also read C Programming Language Overview C Programming Language

Share
Published by
Rabecca Fatima

Recent Posts

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

5 days ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

6 days ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

2 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

2 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

2 weeks ago

Largest Sum of Non-Adjacent Numbers

Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…

2 weeks ago