You are on page 1of 26

C – PROGRAMMING

INTRODUCTION:
 C is a programming language developed at AT & T‟s Bell Laboratories of USA in 1972.
 The C was designed and developed by BRAIN KERNIGHAN and DENNIS RITCHIE.A
 The C language is high speed and easy to understand .So it is also term as a Middle Level Language.
 C began replace the more familiar languages of that time like PL/I, ALGOL, FORTRAN etc. Possibly
why C seems so popular is because it is reliable, simple and easy to use.
 Major parts of popular operating systems like Windows, UNIX, Linux is still written in C.

ADVANTAGES OF C:
 C is portable:
o This means a program written for one computer may run successfully on other computer also.
 C is Compact:
o The Statement in C language are generally short but very powerful.
 Simple / Easy:
o The C language has both the simplicity of High level language and speed of Low level
Language. So it is also known as middle level language.
 C has only 32 keywords.
 C compiler is easily available.
 C has ability to extend itself. Users can add their own functions to the C Library.

THE C CHARACTER SET :


 The character denotes any alphabet, digit or special symbol used to represent the information.
A,B,C,D, …………………………., Y,Z
ALPHABETS
a, b, c, d,……………………………., y, z

DIGITS 0,1,2,3,4,5,6,7,8,9

SPECIAL SYMBOLS `~!@#$%^&*()_-+=|\}]{[“‟:;?/>.<,

CONSTANTS:
 A constant is an entity that doesn‟t change.
 C constants divided into two major categories:
a) Primary Constants
b) Secondary constants
C - CONSTANTS

PRIMARY CONSTANTS SECONDARY CONSTANTS

INTEGER CONSTANT ARRAY


POINTER
REAL CONSTANT
STRUCTURE
CHARACTER CONSTANT Union
Enum etc.

RULES FOR CONSTRUCTING INTEGER CONSTANTS:


 An Integer constant must have at least one digit.
 It must not have a decimal point.
 It can be either positive or negative.
 If no sign precedes an integer constant it is assumed to be positive.
 No commas or blanks are allowed within an integer constant.
 The allowable range for integer constants is -32768 to 32767.
Example of Integer Constant:
a) 426
b) +123
c) -9087

RULES FOR CONSTRUCTING REAL CONSTANTS:


 Real constants are often called Floating Point constants.
 A real constant must have at least one digit.
 It must have a decimal point.
 It could be either positive or negative.
 Default sign is positive.
 No commas or blanks are allowed within a real constant.
Example of Real Constant:
a) +143.65
b) 456.0
c) -678.900
d) 1.0987
We can use the exponential notation as follows:
a) 4E-3
b) 2.3E+9
c) 0.00564e4

RULES FOR CONSTRUCTING CHARACTER CONSTANT:


 A character Constant is single alphabet, a single digit or a single special symbol enclosed within single
inverted commas.
 Both the inverted commas should point to the left.
 The maximum length of a character constant can be ONE character.

Example of Character constant:


a) „T‟
b) „6‟
c) „*‟

IDENTIFIERS/VARIABLES:
 A variable is a data name that may be used to store a data value.
 A variable is an entity that may change.

RULES FOR CONSTRUCTING VARIABLE NAMES:


 A variable name is any combination of 1 to 31 alphabets, digits or underscores.
 Do not create unnecessarily long variable name as it adds to your typing effort.
 The first character in the variable name must be an alphabet or underscore.
 No commas or blanks are allowed within a variable name.
 No special symbol other than an underscore can be used in a variable name.
Example of Identifiers/Variables constant:
a) Cmd_float
b) Add
c) Rate_of_Interest
 C compiler is able to distinguish between the variable names by making it compulsory for you to declare
the type of any variable name that you wish to use in program.
 For examples of type declaration statements:
a) int Simple_interest;
b) float rate_of_interest;
c) char code;

KEYWORDS:
Every word in C language is a keyword or an identifier. Keywords in C language cannot be used as a
variable name. The compiler specifically uses them for its own purpose and they serve as building blocks of a c
program. The following are the Keyword set of C language.
auto double int struct

break else long switch

case enum register typedef

char extern return union


const float short unsigned

continue .for signed void

default goto sizeof volatile

do if static while

DATATYPES:
 Data Type is used to define the type of value to be used in a Program.
 Based on the type the amount of memory will be allocated to the variables used in the program.
 Each of the basic data types is stored differently in the main memory of the computer.
 The typical memory requirements for the basic data types in C are given below.
Data type Description Typical memory
requirements
int Integer 2 bytes
float Floating point number 4 bytes
char Single character 1 byte
double Double Precision floating point number 8 byte

DATATYPE QUALIFIERS:
 The Basic data types can also be augmented using data qualifiers.
 Data types qualifiers are : short, long, signed and unsigned.
 Integer variable can be qualified as short int, long int, signed int or unsigned int.
 Long to be used as a qualifier with float and double as long float and long double.
 Long float can same as double, and long double could be the same as double.

Integer Data Type


 Integer data type can store only the whole numbers.
Name C Representation Size Range Format
(bytes) Delimiter
Integer int 2 -32768 to 32767 %d

Short Integer short int / short 2 -32768 to 32767 %d

Long Integer long int / long 4 -2147483648 to 2147483647 %ld

Signed Integer signed int 2 -32768 to 32767 %u

Unsigned Integer unsigned int 2 0 to 65535 %d

Signed Short Integer unsigned short int / short 2 -32768 to 32767 %d

Unsigned Short Integer unsigned short int / short 2 0 to 65535 %u

Signed Long Integer signed long int / long 4 -2147483648 to 2147483647 %ld

Unsigned Long Integer unsigned long int / long 4 0 to 4294967295 %lu

Floating Point Data Type


Floating Point data types are also known as Real Numbers. It can store only the real numbers (decimal
numbers) with 6 digits precision.
Name C Representation Size Range Format
(bytes) Delimiter
Float float 4 3.4 e-38 to 3.4 e+38 %f
Double double 8 1.7e-308 to 1.7e+308 %f
Long Double long double 10 3.4 e-4932 to 3.4 e+4932 %lf
Character Data Type
Normally a single character is defined as char data type. It is specified by the keyword char. Char data
type uses 8 bits for storage. Char may be signed char or unsigned char.
Name C Representation Size Range Format
(bytes) Delimiter

Character char 1 -128 to 127 %c

Signed Character signed char 1 -128 to 127 %c

Unsigned
unsigned char 1 0 to 255 %c
Character

Empty Data Type


VOID is the empty data type. This data type is used before main function in a C Language. The word
void refers no return data type. It is used before the main function to specify the type of function. If a function is
of type void it does not return any value to the calling function.

DECLARATION OF DATATYPES:
 This type declaration is done at the beginning of the program.
 The following are the example of type declaration statements:
Example:
int sum, fibo;
float rate_of_interest;
char name[25];
double prin;
 The maximum allowable length of a variable name is 31 characters.
 It is a good practice to exploit this enormous choice in naming variables by using meaningful variable
names.
FORMAT DELIMITERS of printf & scanf STATEMENTS:
Format String Purpose

%d int (signed decimal notation)

%o int (unsigned octal notation)

%x int ( unsigned hexadecimal notation)

%u int (unsigned decimal notation)

%c Char

%f Double

%s Char string

ESCAPE SEQUENCES:
Escape Sequences Purpose

\a Alert(Bell) Character

\b Backspace
\f Formfeed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? Question mark
\‟ Single quote

\” Double quote
OPERATORS:
 Operator is a Symbol that tells or instructs the Compiler to perform certain Mathematical or Logical
manipulations (Calculations).
 Operators are used in a program to work on data and variables.
 Operators in general form a part of Mathematical or Logical expression.
 Operators are generally classified into different types. They are described one below another as follows.
a) Arithmetic Operators
b) Relational Operators
c) Logical Operators
d) Assignment Operators
e) Conditional Operators
f) Special Operators
g) Unary operators

a) ARITHMETIC OPERATORS
Arithmetic operators are used to perform Arithmetic operations. They form a part of program. Programs can be
written with or without operators. But calculations are performed only using operators. Operators act along
Operand to produce result.
Operator Meaning Details

Performs addition on integer numbers, floating point numbers.


+ Addition The variable name which is used is the operand and the symbol
is operator.
- Subtraction Subtracts one number from another.

* Multiplication Used to perform multiplication

/ Division It produces the Quotient value as output.

% Modulo It returns the remainder value as output.

b) RELATIONAL OPERATOR:
 Relational Operators are used to compare two same quantities. There are six relational operators.
Operator Meaning Operator Meaning

< is less than >= is greater than or equal to

<= is less than or equal to == is equal to


> is greater than != is not equal to

The general form of Relational Operator:

expression1 relational-operator expression2

 Examples of relational expressions


a) If (x<y)
b) If (55>25)

 Relational expressions are used in decision making statements of C language.

c) LOGICAL OPERATORS:
 Logical Operators are used when we need to check more than one condition.
 It is used to combine two or more relational expressions.
 Logical Operators are used in decision making.
 Logical expression yields value 0 or 1 i.e.,( Zero or One).
 0 indicates that the result of logical expression is TRUE and 1 indicates that the result of logical
expression is FALSE.
Logical Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

Logical AND ( && ):


 The result of the Logical AND operator will be TRUE If both value is TRUE.
 If any one of the value is false then the result will be always False.
Example:
a) If((a>b)&&(a>c))
The expression is true only if both expressions are true i.e., if a is greater than b and a is greater than c.

Logical OR (||):
 If any of the expression is true the result is true else false otherwise. The result is similar to basic Binary
addition.
 The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2
expressions is true.
Example:
If ((a < m) ||( a < n))
It evaluates to true if a is less than either m or n and when a is less than both m and n.

Logical NOT ( ! ):
 It acts upon single value.
 If the value is true result will be false and if the condition is false the result will be true.
 The logical not operator takes single expression and evaluates to true if the expression is false and
evaluates to false if the expression is true.
Example:
(! a)

d) ASSIGNMENT OPERATORS:
 The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the
value or variable on the left of the expression.
 The general format of Assignment Operator:

identifier = expression
Example:
A= X+Y;
Here the value of X+Y is evaluated to the variable A.
 Assignment operator are two types:
(i) Simple assignment operator
(ii) Shorthand assignment operator

(i) SIMPLE ASSIGNEMENT OPERATOR:


 In Simple assignment operator only one „=‟ equal to operator is used.
 It is used to assign a value to variable or expression.
Example:
X=5;
A=a + b - c;
(ii) SHORTHAND ASSIGNEMENT OPERATOR:
 Shorthand assignment operator must be an arithmetic operator or bitwise operator.
 The general form is
identifier <operator> = expression
 The operator must be an arithmetic operator or bitwise operator.
Example Example
Operator Meaning
Simple Assign. Shorthand
+= Assign sum x=x+1 x+ =1
-= Assign difference y=y-1 y-=1
*= Assign product z=z*(x+y) z*=(x+y)
/= Assign quotient y=y/(x+y) y/=(x+y)
%= Assign remainder x=x%z x%=z
~= Assign one‟s
complement
<<= Assign left shift x = x << z x << = z
>>= Assign right shift
&= Assign bitwise AND y = y&x y&=x
|= Assign bitwise OR
^= Assign bitwise X - OR z = z^y z^=y

e) CONDITIONAL OPERATORS:
 Conditional Operator is also known as “Ternary operator”.
 A ternary operator has three operands.
 The question mark (?) and colon (:) symbols are uses as ternary operator.
 The general form of Conditional Operator is

Condition ? true_expression : false_expression;

Example:
x=8;
result = (x<10 ? True : False); // here the value of the result is ―True‖

f) SPECIAL OPERATORS:
Special operators are known as separators or punctuators. Special operators are
Ampersand ( & ) Braces ( { } ) Colon ( : ) Ellipsis ( … )
Asterisk ( * ) Square Brackets ( [ ] ) Comma ( , ) Hash ( # )
Parenthesis ( () ) Semicolon ( ; ) Ampersand ( & )
It is also known as address operator. It is specified before the identifier name. i.e., variable name. It
indicates memory location of the identifier.
Asterisk ( * )
Asterisk ( * ) is also known as indirection operator. It is specified before identifier name. it indicates
creation of pointer variable. It is also a unary operator.

Braces ( { } )
The opening brace ( { ) and closing brace ( } ) specify the start and end of compound statement in a
function. Here semicolon ( ; ) is not necessary. Semicolon ( ; ) is used only in structure declaration.

Square Brackets ( [ ] )
Brackets [] also referred as array subscript operator. It is used to indicate single and multi dimensional
arrays.
E.g.: int x[10]; float l[10][20];

Colon ( : )
Colon ( : ) is used in labels. It is used in unconditional control statement i.e., in goto statement.
E.g.: switch (a)
{
Case 1:
default:
}

Comma Operator ( , )
It is used to link expressions together. It is used together with variables to separate one variable from
another. It is used in for loop. It has the lowest precedence among operators
E.g.: for(n=1,m=10;n<=m; n++, m++)
int a, b, c;
sum= (x=5,y=3,x+y);
Ellipsis ( … )
Ellipsis ( … ) are three continuous dots with no white spaces between them. It is used in function
prototype. It indicate that the function can have any number of arguments.
Eg: void fun(char s, int n, float f, …);

Hash ( # )
Hash ( # ) is also known as pound sign. it is used to indicate preprocessor directives.
E.g.: #include<stdio.h>
Parenthesis ( () )
Parenthesis ( () ) is also known as function call operator. It is used to indicate the open and end of function
prototypes, function call, function parameters, parenthesis are used to group expressions.

Semicolon ( ; )
Semicolon ( ; ) is a statement delimiter. It is used to end a C statement.
E.g.: g=d+h;

g) UNARY OPERATOR:
 A unary operation is an operation with only one operand.
 Unary operators appear before their operand and associate from right to left.
Prefix operator: ++a,--a
a) N=1;
X=++N;
Output: X=2,N=2
b) N=5;
Y= --N
Output: Y = 4,N=4
Postfix Operator: a++ & g—
a) N=1;
X=++N;
Output: X=2,N=2
b) N=5;
Y= --N
Output: Y = 4,N=4

PRECEDENCE OF OPERATORS:
 Outermost Parenthesis is evaluated first.
 Then innermost parenthesis.
 If there is two or more parenthesis, then the order of execution is from left to right.
 Next Multiplication and Division are performed.
 Finally Addition and Subtraction.
Priority Operators Descriptions

I */ % Multiplication, division, modular division

II +- Addition, subtraction
III = Assignment

Example: Determine the hierarchy of operations and evaluate the following expressions:
Z = 3/2 * 4 + 3 / 8 +3
Solution:
Z = 3/2 * 4 + 3 / 8 +3
z = 1* 4 + 3 / 8 +3 operation : /
Z = 4+ 3 / 8 +3 operation : *
Z = 4 + 0 +3 operation : / [3/8 is 0]
Z=4+3 operation : +
Z=7 operation : +

STATEMENTS:
1. COMMENT STATEMENT:
 Comment about program should be enclosed within /* */.
 Though comments are not necessary, it is a good practice to begin a program with a comment indicating
the purpose of the program.
 Any number of comments can be written at any place in the program .
 For example comment can be written before the statement, after the statement or within the statement as
shown below:
/* formula */ si = p*n*r/100;
Si = p*n*r/100; /* formula */
 Sometimes it is not so obvious as to what a particular statement in a program accomplishes. At such
times it is worthwhile mentioning the purpose of the statement( or a set of statements) using a comment.
For example:
/* formula for simple Interest */
si = p*n*r/100;
 Often programmers seem to ignore writing of comments.
 Comments cannot be nested, for example,
/* Cal of Si /* author Shiva date 10/03/2011*/*/
Is invalid.
 A comment can be split over more than one line, as in,
/* This is
A jazzy
Comment */

2. DECLARATIVE STATEMENT:
 Any variable used in the program must be declared before using it.
 For Example,
int add,rate_of_interest;
float r,si;
 C statement always ends with a ;
 When we are declaring multiple variables we need not declare them in separate statements. We can
combine them into one statement, as shown here:
int num, left_digit, right_digit;

3. INPUT/OUTPUT STATEMENT:
INPUT FUNCTION:
 Input Functions are used to accept data from user.
 It is used to transfer the value to the processor memory.
 The input/output functions are collectively known as Standard I/O Library.
 All input/output operations are carried out through function calls.
scanf():
 This input function is used to provide input or accept input from the user in a program.
 It uses Format Specification String and list of variables as parameters.
 Format Specification used to define the type of data used as Input.
The syntax for scanf() is
scanf(“control string”,argument list); (or)
scanf(“cs1, cs2, ……csn”,arg1, arg2,….argn);
Here Control String specifies the format in which the variables values provided as Input.
 Argument List specifies the address of Memory Locations where the values are being stored.
Example:
For integer with d as value to be input %d. scanf (“%d %d”, &sum1, &sum2);
For character with c as value to be input %c. scanf (“%c %c”, &ch, &name):

OUTPUT FUNCTIONS:
printf()
 This function is used to print Numerical values i.e.,(integer, float, double), single characters, strings or
combination of both.
 printf() function is used to redirect data from Computer‟s memory to standard output device.
 For float data type, number can be rounded to nearest decimal places.
 The general form of printf() is
printf(“control string”, arg1, arg2, ... ..., argn); (or)
printf(“control string”, argument list);
 Control string consists of characters that will be printed on the screen. Escape sequences and Format
specification can be used together with the control string.
 Format specification is used to define the output format for each item, which is being displayed.

Example of Printf( ):
#include < stdio.h >
main ( ) Output:
{ Hello! Welcome to world of Engineering.

printf(“Hello!”);
printf(“Welcome to the world of Engineering!”);
} Hello! Welcome to the world
of Engineering.

4. ASSIGNMENT STATEMENT:
 The assignment statement, assigns the value in the right hand side of the expression to the variable in
the left hand side.
 The syntax of the assignment operator is
Variable name = constant / expression;
Example :
Int a = 5; ( here a assigns the value of 5)
5. CONDITIONAL STATEMENT:
 The statements which are helpful in controlling the flow of execution are known as control
statements.
 Control statements are mainly classified into two types.
(i) Unconditional Control statement
(ii) Conditional Control statement.

(i) CONDITIONAL CONTROL STATEMENT:


 Those statements, which is used to check the Condition and based on the given condition carry out
specific operations.
 Conditional Control statement is mainly of three main types. They are :
(i) Decision control
(ii) Looping control

(i) Decision control:


 This statement executes the given condition based on the logical test. It performs two actions based on
the result.
 The final result of the statement may be true or false.
 This includes:
o Simple IF statement
o IF…ELSE statement
o Switch statement

SIMPLE IF STATEMENT:
 The general syntax of if statement is

if(condition)
{
True-statements;
}

 The if statement is used to execute / skip a block of statements on the basis of truth or falsity of a
condition.
 The condition to be checked is put inside the parenthesis which is precede by keyword if.
Example of simple if:
# include<stdio.h>
main( )
{
int x;
printf(“Enter the value of X \n”);
scanf(“%d”,&x);
if(x>=100)
{
printf(“X is greater than or equal to 100 \n”);
printf(“You thing High”);
}
Prinf(“X is less than 100”);
}
Sample input/output:
a) Enter the value of X
300
X is greater than or equal to 100
You thing High
b) Enter the value of X
76
X is less than 100

THE IF-ELSE STATEMENT:


 In an simple if statement, if the condition is false, control is passed to the statement following the if
statement.
 The if-else statement, allows us to explicitly specify one or more statements to be executed when the
condition is false.
 The general format of the statement is,
if(condition)
{
True-statements;
}
else
{
False – statements:
}
Example of If_else statements:
if(avg_marks > 50)
{
printf(“The result is pass \n”);
}
else
{
printf(The result is Fail \n”);
}

SWITCH STATEMENTS:
 Nested if statements are very useful but they can become complex and difficult to debug
As the number of if statements nested increases
 An alternative is to use switch statements
 A switch statement is used to choose one option out of many based on the value of an expression
 The general format of the switch statement is as given below

Switch (variable) {
Case value1: statements;
Break;
Case value2: statements;
Break;
……..
Case value n: statements;
Default : statements;
Break;
}

 If the break statement were not present, the execution would logically flow to the statements of the next
case, which we do not want.
 Example of switch….case construct:
#include <stdio.h>
main() {
int day_num;
printf(“Enter a value between 1 and 7:”);
scanf(“%d”, &day_num);

switch(day_num){
case 1:
printf(“The day is Monday\n”);
break;
case 2:
printf(“The day is Tuesday\n”);
break;
case 3:
printf(“The day is Wednesday\n”);
break;
case 4:
printf(“The day is Thursday\n”);
break;
case 5:
printf(“The day is Friday\n”);
break;
case 6:
printf(“The day is Saturday\n”);
break;
case 7:
printf(“The day is Sunday\n”);
break;
default :
printf(“The input number is not valid \n”);
break;
}
}

LOOPING:
 Computers are best suited for applications where a sequence of steps has to be repeated several times.
 The program construct that is used to repeat such a sequence of steps is called a loop.
 Every loop consists of the sequence of statements are to be repeated and a condition that determines the
number of times those statements are to be repeated.
 They are three different loop statements in C. They are,
1) While loop
2) Do- while loop
3) For –loop

While loop:
 The while construct implements an iterative construct. Iterations are also called loops.
 The general form of the while construct is as follow:

I while(Condition ){
Block of statements;
………;
}

Example of while Loop:


int I = 1;
while ( I <= 3) {
printf(“Hello %d \n”,i);
I = I=1;}
 The condition true only, iterative loops are execute the statements are executed.

Do-while Statements:
 The general structure of do-while statement is as follows.
do {
statement 1;
statement 2;
…;
} while (condition);
 In this do-while statement, the condition is tested at the end.
 This implies that the body of the loop in a do-while statement is executed at least once.
 In the while statement, the condition is tested at the beginning of the loop.
 This implies that the body of the loop in while may not be executed even once.
Example of do-while loop:
int i = 1;
do {
printf(“hai .. how r u \n”);
} while (i<=1);

FOR LOOP:
 The for statement is used to repeat one or more statements for a specified number of times.
 The general format of the for statement is
for(initial ; test ; adjust)
 Where initial is a statement that initializes values prior to the execution of the loop.
 The expression test must hold true for an iteration of the loop to start.
 The adjust statement is executed for each iteration, after the iteration has completed.
Example of for loop:
for (n=1; n<=10; n++)
{
printf("%3d", n);
}

ARRAYS:
 Arrays are storage structures in memory that can help store multiple data items having common
characteristics.
 An array is referred to only by a single name, although it can have multiple data items.
 Arrays are an arrangement of multiple data items.

Definition of Arrays:
Array is a group of elements of the same data type that share a common name. Each array element is
specified by array name with one or more subscripts. Subscripts are enclosed within square brackets [ ].
Individual values in an array are called elements.
(or)
Array is a sequenced collection of related data items that share a common name and same data type.
Each array element is specified by array name with index or subscripts in brackets after array name.
(or)
Array is a collection of identical data objects which are stored in consecutive memory locations in a
common variable name. Individual values in an array are called elements.

ARRAY DECLARATION
In general, array is declared as
data-type arrayname[size]; or
data-type arrayname[subscript];

Data-type refers to a valid data type. Size refers to the maximum number of elements an array can hold
at the maximum. Array must be declared before being used in the C program.

Example
float height[50];
// Program to print sum of ten numbers
#include<stdio.h>
void main()
{
int i;
float num[10] ,total=0.0; /*read array values*/
printf(“enter 10 real numbers\n”);
for(i=0;i<10;i++)
{
scanf(“%f”,&num[i]);
total+=num[i]; /*calculate total*/
}
for(i=0;i<10;i++)
{
printf(“num[%d]=%5.2f\n”,i,x[i]);
printf(“\n Total=%2f\n”,total);
}
getch();
}

ONE DIMENSIONING ARRAY:


 Declaring the name, type of the array and setting the number of elements in the array is known as
dimensioning the array.
 Array declaration helps to define the type of the array, name of the array, number of subscripts in an
array and the total number of memory locations to be allocated.

One Dimensional Array Declaration.


 The One Dimensional Array is generally declared as follows.
data-type arrayname[size];
 Data-type refers to a valid data type. Size refers to the maximum number of elements an array can hold
at the maximum. Array must be declared before being used in the C program.

TWO DIMENSION ARRAY:


 Two dimensional array consist of (or) made up of two dimensions i.e., rows and columns.
 It contains two subscripts. First subscript is represented as „r‟ which stands for the total number of rows
and the Second subscript „c‟ stands for number of columns.
 A Two Dimension Array takes the general form as follows.
data-type array-name[r] [c];
 Here r stands for number of rows and c stands for number of columns. Data-type refers to a valid C data
type. Array-name refers to a valid array name.

Rules for Declaring Two-Dimensional Array


 For matrix addition and subtraction first matrix row, column and second matrix row, column should be
the same.
 For matrix multiplication, first matrix column and second matrix row must be equal.

/* example program to add two matrices & store the results in the 3rd matrix */
/******* MATRIX ADDITION ****/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n;
int c[5][5],a[5][5],b[5][5];
clrscr();
printf("\n\t\t\t MATRIX ADDITION \n\n");
printf("\n Enter the Order of Matrix: \n");
scanf("%d%d",&m,&n);
printf("\nEnter the Elements of 1st Matris:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n");
}
printf("\n Enter the Elements of 2nd Matrix: \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
printf("\n");
}
printf("\n The Resultant Matrix is: \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",&c[i][j]);
}
printf("\n\n");
}
getch();

MULTI-DIMENSIONAL ARRAY
 Multi-dimensional Array consists of (or) requires more than two square brackets and it may contain any
number of values specified within square brackets.
 It may be three, four, five, six and so on. A Multi dimensional Array in general takes the following
form.
data-type array-name[s1][s2]……………[sn];

 Data-type refers to a valid C data type. Array-name refers to a valid array name. s1,s2,s3, ……… are
sub scripts.

You might also like