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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago