Simple c program hello world for beginners

Hello, my friends.

How are you all? I hope you all are doing good and have already come up from the struggle of the pandemic situation.

So let’s start our very first simple c program hello worlds without wasting any time.

Simple C program Hello World

#include <stdio.h>
void main()
{
   printf("Hello, World!");
   return;
}

Output

Hello, World!

Code Explanation

As it is our very first program, let’s discuss every line to understand the program completely.

1. #include <stdio.h> 

  • This is a header file.
  • It tells compiler to include this stdio.h file in the program.
  • stdio stands for Standard Input Output file.
  • Common input output functions (such as scanf() and printf()) definitions are contained in this.
  • # is a pre-processor directive.
  • So basically #include<stdio.h> tells the compiler to include a standard header file which contains the function for input and output.

2. void main()

  • main() is the function name
  • void is the return type of function.
  • The execution of program begins with the main() function.
  • We can use any return type with main() function depending on the type of function we are defining.

3. printf()

  • printf() function is used to displays the content.
  • Content within double quotes appears as it is on the screen.
  • If we have to print the value of any variable/function then we just have to write their name.( We will discuss this in our next example).

4. return

  • return or return 0 is same thing. So please don’t get confused if you see return 0 somewhere.
  • As the name indicates, it give the command to controler to return from wherever it has been called because function has been successfully executed.

5 Statement Termination

  • In C language, every statement must have to be terminated otherwise compiler will give error.
  • ;‘ indicates end of line or linee termination.

Simple C program for adding 2 numbers

Before writing this program one should have knowledge about the following basics of C programming topics

If you don’t have any idea of the topics I mentioned above, please read the article first and come back here.

If you are still reading this then I suppose either you already know about the topics or you read my articles.

So let’s begin.

We can write the code for this program in 2 ways

Method-1 By using static values

#include <stdio.h>
int main() {    

    int a=5, b=3, sum;
    
    sum = a+b;      // adding sum
    
    printf("Sum of the number is %d", sum);
    
    return 0;
}

Output

Sum of the number is 8

Copy the above code and run it here to see the output with different values.

Code Explanation

  • printf(“Sum of the number is %d”, sum);
    • Here the content written in double quotes will appear as it is on the screen, but, the %d is a format specifier for integer and indicates compiler to print the value of the variable/function at that place.

Method-2 By entering the number at the run time of the program

#include <stdio.h>
int main() {    

    int a, b, sum;
    
    printf("Enter first number: ");   // Asking user to enter 1st number
    scanf("%d", &a);     
    
    printf("Enter second number: ");  // Asking user to enter 2nd number
    scanf("%d", &b);

    // calculating sum
    sum = a + b;      
    
    printf("Sum of %d + %d = %d", a, b, sum);
    return 0;
}

Output

Enter first number: 5
Enter second number: 6
Sum of 5 + 6 = 11

Code Explanation

  • The user is asked to enter first and second number.
  • These two numbers are stored in variables a and b respectively.
printf("Enter first number: ");
scanf("%d", &a);     
    
printf("Enter second number: ");
scanf("%d", &b);
  • Using the + operator we have added two numbers and the result is stored in the sum variable.
sum = a + b;
simple c program of sum of 2 numbers
  • Finally, the printf() function is used to display the sum of numbers.
printf("%d + %d = %d", number1, number2, sum);

I hope you have learnt how to write a simple c program.

We will meet with some high-level programs related to basic functioning to understand it better.

Till then, enjoy computer programming and don’t forget to comment if you have any below.

7 thoughts on “Simple c program hello world for beginners”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Please Reload After Disabling Ad\'s