Like any other programming language, C also maintains its own system to execute the code.
There is no starting or ending marks used in C code. So all codes are written at one place without embedding with any other code.
Each instruction in C are written in a statement.
Each statement ends with ; ( semicolon)
One C program consist of a group of statements
Program is executed in a sequence from top to bottom. Based on logic or requirement steps can be shifted in up or down direction.
Small case letters are used and space is used between two commands.
We must declare a variable before using it.
How to write comment
Comment part of the program is not executed by the compiler.
Comment is writtin by using two forward slashs ( // )
Learn more on comments here
We will start with writing a program to display a string Hello World
#include<stdio.h>
#include<string.h>
int main()
{
char str[30] = "Hello World";//string data ( this is comment part)
printf("%s",str); // Print the string ( this is comment part)
return 0;
}
The output of above code is here
Hello Wold
We have first included to header files
#include<stdio.h>
#include<string.h>
First one is to include standard input output and other one for string functions. We will understand these header files later.
Functions are written in C with two brackets, main() function is the function which C compiler first executes. We will also learn how to develop our own functions and use them in our program.
We declare variable str and assigned 30 cells ( memory ) to it. We stored our string ‘hello world’ inside it and
finally our function printf() display the output to screen.
Reading a string and then display
In the above code, we have assigned string data to the variable, now we will ask the user to input a string data and then we will display the same.
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];//
printf("Enter your name ");
scanf("%s", str);
printf("Welcome %30s",str); // Print the string
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
int a,b,c;// Declaring three variables
a=4; // assigning data
b=5;
c= a + b;
printf("Sum = %d", c); // Print the string
return 0;
}
Output
Sum = 9
Sum and average of three numbers
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;// Declaring three variables
float x,avg;
a=3; // assigning data
b=5;
c=6;
x= (a + b + c);
avg=x/3;
printf("Sum of three numbers = %f", x); // Print the string
printf("\n Average of three numbers = %.2f", avg); //the string
return 0;
}
Output is here
Sum of three number = 14.000000
Average of three numbers = 4.67
Data entered by user
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;// Declaring three variables
float x,avg;
printf("enter first number :");
scanf("%d",&a);
printf("enter second number :");
scanf("%d",&b);
printf("enter third number :");
scanf("%d",&c);
x= (a + b + c);
avg=x/3;
printf("Sum of three numbers = %f", x); // Print the string
printf("\n Average of three numbers = %.2f", avg);//the string
return 0;
}
Output is here
enter first number : 4
enter second number : 5
enter third number : 6
Sum of three numbers = 15
Average of three numbers = 3
#include <stdio.h>
#define PI 3.14
int main() {
printf("The value of PI: %.2f\n", PI);
return 0;
}
Explanation:
Command-Line Arguments: Shows how to handle command-line arguments in main().
Preprocessor Directives: Demonstrates the usage of #define to define constants.