C++ Programming
C++ Programming
C++ Programming
UNIT 1
Computational thinking for Problem solving – Algorithmic thinking for
Problem solving-Building Blocks - Problem Solving and Decomposition
–Dealing with Error – Evaluation. Overview of C – Data types – Identifiers
– Variables – Storage Class Specifiers – Constants– Operators - Expressions
– Statements – Arrays and Strings – Single-Dimensional – Two-
Dimensional Arrays – Arrays of Strings – Multidimensional Arrays.
Computational thinking for problem solving
▪ Computational thinking (CT) is the mental skill to apply concepts, methods, problem
solving techniques, and logic reasoning, derived from computing and computer science,
to solve problems in all areas, including our daily lives.
▪ In education, CT is a set of problem-solving methods that involve expressing problems
and their solutions in ways that a computer could also execute. It involves automation of
processes, but also using computing to explore, analyze, and understand processes
(natural and artificial).
▪ Computational thinking is an interrelated set of skills and practices for solving
complex problems, a way to learn topics in many disciplines, and a necessity for
fully participating in a computational world.
Four cornerstones of computational thinking:
❑Decomposition
❑Pattern recognition
❑Abstraction, and
❑Algorithms.
Algorithmic thinking for Problem solving
At its essence, algorithmic thinking is thinking about how to solve a
problem in a systematic way. It’s about:
❖ Defining the problem clearly
❖ Breaking the problem down into small, simple parts
❖ Define the solution for each part of the problem
❖ Implementing the solution
❖ Making it efficient (eventually)
ALGORITHM:
• Algorithm is defined as a step by step method to solve a problem using computer.
• Algorithm is a mechanical procedure for solving problems.
Properties of algorithm:
• Finiteness: An algorithm terminates after a finite numbers of steps.
• Definiteness: Each step in algorithm is unambiguous.
• Effectiveness: Each line in an algorithm must be able to solve easily.
• Input: An algorithm accepts zero or more inputs.
• Output: It produces at least one output.
ALGORITHM EXAMPLE :
Step 1: Start
Step 2: Declare two variables num1,num2
Step 3: Read values for num1, num2
Step 4: Add num1 and num2 and assign the value to a variable sum
Step 5: Display sum
Step 6: Stop
7
Example:
#include <stdio.h>
int main() {
int a,b,c;
printf("enterthe two number\n");
scanf("%d \n %d", &a, &b);
c=a+b;
printf("Addition of %d and %d is %d",a,b,c);
return 0;
}
Output:
enterthe two number
6
7
Addition of 6 and 7 is 13
Algorithm for multiplication of 3 numbers
Step1: Start
Step2: Declare no1,no2, no3
Step3: Read the values for no1,no2,no3
Step4: multiply no1,no2,no3
calculate d=no1 * no2 * no3
Step5: print the value of d
Step6: stop
9
FLOW CHART:
• The graphical representation of an algorithm is called as flowchart.
11
ADVANTAGES OF FLOWCHARTS
• Communication: Flowcharts act as a medium for communicating the program logic.
• Effective analysis: flowchart helps the programmer to analyze problem more effectively.
• Proper documentation: Program flowcharts helps the programmer to document the program.
• Documentation helps future modification and error correction.
• Efficient Coding: The flowcharts act as a guide or blueprint during the program coding.
14
Flowchart for finding area of a circle.
15
BUILDING BLOCKS
Generally,
• sequence
• selection
• Iteration
16
Sequence :
Addition of 2 numbers, Multiplication of 3 numbers…….
Selection:
Biggest of 2 numbers
45 24
Algorithm:
Step1: Start
Step2: Read 2 numbers as a, b
Step3: compare ( if a>b )
print “a is big”
otherwise
print “b is big”
Step 4: stop
17
FLOWCHART FOR
BIGGEST OF 2 NUMBERS
18
Selection :
Write an algorithm for checking a no is odd or even.
Step1: start
Step2:Read a number as number
Step3: if number %2 = = 0
print “even number”
otherwise
print “odd number”
Step4 : stop.
19
FLOW CHART FOR CHECKING A NUMBER IS
ODD OR EVEN
20
Iteration : (loop)
write an algorithm for sum of first ‘n’ natural numbers
Ie sum of 1+2+3+4+…………………+n
Step1 : start
Step2: Read the numbers as ‘n’
Step3: Initialize i=1, sum=0
Step4: while i<=n repeat the following steps
otherwise goto step5
4.1 sum=sum+i
4.2 i=i+1
Step5: print sum
Step6: stop 21
22
PROBLEM SOLVING AND DECOMPOSITION
Typos include:
•
• spelling errors;
• incorrect capitalizations;
• missing words;
• wrong words (for example, ‘phase’ instead of ‘phrase’);
• numbered lists with incorrect numbering, and so on.
LOGICAL ERROR
Simple bugs might be found in logical expressions. For example, the following
algorithm is supposed to print the maximum value of two numbers, x and y
• If x > y, then print y
• Otherwise if y > x, then print x
• This actually contains two bugs. One should be obvious: the algorithm
prints the minimum instead of the maximum.
• The second is slightly more subtle: what happens if both numbers are
equal? You would expect a value to be printed, but in fact, nothing is
printed at all.
• This example demonstrates that errors can result from expressions that
are valid (that is, they can be evaluated without a problem) but whose
semantic mean- ng is incorrect
Checking user input
• Humans may make mistakes, so it’s best to assume that user will enter
faulty input at some point. Think of all the ways an input could be
wrong.
• Entering ‘9O’ instead of ‘90’ is an easy enough mistake to make.
is quick and easy. All you have to do is count the number of digits in
the input and ensure it’s the right amount.
• Incorrect formats: phone numbers, website URLs, email addresses,
dates and timesare all examples of data that must match a specific
format.
Testing, a method for locating hidden bugs in a fully or partially working system.
• We can choose between two approaches to testing, depending on what you want to
achieve.
• The first is top-down testing, where you test the solution as a whole to
ensure that it works. This is most effective at finding design flaws and
verifying that the system hangs together well.
• The second approach is bottom-up, which requires you to begin by
testing the smallest parts of the solution individually. This allows you to
verify that they correctly fulfil their own obligations within the whole
system and to show that your solution is built on solid foundations.
EVALUATION
You did it. You analyzed the problem, broke it down into pieces, came up
• with a solution design, which you then implemented, tested and debugged. You have something that
functions. So, you’re finished, right? Well, not quite.
• The work is not over just because you have a solution. Before you can really finish, you have to
make sure you’ve produced a good solution. You must evaluate its quality.
•There are many aspects to quality.
• Evaluating a solution involves asking several basic questions, each addressing a specific aspect.
Important questions about the solution include:
Is it correct? Does it actually solve the problem you set out to solve?
Is it efficient? Does it use resources reasonably?
Is it elegant? Is it simple yet effective?
Is it usable? Does it provide a satisfactory way for the target audience to use it
OVERVIEW OF C
• C is a general-purpose, procedural, high-level programming language
used in the development of computer software and applications,
system programming, games, web development, and more.
• C language was developed by Dennis M. Ritchie at the Bell
Telephone Laboratories in 1972.
• It is a powerful and flexible language which was first developed for
the programming of the UNIX operating System.
• C is one of the most widely used programming language.
STRUCTURE OF C
DOCUMENTATION SECTION
Example c= a+b;
Comment
/* we
/* we canadd
adda,ba,b
and store
and results
store in c */ in c*/
the result section
DEFINITION SECTION
#define macro constant
#define PI 3.14
(we can write macro in lowercase also. But it is preferable to write
in upper case)
DATA TYPES
INTEGER DATA TYPE
Integers are used to store whole numbers
Syntax
int variable name
Eg: int a;
FLOATING POINT DATA TYPE
Floating points are used to store real numbers
Syntax
float variable name;
float num1;
Example
10.23
CHARACTER DATA TYPE
Character data types are used to store the character values
Syntax
char variable name;
char ch= ‘s’;
VOID TYPE
• Void type means no value.
• This is usually used to specify the types of functions which returns
nothing.
ENUM DATA TYPE
• The enum in C is also known as the enumerated type.
• It is a user-defined data type that consists of integer values, and it
provides meaningful names to these values.
• The enum is defined by using the enum keyword
ENUMERATED DATA TYPE(Cont…)
enum fruits{mango, apple, strawberry, papaya};
The default value of mango is 0, apple is 1, strawberry is 2, and papaya is 3. If
we want to change these default values, then we can do as given below:
enum fruits{
mango=2,
apple=1,
strawberry=5,
papaya=7,
};
VARIABLES
Variable is the name of the memory location is used to store the data
Its value can be changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can
be easily identified.
Syntax to declare a variable
datatype variable_list;
Examples
int a;
float b;
char c;
RULES FOR DEFINING A VARIABLES
A variable can have alphabets, digits and underscore
A variable name can starts with the alphabet and underscore only. It
cant start with a digit.
No whitespace is allowed within the variable name
A variable name must not be any reserved word or keyword eg: int,
float, etc,
Valid variables names
int a;
int_ab;
int a30;
DECLARING A VARIABLE
For example
int a;
float a,b,c;
char a;
INITIALIZATION OF VARIABLE
Example
int a=10;
C TOKENS
• Octal integer constants are written using the digits 0 through 7 and start with
a leading 0.
Example: 0122, 344, etc.
• Hexadecimal integer constants are written using the digits 0 through 9 and
the letters A through F and start with a leading 0x or 0X.
Example: 1A3, 235, 9F, etc.
Floating-Point Constants in C
1) Floating-point constants are used to represent real numbers.
2) A floating-point constant can be written in decimal or exponential
notation.
String Constants in C
String constants are used to represent a sequence of characters. A string
constant is written using double quotes (" ") and consists of one or more
characters enclosed in the quotes.
For example, the following are valid string constants in C: "Hello World",
"123", etc.
▪ Enumeration Constants in C
▪ Enumeration constants are used to represent a set of named values.
▪ Enumeration constants are defined using the enum keyword and
consists of a list of names enclosed in braces.
For example, the following is a valid enumeration constant in C:
enum days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday };
In the above example, Sunday is assigned the value 0, Monday is
assigned the value 1, and so on.
2 ways to define constant in C
There are two ways to define constant
1. const keyword
2. #define preprocessor
1) C const keyword
OPERATORS IN C
▪ An operator is a special symbol that tells the compiler to perform
mathematical or logical operations.
▪ C language is rich in built in operators and provides the following
types of operators
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Miscellaneous operators
ARITHMETIC OPERATORS
▪ An arithmetic operator is a mathematical function that takes two
operands and performs a calculation on them.
▪ The arithmetic operators perform addition, subtraction, multiplication,
division, exponentiation and modulus operations.
▪ A following table shows all the arithmetic operators supported by the
C language
▪ Let us assume A holds 10 and variable B holds 20
ARITHMETIC OPERATORS
OPERATOR DESCRIPTION EXAMPLE
&& (x>5)&&(y<5)
(Logical AND) It returns true when both conditions are true
|| (x>=10)||(y>=10)
(logical OR) It returns true when atleast one of the condition is
true
! !(x>5)&&(y<5)
(Logical NOT) It reverse the state of the operand. If a condition is
true, the logical NOT operator will make it false
BITWISE OPERATORS
• A bitwise operator is an operator used to perform bitwise operations
on bit patterns or binary numerals that involve the manipulation of
individual bits
• It consists of two digits either 0 or 1.
• It is mainly used in numerical computations to make the calculations
faster.
OPERATOR MEANING OF OPERATOR
| Bitwise OR operator
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
BITWISE LOGICAL SHIFT
(Left shift operator<< , Right shift operator >>)
Input a=179
ASSIGNMENT OPERATOR
• Assignment operators are used to assigning value to a variable.
• The left side operand of the assignment operator is a variable and right
side operand of the assignment operator is a value…”=“
• The following table shows the assignment operator supported by the C
language
OPERATOR DESCRIPTION EXAMPLE
* Pointer to a variable *a
?: Conditional Expression If condition is true? Then
value X: otherwise value y
OPERATOR PRECEDENCE IN C
▪ Operators precedence determines the grouping of terms in an
expression and decides how an expression is evaluated.
▪ Certain operators have higher precedence than others, for example, the
multiplication operator has a higher precedence than the addition
operator
▪ For eg, 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.
▪ Within an expression, higher precedence operators will be evaluated
first.
CATEGORY OPERATOR ASSOCIATIVITY
Postfix ()[] -> ++-- Left to Right
Unary !~ ++-- (type)* &sizeof Right to left
Multiplicative */ % Left to Right
Additive +- Left to Right
Shift << >> Left to Right
Relational <<= >>= Left to Right
Equality == != Left to Right
Bitwise AND & Left to Right
Bitwise XOR ^ Left to Right
Expression Example:
c=a+b
a-b+c
a+b-(a*c)
Types of Expressions in C
•Arithmetic expressions
•Relational expressions
•Logical expressions
•Conditional expressions
Arithmetic Expressions:
The arithmetic expression is evaluated in specific order considering the
operator's precedence, and the result of an expression will be based on
the type of variable.
Relational Expressions:
Relational operators >, <, ==,!= etc are used to compare 2 operands.
Relational expressions consisting of operands, variables, operators, and
the result after evaluation would be either true or false.
Example:
C=a>b
a*b == a+b
a+b*c > a*b+c
Logical Expressions:
Relational expressions and arithmetic expressions are connected with
the logical operators, and the result after an evaluation is stored in the
variable, which is either true or false.
Example:
C= (a+b)>c && a<b
a>b || b>a
Conditional Expressions:
The general syntax of conditional expression is:
Exp1? Exp2: Exp3
From the given above expressions, the first expression (exp1) is conditional,
and if the condition is satisfied, then expression2 will be executed;
otherwise, expression3 will be performed.
Example:
2<3? 2 : 3
a*b>c ? true : false
Evaluate the following expression:
5+8/(3+1)+12*4
(expression inside the parenthesis is done)
Step1 :5+8/4+12*4
(division is done)
Step2 :5+2+12*4
(multiplication is done)
Step3 :5+2+48
(addition is done)
Step4 :55
CONTROL STATEMENTS
▪ Control statements in C are used to execute and transfer the control
from one part of the program to another depending on a condition.
▪ These statements are also called as conditional statements.
DECISION MAKING AND BRANCHING
STATEMENTS
▪ The decision control statements are the decision making statements that
decides the order of execution of statements based on the conditions.
▪ In the decision making statements the programmer specify which conditions
are to be executed or tested with the statements to be executed if the condition
is true or false.
Simple if statement:
▪ if the test expression evaluates to true, the corresponding statement is
executed , if the test expression evaluates to false, control goes to the
next executable.
▪ Statement must be enclosed in {} braces.
Syntax
if(testExpr)
{
statement;
}
Simple if statement:
if else statement:
▪ An if else statement allows to evaluate the expression first then depending on
whether the value of the expression is “true” or “false”, it transfer the control to a
particular statement.
▪ If the test expression is true, statement will be executed, if the expression is false,
statement will be executed.
Syntax
if(testExpr)
{
statement true;
}
else
{
statement false;
}
if else statement:
Program to check whether a person is eligible to vote or not.
if else if ladder:
▪ The else if ladder statement is an extension to the if-else statement .
▪ It is used in the scenario where there are multiple cases to be performed for different conditions
Syntax
if(condition1)
{
Statement;
}else if(condition2)
{
Statement;
}
else if (condition 3)
{
Statements;
}
else
{
//code to be executed if all the conditions are false
}
if else if ladder:
Nested if statement:
When any if statements is written under another if statement, this is called nested if
Syntax
if(condition)
{
if(condition)
{
statement1;
}
else
{
statement2;
}
else
statement3;
Nested if statement:
switch statement
A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable along switched on is checked for each
switch case.
Syntax
switch(expression)
{
case label:
statement1;
break;
case label:
statement2;
break;
default:
statements;
}
RULES FOR SWITCH STATEMENT
• The switch expression must be of an integer or character type
• The case value must have an integer or character constant
• The case value can be used only inside the switch statement.
• Case labels always end with a colon(:) Each of this cases is associated
within a block
• The break statement is optional. If there is no break statement found in
the case, all the cases will be executed present after the matched case.
switch statement
LOOP STATEMENTS
• A loop is used for executing a block of statements repeatedly until a
particular condition is satisfied.
• Loops are control structures used to repeat a given section of code a
certain number of times or until a particular condition is met.
• It has three main types of loops
1. while loop
2. do loop
3. for loop
while loop
A while loop repeatedly executes a target statement as long as a given
condition is true.
Syntax
while(condition)
{
statement(s);
}
▪ Here statements may be a single statement or a block of statements.
▪ The condition may be any expression
▪ The loop iterates while the condition is true
while loop
do while loop
• Using the do -while loop, we can repeat the execution of several parts
of the statements.
• The do-while loop is mainly used in the case where we need to
execute the loop at-least once.
Syntax
do
{
// the body of the loop
}
while(testExpression);
do while loop
for loop
The for loop is used to iterate the statements or a part of a program
several times.
Syntax
for(initialization; condition; increment)
{
//code to be executed
}
for loop
NESTED LOOPS
• A loop inside another loop is called a nested loop
• The depth of nested loop depends on the complexity of a problem
• We can have any number of nested loops as required
• There can be mixed type of nested loop ie a for loop inside a while
loop or while loop inside a do while loop
Types of Nested loop
1. Nested while loop
2. Nested do while loop
3. Nested for loop
Nested while loop
A while loop inside another while loop is called as nested while loop
Syntax of Nested while loop
while(condition1)
{
statements;
while(condition2)
{
statements;
……..
}
}
Nested do while loop
A do while loop inside another do while loop is called nested do while loop
Syntax
do
{
do
{
statements(s);
}
while(condition2);
……
}
while(condition1);
Nested for loop
A for loop inside another for loop is called nested for loop
Syntax
for(initialization;condition;increment)
{
statements;
for(initialization;condition;increment)
{
statements(s);
…..
}
…..
}
UNCONDITIONAL BRANCHING
STATEMENTS
• break statement
• continue statement
• goto statement
break statement
▪ The break is a keyword in C which is used to bring the program control out
of the loop
▪ The break statements is used inside loops or switch statements
▪ The break statements breaks the loop one by one, ie it breaks the inner loops
first and then proceeds to outer loops.
▪ The break statements are used in the following two scenarios
1. With switch case
2. With loop
The form of a break statement is
break;
break statement
continue statements
• The continue statement is used to bring the program control to the
beginning of the loop.
• The continue statement skips some lines of code inside the loop and
continues with the next iteration.
• It is mainly used for a condition so that we can skip some code for a
particular condition.
Syntax
continue;
CONTINUE STATEMENT
Break statement Continue statement
The break statements is used to exit from The continue statements is not used to exit
the loop constructs from the loop constructs
The break statements is usually used with The continue statement is not used with the
the switch statement, and it can also use it switch statement but it can be used within
within the while loop, do-while loop or the the while loop, do while loop, or for loop
for-loop
When a break statements is encountered When a control statement is encountered
then the control is exited from the loop then the control automatically passed from
construct immediately the beginning of the loop statement
Syntax Syntax
break; continue;
goto statement
▪ The goto statement is a jump statement which is sometimes also
referred to as unconditional jump statements.
▪ The goto statements can be used to jump from anywhere to anywhere
within a function
Syntax
goto label;
…
…
label: statement
IMPLENTATION OF GOTO
Forward jump Backward jump
goto label; lable: statements
….. ………
….. Skipped
………
….. ……..
label: statements goto label;
…… …..
Skipped
….. …….
Forward jump
if(age>=18)
goto vote;
else
printf(“Not eligible”);
vote: printf(“eligible for vote”);
Output
eligible for vote
STORAGE CLASSES IN C
A storage class defines the scope and life-time of variables and/or
functions within a c program
A storage class in C is used to describe the following things
o The variable scope
o The location where the variable will be stored
o The initialized value of a variable
o A lifetime of a variable
TYPES OF STORAGE CLASSES
auto
static
extern
register
Syntax
storage_class data_type variable_name;
Examples
auto int i;
static int i;
extern int i;
register int i;
auto storage class
o auto stands for automatic storage class
o The variables defined using auto storage class are called as local
variables.
o The scope of the auto variable is limited with the particular block only
o A keyword auto is used to define an auto storage class
o By default, an auto variable contains a garbage value
Syntax
auto int i ;
Auto storage class
Scope : in the same block in which the variable has been declared
Lifetime: only in the block in which the variable has been declared
Alive
Default value: garbage value
Auto storage class
Static storage class
• The keyword used to define static variable is static.
• Static local variables are visible only to the function or the block in
which they are defined.
• Default initial value of the static integral variable is 0.
• The variables defined as static specifier can hold their value between
the multiple function calls.
Example
static int count=10;
Static storage class
Scope: In the same block in which the variable has been declared
Lifetime: Until the completion of the program the variable will be alive
Default value : zero
Example
static int i;
extern storage class
• extern stands for external storage class
• The external storage class is used to tell the compiler that the variable defined as
extern is declared with an external linkage elsewhere in the program.
• The variables declared as extern are not allocated any memory. It is only
declaration and intended to specify that the variable is declared elsewhere in the
program.
• The default initial value of external integral type is 0.
• We can only initialize the extern variable globally, i.e., we can not initialize the
external variable within any block or method.
• If a variable is declared as external then the compiler searches for that variable to
be initialized somewhere in the program which may be extern or static.
Example
extern int i;
Extern storage class
Scope : throughout the program
Lifetime : Alive until the program completes its execution
Default value : Zero
Register storage class
o The keyword register is used to declare a register storage class
o The variables declared using register storage class has lifespan
throughout the program
o The variables is limited to the particular block
o The only difference is that the variables declared using register storage
class are stored inside CPU registers instead of a memory
o Register has faster access that of the main memory
Example
register int a;
Registers storage class
Scope : within the block in which variable has been declared
Lifetime : Alive only within the block
Default value :Garbage value
ARRAYS
▪ An array is defined as the collection of similar type of data items
stored at contiguous memory locations
▪ Arrays are the derived data type in C programming language which
can store the primitive data type such as int, char, double, float, etc.
▪ It also has the capability to store the collection of derived data types,
such as pointers, structures, etc.
▪ The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
PROPERTIES OF ARRAY
• Each element of an array is of same data type and carries the same
size, i.e., int = 4 bytes.
• Elements of the array are stored at contiguous memory locations
where the first element is stored at the smallest memory location.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of
an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of
code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array,
we can't exceed the limit.
APPLICATIONS OF ARRAY
▪ Used to store list of values
▪ Used to perform matrix operations (2D arrays are used to create matrix
&perform operations like add, sub, multp.
▪ Used to implement search algorithm : Linear, binary search
▪ Used to implement sorting algorithms
▪ Used to implement data structures: stack, queue, etc
Declaration of C Array
We can declare an array in the c language in the following way.
data_type array_name[array_size];
Now, let us see the example to declare the array.
int marks[5];
Initialization of C Array
• The simplest way to initialize an array is by using the index of each element.
• We can initialize each element of the array by using the index. Consider the
following example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
C Array: Declaration with Initialization
We can initialize the c array at the time of declaration.
int marks[5]={20,30,40,50,60};
Here, type is the data type of the elements that will be stored in the
array, array_name is the name of the array, and size1, size2, ..., sizeN are the
sizes of each dimension of the array.
Initialization of a 3d array
data_type arr_name[size1][size2][size[3]…….size[n];
Example : int test[2][3][4];
Total number of elements= 2*3*4=24
Initializing three-dimensional array
Method 1
int x[2][3][4]
={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
Better method
int x[2][3][4] =
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23}};
};
MATRIX ADDITION
MATRIX MULTIPLICATION
STRINGS
• The string can be defined as the one-dimensional array of characters
terminated by a null ('\0’).
• Each character in the array occupies one byte of memory, and the last
character must always be 0.
• The termination character ('\0') is important in a string since it is the
only way to identify where the string ends.
Example : ‘h’ ‘i’ character
“hi” string
Declaration of strings
Declaring a string is as simple as declaring a one-dimensional array
Syntax
char str_name[size];
There are two ways to declare a string in c language.
1. By char array
2. By string literal
Let's see the example of declaring string by char array in C language.
char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
While declaring string, size is not mandatory. So we can write the above
code as given below:
char ch[]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};