First C Program and its Structure
Lets see how to write a simple and most basic C program:
#include <stdio.h>
int main()
{
printf("Hello,World"); //single line comment
return 0;
/*
multi
line
comments
/*
}
Hello,World
Let us now understand every line of the above program:
- // Simple C++ program to display “Hello World” : This line is a comment line. A comment is used to display additional information about the program. Comment does not contain any programming logic. When a comment is encountered by a compiler, the compiler simply skips that line of code. Any line beginning with ‘//’ without quotes OR in between /*…*/ in C++ is comment.
- #include: In C++, all lines that start with pound (#) sign are called directives and are processed by preprocessor which is a program invoked by the compiler. The #include directive tells the compiler to include a file and #include<iostream> . It tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions.
- int main(): This line is used to declare a function named “main” which returns data of integer type. A function is a group of statements that are designed to perform a specific task. Execution of every C++ program begins with the main() function, no matter where the function is located in the program. So, every C++ program must have a main() function.
- { and }: The opening braces ‘{‘ indicates the beginning of the main function and the closing braces ‘}’ indicates the ending of the main function. Every thing between these two comprises the body of the main function.
- std::cout<<“Hello World”;: This line tells the compiler to display the message “Hello Worlld” on the screen. This line is called a statement in C++. Every statement is meant to perform some task. A semi-colon ‘;’ is used to end a statement. Semi-colon character at the end of statement is used to indicate that the statement is ending there.
The std::cout is used to identify the standard character output device which is usually the desktop screen. Every thing followed by the character “<<” is displayed to the output device.
- return 0; : This is also a statement. This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function.
- Indentation: As you can see the cout and the return statement have been indented or moved to the right side. This is done to make the code more readable. In a program as Hello World, it does not hold much relevance seems but as the programs become more complex, it makes the code more readable, less error prone. Therefore, you must always use indentations and comments to make the code more readable.
The std::cout is used to identify the standard character output device which is usually the desktop screen. Every thing followed by the character “<<” is displayed to the output device.
Different parts of C program
- Pre-processor
- Header file
- Function
- Variables
- Statements & expressions
- Comments
All these are essential parts of a C language program.
Pre-processor
#include
is the first word of any C program. It is also known as a pre-processor. The task of a pre-processor is to initialize the environment of the program, i.e to link the program with the header files required.
So, when we say
#include <stdio.h>
, it is to inform the compiler to include the stdio.h header file to the program before executing it.Header file
A Header file is a collection of built-in(readymade) functions, which we can directly use in our program. Header files contain definitions of the functions which can be incorporated into any C program by using pre-processor
#include
statement with the header file. Standard header files are provided with each compiler, and covers a range of areas like string handling, mathematical functions, data conversion, printing and reading of variables.
With time, you will have a clear picture of what header files are, as of now consider as a readymade piece of function which comes packaged with the C language and you can use them without worrying about how they work, all you have to do is include the header file in your program.
To use any of the standard functions, the appropriate header file must be included. This is done at the beginning of the C source file.
For example, to use the
printf()
function in a program, which is used to display anything on the screen, the line #include <stdio.h>
is required because the header file stdio.h contains the printf()
function. All header files will have an extension .h
main() function
main()
function is a function that must be there in every C program. Everything inside this function in a C program will be executed. In the above example, int
written before the main()
function is the return type of main() function. we will discuss about it in detail later. The curly braces { }
just after the main() function encloses the body of main() function.
We will learn what functions are in upcoming tutorials.
Comments
We can add comments in our program to describe what we are doing in the program. These comments are ignored by the compiler and are not executed.
To add a single line comment, start it by adding two forward slashses
//
followed by the comment.
To add multiline comment, enclode it between
/* .... */
, just like in the program above.Return statement - return 0;
A return statement is just meant to define the end of any C program.
All the C programs can be written and edited in normal text editors like Notepad or Notepad++ and must be saved with a file name with extension as
.c
If you do not add the extension
.c
then the compiler will not recognise it as a C language program file.C Program to Add two Integers
Given two numbers A and B. The task is to write a program to find the addition of these two numbers.
Examples:
Input: A = 2, B = 3 Output: 5 Input: A = 3, B = 6 Output: 9
In the below program to add two numbers, the user is first asked to enter two numbers and the input is scanned using the scanf() function and stored in the variables
and
. Then, the variables
and
are added using the arithmetic operator
and the result is stored in the variable sum.
Below is the C program to add two numbers:
0r
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the value of a and b\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("SUM=%d",c);
getch();
}
Output:
Enter two numbers A and B : 2 3 Sum of A and B is: 5
gud
ReplyDeleteTHANKS
Delete