0% found this document useful (0 votes)
42 views

C Class Notes

Uploaded by

neelshreyan2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

C Class Notes

Uploaded by

neelshreyan2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Programming & Problem Solving 21 CSS 101 J - Class Notes

Faculty of Engineering & Technology

SRM Institute of Science & Technology

Class Handouts for Reference & Practice

Disclaimer: This is a class handout and running notes model mostly of rough works, if
you continue to view and study the below contents which means you are at your own
risk.

This is a very basic material and handout probably for beginners but anyone can use
this material to gain very basic knowledge about C programming.

In addition to this, you may also look into other books, online resources, blogs and e-
books of your choice.

You may also contact me at anytime if you are seriously looking for advanced concepts
for best coding practice.

Regards

ARM Ravi Shankar M.E., M.S(Cybercrime-UK)., MBA., (PhD - Medical Engineering)

Special Thanks to

Dr. S. Shanmuga Priya M.E., PhD


Yasmin Z – M.E., (PhD – Food Tech)
Lojini V M.E., (PhD – Finance)
Gurusamy S M.E., (PhD – Deep Learning)
What is a data type?
● Datatype is simply defined as “type of data”.
● And every data type has its own size.
● Which is used to declare a data or a function.
● We use format specifiers to store and retrieve data.

Type Keyword Size Format specifier

Integer int 2 bytes %d or %i

Floating point float 4 bytes %f

Double double 8 bytes %lf

Character char 1 byte %c

String char Depends %s

Note:
While using float or double there is a chance of trailing decimal values, we can stop such
trailing decimal to 2 decimal or 3 decimal etc by using %.2lf for 2 decimals, %.3lf for 3
decimals etc

Variable Constants

● A named memory location to store & ● Constants are also a named


retrieve data. memory location.
● The data can be of any type. ● The data can be of any type
● It takes various values during ● We cannot change the value of a
runtime. variable once its assigned as
● Variables can be declared, defined constant
and initialized. ● Syntax:
● Syntax:
○ data_type variable name; const data_type variable_name
○ int a; or float b or char c; const int a = 5;
● Variables can be declared and ● Constants have to be initialised
initialization is optional during declaration to avoid garbage
values.

Keywords Identifiers

● Reserved words which have a ● Identifiers is a concept of naming


special meaning and functionalities, variable, function, arrays, pointers
in total C has 32 keywords etc.
● Some keywords are break, case, ● Identifiers has a combination of
auto, static, extern, register, for, alphabets (upper or lower case),
while, do, if, else, goto, switch, return, alpha-numerics and underscore
void, int, float etc. symbol (no other symbol can be
● Keywords are always case sensitive used)
and no numeric and no special ● Int Sum or int test_score=5;
symbols allowed.
Local Variable Global Variable
• Variables declared with in the main • Variables declared before the main
function or function is always local to is said to be global variables and
the scope of the function these variables has the scope
• Example throughout the code.

#include<stdio.h> #include<stdio.h>
int main() float pi=3.14;//global variable
{ int main()
int a=5; //local variable {
printf(“%d”,a); int a=5;
} printf("%d",a);
printf("%f",pi);
}

Algorithm Pseudocode
• Algorithm is a step-by-step • Pseudocode is a simplified version
procedure to solve any problem. (or) of programming language.
• Algorithm is a set of instructions to • It gives a complete outline of
solve any problem. program.
• Algorithm is basically written in plain • Pseudocodes cannot be compiled.
English • Example
• Example o Pseudocode for sum of 2
o Algorithm for sum of 2 no’s no’s
o Step1: start ▪ Begin:
o Step2: Declare a,b,c ▪ int a,b,c;
o Step3: Read input from user ▪ read a;
o Step4: compute c=a+b; ▪ read b;
o Step5: Print c ▪ c=a+b;
o Step6: Stop ▪ print c;
▪ end:

Expression Evaluation
• An expression is evaluated based on the precedence and associativity of the
operators.
• Consider an expression
o x= 5+2*3/2
o Multiplication and Division as high and equal precedence
o So, 2*3 is evaluated first so 2*3=6
o And 6/2 is evaluated so its 6/2=3
o 5+3 is evaluated so 5+3=8
o X=8 is the answer
• C use BODMAS rule to evaluate expression
• BODMAS stands for Brackets, Orders, Division/Multiplication, Addition, Subtraction
Structure of a C program
Header files #include<stdio.h>
Global variable declaration - optional float pi=3.14;
Function declaration – optional int main()
Main function {
{ int a=5;
Local variable declaration – optional printf(“%d”,a);
//body of main & statements printf(“%f”,pi);
} }
//optional blocks
Function1(argument list….)
{
//body of function
}
//function definitions

Simple if
If(condition) If(age<18)
{ {
//body of if printf(“Access denied”);
} }

If else
if(condition) If(age<18)
{ {
//body of if printf(“Access denied”);
} }
else else
{ {
//body of else printf(“Access Granted”);
} }

Else if ladder
if(condition) if(age<18)
{ {
//body of if printf(“Access denied”);
} }
else if(condition) else if(age<=18)
{ {
//body of else printf(“Access Denied”);
} }
else if(condition) else if(age>18)
{ {
//body of else printf(“Access Granted”);
} }
else else
{ {
//body of else printf(“Enter a valid age”);
} }
Note: You can use as many “else if” but it should start with a simple if and end with a else
part
Switch case
switch(expression) switch(choice)
{ {
case 1: case 1:
statements; printf(“Hello”);
break; break;
case 1: case 1:
statements; printf(“Hi”);
break; break;
case n: default:
statements; printf(“Enter valid choice);
break; break;
default: }
statements;
break;
}

Goto
goto label_name; is the syntax #include<stdio.h>
int main()
{
int a;
printf("A:");scanf("%d",&a);
if(a==5)
goto five; //label name
else
printf("%d",a);
return 0;

five: //label definition


printf("Hi FIVE");
}

Loops For
Syntax: #include<stdio.h>
int main()
for(initialization; condition; inc/dec counter) {
{ for(int i=1;i<=5;i++)
//body of for loop {
} printf("%d",i);
}
}

o/p 12345
Note:

inc is increment
dec is decrement as I have just used for my
reference
While Loop Do while Loop
• Entry control loop • Exit control loop
• Checks the condition before • Checks the condition after execution
execution • Do must always end with a while
loop
• Syntax:
• Syntax:
while(condition)
{ do
//body of while; {
//increment/decrement //body of do;
} //increment/decrement
}while(condition);

(semicolon is important at the end of


while when its with do while)

Sample code with while Sample code with do while

while(i<5) do
{ {
printf(“%d”,i); printf(“%d”,i);
i++; i++;
} }while(i<5);

For While Do While


#include<stdio.h> #include<stdio.h> #include<stdio.h>
int main() int main() int main()
{ { {
int i; int i=1; int i=1;
for(i=1;i<=5;i++) while(i<5) while(i<5)
{ { {
printf("%d",i); printf(“%d”,i); printf(“%d”,i);
} i++; i++;
} } }
} }

Arrays
• Array is a data structure
• Array is used to store data of same type
• Array elements are stored in SEQUENTIAL order
• Array index always starts with zero
• Arrays as three main types
o One Dimension (1D)
o Two Dimension (2D)
o Three Dimension(3D)
Syntax Code
data_type variable_name[size] – for 1D int a[10];
data_type variable_name[size][size] -2D int a[10][10];
Data_type var_name[size][size][size] – 3D int a[10][10][10];
Implementing Array To get 5 numbers from user and to
display the 5 numbers as output

#include<stdio.h> Limit:5
int main() Enter Array Elements:
{ 1
int a[10]; 2
int i; 3
int l; 4
printf("\nLimit:");scanf("%d",&l); 5
The Array Elements are:
printf("Enter Array Elements:\n"); 12345
for(i=1;i<=l;i++)
{
scanf("%d",&a[i]);
}

printf("The Array Elements are:\n");


{
for(i=1;i<=l;i++)
{
printf("%d",a[i]);
}
}
}

int Arr[5];

sets 5 memory locations for same


type of elements – here the type is int

Array index always starts from zero

So for 5 locations it is

arr[0] arr[1] arr[2] arr[3] arr[4] is 5


count
Code base

Convert Fahrenheit to Celsius

#include <stdio.h> Enter Fahrenheit Temperature:


int main() 104
{ Temperature in Celsius: 40
int f; //for ferenheit input
int c; //for celcius output
printf("Enter Fahrenheit Temperature: ");
scanf("%d",&f);
c = (f-32)*5/9; //formulae
printf("Temperature in Celsius: %d\n", c);
return 0;
}

Odd or Even using Goto

#include<stdio.h> Enter any number:4


int main()
{ Even Number
int n;
printf("\n Enter any number:");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;

even:
printf("\nEven Number");
return 0;

odd:
printf("\n Odd Number");
}
scanf(“%d”,&a);
Input & Output function in C
printf(“Value of a is %d”, a);
i/o operations are defined in stdio.h header file
of C compiler

we get input from the user using scanf()


function
and we print output using printf() function

scanf(“format_specifier”, &variable_name);

printf(“any_text format_specifier”,
variable_name);

Salary Calculation
#include<stdio.h> Emp no:1
int main()
{ Emp Name: Ajay
int eno;//employee number
char ename[10][10];//emp name
int sal,bp,gp,da,ta,hra; Salary:145000
DA is 43750
//salary, basicpay etc - all allowances HRA is 31250
//da=35%,hra=25%,ta=10% TA is12500:

printf("\n Emp no:");scanf("%d",&eno); Gross pay is: 87500


printf("\n Emp Name:");scanf("%s",ename); Basic Pay is: 37500
//ename is declared as char

printf("\n Salary:");
scanf("%d",&sal);

da=(sal*35)/100;
hra=(sal*25)/100;
ta=(sal*10)/100;

printf("DA is %d\n",da);
printf("HRA is %d\n",hra);
printf("TA is%d:",ta);

gp=da+hra+ta; bp=sal-gp;

printf("\nGross pay is: %d",gp);


printf("\nBasic Pay is: %d",bp);
}

Factorial of a number

#include <stdio.h> N:5


int main() 120
{
int n;
int fact=1;
printf("N:");
scanf("%d",&n);
for(int i=2;i<=n;i++)
{
fact=fact*i;
}
printf("%d",fact);
}
Sum of N Natural Number

#include <stdio.h> N:5


int main() 15
{
int n;
int sum; Note:
printf("N:");
scanf("%d",&n); N takes 5
for(int i=0;i<=n;i++) So it is 0,1,2,3,4,5
{
sum+=i; //can be written as sum=sum+i; 0+1+2+3+4+5 = 15
}
printf("%d",sum); 15 is the output
}

Increment & decrement (pre & post) ++ or --

#include <stdio.h> 0
int main() 0
{ 1
int i; 1
printf("%d\n",i); // i is zero 0
printf("%d\n",i++);//zero -1
printf("%d\n",i);//one after increment
printf("%d\n",i--);//one
printf("%d\n",i);//zero
printf("%d\n",--i);//-1
}

Biggest of Three Number

#include<stdio.h> A:12
int main()
{ B:3
int a,b,c;
printf("\n A:");scanf("%d",&a); C:6
printf("\n B:");scanf("%d",&b);
printf("\n C:");scanf("%d",&c); C is big

if(a>b && a>c)


printf("\n A is Big");
else if(b>a && b>c)
printf("\n B is Big");
else if(c>a && c>b)
printf("\n C is big");
else
printf("\n All are equal");
}
Simple & Compound Interest

#include<stdio.h> Principle:20000
int main()
{ Time: 12
int p,n,r;
int si,ci; Rate:3
printf("\n Principle:");
scanf("%d",&p); Simple Interest is 7200
printf("\n Time:"); Compound Interest is 20012
scanf("%d",&n);
printf("\n Rate:");
scanf("%d",&r);
si=(p*n*r)/100;
ci=p*(1+(r/n))^n;
printf("\nSimple Interest is: %d",si);
printf("\nCompound Interest is: %d",ci);
}

Problem Solving Criteria

• Define the problem

• Analyze the problem

• Design a proper solution

• Implement it through a proper code

• Test the code to check flaws

• Deploy the code

• Maintain the code


Codes for Reference: screenshots for your reference

You might also like