Академический Документы
Профессиональный Документы
Культура Документы
Design and programming are human activities; forget that and all is
lost.
COMPILATION PROCESS
C++ TOKENS
RULES
•They must begin with a letter or underscore(_).
•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 first 31 characters are significant.
NAME VALID OR INVALID IDENTIFIER??
_A9 Valid
Temp.var Invalid as it contains special character other than the underscore
void Invalid as it is a keyword
C++ program:
int main()
{
int a = 10;
return 0;
}
• Data types are used to tell the variables the type of data it can store.
• Whenever a variable is defined in C++, the compiler allocates some
memory for that variable based on the data-type with which it is
declared. Every data type requires different amount of memory.
• Data types in C++ is mainly divided into two types: Primitive Data
Types and Abstract/User-Defined Data Types
• Abstract /User defined data typesà Classes
Primitive Data Types
These data types are built-in or predefined data types and can be used directly by the user
to declare variables.
These are used with the built-in data types to modify the length of
data that a particular data type can hold.
• Signed
• Unsigned
• Short
• Long
We can display the size of all the data types by using the sizeof()
operator and passing the keyword of the datatype as argument to this
operator as shown below:
Using char datatype
#include //for cout, etc.
using namespace std;
int main()
{
char charvar1 = ‘A’; //define char variable as character
char charvar2 = ‘\t’; //define char variable as tab
cout << charvar1; //display character
cout << charvar2; //display character
charvar1 = ‘B’; //set char variable to char constant
cout << charvar1; //display character
cout << ‘\n’; //display newline character
return 0;
}
Constants
Constants are also like normal variables. But, only difference is, their values
can not be modified by the program once they are defined.
Types of Constants:
Integer constants – Example: 0, 1, 1218, 12482
Floating point constants – Example: 0.0, 1203.03, 30486.184
Character constants -Example: ‘a’, ‘A’, ‘z’
String constants -Example: “PunjabEngineeringCollege”
Area of circle
Using # for MACROS
Macros are piece of code in a program which is given some name.
Whenever this name is encountered by the compiler the compiler replaces the
name with the actual piece of code. The ‘#define’ directive is used to define a
macro.
#define PI 3.14159
Macros with arguments:
We can also pass arguments to macros. Macros defined with arguments works
similarly as functions. Let us understand this with a program:
File inclusion using #
• #include< file_name > (for standard/header files)
• #include"filename” ( for user defined files)
Strings
Strings are nothing but an array of characters ended with a null character
(‘\0’).
• Brackets[]
• Parentheses()
• Braces{}
• comma (,)
• semi colon (;)
• asterisk (*)
• assignment operator (=)
• pre processor(#)
Expressions
int n; // declaration
n = 1; // expression
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
cout<<"Do you want to continue (Y/N)?\n";
cout<< "You must type a 'Y' or an 'N'.\n";
cin >> ans;
} while((ans =='Y')|| (ans =='y'));
}
/*the loop continues until the user enters the correct response*/