Skip to main content

First c program

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:
  1. // 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.
  2. #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.
  3. 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.
  4. { 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.
  5. 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.
  6. 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.
  7. 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.


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 A and B. Then, the variables A and B are added using the arithmetic operator + and the result is stored in the variable sum.
Below is the C program to add two numbers:
// C program to add two numbers
#include<stdio.h>
  
int main()
{
    int A, B, sum = 0;
      
    // Ask user to enter the two numbers
    printf("Enter two numbers A and B : \n");
      
    // Read two numbers from the user || A = 2, B = 3
    scanf("%d%d", &A, &B);
      
    // Calclulate the addition of A and B
    // using '+' operator
    sum = A + B;
      
    // Print the sum
    printf("Sum of A and B is: %d", sum);
      
    return 0;
}
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

Comments

Post a Comment

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

C LANGUAGE - OVERVIEW C - VARIABLES

C LANGUAGE - OVERVIEW Advertisements C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute  A N S I A N S ...

environment

It looks like you meant "environment." The environment refers to the surroundings or conditions in which a person, animal, or plant lives or operates. It encompasses various elements, including natural resources, ecosystems, climate, and human-made structures. Protecting and preserving the environment is crucial for the well-being of all living organisms on Earth. Is there anything specific you'd like to discuss or learn about regarding the environment?