Program to Display "Hello, World!
"
#include <stdio.h>
int main()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
Output
Hello, World!
How "Hello, World!" program works?
The #include <stdio.h> is a preprocessor command. This command tells
compiler to include the contents of stdio.h (standard input and output) file in the
program.
The stdio.h file contains functions such as scanf() and print() to take input and
display output respectively.
If you use printf() function without writing #include <stdio.h>, the program will
not be compiled.
The execution of a C program starts from the main() function.
The printf() is a library function to send formatted output to the screen. In this
program, the printf() displays Hello, World! text on the screen.
The return 0; statement is the "Exit status" of the program. In simple terms,
program ends with this statement.
Program to Print an Integer
#include <stdio.h>
int main()
{
int number;
// printf() dislpays the formatted output
printf("Enter an integer: ");
// scanf() reads the formatted input and stores them
scanf("%d", &number);
// printf() displays the formatted output
printf("You entered: %d", number);
return 0;
}
Output
Enter a integer: 25
You entered: 25
In this program, an integer variable number is declared.
The printf() function displays Enter an integer: on the screen. Then,
the scanf()function reads an integer data from the user and stores in variable number.
Finally, the value stored in the variable number is displayed on the screen
using printf()function.
Program to Add Two Integers
#include <stdio.h>
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
printf("Enter two integers: ");
// Two integers entered by user is stored using scanf() function
scanf("%d %d", &firstNumber, &secondNumber);
// sum of two numbers in stored in variable sumOfTwoNumbers
sumOfTwoNumbers = firstNumber + secondNumber;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);
return 0;
}
Program to Multiply Two Numbers
#include <stdio.h>
int main()
{
double firstNumber, secondNumber, productOfTwoNumbers;
printf("Enter two numbers: ");
// Stores two floating point numbers in variable firstNumber and
secondNumber respectively
scanf("%lf %lf", &firstNumber, &secondNumber);
// Performs multiplication and stores the result in variable
productOfTwoNumbers
productOfTwoNumbers = firstNumber * secondNumber;
// Result up to 2 decimal point is displayed using %.2lf
printf("Product = %.2lf", productofTwoNumbers);
return 0;
}
Output
Enter two numbers: 2.4
1.12
Product = 2.69
In this program, user is asked to enter two numbers. These two numbers entered by the
user is stored in variable firstNumber and secondNumber respectively. This is done
using scanf() function.
Then, the product of firstNumber and secondNumber is evaluated and the result is
stored in variable productOfTwoNumbers.
Finally, the productOfTwoNumbers is displayed on the screen using printf() function.
Notice that, the result is round to second decimal place using %.2lf conversion
character.
Program to Print ASCII Value
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
// Reads character input from the user
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}
Output
Enter a character: G
ASCII value of G = 71
In this program, user is asked to enter a character which is stored in variable c. The
ASCII value of that character is stored in variable c rather than that variable itself.
When %d format string is used, 71 (ASCII value of 'G') is displayed.
When %c format string is used, 'G' itself is displayed.