C Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 73

C programming - basic

Created by - dennis ritchie


Imperative (procedural oriented program)
1972- 44 years
Implementation – GCC(GNU Compiler
Collection) , clang, Intel C
Come from - B (BCPL, CPL), ALGOL 68,
Assembly, PL/I, FORTRAN
Base for - C++, C--, C#, JAVA, JavaScript,
PHP, Python
Structure of c program

.c - extension

"Hello world"

printf("Hello world")

main(){ // compiler search main fn


printf("Hello world") // printf-
predefined fn
}

stdio.h // header file


main(){
printf("Hello world")
}

#include<stdio.h>
main(){
printf("Hello world")
}
Variables

#include<stdio.h>
main(){
23
printf("Hello world")
}

#include<stdio.h>
main(){
i=2;
j=3; // i, j is container called variable - it
can be change value
printf("Hello world")
}

#include<stdio.h>
main(){
a=5.5 // different type of value thats why
mention datatype
i=2;
j=3;
printf("Hello world")
}

#include<stdio.h>
main(){
float a=5.5 // different type of value thats
why mention datatype
int i=2;
int j=3;
char c = 's'; // these are all primitive data
type
printf("Hello world")
}
Add two values:

#include<stdio.h>
main(){

int i=2;
int j=3;
int k=i+j;
//printf("Add of two values",k)
printf("Add of two values: %d\n",k)
// printf("Add of 2 and 3 values:
%d\n",k)
// printf("Add of i and j values: %d\n",k)
// printf("Add of %d and %d values:
%d\n",i,j,k)
// printf - print format
}

Return type

#include<stdio.h>
main(){ // warning
// void main(){
// int main(){

int i=2;
int a=5; // define but not use - warning
int j=3;
int k=i+j;
printf("Add of two values: %d\n",k)
// return 1; return 0;
}

User input

#include<stdio.h>
void main(){
int i,j,k; // not specify value - taking the
grabage value
printf("Enter the values\n");
scanf("%d",&i);
scanf("%d",&j)
int k=i+j;
printf("Add of %d and %d values:
%d\n",i,j,k)
}

User multiple input

#include<stdio.h>
void main(){
int i,j,k; // not specify value - taking the
grabage value
printf("Enter the values\n");
scanf("%d %d",&i,&j);
int k=i+j;
printf("Add of %d and %d values:
%d\n",i,j,k)
}

Operators in C

Arithmetic operator

#include<stdio.h>
void main(){
int i=9,j=5,k;
int k=i+j; // Addition +
printf("Add of %d and %d values:
%d\n",i,j,k)
int k=i-j; // Subtract -
printf("Subtract of %d and %d values:
%d\n",i,j,k)
int k=i*j; // Multiply *
printf("Multiply of %d and %d values:
%d\n",i,j,k)
int k=i/j; // Divison /
printf("Divison of %d and %d values:
%d\n",i,j,k)
}
Shorthand operator

#include<stdio.h>
void main(){
int i=9;
// i = i+3;
i+=3;
printf("%d\n",i)
i-=3
printf("%d\n",i)
i*=3;
printf("%d\n",i)
i/=3
printf("%d\n",i)
}

Post and pre increment

#include<stdio.h>
void main(){
int i=1;
//i=i+1;
//i+=1;
//i++; // post increment
//i--;
printf("%d\n", i); // here use i++ - does
not change

#include<stdio.h>
void main(){
int i=1;

//printf("%d\n", i++); // here use i++ -


does not change
printf("%d\n", ++i); // pre increment
}
#include<stdio.h>
void main(){
int i=1;

//int j=i++; // post increment


int j= ++i; // pre increment

printf("%d %d\n",j,i);
}

#include<stdio.h>
void main(){
int i=1;

int i=i++; // post increment

printf("%d\n",i);
}

#include<stdio.h>
void main(){
int i=1,temp;

temp = i; // function going on in this step


i++;
i=temp;
printf("%d\n",i);
}

Relational operator

#include<stdio.h>
void main()
{
int result = 5>3 // true=1, false=0
//int result = 5<3
//int result = 5>=3
//int result = 5<=3
//int result = 5==3
//int result = 5!=3
printf("%d\n",result);

Logical operator

#include<stdio.h>
void main()
{
int result = 5>3 && 5<3;
int result = 5>3 || 5<3;

printf("%d\n",result);
}

Bitwise operator
#include<stdio.h>
void main()
{
/*
25 - 1 1 0 0 1
15 - 0 1 1 1 1
--------------
OR 31 - 1 1 1 1 1
AND 09 - 0 1 0 0 1
*/
//int result = 25 & 15;
//int result = 25 | 15;

printf("%d\n",result);

}
Left and Right Shift
#include<stdio.h>
void main()
{
/*
x<<2
1 0 0 0 0 // add 2 0s in 1000000 = 64
x>>2
1 0 0 0 0 // remove 2 0s in 100=4
*/

int x = 16;
//int result = x<<2;
int result = x>>2;
printf("%d\n",result);

}
Tenary operator:

#include<stdio.h>

// Tenary operator (?:)


void main()
{
int i=1;
int j=0;
if(i==1){
j = 5;
}else{
j = 6;
}
// j = i==1?5:6;
printf("%d\n",j);
}
Flow control - branching stmt, loop

branching statement (if,else)

if:

#include<stdio.h>
void main()
{

int x = 16;

if(x>10)
{
printf("Yes, It is greater\n");

}
ifelse:
#include<stdio.h>
void main()
{
int x = 16;

if(x>10)
{
printf("Yes, It is greater\n");

}
else
{
printf("No, It is not greater\n");

}
}
if elseif:

#include<stdio.h>
void main()
{
int x=16,y=13,z=17;

if(x>y && x>z)


{
printf("%d is greater",x);

}
else if(y>z)
{
printf("%d is greater",y);
}
else
{
printf("%d is greater",z);

}
}

// define preprocessor

#include<stdio.h>
#define x 5 // define preprocessor
void main()
{
int i=x;
printf("%d",i);

}
}

printf tricks:

#include<stdio.h>
#define x printf(hello)
void main()
{
x;

#include<stdio.h>
void main()
{
if(printf("Hello\n"))
{

Switch stmt:
#include<stdio.h>

void main()
{
int i=1;
if(i==0)
{
printf("zero");
}
else if(i==1)
{
printf("One");
}
else if(i==2)
{
printf("Two");
}
else if(i==3)
{
printf("Three");
}
else if(i==4)
{
printf("Four");
}
else
{
printf("Invalid");
}
}
Switch use:

#include<stdio.h>

void main()
{
int i=1;
switch(i)
{
case 0:
printf("zero");
break;
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
case 4:
printf("Four");
break;
default:
printf("Invalid");

}
}
Swapping two numbers:

#include<stdio.h>

void main()
{
int a=5;
int b=6;
int temp;
printf("a: %d, b: %d\n",a,b);

//a=b;
//b=a;
printf("a: %d, b: %d\n",a,b);

temp=a;
a=b;
b=temp;
printf("a: %d, b: %d\n",a,b);
}
without third variable:

#include<stdio.h>

void main()
{
int a=5; // 2 byte
int b=6; // 2 byte
int temp; // 2 byte total 6 byte
printf("a: %d, b: %d\n",a,b);
a = a + b; //a*b // 11 - 4 bits all are 3 bits
b = a - b; //a/b // 5
b = a - b; //a/b // 6
printf("a: %d, b: %d\n",a,b);

// XOR 1 1-0, 1 0-1.


a=a^b; //3
b=a^b; //5
a=a^b; //6
printf("a: %d, b: %d\n",a,b);
}
Swapping- easy way:

#include<stdio.h>

void main()
{
int a=5;
int b=6;
printf("a: %d, b: %d\n",a,b);

b = a+b-(a=b);

printf("a: %d, b: %d\n",a,b);


}

while loop:

//execute the same task multiple times

#include<stdio.h>

void main()
{
printf("hi");
printf("hi");
printf("hi");
printf("hi");
printf("hi");

#include<stdio.h>

void main()
{
while(1) // infinite loop
{
printf("hi\n");
}
}

void main()
{
int i =1;
while(1<=5) // infinite loop
{
printf("hi\n");
}
}

void main()
{
int i =1; // initialization
while(1<=5) // condition
{
printf("hi\n");
i++; // increment or decrement
}
}

Do while:

void main()
{
int i = 6; //i=1 // initialization // condition
false
do
{
printf("hi\n");
i++; // increment or decrement
}while(i<=5); // condition
}

for loop:

while loop only consider condition not


consider initial and increment step;
void main()
{
int i;
for(i=1;i<=5;i++)
{
printf("hi\n");
}
}

Loop Extension:

void main()
{
int i;
for(i=1;i<=5;i++)
{
printf("%d\n",i);
}
}

void main()
{
int i;
for(i=1;i<=4;i++)
{
printf("* ");
}
}

/*
****
****
****
****
*/

void main()
{
int i;
for(i=1;i<=4;i++)
{
printf("* ");
}
printf("\n");
for(i=1;i<=4;i++)
{
printf("* ");
}
printf("\n");
for(i=1;i<=4;i++)
{
printf("* ");
}
printf("\n");
for(i=1;i<=4;i++)
{
printf("* ");
}
printf("\n");
}
Nested for loop:

void main()
{
int i,j;
for(j=1;j<=4;j++)
{
for(i=1;i<=4;i++)
{
printf("* ");
}
printf("\n");
}
}
/*
1234
1****
2* *
3* *
4****
*/

void main()
{
int i,j; //i=row, j=coloumn
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
if(i==1||i==4||j==1||j==4){
printf("* ");
}
else{
printf(" ");
}
}
printf("\n");
}
}
Float, char:

void main()
{
// int - 2 bytes
// float - 2 bytes
// char - 1 byte
int i=5; // i=9.5
float f=5.5 //f=5
char c='s' // c =66 - ASCII (American
standard code for information Interchange)
printf("%d \n",i);
printf("%f \n",f);
printf("%c \n",c);
}
}
ASCII - STD:
0- 127 base system
void main()
{
int i;
for(i=0;i<=127;i++) //127 replace 255
{
printf("%d: %c \n",i,i);
}
}
Array:

collection of similar datatype elements

void main()
{
//int a=1,b=2,c=3,d=4;

int a[4]; //count = 1-4, index= 0-3


a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
printf("%d \n",a[0]);
printf("%d \n",a[1]);
printf("%d \n",a[2]);
printf("%d \n",a[3]);

// printf("%d %d %d %d \n",
a[0],a[1],a[2],a[3]);

Using loop:

void main()
{
//int a=1,b=2,c=3,d=4;
int a[4]; //count = 1-4, index= 0-3
int i;
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;

for(i=0;i<=3;i++){
printf("%d \n",a[i]);
}

2D Array:
void main()
{
/*
0123
0-1234
1-3579
2-8531
*/
//int z[] - it is consider one dimensional
array
//int i,j;
int d[3][4];

/* d[0][0]=1;
d[0][1]=2;
d[0][2]=3;
d[0][3]=4;
d[1][0]=3;
d[1][1]=5;
d[1][2]=7;
d[1][3]=9;
d[2][0]=8;
d[2][1]=5;
d[2][2]=3;
d[2][3]=1; */
// alternet way assign

int d[3][4]=
{
{1,2,3,4}
{3,5,7,9}
{8,5,3,1}
};
//printf("%d \n",d[1][2]);
for(i=0;i<3;i++){
for(j=0;j<4;j++){
printf("%d ",d[i][j]);
}
print("\n")
}

}
User defined function:
#include<stdio.h>

/*
Declaration
Defintion
Calling
*/
void display();
void main()
{
display();

}
void display(){
print("Hi \n");
}
Another way:

#include<stdio.h>

void display(){
print("Hi \n");
}

void main()
{
display();

Why user defined function:

dividing a big task into small sub tasks:

wherever need use this sub task:


Using return type in function:

#include<stdio.h>

int add();
void main()
{
int res = add()
printf("ans is %d\n",res);

}
int add(){
int i=5,j=6;
int k=i+j;
return k;
}

Parameter function:

int add(int,int);
void main()
{
int res = add(5,4)
printf("ans is %d\n",res);

}
int add(int i,int j){

int k=i+j;
return k;
}
Recursion function:

A function calling itself

abc()-> abc()-> abc()

5! = 5*4*3*2*1=120

int fact(int);
void main()
{
int n=5;
int res = fact(n)
printf("ans is %d\n",res);
}
int fact(n){

int i;
int f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}

Another way:
5! = 5*4!
4! = 4*3!
3! = 3*2!
2! = 2*1!

int fact(int);
void main()
{
int n=5;
int res = fact(n)
printf("ans is %d\n",res);

}
int fact(n){

int i;
if(n!=1)
return n*fact(n-1);
return 1;
}

pointers in C:
Storing the memory address in pointer:

void main()
{
int i=5;
int *p;
p = &i;

printf("%d\n",p);
printf("%d\n",&i);
printf("%d\n",i);
printf("%d\n",*p);
}
Structure in c:

#include<stdio.h>

struct laptop
{
int id;
char model[20];
int price;
};

void main()
{
struct laptop xps,ins;

printf("enter the 1st laptop details\n");


print("enter id\n");
scanf("%d",&xps.id);
print("enter Model\n");
scanf("%s",&xps.model);
print("enter price\n");
scanf("%d",&xps.price);

printf("1st laptop = %d : %s :
%d",xps.id,xps.model,xps.price);

printf("enter the 2nd laptop details\n");


print("enter id\n");
scanf("%d",&ins.id);
print("enter Model\n");
scanf("%s",&ins.model);
print("enter price\n");
scanf("%d",&ins.price);

printf("1st laptop = %d : %s :
%d",ins.id,ins.model,ins.price);

You might also like