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

C++ notes

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

C++ notes

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

C++ for Beginners

▪ C++ is the most used and most popular programming


language developed in 1979 by Bjarne Stroustrup at AT
& T Bell Laboratory . C++ is a high-level and object-
oriented programming language. This language allows
developers to write clean and efficient code for large
INTRODUCTION applications and software developments, game
developments, and operating system programming.
▪ It is an expansion of the C programming language to
include Object Oriented Programming(OOPs) and is
used to develop programs for computers. As Compared
to other programming languages like Java, and Python,
it is the fastest programming language.
Structure of C++
Program
▪ C++ program to display "Hello World"

#include <iostream>
using namespace std;
int main()
{
Writing First C++ // Prints hello world
Program cout << "Hello World";

return 0;
}
▪ We have used the cout method which is the basic
output method in C++ to output anything in the
standard output stream.
Basic Output- ▪ Syntax:
cout cout <<“ My name is Atul” ;
▪ Note: Every C++ statement except the function
declarations will end with semicolon.
▪ Comments in C++ are meant to explain the code as
well as to make it more readable. The purpose of the
comments is to provide information about code lines.
Programmers commonly use comments to document
their work.

C++ Comments ▪ Types of Comments in C++


In C++ there are two types of comments:
▪ Single-line comment
▪ Multi-line comment
▪ // C++ Program to demonstrate single line comment
#include <iostream>
using namespace std;
int main()

C++ Single-line {

// Single line comment which will be ignored by the


comment
// compiler
example cout << "Hello!";

return 0;
}
▪ // C++ Program to demonstrate Multi line comment
#include <iostream>

using namespace std;


int main()
{
C++ Multi-line
/* Multi line comment which
comment
will be ignored by the compiler
example
*/

cout << "Hello!";


return 0;
}
▪ A token in C can be defined as the smallest individual
element of the C programming language that is meaningful
to the compiler. It is the basic component of a C program.

▪ Types of Tokens in C
▪ The tokens of C language can be classified into six types
based on the functions they are used to perform. The types
of C tokens are as follows:
Tokens in C ▪ Keywords
▪ Identifiers
▪ Constants
▪ Strings
▪ Special Symbols
▪ Operators
▪ The keywords are pre-defined or reserved words in a
programming language. Each keyword is meant to
perform a specific function in a program. Since
1. C Token - keywords are referred names for a compiler, they can’t
be used as variable names because by doing so, we are
Keywords trying to assign a new meaning to the keyword which is
not allowed. You cannot redefine keywords. C language
supports 32 keywords which are given below:

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
▪ Identifiers are used as the general terminology for the naming of
variables, functions, and arrays. These are user-defined names
consisting of an arbitrarily long sequence of letters and digits with
either a letter or the underscore(_) as a first character. Identifier
names must differ in spelling and case from any keywords. You cannot
use keywords as identifiers; they are reserved for special use. Once
declared, you can use the identifier in later program statements to
refer to the associated value.

▪ Rules for Naming Identifiers


C Token – ▪ They must begin with a letter or underscore(_).

Identifiers ▪ They must consist of only letters, digits, or underscore. No other


special character is allowed.
▪ It should not be a keyword.
▪ It must not contain white space.
▪ It should be up to 31 characters long as only the first 31 characters
are significant.

▪ Note: Identifiers are case-sensitive so names like variable and


Variable will be treated as different.
▪ Strings are nothing but an array of characters ended
with a null character (‘\0’). This null character indicates
C Token – the end of the string. Strings are always enclosed in
double quotes. Whereas, a character is enclosed in
Strings single quotes in C++.
▪ Examples of String

char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘\0’};
char string[20] = “geeksforgeeks”;
char string [] = “geeksforgeeks”;
▪ Operators are symbols that trigger an action when applied to C
variables and other objects. The data items on which operators
act are called operands.
Depending on the number of operands that an operator can act
upon, operators can be classified as follows:

▪ Unary Operators: Those operators that require only a single


operand to act upon are known as unary operators. For
Example increment and decrement operators
C Token – ▪ Binary Operators: Those operators that require two operands
Operators to act upon are called binary operators. Binary operators can
further are classified into:
▪ Arithmetic operators
▪ Relational Operators
▪ Logical Operators
▪ Assignment Operators
▪ Bitwise Operator
▪ Ternary Operator: The operator that requires three operands
to act upon is called the ternary operator. Conditional
Operator(?) is also called the ternary operator.
▪ The constants refer to the variables with fixed values.
They are like normal variables but with the difference
that their values can not be modified in the program
C Token – once they are defined.
Constants ▪ Constants may belong to any of the data types.
▪ Examples of Constants in C
▪ const int a = 20;
▪ The following special symbols are used in C having some special meaning and
thus, cannot be used for some other purpose. Some of these are listed below:
▪ Brackets[]: Opening and closing brackets are used as array element references.
▪ Parentheses(): These special symbols are used to indicate function calls and
function parameters.
▪ Braces{}: These opening and ending curly braces mark the start and end of a
block of code containing more than one executable statement.
▪ Comma (, ): It is used to separate more than one statement like for separating
parameters in function calls.
▪ Colon(:): It is an operator that essentially invokes something called an
initialization list.
C Token – ▪ Semicolon(;): It is known as a statement terminator. It indicates the end of one
Special Symbols logical entity. That’s why each individual statement must be ended with a
semicolon.
▪ Asterisk (*): It is used to create a pointer variable and for the multiplication of
variables.
▪ Assignment operator(=): It is used to assign values and for logical operation
validation.
▪ Pre-processor (#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
▪ Period (.): Used to access members of a structure or union.
▪ Tilde(~): Bitwise One’s Complement Operator.
▪ A variable in C is a memory location with some name
that helps store some form of data and retrieve it when
required. We can store different types of data in the
variable and reuse the same variable for storing some
other data any number of times.
C Variables ▪ They can be viewed as the names given to the memory
location so that we can refer to it without having to
memorize the memory address. The size of the variable
depends upon the data type it stores.
▪ You can assign any name to the variable as long as it
follows the following rules:
▪ A variable name must only contain alphabets, digits,
and underscore.

▪ A variable name must start with an alphabet or an


underscore only. It cannot start with a digit.
Rules for Naming ▪ No whitespace is allowed within the variable name.
Variables in C ▪ A variable name must not be any reserved word or
keyword.
▪ The syntax to declare a variable in C specifies the name and the
type of the variable.
▪ data_type variable_name = value; // defining single variable
or
data_type variable_name1, variable_name2; // defining multiple
variable
▪ Here,

C Variable ▪ data_type: Type of data that a variable can store.


▪ variable_name: Name of the variable given by the user.
Syntax ▪ value: value assigned to the variable by the user.
▪ Example
▪ int var; // integer variable
char a; // character variable
float fff; // float variables
▪ Note: C is a strongly typed language so all the variables types must
be specified before using them.
▪ Variable Declaration
There are 3
▪ Variable Definition
aspects of defining
▪ Variable Initialization
a variable:
1. C Variable Declaration
▪ Variable declaration in C tells the compiler about the
existence of the variable with the given name and data
type. When the variable is declared compiler automatically
allocates the memory for it.
2. C Variable Definition
▪ In the definition of a C variable, the compiler allocates
some memory and some value to it. A defined variable will
There are 3 contain some random garbage value till it is not initialized.
aspects of defining int var;
char var2;
a variable: 3. C Variable Initialization
▪ Initialization of a variable is the process where the user
assigns some meaningful value to the variable.
Example
int var; // variable definition
var = 10; // initialization
or
int var = 10; // variable declaration and definition

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy