Learn C Programming Step by Step
Through amazing Videos
What you’ll learn in This Course
Understand the fundamentals of the C Programming Language, learn how to write high-quality effective code.
Create your first c program by using Devc++ IDE.
Understand variables and the different data types used in c programming
if else statements, switch-case, loop for ,while and do – while loop with so many examples.
Array – 1 dimensional and 2-dimensional with many examples.
String in c programming ,use of string library function .
Writing User Define function, parameter passing to function, returning value from function.
Storage classes – auto, extern ,static, and register
Pointer – how to use pointer ,void pointer, null pointer, pointer as a parameter in function, Relationship between arrays and pointers. array of pointers.
Command line arguments.
Reading and writing with files, both text and binary.
Recursion – c programming language recursion.
User Define Function and pointers as a parameter in function
Bit wise operators in C programming.
Structure , union and enumeration.
Different type of preprocessor directives in c programming
The C Language is developed by Dennis Ritchie for creating system applications that directly interact with the hardware devices such as device drivers, kernels,databases, compilers, games etc.
C programming is considered as the base for other programming languages, that is why it is known as mother language.
C Programming can be defined as:
Mother language
System programming language
Procedure-oriented programming language
Structured programming language
Mid-level programming language
How to Use Devc++ Ide to write and Execute Hello World Program
How to Use Turboc++ Ide to write and Execute Hello World Program
Program to Add Two Integers
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculating sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
}
Output
Enter two integers: 12
11
12 + 11 = 23
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d n",c);
c = a-b;
printf("a-b = %d n",c);
c = a*b;
printf("a*b = %d n",c);
c = a/b;
printf("a/b = %d n",c);
c = a%b;
printf("Remainder when a divided by b = %d n",c);
}
# include <stdio.h>
int main ()
{
int ivalue;
float fvalue;
char cvalue;
//input
printf("Input integer, float and character values: ");
scanf ("%d%f%*c%c", &ivalue, &fvalue, &cvalue);
printf ("Integer value: %dn", ivalue) ;
printf ("Float value: %fn", fvalue) ;
printf ("Character value: %cn", cvalue) ;
return 0;
}
Output
Input integer, float and character values: 10 2.34 X
Integer value: 10
Float value: 2.340000
Character value: X
#include<stdio.h>
int main()
{
int a,b,c, sum;
float d;
printf("Please enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
d=(a+b+c)/3;
printf("nSum is %d", sum);
printf("nAverage is %f",d);
}
#include<stdio.h>
int main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
printf("nSquare of the number %d is %d",n,n*n);
printf("nCube of the number %d is %d",n,n*n*n);
}
#include<stdio.h>
int main() {
int rad;
float PI = 3.14, area, ci;
printf("nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("nArea of circle : %.2f ", area);
ci = 2 * PI * rad;
printf("nCircumference : %.2f ", ci);
}
Output :
Enter radius of a circle : 1
Area of circle : 3.14
Circumference : 6.28
#include<stdio.h>
int main()
{
int p,r,t,int_amt;
printf("Input principle, Rate of interest & time to find simple interest: n");
scanf("%d%d%d",&p,&r,&t);
int_amt=(p*r*t)/100;
printf("Simple interest = %d",int_amt);
return 0;
}
Output:
Input Data: p = 10000, r = 10% , t = 12 year
Input principle, Rate of interest & time to find simple interest:
Simple interest = 12000
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
/* Input temperature in celsius */
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
/* celsius to fahrenheit conversion formula */
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);
}
%.2f is used to print fractional numbers up to two decimal places. You can also use %f to print fractional normally up to six decimal places.
**
* file: algebra.c
#include <stdio.h>
int main()
{
float a, b, x;
printf("Enter value of a: ");
scanf("%f", &a);
printf("Enter value of b: ");
scanf("%f", &b);
x = (a * a) + (b * b) + (2 * a * b);
printf("Result = %fn", x);
printf("End of coden");
}
Output
Enter value of a: 4
Enter value of b: 2
Result = 36.000000
When sizeof() is used with the data types, it returns the amount of memory allocated to that data type.
#include <stdio.h>
int main() {
int a = 16;
printf("Size of variable a : %dn",sizeof(a));
printf("Size of int data type : %dn",sizeof(int));
printf("Size of char data type : %dn",sizeof(char));
printf("Size of float data type : %dn",sizeof(float));
printf("Size of double data type : %dn",sizeof(double));
return 0;
}
Output
Size of variable a : 4
Size of int data type : 4
Size of char data type : 1
Size of float data type : 4
Size of double data type : 8
If any variable is not initialized then that variables having garbage value(unassigned value).
For example:
int i; Variable ‘i’ is integer type but it wont assign any value.so,it take garbage value.It maybe any value.
An operator is a symbol that tells the compiler to perform a certain mathematical or logical manipulation. Operators are used in programs to manipulate data and variables.
C operators can be classified into following types:
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators
Conditional operators
Special operators
OperatorDescriptionExample
+ Adds two operands. A + B = 30
− Subtracts second operand from the first .A − B = -10
* Multiplies both operands.A * B = 200
/ Divides numerator by de-numerator .B / A = 2
% Modulus Operator and remainder of after an integer division.B % A = 0
++ Increment operator increases the integer value by one. A++ = 11
-- Decrement operator decreases the integer value by one. A-- = 9
#include<stdio.h>
int main()
{
int x = 12, y = 1;
printf("Initial value of x = %dn", x); // print the initial value of x
printf("Initial value of y = %dnn", y); // print the initial value of y
y = ++x; // increment the value of x by 1 then assign this new value to y
printf("After incrementing by 1: x = %dn", x);
printf("y = %dnn", y);
y = --x; // decrement the value of x by 1 then assign this new value to y
printf("After decrementing by 1: x = %dn", x);
printf("y = %dnn", y);
}
Expected Output:
1
2
3
4
5
6
7
8
Initial value of x = 12
Initial value of y = 1
After incrementing by 1: x = 13
y = 13
After decrementing by 1: x = 12
y = 12
If else statement
Syntax of if else statement:
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
}
Output
Enter an integer: 7
7 is an odd integer.
/ Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
}
Output
Enter an integer: 7
7 is an odd integer.
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: n");
scanf("%d", &num);
if (num > 0)
printf("%d is a positive number n", num);
else if (num < 0)
printf("%d is a negative number n", num);
else
printf("0 is neither positive nor negative");
}
* C Program to Find Largest of Two numbers */
#include <stdio.h>
int main() {
int a, b;
printf("Please Enter Two different valuesn");
scanf("%d %d", &a, &b);
if(a > b)
{
printf("%d is Largestn", a);
}
else if (b > a)
{
printf("%d is Largestn", b);
}
else
{
printf("Both are Equaln");
}
}
#include <stdio.h>
int main()
{
int num;
printf("Enter your mark ");
scanf("%d",&num);
printf(" You entered %d", num); // printing outputs
if(num >= 80)
{
printf(" You got A grade"); // printing outputs
}
else if ( num >=60)
{
printf(" You got B grade");
}
else if ( num >=40)
{
printf(" You got C grade");
}
else if ( num < 40){
printf(" You Failed in this exam");
}
}
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
} else {
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
}
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
TC Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.
The syntax of switch statement in c language is given below:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
default:
code to be executed if all cases are not matched;
}
// Program to create a simple calculator using switch case
#include <stdio.h>
int main() {
char operator;
double n1, n2;
printf("Enter an operator +, -, *, / ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;
// operator doesn't match any case constant +, -, *, /
default:
printf("Error! operator is not correct");
}
}
Output
Enter an operator +, -, *, /
Enter two operands: 32 12
32.5 - 12.4 = 20.0
for loop in C
The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance.
The syntax of for loop in c language is given below:
for(initialization;condition;incr/decr)
{
//code to be executed
}
// Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < =10; i++)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
How for loop works?
The initialization statement is executed only once.
Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated.
However, if the test expression is evaluated to true, statements inside the body of for loop are executed, and the update expression is updated.
Again the test expression is evaluated.
This process goes on until the test expression is false. When the test expression is false, the loop terminates.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum =sum+ i;
}
printf("Sum = %d", sum);
}
The above program takes input from the user and stores it in the variable n. Then, for loop is used to calculate the sum up to n.
#incllude<stdio.h>
int main()
{
int i,no1,no2;
printf("Input 2 no numbers : n");
scanf("%d",&no1,&no2);
for (i=no1;i<=no2;i++)
{
printf("Number-%d :",i);
}
}
#incllude<stdio.h>
int main()
{
int i,no1,no2;
printf("Input 2 no numbers : n");
scanf("%d",&no1,&no2);
for (i=no1;i<=no2;i++)
{
if(i%2==0)
printf("Number-%d is even no :",i);
}
}
#include <stdio.h>
int main()
{
int i,num,odd_sum=0,even_sum=0;
for (i = 1; i <= 10; i++)
{
if (i%2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers = %dn", odd_sum);
printf("Sum of all even numbers = %dn", even_sum);
}
Different Ways of Using For Loop in C Programming
In order to do certain actions multiple times , we use loop control statements.
For loop can be implemented in different verities of using for loop –
Single Statement inside For Loop
Multiple Statements inside For Loop
No Statement inside For Loop
Semicolon at the end of For Loop
Multiple Initialization Statement inside For
Missing Initialization in For Loop
Missing Increment/Decrement Statement
Infinite For Loop
Condition with no Conditional Operator.