C

The Structure Of The First C Program – The Most Detailed?

In this tutorial, we will learn how to write our first C program and then analyze its structure. First and foremost, let’s take a look at how to write a simple Hello World program in C. Let’s get this party started.
Here is the C program for printing “Hello World.” Below will share about the structure of the first C program.

Understanding the C Program’s Structure

Some of the various components of a C program are listed below:

  • Pre-processor
  • Main() function in the header file
  • In C, variables
  • C statements and expressions

All of these are necessary components of a C language program. Don’t worry about it; we’ll learn about everything one by one and clear up any confusion. Let’s begin with a basic overview of the various code statements used in the Hello World program.

Preparator

The first statement in any C program is #include. It is referred to as a pre-processor. A pre-job processor’s is to initialize the program’s environment, or to link the program with the necessary header files.
As the name implies, this line of code is in charge of pre-processing before the actual code (logic) is executed.
So, #includestdio.h> instructs the compiler to include the stdio.h header file, which is the standard I/O library.
The standard I/O library is a very useful library that allows you to read input from the keyboard (standard in) and then write the output to the console screen (standard out).

Structure of the C Program

The source code for a C program can be written in any text editor; however, the file must be saved with the.c extension. Let’s create our first C program.

First C Program

/* Demo Program written by Chaitanya on BeginnersBook.com*/
#include<stdio.h>
int main()
{
      int num;
      printf("Enter your age: ");
      scanf("%d", &num);
      if (num <18)
      {
             printf("you are not eligible for voting");
      }
      else
      {
             printf("You can vote!!");
      }
      return 0;
}

Output:

Enter your age:25
You can vote!!

Let’s break down this program:

Comment: Begin with ‘/*’ and end with ‘*/’. Although comments are not required, it is a good practice to use them because they improve code readability. A program may contain an unlimited number of comments.
Include the following section: When writing a program, we use a variety of keywords, statements, and functions such as printf(), scanf(), and so on. The file containing the definitions of these functions must be included in the program. We used stdio.h in the preceding program. There are several libraries, one of which is “stdio.h,” which is used to read data from the terminal and display it on the terminal.
The printf function: is used in a couple of places in the preceding code to display statements. Whatever you put inside double quotes is printed exactly as it is at the console. You can also use printf format specifiers like %d,%c,%p to display the values of variables and pointers.
Take user input: The scanf function is used to accept user input. When you run this program, it waits for a user input (age) and then processes the rest of the statements based on the age entered by the user.
Function Main(): It serves as the starting point for all C programs. This function starts the execution of C source code.

More information on the main () function in a C program

The main () function should be present in all C programs because your program will not start without it.
The following is the return type of the main () function: The main () function’s return type should always be int.
What is the purpose of having a return type? The compiler should know whether your program successfully compiled or failed. It determines this by examining the return value of function main (). If the return value is 0, the program is successful; otherwise, it assumes there is a problem, which is why we have a return 0 statement at the end of the main function.
Main function structure: The name of the function is followed by the type of return. After the function name, close parenthesis should be used. If there are any parameters or arguments, they must be contained within this parenthesis. Function body is the block of code enclosed by braces. More on functions will be covered in a separate tutorial: Functions in C Programming.

On my computer, how do I write a C program?

All C programs must be saved with a file name and the.c extension and can be written and edited in standard text editors such as Notepad or Notepad++. For example, the Hello World program’s file name could be helloworld.c.
If you do not include the.c extension, the compiler will not recognize the file as a C language program file.

Conclusion

In this tutorial, we looked at a simple Hello World program written in C and discussed its structure. In the following tutorial, we will install the C compiler on our local computer/laptop so that you can run this program as well.

Related Articles

Leave a Reply

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

Back to top button