Introduction To C Programming

 Introduction to C:

  • Introduction to C
  • Why C Programming?
  • Fundamentals of programming.
  • Good Performance.
  • You will be able to write efficient programs.
  • You will find it much easier to learn other programming languages.
  • Job Interviews.

“HELLO WORLD PROGRAM.”

#include<stdio.h>
int main(){
//start
printf("Hello World");
return 0;

OUTPUT: Hello World

moyasite

Data Types in C

 DATA TYPE        VALUE               
1.Integer              19             
2.Float                   3.4                    
3.Char                    Mohan                  
4.String                  “Yashu”             
5.Boolean               True/False          
OUTPUT:10
* How to round the print value of float?
#include<stdio.h>
int main()
{
float height_in_feet = 12.750;
printf("%.2f", height_in_feet);
return 0;
}
OUTPUT:12.75
DATA TYPE     FORMAT SPECIFIERS
    1.Integer              %D
    2.Float                   %F   
    3.Char                    %C             
   4.String                  %S         

Arithmetic Operators in C: 

Using “+”  Operator:

INPUT:30

OUTPUT:40

Arithmetic Operators  Precedence & Associativity:

 

Precedence:

 

#include<stdio.h>
int main()
{
int result = 55 / 10 / 2;
printf("%d", result);
return 0

}

=>result = 55/10/2
Grouping based on associativity:
=>result = (55/10) / 2
=>result = ((55/10) / 2)
By evaluating:
=>result = ((55/10) / 2)
=>result = (5/2)
=>result = 2 

OUTPUT:2

Associativity:

 

#include<stdio.h>
int main()
{
int result = 6 / 3 * 2;
printf("%d", result);
return 0

}

=> result = 6/32
Grouping based on associativity:
=> result = (6/3) 2
=> result = ((6/3)* 2)
By evaluating:
=> result = ((6/3) 2)
=> result = (22)
=> result 4 

OUTPUT:4

Relational Operators in C:

 

In C, if the Relation is True, if returns true

if the Relation is False if returns false

“>” Operator:

INPUT:3.45

OUTPUT:0

Logical Operators:

In C, if the Logical Operation is True, if returns 1

if the Logical Operation is False, if returns 0

“&&” Operator:

 

INPUT:10

OUTPUT:0

Conditional Statements:

1.If Statement

2.If Else Statement

3.Nested If Else Statement

4.If-Else Ladder

5.Switch Statement

 

 1. If Statement:

Definition: If the condition is true then the  (if) block of code  will execute

Example Program Of If Statement:

#include<stdio.h>
#include<conio.h>
int main()
{
if(5>2)
{
printf("5 is greater");
}
return 0;

2.If Else Statement:

Definition: It allows the program depends upon the condition is true or false:

if condition is true inside the if statement is true, if condition is false else part will be executed 

Example Program Of If Else Statement:

#include<stdio.h>
int main()
{
int input_number, remainder;
scanf("%d", &input_number);
remainder input_number%2;
if(remainder == 0)
{
printf("even");
}
else
{
printf("odd");
}
return 0;
}

INPUT:5

OUTPUT:ODD

3.Nested If Else Statement:

Definition: It can added the one if else function inside the another if else function is called nested if else statement..

Example Program Of  Nested If Else Statement:

#include<stdio.h>
#include<conio.h>
int main()
{
int x;
scanf("%d",&x);
if(x>20)
{
if(x>=50)
{
x=x+100;
}
else
{
x=x*2;
}
}
else
{
x=x+50;
}
printf(("%d",x);
return 0;

INPUT: 15

OUTPUT:65

4.If-Else If Ladder:

Definition: A Series of if else ladder is called If-Else Ladder 

Example Program Of   If Else Ladder Statement:

#include<stdio.h>
#include<conio.h>
{
char color;
scanf("%c",color);
if(color == 'r')
{
printf("red");
}
else if (color == 'g')
{
printf("green")
}
else if (color == 'b')
{
printf("blue")
}
else
{
printf("White");
}
return 0;
}

INPUT: g

OUTPUT: green

5.Switch Statement:

Example Program Of   Switch Case Statement:

#include<stdio.h>

#include<conio.h>
int main()
{
char color;
scanf("%c",color);
switch(color)
{
case 'r':
printf("red");
break;
case 'g':
printf("green");
break;
case 'b':
printf("blue");
break;
default:
printf("white");
}
return 0;
}

INPUT:b

OUTPUT:blue

LOOPS IN C:

1.While Loop

2.For Loop

3.Do While Loop

 

1.While Loop:

Definition: While loop is control statement that repeatedly executes a block of code as long as condition is true.

 

Example Program of While Loop

#include<stdio.h>
#include<conio.h>
int main()
{
int n;
scanf("%d",n);
int i=0;
while(i<n)
{
printf("%d \n", i);
i=i+1;
}
return 0;
}

INPUT:4
OUTPUT:
1
2
3
4

2.For Loop:

Definition: It is a control loop statement will execute certain number of times.

Example Program Of For Loop

#include<stdio.h>
int main()
{
int n,i;
scanf("%d",&n);
int sum = 0;
for (i=0; i< n; i = i + 1){
sum=sum+i;
}
printf("%d",sum);
return 0;

INPUT:4
OUTPUT:6

3.Do While Loop:

Definition: Do While loop is control statement that repeatedly executes a block of code as long as condition is true.

Example Program Of Do While  Loop

#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i= 0, sum = 0;
do{
sum = sum + i;
i = i + 1;
}while (i<n);
printf("%d",sum);
return 0;

INPUT:4
OUTPUT:6

Definition of Break Statement:

The break statement is used to exit from a loop prematurely. When encountered within a for, while, or do-while loop, it causes the program to jump out of the loop, skipping the remaining iterations. The break statement is also used to exit from a switch statement.

Definition of Continue Statement:

The continue statement is used inside loops to skip the remaining code within the loop for the current iteration and move on to the next iteration. It is typically used to bypass certain statements within the loop based on a specific condition without terminating the loop itself.

Arrays:

An array is collection of items belonging to the same data type is called Array.

Accessing Elements In Array:

 

Program:

#include
int main()
{
int arr[7] = {3, -45, 7, 12, 98, 71, 5);
printf("%d %d", arr[0], arr[3]);
return 0;

Program With Array of Integers:

#include<stdio.h>
int main() {
// Declare an array of integers
int numbers[5] = {1, 2, 3, 4, 5};
// Access and print elements of the array
printf("Elements of the array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;

Output:1 2 3 4 5

Two Dimensional Array:

A two-dimensional array is a data structure that organizes data elements in a grid with rows and columns. It can be visualized as an array of arrays, where each element is identified by two indices – one for the row and another for the column.

Example Program Of Two Dimensional Array:

#include<stdio.h>
int main()
{
int arr[2][3];
int i, j;
//scanning array elements
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
scanf("%d", &arr[i][j]);
}
//printing array elements
for(i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
return 0;

Introduction to Strings: 

A string is a data type, that is used to represent text rather than numbers. A string is a sequence of characters and can contain letters, numbers, symbols and even spaces. It must be enclosed in quotation marks for it to be recognized as a string.

 

 

1 thought on “Introduction To C Programming”

Leave a comment