C

Structure Of A C Program – The First 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.

The 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.

Related Articles

Leave a Reply

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

Back to top button