Browse by Domains

C++ Tutorial for Beginners

History of C++

C++ evolved from C, which earlier from two previous programming languages, BCPL and B language.

  1. BCPL:

BCPL was developed in 1967 by Martin Richard as a language for writing operating system software and compilers.

  1. B language:

B language was developed by Ken Thompson.

  1. C language:

C language evolved from B language, else a programming language designed by Dennis Ritchie in AT and T laboratories in 1972.

  1. C++:

An extension of C, was developed by Bjarne Stroustrup in the early 1980s at Bell laboratories.

What is meant by a programming language?

A programming language is a set of rules that provides a way of talking with the computer about what operation to be performed.

A programming language is a set of rules for communicating algorithms.

A programming language is a notational system for describing competition in machine in readable and human readable form. 

What is a Compiler?

Compiler scans the whole source code and translates it into machine readable form.

Execution time required is less.

We can save the file after the competition.

As the whole code is compiled at once so no need for further compiling.

Compiler generates the error code after scanning the whole code.

Compiler is not so good for debugging.

CPU utilization is more in the compiler.

It is used in C, C++ languages.

What is an interpreter?

Interpreter translates the whole code line by line.

Interpreter required execution time is more.

Interpreter does not save the file after interpretation.

Each line of the code should be compiled. It means the interpreter compiles the whole code line by line.

It continues to translate until the error is found.

It is good for debugging.

CPU utilization is less. 

It is used in python, java.

Also read C interview articles.

What is an Object oriented programming (OOP):-

              C++ is Object Oriented programming (OOP) language, it allows us to divide a complex code into smaller sets by using objects. It gives a clear structure to the program and allows code to be reused. There are six features of OOPs: Classes and objects, Message and Methods, Encapsulation, Inheritance, Polymorphism, Abstraction. The four major pillar of OOP are: 

  1. Encapsulation
  2.  Inheritance
  3. Polymorphism
  4. Abstraction.
  • Four pillars of OOPs: –
  1. Encapsulation: –

Data is very important so we need to protect that data. Such type of protection to data is known as encapsulation so we can say that encapsulation is the packing of data and functions.

  1. Inheritance: –

One object acquires the property of another object through relationships. The relationship that is simulated is known as inheritance.

For example-

Children inherit some features of their parents.

Teacher teaches some good things to students.

  1. Polymorphism: –

Polymorphism means “many forms” but in the terms of programming language polymorphism  is the ability to use an operator or method in different ways to make end users use the software on different platforms.

  1. Abstraction: –

Selecting a few attributes from the rest is called abstraction so we can say that the abstraction hides the unnecessary data and pops up the important data.

  1. Advantages of OOP: –
  1. To modularize software development, just like other engineering disciplines.
  2. To make software projects more manageable and predictable.
  3. For letter maintainability, since software maintenance costs were then the development costs.
  4. For reusing code and preventing reinvention of the wheel every time.

Object-Oriented Programming finds a lot of application in the real business system. The various place where OOP is required is: –

  1. Database management system.
  2. Intelligence system.
  3. Pattern recognition system.
  4. Image processing system.
  5. Parallel computing.
  6. Mobile computing.
  7. Data security system.
  8. Features of OOP: –
  1. Emphasis on data rather on procedure.
  2. Programs are divided into what are known as objects.
  3. Functions that operate on data of an object are laid together in a data structure.
  4. Objects may communicate with each other through functions.
  5. New data and functions can be added easily whenever necessary.
  • How to write a basic C++ code: –
  1. The # include is a preprocessor directive.
  2. Pre-processor directive begin with a # sign
  3. They are processed before compilation.
  4. The directive “# include<iostream>” tells the pre-processor to include the “iostream” header file to support I/O operations.
  5. By using namespace std (“) declares std as the default namespace used in this program. The names count and end which is used in the program belong to std namespace.
  6. These two lines shall be present in all our programs.
  7. “count” refers to the standard output (or the console output).
  8. The symbol << is called the Stream insertion operator (or put to operator), which is used to put the console.
  9. “end l” denotes the end of line or new line which is put to the console to string the error to the beginning of the new line.
  10.  “return 0;” Terminates the main( ) function and returns a value 0 to the operating system.

Example : –

#include<iostream>
using namespace std;
int main()
{
     cout<<"Hello world";
     return 0;
}

Basic code of C++ 

    #include<iostream>

using namespace std;

int main()

{

     cout<<“Hello world”;

     return 0;

}

This is the basic example of C++

  • The C++ environment: –
  • Editor: –

The first phase of C++ environment, used to create program source code. It is where you type the program.

  • Pre-processor: –

 The second phase of C++ environment use to the programs executes automatically before the compiler’s translation phase begins.

  • Compiler: –

The third phase of C++ environment, used to compile source codes into machine language (object code file/ binary format).

  • Linker: – 

The fourth phase of C++ environment is to convert (.cc) file to an executable file(.c).

  • CPU: –

T

The Integrated Development Environment (IDE) : –

An IDE is a program that has the compiler, debugger and application building tools. You can create programs debugging without leaving the IDE.

  • Debugging: –

BUG: – 

An error in a program can refer to anything that produces difficulty in program execution or compilation.

Debugging: –

Looking for correcting errors or mistakes that cause your programs to behave unexpectable. It means debugging corrects the programming errors.

Types of Errors: 

  1. Syntax errors: –

Syntax errors are mistakes caused by violating the grammar rules of C++.

A program with no syntax errors may still not produce correct results.

For example: – 2+3*5 and (2+3)*5 Both are syntactically correct expressions, but have different meanings.so if you use one of these expressions where you should use one of these expressions where you should use the other one, your program will produce incorrect results.

  1. Run-time error: –

Run-time errors also called as Semantic or Smart errors are caused by input output rules during the execution of our programs.

  1. Logical error: –

Logical errors are the most difficult errors to recognize and correct because the computer does not indicate those errors in your programs as it does with syntax and runtime errors.

Your program may have appeared to have executed successfully, however; the answers may be completely wrong.

Difference between Procedure Oriented Programming (POP) and Object-Oriented Programming (OOPs): –

Procedure Oriented Programming (POP): –

1) In Procedure Oriented Programming, the program is divided into small parts known as function.

2)Importance- In Procedure Oriented Programming, importance is not given to data but to functions.

3)In Procedure Oriented Programming, the top to Down approach is taken.

4) To add new data and function is not easy.

5) In Procedure Oriented Programming, most functions use global data for sharing that can be accessed freely from function to function.

6) Procedure Oriented Programming does not have any proper way for hiding data so it is less secure.

  • Object-Oriented Programming: –

1) IN OOP, the program is divided into parts called objects.

2)In OOP, importance is given to data rather than functions.

3)In OOP bottom to up approach.

4) OOP provides more functionality to add data.

5)In OOP, data cannot move easily from function to function, it is kept public or private.

                               6)OOP provides data hiding so it is more secure.

Advantages and disadvantages of POP: –

  1. A procedure -oriented programming language is one in which procedure or methods or functions are given more significations. Here, function calls another.
  2. There can be global variables and local variables in each function.
  3. In this language, the task is divided into smaller tasks or subtasks called as procedure or functions or methods.
  4. Any function can be called at any instant the global variable can be accessed by all functions. While the local variables are local to the corresponding function.
  5. A procedure-oriented programming language follows a top to bottom approach.
  6. Top-bottom approach refers to approaching a problem as a big task or as a whole. then dividing this task and then small tasks are written in defaults.
  7. The disadvantage of such type of programming is that it is difficult to trace which functions are using the data and also error correction is difficult.
  8. C programming language is an example of procedure-oriented language.
  9. Data hiding expansion of data is not possible in procedure -oriented programming.
  • Object-oriented programming and Its Development: –
  1. Object-oriented programming as the name says gives more significance to objects which have data of the object that can be accessed by the functions. The function of one object can access the data of another object through the function of that object.
  2. Object-oriented programming uses a bottom-up approach; smaller tasks are first dealt in detail and gradually creating the entire huge system.
  3. C++ and java are examples of Object-oriented programming.
  4. Object-oriented programming, on other hand, given more important to data than procedure.

C++ Terminology: –

  • Statement: – A programming statement performs a piece of programming action. It must be terminated by semicolon (;).
  • Preprocessor Directive: – the #include is a preprocessor directive and not a programming statement. It begins with # It is processed before compiling the program. A preprocessor directive is not terminated with a semicolon.
  • Block: – A block is a group of statements enclosed by braces. This group is treated as one single unit. This is one block in this program which contains the body of the main function. There is no need to put a semicolon after closing braces.
  • Whitespace: – Blank, top, new line are collectively called whitespaces. Extra white space is ignored, that is, one whitespace area is needed to separate the tokens.
  • Case sensitive: – C++ is case sensitive. 

For example: – HELLO is not Hello and not a hello. 

  • DATA TYPES:

The data types in C++ are classified as User defined, Derived, Primitive type.  

  • Primitive data type: –

Primitive data type have following data types:

  1. Integer-int
  2. Character-char
  3. Floating value
  4. Floating -float
  5. double

and 

             Void

  • Derived data type: –

Derived data type includes the following data type:

  1. Arrays
  2. Pointers
  3. References
  • User-defined: –
  1. Structure
  2. Union
  3. Classes
  4. Enumerations
  • POINTER

A pointer is a variable whose value is the address of another variable.

A general from of pointer variable declaration is type

    *var_name;

Here, type is a pointer base type; it must be a valid C++ data type and var_name is a name of a pointer variable.

For Example:

int*ip;

double*bp;

float*fp;

char*ch;

  Pointer can have a data type int, double, float, char etc.

Code for pointer: –

#include<iostream>

Using namespace std;

Int main ( )

{

Int var=20*ptr1;

Ptr1=&var;

cout << “Address of ptr”;

cout<<ptr1<<end1;

cout<< “Value of ptr” <<*ptr<<end1;

Code 2: –

#include<iostream>

Using namespace std;

int main()

{

int*ptr1, *ptr2, a, b, c;

cout<< “enter value of a and b”;

cin>>a>>b;

ptr1=&a;

ptr2=&b;

c=*ptr1+*ptr2;

cout<< “Addition of pointer”;

cout<<c<<end1;

}

  • COUNTER: –
  1. Counter variable that is incremented or decremented each time a loop

repeats.

  1. Can be used to control execution of loop (loop control/ variable).
  2. Must be initialized before entering lop.
  3. Must be incremented/decremented either inside the loop in the loop test.
  • Comments: –

The compiler simply ignores comments when it translates the program into executable code.

To identify a comment, C use opening /*and closing */ comment taken.

Comments can appear anywhere in a program.

Comments are also found whenever it is necessary to explain a point about a code.

Comments cannot be nested, that means comments can not have comments inside comments.

  • Single line comment

Example=

// main function starts the character//

This is an example of a single line comment.

  • Multiline Comment

Example=

/* this program takes two integers and provides the sum to the user */

               This is an example of multiline comment

Comments are required to make the program more readable and understandable if anyone analyzes the code.

#include<iostream>

          using namespace std;

          int main()

          {

          cout<<“Hello world”;

          return 0;

           }

  • Desirable program characteristic: =
  1. Clarity= the program should be written clearly
  2. Accuracy = The calculation should be accurate.
  3. Simplicity = The program should be clear.
  4. Efficiency = Execution speed and efficient memory utilization are the important ingredients of efficiency.  
  5. Modularity = The program should be broken into small modules in order to clarity and accuracy of the program is known as modularity. 
  6. Generality = the program should generate a re enable limit.
  • Character set: –
  1. Character set is the set of character
  2. This character is used to form the word, numbers and expression.
  3. The characters are classified into following categories.
  4. Letter:  Uppercase- A, B, C

             Lower case- a, b, c

  1. Digital: decimal digits 0 to 9
  2. Special Character:  ,(comma),       ;(semicolon) ,   : (colon),  &(ampersand),  # (number line) etc.
  3. White space: Blank space.
  • TOKEN:
  • Smallest individual units are known as tokens.
  • There are six type of the token
  1. Keyword
  2. Identifiers
  3. Constants
  4. String
  5. Special symbols 
  6. Operators
  • Keywords
  1. Every word is classified as either keyword or identifier’
  2. All keyword have fixed meaning and can change its meanings
  3. Keywords serve as a basic building for a program statement, ANSIC supports 32 keywords.
  4. Ex. Int. float, double, state, auto, continued, goto, short, long etc. are the keyword.
AutoIntReturnLong 
BreakDoubleSignedStatic
CaseSwitchVoidWhile
CharResisterShortUnsigned
ConstGo toAnumStract
ContinueIfExternType of
DefaultFloatSize ofUnion
DoElseFor
  • Identifiers: –
  1. Identifiers refer to the name of variables, function, arrays.
  2. There are user defined names and consist of sequences of letters and digits.
  3. Both uppercase and lowercase letters are permitted to make the identifier but generally lowercase letters are used to make the variable.

Rule for identifiers.

  1. First character must be alphabet
  2. Must not contain white space.
  3. Only the first 31 characters are significant.
  4. Cannot use keyword as an identifier.

Mantissa (‘ ’) (“  ”)

And exponential  

Decimal Octal    Hexadecimal

o-9                0-7        0-9

+, –                   0          A-F

 

  • Integer constant:

An integer constant refers to a sequence of digits.

There are three types of integer constants.

  1. Decimal integer
  2. Octal integer
  3. Hexadecimal
  1. Decimal integer:

It consists of 0-9 digits, preceded by on optional -or+ sign.

Valid examples of decimal integers are: 123, -321, 0, 6554321, +78.

Embedded spaces, comma and nondigital character are not permitted between digits.

15  750,      20,000,    $100 are illegal.

  1. Octal integer 

An octal integer consists of digits 0-7 with a leading 0.

Example; 037,0,0435,0551.

  1. Hexadecimal

A sequence of digits preceded by ox or OX is considered as hexadecimal integer.

Hexadecimal integers include 0 to 9 digits and A and F letters.

Example – ox9, OX9F, oxbc etc. are valid.

  • Trigraphs 

A few characters have an alternative representation, called a trigraph sequence. A trigraph is a three-character sequence that represents a single character and the sequence always starts with two question marks. Trigraphs are expanded anywhere they appear, including within string literals and character literals, in comments, and in preprocessor directives. Following are most frequently used trigraph sequences:

Trigraph Replacement
??=#
??/\
??’^
??([
??)]
??!|
??<{
??>}
??-~
  • Variable Scope

A scope is a region of the program and broadly speaking there are three places, where variables can be declared:

∙ Inside a function or a block which is called local variables, 

∙ In the definition of function parameters which are called formal parameters.

∙ Outside of all functions which are called global variables. We will learn what a function is, and it’s parameters in subsequent chapters. Here let us explain what local and global variables are.

  • Local Variables 

Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables:

#include<iostream>

using namespace std;

 int main ()

 {

 // Local variable declaration: int a, b; int c; // 

actual initialization

 a = 10;

 b = 20;

 c = a + b;

 cout << c;

 return 0;

}

  • Global Variables

Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program. A global variable can be accessed by any function. That is, a global 

#include<iostream>

using namespace std; // Global variable declaration: int g;

 int main ()

  {

// Local variable declaration: int a, b; 

actual initialization //

a = 10;

  b = 20;

g = a + b;

 cout << g;

 return 0;

 }

variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:

A program can have the same name for local and global variables but the value of the local variable inside a function will take preference. For example:

Include<iostream>

Using namespace std;

// Global variable declaration: //

int g = 20;

 int main () 

{ // Local variable declaration:// 

int g = 10;

 cout << g; 

return 0;}

When the above code is compiled and executed, it produces the following result:

10

  • Initializing Local and Global Variables:

When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows:

Data TypeInitializer
int 0
char‘\0’ 
float o
double 0
pointerNull 

It is a good programming practice to initialize variables properly, otherwise sometimes programs would produce unexpected results.

  • CONSTANTS

    Constants refer to fixed values that the program may not alter and they are called literals. Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values. Again, constants are treated just like regular variables except that their values cannot be modified after their definition.

  • Integer Literals

 Here are some examples of integer literals. An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order:

212 // Legal 

215u // Legal

 0xFeeL // Legal

 078 // Illegal: 8 is not an octal digit 

032UU // Illegal: cannot repeat a suffix

Following are other examples of various types of Integer literals:

85 // decimal 

0213 // octal

 0x4b // hexadecimal 

30 // int

 30u // unsigned int 

30l // long

 30ul // unsigned long

  • Floating-point 

Literals A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form. While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E. Here are some examples of floating-point literals:

3.14159 // Legal 

314159E-5L // Legal

  510E // Illegal: incomplete exponent

  210f // Illegal: no decimal or exponent 

.e55 // Illegal: missing integer or fraction.

  • Boolean Literals 

There are two Boolean literals and they are part of standard C++ keywords: ∙ A value of true representing true. ∙ A value of false representing false. You should not consider the value of true equal to 1 and value of false equal to 0.

  •  Character Literals

 Character literals are enclosed in single quotes. If the literal begins with L (uppercase only), it is a wide character literal (e.g., L’x’) and should be stored in wchar_t type of variable. Otherwise, it is a narrow character literal (e.g., ‘x’) and can be stored in a simple variable of char type. A character literal can be a plain character (e.g., ‘x’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u02C0’). There are certain characters in C++ when they are preceded by a backslash they will have special meaning and they are used to represent newlines (\n) or tab (\t). Here, you have a list of some of such escape sequence codes:

Escape sequenceMeaning
\\\ character
\’ ‘ character
\?? character
\aAlert or bell
\bBackspace
\fForm feed
\nnewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\oooOctal number of one to three digits
\xhhHexadecimal number of one or more digits

Following is the example to show a few escape sequences characters:

 #include<iostream>

 using namespace std;

 int main ()

 { cout << “Hello\t World\n\n”; 

cout<< “this is my first project”; 

return 0; }

When the above code is compiled and executed, it produces the following result:

Hello World

this is my first project

  • String Literals 

String literals are enclosed in double quotes. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters. You can break a long line into multiple lines using string literals and separate them using whitespaces. Here are some examples of string literals. All the three forms are identical strings. 

hello, dear” 

“hello, \ dear” 

“hello, ” 

“d”

 “ear”

  • Defining Constants

 There are two simple ways in C++ to define constants: 

∙ Using #define a preprocessor. 

∙ Using const keyword.

The #define Preprocessor Following is the form to use #define preprocessor to define a constant:

#define identifier value

Following example explains it in detail:

#include<iostream> 

using namespace std;

#define LENGTH 10

 #define WIDTH 5

 #define NEWLINE ‘\n’

 int main()

 {

 int area;

 area = LENGTH * WIDTH;

 cout << area; 

cout << NEWLINE; 

return 0;

 }

When the above code is compiled and executed, it produces the following result:

50

The const Keyword You can use const prefix to declare constants with a specific type as follows:

const type variable = value;

Following example explains it in detail: 

#include<iostream>

 using namespace std;

 int main() 

{

 const int LENGTH = 10;

 const int WIDTH = 5;

 const char NEWLINE = ‘\n’; 

int area;

area = LENGTH * WIDTH;

 cout << area; 

cout << NEWLINE;

 return 0; 

}

 When the above code is compiled and executed, it produces the following result:

 50

 Note that it is a good programming practice to define constants in CAPITALS.

  • ARRAYS:

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

 That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier. For example, an array to contain 10 integer values of type int called billy could be represented like this:

1     2 3 4 5     6     7       8         9        10

  • MULTI-DIMENSIONAL ARRAYS:

Multi-dimensional arrays are derived from the basic or built in data types

Of C language.

Two dimensional arrays are understood as rows and columns with applications including two dimensional matrices.

Mostly two-dimensional arrays are used in multi-dimensional array.

This is the three-dimensional array.

  • TWO-DIMENSIONAL ARRAY:

                     int b[2][3]={(b1, b2, b3)(b4, b5, b6)};

in this example -“int b” is name of array 

      “[2]”is indicating the number of rows  

      “[3]”is indicating the number of columns

    (b1, b2, b3) are the elements in the first row

    (b4, b5, b6) are the elements in second row 

  • Declaration of array: –

How to declare a multidimensional array?

Int b[2][3]

The name of the array is b

The type of elements is int

The size of element is 2*3=6

  • Initialization: –

Use braces to separate rows in 2-d array: –

int c [4][3] = {(1,2,3), (4,5,6), (7,8,9), (10,11,12)};

  • Input for two dimensional arrays: –

Data may be input into two-dimensional arrays using nested for loops interactively or with data type.

A nested loop is used to input elements in a two-dimensional array.

In this way by increasing the index value of the array the elements can be entered in a two-dimensional array.

  • Output for two-dimensional array: –

The output of a 2-dimensional array should be informed of rows and columns for readability.

Nested for loops are used to print the rows and column order.

By increasing the index value of the array the elements stored at that index value are printed on the output screen.

Code for array: –

#include<iostream>

Using namespace std;

Void main ()

{

int a [3] [4];

int i, j;

clrscr ();

print f (“enter the element in the array:”);

for (i=0; i<3; i++)

{

for(j=0; j<3: j++)

{

scanf(“%d”, & a[i] [j]);

}

}

for(i=0;i<3:i++)

{

for(j=0;j<3;j++)

{

printf(“%d”,a[i][j]);

}

}

print(“\n”);

getch();

}

This code give you output:

1

2

3

4

5

6

7

8

9

1 2 3

4 5 6

  1. 8 9
  • Storage Allocation: –

In storage allocation of array contiguous memory is allocated in all the array elements.

The storage arrangement shows in the example uses the array subscript it is also called the array indices.

Array declaration: int a [3] [4];

Array elements:

a [0] [0] a [0] [1] a [0] [2] a [0] [3]

a [1] [0] a [1] [1] a [1] [2] a [1] [3]

a [2] [0] a [2] [1] a [2] [2] a [2] [3]

Storage Allocation:

a [0] [0]

a [0] [1]

row0: a [0] [2]

a [0] [3]

a [1] [0]

row1: a [1] [1]

a [1] [2]

a [1] [3]

a [2] [0]

row2: a [2] [1]

a [2] [2]

a [2] [3]

code for array: –

#include<iostream>

using namespace std;

void main ()

{

int a [3] [3], b [3] [3], c[3] [3];

int I, j;

clrscr ();

print f (“enter the element in both the array:”);

for (i=0; i<3; i++)

{

for (j=0; j<3; j++);

{

scan f (“%d”, & a [i] [j]);

}

}

for (i=0; i<3; i++)

{

for (j=0; j<3; j++)

{

c=a [i] [j] +b [i] [j];

print f(“%d”, c [i] [j]);

}

}

print(“\n”)

}

}

getch;

this code will give you output as-

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

         2  4 6 

          8 10 12

14 16 18 

  • String in array: –

The group of characters are known as strings when the array is implemented. It is known as string in array.

For example-

Char name [5] =

“sunny”

It is compulsory to include header file <string.h> The following functionality can be implemented they are as follows:

  1. strlen ()
  2. strcpy ()
  3. strcmp ()
  4. strmpi ()
  5. strcat ()
  • strlen ()

stands for string length This function is used when we need to find the length of string.

For example, we have a char string name [10] in brackets. Size 10 and we are trying to store “sunny”.

     String name[10]= “sunny”.

Size of the array is 10 means we can store maximum 10 bytes of data but output of the above statement would be above statement5.

  • Strcmp ()

The function is used to compare to string. Strcmp() uses ASCII standard to compare two strings.

Char n[10] = “Poonam”

Char n[10]= “sunny”

Strcmp (destination string, source string)

Strcmp (n, m)

  • Strcmp i( )

This function is also used for comparing two strings.

But the difference is for this function both “poonam” and poOnaM are the same.

This will return() when compared.

But in case of strcmpi() it will be the value

Strcmpi (destination string, source string)

Strcmpi (n, m);

  • Strcpy ()

This function is used to copy one string to another string.

For example, we have two character arrays.

m [10]= “sunny”, n [10];

Strcpy()also has two parameters.

Strcpy (destination string, source string)

We will write: strcpy (n, m);

We will get: m [10]: “sunny”, n [10]= “sunny”

  • Strcat()/appends

This function is use to concatenate two strings strcat has two parameters 

Syntax:

Strcat (destination string, source string)

Char n[5]= “hello”

Cher n [5]=”world”

Strcat (n, m)

  • Strpy()

The function used to copy are string to another string

For example, are have two character

m [10]= “sunny”, n [10];

Strcpy(destination string, source code)

We can also write as= strcpy(n/m)

We will get =m [10]= “sunny”, n [10]= “sunny”

Check Great Learning Academy. For a more detailed understanding of C++, avail our free course content on Data Science and Machine Learning.

Avatar photo
Great Learning Team
Great Learning's Blog covers the latest developments and innovations in technology that can be leveraged to build rewarding careers. You'll find career guides, tech tutorials and industry news to keep yourself updated with the fast-changing world of tech and business.

Leave a Comment

Your email address will not be published. Required fields are marked *

Great Learning Free Online Courses
Scroll to Top