0% found this document useful (0 votes)
46 views23 pages

Constants, Variables and Datatypes

This document discusses C datatypes, constants, variables, keywords, identifiers, and operators. It provides details on integer, real, character, and string constants. It also covers fundamental and derived C datatypes like integer, character, float, double, arrays, pointers, structures, unions, enums, functions, and their sizes and ranges on a 16-bit machine. The document concludes with an example demonstrating defining member functions inside a C++ class definition.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views23 pages

Constants, Variables and Datatypes

This document discusses C datatypes, constants, variables, keywords, identifiers, and operators. It provides details on integer, real, character, and string constants. It also covers fundamental and derived C datatypes like integer, character, float, double, arrays, pointers, structures, unions, enums, functions, and their sizes and ranges on a 16-bit machine. The document concludes with an example demonstrating defining member functions inside a C++ class definition.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Constants, Variables

and Datatypes
Character Set
Characters in C are grouped into following categories :

1. Letter : a…..z A…..Z


2. Digits : 0,1,2…….9
3. Special Characters :
, . ;: ^& #\? ‘“!|/~_ $%
*- +< > ( ) [ ] { } #

4. White spaces :
Blank Space
Horizontal tab
Carriage return
New line
Form Feed
Trigraph Sequences
Trigraph Sequence Translation
??= # number sign
??( [ left bracket
??) ] right bracket
??< { left brace
??> } right brace
??! | vertical bar
??/ \ back slash
??’ ^ caret
??- ~ tilde
C Tokens
keywords : float, for, while

identifiers : amount num

Constants : -15.5 100

strings : “ABC” “Hello” “5”

operators : + > < * -

special Symbols : [ ] { } ( ) $
Keywords and Identifiers
Keyword :
meaning of word is known to compiler

Identifier :
name given by user to variable, function ….
Keywords
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
Identifiers refer to the names of variables, functions and arrays.
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.
Constant
Constant

Primary Secondary

Numeric Character

Integer Real Single String


constants constants character constants
constants
Integer Constant
Sequence of digits
Integer Constant

Hexadecimal
Decimal Integer Octal Integer
Integer
digits 0,1,..9 digits o,1,….7 digits 0,1,….9
A..F/a…f
Ex., 123 037 0X2
-321 0435 0x
+78 0551 0x9F
0 0 0Xbcd
Integer Constants
16-bit m/c : -32768 to +32767
32-bit m/c : -2147483648 to +2147483647

56789U or 56789u (unsigned integer)


987612347UL or 987612347ul (unsigned long integer)
9876543L or 9876543l (long integer)
Real Constant
Numbers containing fractional part
Real Constant

Exponential(Scientific)
Decimal Notation
notation
Ex., 0.0083
mantissa e exponent
-0.75
435.36 Ex., 12e-2 -1.2E-1 3.1e3
+247.0
-.71 7500000000 7.5e9 / 75e8
.95 0.000000368 -3.68E-7
Example of Numeric Constant
698354L
25,000
+5.0E3
3.5e-5
7.1e 4
-4.5e-2
1.5E+2.5
$255
0X7B
Single Character Constants
Single character enclosed within a pair of single quote
marks.
Ex.,
‘X’ ‘;’ ‘’ ‘5’

Character constant has integer value, i.e., ASCII value.

printf(“%d”, ‘a’); 97
printf(“%c”, ‘97’); a

Arithmetic operation can be performed using character


constant.
String Constants
Sequence of characters enclosed within a pair of double
quote marks.

Ex.,
“Hello” “X” “?---…!” “5+3”
“6587” “Have a nice day”
Backslash Character Constant/
Escape Sequence
Constant 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
Datatypes
Datatypes

Primary/Fundamental /
Primitve Derived User-defined
Data types Data types Data types

Integer
Array Structure
Character
Pointer Union
Float
Function Enum
Double
Datatypes
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 %u
short 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
Operators & Expression
Operators

Assign- Increment Condi-


Arithmetic Relational Logical Bitwise Special
ment & tional
operator operator operator operators operators
operator Decrement operator

<
+ &
<= ,
- && |
> ++ sizeof
* || = ?: ^
>= -- * &
/ ! <<
== . ->
% >>
!=
Defining Member Functions :
Inside the Class Definition
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 << "Number :" << number << "\n";
cout << "Cost :" << cost << "\n";
}
};
Defining Member Functions :
Inside the Class Definition
 When a function is defined inside a class, it is treated as an
inline function.
Therefore, all the restrictions and limitations that apply to an
inline function are also applicable here.

Normally, only small functions are defined inside the class


definition.
#include <iostream.h> int main()
#include <stdio.h. {
#include <conio.h. item x, y;
class item cout << "\n object x " << "\n";
{
int number; x.getdata(100, 299.95);
float cost; x.putdata();

public : cout << "\n object y " << "\n";


void getdata(int a, float b);
void putdata(void) y.getdata(200, 175.50);
{ y.putdata();
cout << "Number : " << number << "\n";
cout << "Cost : " << cost << "\n"; return 0;
} }
};
Output :
void item :: getdata(int a, float b) object x
{ number = a; Number : 100
cost = b; Cost : 299.95
}
object y
Number : 200
Cost : 175.5

You might also like