CS3353 Unit1
CS3353 Unit1
CS3353 Unit1
Syllabus
The data type, of a variable determines a set of values that a variable might take and aset
of operations that can be applied to those values.
Data type refers to the type and size of data associated with the variable and functions.
Allows 6 digits
after decimal point.
double 8 -1.7e-308 to +1.7e308 %lf
Allows 15 digits
after decimal
point.
long double 10 -1.7e-4932 to 1.7e4932 %LF
Allows 15 digits
after decimal
point.
/*Program*/
#include<stdio.h>
int main()
{
char a;
unsigned char b;
int i;
unsigned int j;
long int k;
unsigned long int m;
float x;
double y
long double z;
return 0;
}
The specifiers and qualifiers for the data types can be broadly classified into
three types
Size qualifiers alter the size of the basic data types. There are two such qualifiers that can
be used with the data type int; these are short and long.
short, when placed in front of the data type int declaration, tells the C compiler that the
particular variable being declared is used to store fairly small integer values. Long specifies it
is a very big integer value.Long integers require twice the memory of than small ints.
Sign specifiers: for example fot int data type out of 2bytes(2*8=16bits) of its size the
highest bit(the sixtheenth bit) is used to store the sign of the integer value. The bit is 1 if
number is negative and 0 if the number is positive.
Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Sign of number
(1 for –ve and 0
for +ve0
Type qualifiers : There are two type qualifiers, const and volatile;
Eg: const float pi = 3.14156; // specifies that the variable pi can never be changed bythe
Program.
Table:Size and range in (16-bit machines)
Allowed combinations of basic data types and modifi ers in C for a 16-bit
computer
STUDENTSFOCUS
VARIABLES
Variable is the name of memory location which holds the data. Unlike constant, variables are
changeable, value of a variable can be changed during execution of a program. A
programmer must chose a meaningful variable name.
Variables are used for holding data values so that they can be utilized for various
computations in a program.A variable must be declaed and then used for coputation work in
program./A variable is an identifier used for storing and holding some data(value).
1.A data type: Like int, double, float. Once defined,the type of a C variable
cannot be changed.
2.A name of the variable.
3.A value that can be changed by assigning a new value to the variable. The
kind of values a variable can assume depends on its type.
Eg : for variable int salary,it can only take integer values can only take integer
values like 65000 and not 6500.0
Rules For Constructing Variables
Declaration of a variable must be done before it is used for any computation inhte
program.
Declaration tells the compiler what the variable name is.
Declaration tells what type of data the variable will hold.
Until the variable is not defined/or/declared compiler will not allocate memory space tothe
variables.
A variable can also be declared outside main() function.
A variable can also be declared in other program and declared using extern keyword.
int yearly_salary;
float monthly_salary;
int a;
double x;
int ECE1111;
Initializing a variable:=
Initializing a variable means to provide a value to variable
int yearly salary=5,00,000
float monthly salary=41666.66
Difference between identifier and variable
Identifier Variable
Indentifier is the name given to a While variable is used to name a memory
variable,function etc. location which stores data
An identifier can be a variable ,but not all All variables names are identifiers
identifiers are variables
Example : void average() Example: int average
{
}
Variables are a way of reserving memory to hold some data and assign names to them so that
we don’t have to remember the numbers like REG46735 or memory address like FFFFoxFF
and instead we can use the memory location by simply referring to the variable.
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Special Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language.
Assume variable A holds 10 and variable B holds 20
Relational Operators
Following table shows all the logical operators supported by C language. Assume variable A
holds 1 and variable B holds 0, then −
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is as follows −
P q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B'
holds 13, then −
Operator Description Example
Binary AND
& (A & B) = 12, i.e., 0000 1100
It takes 1 if both operands has value 1.
Binary OR
Operator copies a bit if it exists in either operan
| (A | B) = 61, i.e., 0011 1101
The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1.
Binary XOR
^ 1 if the corresponding bits of two operands are (A ^ B) = 49, i.e., 0011 0001
opposite
Binary Ones Complement
~ (~A ) = -60, i.e,. 1100 0100
'flipping' bits- 0 changed to 1and 1 changed to 0
Binary Left Shift Operator.
<< The left operands value is moved left by the A << 2 = 240 i.e., 1111 0000
number of bits specified by the right operand.
Binary Right Shift Operator.
>> The left operands value is moved right by the A >> 2 = 15 i.e., 0000 1111
number of bits specified by the right operand.
Assignment Operators
The following table lists the assignment operators supported by the C language
special Operators
For example, x = 7 + (3 * 2); here, x is assigned 13, not 20 because operator * has
a higher precedence than +, so it first gets multiplied with 3*2 and then adds into
7.
Expression
Eg : c+d
x/y+b+a*a*a
3.14 *r *r
#include<stdio.h>
int main()
{
int num1,num2,num3;
scanf("%d %d %d",&a,&b,&c);
if((a>b)&&(a>c))
printf("\n %d is greatest",a);
else if(b>c)
printf("\n %d is greatest",b ");
else
printf("\n %d is greatest",c);
return 0;
}
Output = 21
complement = 220
OutputAND = 8
OutputOR = 29
Decision Making and Branching
if(test-expression)
It allows the computer to evaluate the expression first and them depending on whether
the value of the expression is "true" or "false", it transfer the control to a particular
statements. This point of program has two paths to flow, one for the true and the other
for the false condition.
if(test-expression)
statement-block
{
statement-x;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n;
clrscr();
printf(" \n enter two numbers:");
scanf(" %d %d", &m, &n);
if(m-n= = 0)
Eg-2 {
if (code = = 1) printf(" \n two numbers are equal");
{ }
salary = salary + 500; getch();
} }
printf("%d",salary);
Output:
44
Two Numbers are Equal
Nested if
if( cond 1)
{
/* Executes boolean expression when cond 1 is true */
if(cond 2) {
/* Executes when the boolean expression 2 is true */
}
}
Example:
#include <stdio.h>
int main ()
{
int a = 100;
int b = 200;
if( a == 100 ) {
/* if condition is true then check the following */
if( b == 200 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
Syntax-1 Syntax-2
if(test-condition-1) If(test-condition-1)
(stmts) if(test-condition-2)
else
statement-1;
if(condition 2)
else
Statement-1;
statement-2;
else
statement-2; else
statement-3;
statement-x statement-x
If the test-condition-1 is false, the statement-3 will be executed; other wise it continues
the second test. If the condition-2 is true, the statement-2 will be evaluated and then the
control is transferred to the statement-x.
Example:Program to relate two integers using =, > or <
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
BREAK is a keyword that allows us to jump out of a loop instantly, without waiting to
get back to the conditional test.
break;
Example program
#include <stdio.h> Output:
int main ()
{ value of a: 10
int a = 10; value of a: 11
while( a < 20 ) value of a: 12
{ value of a: 13
printf("value of a: %d\n", a); value of a: 14
a++; value of a: 15
if( a > 15)
{
break;
}
}
return 0;
}
Continue
The continue statement in C programming works somewhat like the break statement.
Instead of forcing termination, it forces the next iteration of the loop to take place,
skipping any code in between.
For the for loop, continue statement causes the conditional test and increment portions
of the loop to execute. For the while and do...while loops, continue statement causes the
program control to pass to the conditional tests.
Syntax: continue;
GOTO
GOTO STATEMENT
„C‟ supports goto statement to branch unconditionally from one point to another in the program.
Syntax
goto label;
..
.
label: statement;
Or
label: statement;
...
...
goto label;
Output
value of a: 10
value of a: 11
value of a: 12
Example program value of a: 13
#include <stdio.h> value of a: 14
int main () value of a: 16
{ value of a: 17
int a = 10; value of a: 18
ABCL:do value of a: 19
{
if( a == 15)
{
a = a + 1;
goto ABCL;
}
printf("value of a: %d\n", a) ;
a++;
}while( a < 20 );
return 0;
}
program to print “n‟ natural number
#include<stdio.h>
void main( )
{
int n,i=1;
clrscr();
printf("enter number");
scanf("%d\t",n);
printf("natural numbers from 1 to %d", n);
lb: printf("%d\t",i);
i++;
if(i<=n)
goto lb;
getch();
}
LOOPING STATEMENTS
If the loop Test Condition is true, then the loop is executed, the sequence of
statements to be executed is kept inside the curly braces {} is known as the Loop body.
After every execution of the loop body, condition is verified, and if it is found to be true the
loop body is executed again. When the condition check returns false, the loop body is not
executed, and execution breaks out of the loop.
Types of Loop
1. while loop
2. for loop
3. do while loop
while loop
Repeats a statement or group of statements while a given condition is true. It
tests the condition before executing the loop body.
while(condition)
statements;
In some situations it is necessary to execute body of the loop before testing the
condition. Such situations can be handled with the help of do-while loop. do statement
executes the body of the loop first and at the end, the condition is checked using while
statement. It means that the body of the loop will be executed at least once, even though the
starting condition inside while is initialized to be false.
General syntax
do {
statement(s);
} while( condition );
Output
5 10 15 20 25 30 35 40 45 50 55 60
Jumping Out of Loops
1) break statement
It causes the control to go directly to the test-condition and then continue the loop process.
On encountering continue, cursor leave the current cycle of loop, and starts with the next
cycle.
For Loop
for loop is used to execute a set of statements repeatedly until a particular condition is
satisfied. We can say it is an open ended loop. General format is,
In for loop we have exactly two semicolons, one after initialization and second after the
condition. In this loop we can have more than one initialization or increment/decrement,
separated using comma operator. But it can have only one condition.
#include<stdio.h> Output
void main( ) 1
{ 21
int i, j; 321
4321
for(i = 1; i < 5; i++) /* first for loop */
54321
{ printf("\n");
/* second for loop inside the first
*/ for(j = i; j > 0; j--)
{
printf("%d", j);
}
}
}
ARRAYS
4. MULTIDIMENTIONAL ARRAYS
An array is a collection of similar data items, accessed using a common name. The
collection of element can all be integers or be all decimal value or be all characters or
be all strings.
A one-dimensional array is like a list
A two dimensional array is like a table
The C language places no limits on the number of dimensions in an array
Array Declaration:
To declare an array in C, a programmer specifies the type of the elements and the number
of elements required. The arraySize must be an integer constant greater than zero and
datatype can be any valid C data type.
Syntax1:
datatype arrayName[ arraySize ];
int n;
Example-1 Scanf(“%d”,&n);//getsize
int x[n]; //array declaration
int number[20]; int marks[44]; float
salary[10];
double value[25]; #include<stdio.h>
int main( )
#define N 100 {
int n=25; int main( ) int N=10,M=20;
double x[n], y[n]; int marks[N*M];//array dec
//array declaration { ....
int marks[N];//array dec return 0;
.... }
return 0;
}
Syntax-2
mark[0] = 55
mark[1] = 66
mark[2] = 77
mark[3] = 88
mark[4] = 99
it means. .
balance[0] = 1000.0;
balance[1] = 2.0;
balance[2] = 3.4;
balance[3] = 7.0;
balance[4] = 50.0;
Automatic sizing
Here, the size of the array in these cases is equal to the number of elements present in the initializer
list as the compiler can automatically deduce the size of the array.
OPERATIONS ON ARRAYS
o Traversing an array
o Inserting an element in an array
o Searching an element in an array
o Deleting an element from an array
o Merging two arrays
o Sorting an array in ascending or descending order
Working with one dimensional array
STORE and DISPLAY VALUES IN AN ARRAY (traversing an array)
#include<stdio.h>
int main( ) Output:
{ Enter the array elements
int k,array[10];//array declaration 2
printf(―Enter the array elements:‖);
for(k=0;k<5;k++)
{
scanf(―%d ‖,&array[i]); // storing values in array
} Display the array elements
printf(―\n Display the array elements:‖); 2
for(k=0;k<5;k++)
{
printf(―%d \n‖,array[i]);//displaying values of array
}
return 0;
}
FIND SUM AND AVERAGE OF N NUMBERS
#include<stdio.h>
int main( ) Output:
Enter the array size: 6
{
Enter the array elements
int k,n,sum=0;array[10];//array declaration 9
float avg;
printf(―\n Enter the array size:‖);
scanf(―%d‖,&n);
printf(―\n Enter the array elements:‖);
for(k=0;k<n;k++)
{
scanf(―%d ‖,&array[i]); // storing values in array sum=27 and avg=4.50000
}
for(k=0;k<n;k++)
{
sum=sum+array[i]; //sum of array elements
}
avg=sum/n;
printf(―\n sum=%d and avg=%f ‖,sum,avg);
}
return 0; Output:
} Enter the arrayelements
REVERSE OF ARRAY ELEMENTS 2
#include<stdio.h> 4
int main( ) 3
{ 1
int k,n,array[10];//array declaration 8
Display the arrayelements
printf(―\n Enter the array size:‖);
8
scanf(―%d‖,&n);
1
printf(―\n Enter the array elements:‖);
3
for(k=0;k<n;k++)
4
2
{
scanf(―%d ‖,&array[i]); // storing values in array
}
printf(―\n array elements in reverse order:‖);
MM for(k=n-1;k>=0;k--)
{
printf(―%d \n‖,array[i]);//displaying values of array
}
return 0;
}
Write a program to print the position of the smallest number of n numbers using
arrays.
#include <stdio.h>
int main()
{
int i, n, arr[20], small, pos;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
small = arr[0] Output
for(i=1;i<n;i++) Enter the number of elements in the array : 5
{ Enter the elements : 7 6 5 14 3
if(arr[i]<small) The smallest element is : 3
{ The position of the smallest element in the
small = arr[i]; array is : 4
pos = i;
}
}
printf("\n The smallest element is : %d", small);
printf("\n The position of the smallest element in the array is:
%d", pos);
return 0;
}
Logic Here the remainders of the integer division of a decimal number by 2 are storedas
consecutivearrayelements.Thedivisionprocedure is repeateduntilthenumberbecomes
0.
Two dimentional arrays stores data in tabular column format represented as rows and columns
Array Declaration:
datatype arrayname[size][size];
Array Initialization:
int a[2][2]={ {1,4 },{2,3}}
int b[2][2]={1,4,2,3}
1 4
2 3
12.3 45.2
19.3 23.4
#include <stdio.h>
int main()
{
int i,j;
int a[3][2] = {{4,7},{1,0},{6,2}};
for(i = 0; i < 3; i++)
{
for(j = 0; j < 2; j++)
{
printf(“%d”, a[i][j]);
}
printf(“\n”);
}
return 0;
}
Row-1 4 7
Row -2 1 0
Row-3 6 2
1 2 3 4 5 6 7 8 9
Row 0 Row 1 Row 2
WORKING WITH TWO-DIMENSIONAL ARRAYS
Transpose of a matrix
Transpose of A is AT=(aji), where i is the row number and j is the column number.
Program
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
Sample output
// getting elements of the matrix Enter rows and columns of
printf("\nEnter elements of matrix:\n"); matrix: 2
for(i=0; i<r; ++i) 3
for(j=0; j<c; ++j)
{ Enter element of matrix:
printf("Enter element a%d%d: ",i+1, j+1); Enter element a11: 2
scanf("%d", &a[i][j]); Enter element a12: 3
} Enter element a13: 4
Enter element a21: 5
// Displaying the matrix a[][] */ Enter element a22: 6
printf("\n Entered Matrix: \n"); Enter element a23: 4
for(i=0; i<r; ++i)
for(j=0; j<c; ++j) Entered Matrix:
{ 234
printf("%d ", a[i][j]);
if (j == c-1) 564
printf("\n\n");
}
Transpose of Matrix:
// Finding the transpose of matrix a 2 5
for(i=0; i<r; ++i)
for(j=0; j<c; ++j) 3 6
{
transpose[j][i] = a[i][j]; 4 4
}
return 0;
}
Program -2(Transpose)
#include <stdio.h>
void main()
{
int array[10][10]; int i, j, m, n;
$ cc pgm85.c
$ a.out
Enter the order of the matrix 3 3
Enter the coefficients of the matrix 3 7 9
2 7 5
6 3 4
The given matrix is 3 7 9
2 7 5
6 3 4
Transpose of matrix is 3 2 6
7 7 3
9 5 4
Matrix addition and subtraction
#include <stdio.h>
int main(){
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
#include <stdio.h>
#include <math.h>
#defi ne row 10
#defi ne col 10
int main()
{
fl oat mat[row][col], s;
int i,j,r,c;
printf(“\n Input number of rows:”);
scanf(“%d”, &r);
printf(“\n Input number of cols:”);
scanf(“%d”, &c);
for(i = 0 ; i< r; i++)
{
for(j = 0 ;j<c; j++)
{
scanf(“%f”, &mat[i][j]);
}
}
printf(“\n Entered 2D array is as follows:\n”);
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf(“%f”, mat[i][j]);
}
printf(“\n”);
}
s = 0.0;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
s += mat[i][j] * mat[i][j];
}
}
printf(“\n Norm of above matrix is: %f”, sqrt(s));
return 0;
}
C Program to read a matrix and find sum, product of all elements of two dimensional (matrix)
array
include <stdio.h>
#define MAXROW 10 Enter number of Rows :3
#define MAXCOL 10 Enter number of Cols :3
int main()
{ Enter matrix elements :
int matrix[MAXROW][MAXCOL]; Enter element [1,1] : 1
int i,j,r,c; Enter element [1,2] : 1
int sum,product; Enter element [1,3] : 1
Enter element [2,1] : 2
printf("Enter number of Rows :");
Enter element [2,2] : 2
scanf("%d",&r);
Enter element [2,3] : 2
printf("Enter number of Cols :");
scanf("%d",&c); Enter element [3,1] : 3
Enter element [3,2] : 3
printf("\nEnter matrix elements :\n"); Enter element [3,3] : 3
for(i=0;i< r;i++)
{ SUM of all elements : 18
for(j=0;j< c;j++) Product of all elements :216
{
printf("Enter element [%d,%d] : ",i+1 ,j+1);
scanf("%d",&matrix[i][j]);
}
}
sum=0;
product=1;
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
sum+=matrix[i][j];
product*= matrix[i][j];
} }
printf("\nSUM of all elements : %d \nProduct of all elements :%d",sum,product);
return 0;
}
Find the sum of diagonal elements of a matrix 1 2 3
#include < stdio.h > 2 4 6
int main() 3 5 8
{
int a[10][10],i,j,sum=0,r,c; Sum of diagonal=13
clrscr();
printf("\n Enter the number of rows and column ");
scanf("%d%d",&r,&c);
printf("\nEnter the %dX%d matrix",r,c);
for(i=0;i < r;i++)
{
for(j=0;j < c;j++)
{
scanf("%d",&a[i][j]);
}//for
}//for
for(i=0;i < r;i++)
{ for(j=0;j < c;j++)
{ if(i==j)
{
sum+=a[i][j];
}
}//for
}//for
printf("\nThe sum of diagonal elements is %d",sum); return 0;
}//main
#include <stdio.h>
void main () Output
{
int array[10][10]; Enter the order of the matrix
int i, j, m, n, sum = 0; 22
printf("Enter the order of the matrix\n"); Enter the co-efficients of the matrix
scanf("%d %d", &m, &n); 23 45
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i) 80 97
{ Sum of the 0 row is = 68
for (j = 0; j < n; ++j) Sum of the 1 row is = 177
{ Sum of the 0 column is = 103
scanf("%d", &array[i][j]); Sum of the 1 column is = 142
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
sum = 0;
}
}
C Program to do the Sum of the Main & Opposite Diagonal Elements of a MxN Matrix
#include <stdio.h>
void main ()
{ static int array[10][10]; Enter the order of the matix
int i, j, m, n, a = 0, sum = 0; 22
printf("Enetr the order of the matix \n"); Enter the co-efficients of the matrix
scanf("%d %d", &m, &n); 40 30
if (m == n ) 38 90
{ The given matrix is
printf("Enter the co-efficients of the matrix\n"); 40 30
for (i = 0; i < m; ++i) 38 90
{
for (j = 0; j < n; ++j) The sum of the main diagonal elements is
{ = 130
scanf("%d", &array[i][j]); The sum of the off diagonal elements is
} = 68
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
C Program to Find the Frequency of Odd & Even Numbers in the given Matrix
#include <stdio.h>
void main()
{