Basics of C
Basics of C
Aspect of ‘C++’
Prof. A.P.Khan
Department of Computer Engineering
D.N.Patel College of Engineering, Shahada
Content
Introduciton
Data Types
Algorithms
Program
2
General Aspect of ‘C++’
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++’
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
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.
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
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++
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
= 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
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