Low level languages
The languages in this category are the Machine level language and Assembly language.
Machine Level Language
Assembly Language
High level language
Assembler
Compiler and interpreters
Difference between compiler and interpreter
Facts about C
Why to use C?
Characteristics of 'C'
Properties of C language
Every language has some basic elements and grammatical rules. Before programming in C, it is must to know the basic elements of the language. These basic elements are character set, variables, datatypes, constants, keywords, variable declaration, expressions, statements etc. All of these are used to construct a C program.
Characters like newline, tab, backspace can't be displayed like normal characters. To display them
we use '\'(backslash) and some character from the C character set.
Some escape sequences ↓
Constant is a value that cannot be changed during execution of the program.
Declaration of Variables
Syntax ↓
int x;
float salary;
char grade;
int max;
Initialization of variables
For example ↓
int a = 5;
float x = 8.9, y = 10.5;
Header file
int main()
{
// statements
}
#include <stdio.h>
int main()
{
// code
}
Some more header files and their functions
Program to print 'hello' on the screen
#include <stdio.h>
int main()
{
printf("hello\n"); // here '\n' is a escape sequence which gives a newline
printf("how are you?")
}
output ↓
hello
how are you?
Program to calculate sum of two numbers
#include <stdio.h>
int main()
{
int a, b, sum;
printf("Enter first number : ");
scanf("%d", &a);
printf("Enter second number : ");
scanf("%d", &b);
sum = a + b;
printf("The sum is : %d", sum);
return 0;
}
Program to calculate area of a rectangle
#include <stdio.h>
int main()
{
int l, b, area;
printf("Enter length :");
scanf("%d", &l);
printf("Enter breadth :");
scanf("%d", &b);
area = l * b;
printf("The area is : %d", area);
return 0;
}
Program to calculate area of a circle
#include <stdio.h>
int main()
{
int r;
float area;
printf("Enter radius :");
scanf("%d", &r);
area = 3.14 * r * r;
printf("The area is : %f", area);
return 0;
}
Types of Errors
There are two types of error logical and syntax error
Warning vs Error
Return type
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter the value of 'a' before swap : ");
scanf("%d",&a);
printf("Enter the value of 'b' before swap : ");
scanf("%d",&b);
c = a;
a = b;
b = c;
printf("The value of 'a' and 'b' after swap are : %d \t %d",a,b);
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
printf("Enter the value of 'a' and 'b' before swap : ");
scanf("%d %d", &a, &b);
// above we are only using single statement to input two values
a = a + b;
b = a - b;
a = a - b;
printf("The value of 'a' and 'b' after swap are : %d \t %d", a, b);
return 0;
}
#include <stdio.h>
int main()
{
float bs, hra, da, ta, tax, netS;
printf("Enter employee's basic salary : ");
scanf("%f", &bs);
hra = 0.05 * bs;
da = 0.06 * bs;
ta = 0.03 * bs;
tax = 0.02 * bs;
netS = (bs + hra + da + ta) - tax;
printf("Net salary of employee is : %f", netS);
return 0;
}
#include <stdio.h>
int main()
{
float fahren, celsius;
printf("Enter the temperature in Fahrenheit : ");
scanf("%f", &fahren);
celsius = (fahren - 32) * 1.8;
printf("Temperature in celsius is : %f", celsius);
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
float P, R, T, SI, CI;
printf("Enter the value of Principle Amount : ");
scanf("%f", &P);
printf("Rate of Ineterest : ");
scanf("%f", &R);
printf("Time (in years) : ");
scanf("%f", &T);
SI = (P * R * T) / 100;
CI = pow(P * (1 + (R / 100)), T);
printf("The Simple Interest is : %f\n", SI);
printf("The Compound Interest is : %f\n", CI);
return 0;
}
#include <stdio.h>
int main()
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
if (num < 0)
printf("The entered number is negative");
else if (num > 0)
printf("The entered number is positive");
else
printf("The entered number is zero");
return 0;
}
#include <stdio.h>
int main()
{
int m1, m2, m3, m4;
float per;
printf("Enter m1, m2, m3 and m4 : ");
scanf("%d%d%d%d", &m1, &m2, &m3, &m4);
per = (m1 + m2 + m3 + m4) / 4;
if (per >= 70)
printf(" 1st division with honours");
else if (per >= 60)
printf(" 1st division");
else if (per >= 50)
printf(" 2nd division");
else if (per >= 40)
printf(" 3rd division ");
else
printf("failed\n");
return 0;
}
Note
We cannot use ';' in main function, conditional statement and looping statement.