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

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

1 month ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

2 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

3 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

3 months ago

Estimate π Using Monte Carlo Method

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

3 months ago