Upload
bhavini-rajendrakumar-bhatt
View
250
Download
0
Embed Size (px)
Citation preview
8/7/2019 C LECTURE02
1/23
Constants, Variables
and Datatypes
8/7/2019 C LECTURE02
2/23
8/7/2019 C LECTURE02
3/23
Trigraph SequencesTrigraph Sequence Translation??= # number sign
??( [ left bracket
??) ] right bracket
??< { left brace
??> } right brace
??! | vertical bar
??/ \ back slash
?? ^ caret
??- ~ tilde
8/7/2019 C LECTURE02
4/23
C Tokensy keywords : float, for, while
y identifiers : amount num
y Constants : -15.5 100
y strings : ABC Hello 5
y operators : + > < * -
y special Symbols :[ ] { } ( ) $
8/7/2019 C LECTURE02
5/23
Keywords and Identifiers
y Keyword :
meaning ofwordis known to compiler
y Identifier :
name given by user to variable, function .
8/7/2019 C LECTURE02
6/23
Keywordsauto double int struct
break else long switch
case enum register typedef
char extern return unionconst float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
8/7/2019 C LECTURE02
7/23
Identifiersy Identifiers refer to the names of variables, functions and arrays.
y Permissible character for naming :
alphabet a-z, A-Z
underscore _
digit 0,1,2..9
Rules for identifiers :
1. First character must be an alphabet (or underscore).2. Must consist of only letters, digits or underscore.
3. Only first 31 characters are significant.
4. Cannot use a keyword.
5. Must not contain white space.
8/7/2019 C LECTURE02
8/23
ConstantConstant
Primary
Numeric Character
Integer Real ingle ing
constants constants character consta
constants
8/7/2019 C LECTURE02
9/23
Integer Constanty Sequence of digits
Integer Constant
digits 0,1,..9 digits o,1,.7
A..F/af
Ex., 123
-321
+78
0
Decimal Integer Octal IntegerHexadecimal
Integer
8/7/2019 C LECTURE02
10/23
Integer Constants16-bit m/c : -32768 to +32767
32-bit m/c : -2147483648 to +2147483647
56789U or 56789u
987612347UL or 987612347ul (unsigne
9876543L or 9876543l
8/7/2019 C LECTURE02
11/23
Real Constanty Numbers containing fractional part
Real Constant
Ex., 0.0083
-0.75
435.36
+247.0
-.71
.95
Decimal NotationExponential(Scientific)
notation
mantissa e exponent
8/7/2019 C LECTURE02
12/23
Example of Numeric Constant698354L
25,000
+5.0E3
3.5e-57.1e 4
-4.5e-2
1.5E+2.5
$255
0X7B
8/7/2019 C LECTURE02
13/23
Single Character Constantsy Single character enclosed within a pair of single quote
marks.
y Ex.,
X ; 5
y Character constant has integer value, i.e., ASCII value.
printf(%d, a);
printf(%c, 97);
y Arithmetic operation can be performed using characterconstant.
8/7/2019 C LECTURE02
14/23
String Constantsy Sequence of characters enclosed within a pair of double
quote marks.
y
Ex.,Hello X ?---!
6587 Have a nice day
8/7/2019 C LECTURE02
15/23
Backslash Character Constant/
Escape SequenceConstant Meaning
\a Audible
\b Back space
\f Form feed
\n New line\r Carriage return
\t Horizontal tab
\v Vertical tab
\ Single quote
\ Double quote
\? Question mark
\\ Backslash
\0 Null
8/7/2019 C LECTURE02
16/23
DatatypesDatatypes
Primary/Fundamental /PrimitveData types
User-definedData types
DerivedData types
Integer
Character
Float
Double
Array
Pointer
Function
Structure
Union
Enum
8/7/2019 C LECTURE02
17/23
Datatypes
8/7/2019 C LECTURE02
18/23
Size and Range of Data Types on a 16-bit Machine
Type Size(Bits) Range
char / signed char 8 -128 to +127 %c
unsigned char 8 0 to 255 %c
int / signed int 16 -32,768 to 32,767 %d
unsigned int 16 0 to 65535 %ushort int / signed short int 8 -128 to 127 %d
unsigned short int 8 0 to 255 %u
long int / signed long int 32 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 32 0 to 4,294,967,295 %lu
float 32 3.4E-38 to 3.4E+38 %f
double 64 1.7E-308 to 1.7E+308 %lf
long double 80 3.4E-4932 to 1.1E+4932 %Lf
8/7/2019 C LECTURE02
19/23
Operators & Expression
Operators
Arithmeticoperator
Bitwiseoperators
Relationaloperator
Logicaloperator
Assign-ment
operator
Increment&
Decrement
Condi-tional
operator
Specialoperators
+-
*/%
&&
||! =
++
-- ?:
&|
>
,sizeof
* &. ->
===!=
8/7/2019 C LECTURE02
20/23
8/7/2019 C LECTURE02
21/23
Defining Member Functions :
Inside the Class Definitiony Replace the function declaration by the actual function definition
inside the class.class item{
int number;
float cost;
public :void getdata(int a, float b);
void putdata(void){
cout
8/7/2019 C LECTURE02
22/23
Defining Member Functions :
Inside the Class Definitiony When a function is defined inside a class, it is treated as an
inline function.
Therefore, all the restrictions and limitations that apply to aninline function are also applicable here.
y Normally, only small functions are defined inside the classdefinition.
8/7/2019 C LECTURE02
23/23
#include #include