0% found this document useful (0 votes)
24 views31 pages

Basics of C

Basics of c

Uploaded by

sudarshanpp22
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)
24 views31 pages

Basics of C

Basics of c

Uploaded by

sudarshanpp22
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/ 31

General

Aspect of ‘C++’

Prof. A.P.Khan
Department of Computer Engineering
D.N.Patel College of Engineering, Shahada
Content

Introduciton

Data Types

Basic Operations of Data

Algorithms

Program
2
General Aspect of ‘C++’

C++ is an object-oriented programming language. It is


an extension to C programming.
C++ is a general purpose, case-sensitive, free-form
programming language that supports object-oriented,
procedural and generic programming.
C++ is a middle-level language, as it encapsulates
both high and low level language features.
2
Why Use C++
C++ is one of the world's most popular programming languages.
C++ can be found in today's operating systems, Graphical User
Interfaces, and embedded systems.
C++ is an object-oriented programming language which gives a
clear structure to programs and allows code to be reused,
lowering development costs.
C++ is portable and can be used to develop applications that can
be adapted to multiple platforms.
C++ is fun and easy to learn!
As C++ is close to C# and Java, it makes it easy for 2
programmers to switch to C++ or vice versa.
Object-Oriented Programming (OOPs) C++
supports the object-oriented programming, the
four major pillar of object-oriented
programming (OOPs) used in C++ are:
1) Inheritance
1) Polymorphism
2) Encapsulation
3) Abstraction
5
Usage of C++ : By the help of C++
programming language, we can develop
different types of secured and robust
applications:
Window application
Client-Server application
Device drivers
Embedded firmware etc
6
Difference between C and C++
C++ was developed as an extension of C,
and both languages have almost the same
syntax.
The main difference between C and C++ is
that C++ support classes and objects, while
C does not.
7
C is a structural programming language, and it does not
support classes and objects, while C++ is an object-oriented
programming language that supports the concept of classes
and objects.
C supports the structural programming language where the
code is checked line by line, while C++ is an object-oriented
programming language that supports the concept of classes
and objects.
Dennis Ritchie developed C language at Bell Laboratories
while Bjarne Stroustrup developed the C++ language at Bell
Labs circa 1980. 8
C++ is a superset of C programming language. C++ can run
99% of C code but C language cannot run C++ code.
C follows the top-down approach, while C++ follows the
bottom-up approach. The top-down approach breaks the
main modules into tasks; these tasks are broken into sub-
tasks, and so on. The bottom-down approach develops the
lower level modules first and then the next level modules.
C contains 32 keywords, and C++ supports 52 keywords.
C program uses <stdio.h> header file while C++ program
uses <iostream.h> header file.
9
#include <iostream.h>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}

Line 1: #include <iostream> is a header file library that lets us work with input and output objects
Line 2: using namespace std means that we can use names for objects and variables from the
standard library.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function.
Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to
output/print text. In our example it will output "Hello World".
int main () { cout << "Hello World! "; return 0; }
Line 6: return 0 ends the main function.
Line 7: Do not forget to add the closing curly bracket } to actually end the main function.
10
The Character set of ‘C/C++’

C/C++ language consist of some characters set, numbers


and some special symbols. The character set of C consist
of all the alphabets of English language. C consist of
Alphabets a to z, A to Z
Numeric 0,1 to 9
Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more

2
The words formed from the character set are
building blocks of C/C++ and are sometimes
known as tokens. These tokens represent the
individual entity of language.
The following different types of token are used
in C/C++
1) Identifiers 2)Keywords 3)Constants
4) Operators 5)Punctuation Symbols

12
Identifiers

A ‘C/C++' program consist of two types of elements


, user defined and system defined. Identifiers is
nothing but a name given to these elements.
An identifier is a word used by a programmer to
name a variable , function, or label.
Identifiers consist of letters and digits, in any order,
except that the first character or label. Identifiers
consist of letters and digits if any order, except that
the first character must be letter.
Both Upper and lowercase letters can be used 2
Keywords
Keywords are nothing but system auto double int struct

defined identifiers. break else long switch

Keywords are reserved words of the case enum register typedef

language. char extern return union


const float short unsigned
They have specific meaning in the
language and cannot be used by the continue for signed void

programmer as variable or constant default goto sizeof volatile


do if static while
names.
C/C++ is case sensitive, it means these must be used
as it is 32 Keywords in C/C++ Programming 2
Variables

A variable is nothing but a name given to a storage


area that our programs can manipulate. Each
variable in C/C++ has a specific type, which
determines the size and layout of the variable's
memory; the range of values that can be stored
within that memory; and the set of operations that
can be applied to the variable.

2
The name of a variable can be composed of letters, digits, and
the underscore character. It must begin with either a letter or
an underscore. Upper and lowercase letters are distinct
because C is case-sensitive. There are following basic
variable types −
Type Description
char Typically a single octet(one byte). This is an integer type.

int The most natural size of integer for the machine.

float A single-precision floating point value.

double A double-precision floating point value.

void Represents the absence of type.


16
Constants

Floating-point constants
A floating point constant is a numeric constant
that has either a fractional form or an exponent
form. For example: 2.0,0.0000234,-0.22E-5
Character constants
A character constant is a constant which uses
single quotation around characters. For example:
'a', 'l', 'm', 'F'

2
String constants
String constants are the constants which are
enclosed in a pair of double-quote marks.
For example: "good" ,"x","Earth is
round\n"

18
Escape Sequences

Sometimes, it is necessary to use characters which


cannot be typed or has special meaning in C
programming. For example: newline(enter), tab,
question mark etc. In order to use these characters,
escape sequence is used.
For example: \n is used for newline. The backslash
( \ ) causes "escape" from the normal way the
characters are interpreted by the compiler. Escape

2
Sequences Character

\b Backspace
\f Form feed
\n New line
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\’ Single quotation mark
\” Double quotation mark
\? Question mark
\0 Null character 20
Operators in C/C++

An operator is a symbol which operates on a value or a


variable. For example: + is an operator to perform addition.
C programming has wide range of operators to perform
various operations. For better understanding of operators,
these operators can be classified as:
Arithmetic Operators , Increment and Decrement Operators
Assignment Operators, Relational Operators,
Logical Operators, Conditional Operators, Bitwise Operators
Special Operators 2
Following types of operators to perform different types of
operations in C language.

Arithmetic Operators.
Relational Operators.
Logical Operators.
Bitwise Operators.
Assignment Operator.
Unary operator.
Ternary or Conditional Operator.
Misc. Operator
22
23
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Right to left
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == !=/td> Right to left
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Right to left
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
= += -= *= /= %=>>= <<= &=
Assignment Right to left
^= |=
24
Increment and Decrement Operators

C/C++ programming has two operators increment ++ and


decrement -- to change the value of an operand (constant
or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -
- decreases the value by 1.
These two operators are unary operators, meaning they
only operate on a single operand.
eg. int a=10, b=100
++a = 11
2
--b = 99
C Assignment Operators
An assignment operator is used for assigning a value to a
variable. The most common assignment operator is =
Operator Example Same as

= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b

*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
2
C Relational Operators
A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation is
false, it returns value 0.
Relational operators are used in decision making and loops.
Operator Meaning of Operator Example

== Equal to 5 == 3 returns 0
> Greater than 5 > 3 returns 1
< Less than 5 < 3 returns 0
!= Not equal to 5 != 3 returns 1
>= Greater than or equal to 5 >= 3 returns 1
2
<= Less than or equal to 5 <= 3 return 0
Separate compilation
A C/C++ program consists of source code in one or more files.
Each source file is run through the preprocessor and compiler,
resulting in a file containing object code.
Object files are tied together by the linker to form a single
executable program.
Preprocessor/ Source code
Source code Compiler file1 .cpp
file1 .cpp

Preprocessor/ Source code


Source code
Compiler file1 .cpp
file2 .cpp

Libraries
Linker Source code 2
file1 .cpp
The preprocessor
The preprocessor takes your source code and – following
certain directives that you give it – tweaks it in various ways
before compilation.
A directive is given as a line of source code starting with the #
symbol
The preprocessor works in a very crude, “word-processor”
way, simply cutting and pasting – it doesn’t really know
anything about C++!

2
Enhanced and
your source obfuscated
source code Object Code
code

Preprocessor Compiler

30
THANKS!
Any questions?
+91-9893092930
[email protected]

31

You might also like