Character Set & Identifiers
Character Set & Identifiers
1
Special Characters in C Programming
, < > . _
( ) ; $ :
% [ ] # ?
^ ! * / |
- \ ~ +
2
3
Identifiers
identifier – name of a variable or any other named construct
identifier must start with a letter or underscore symbol (_), the rest of the
characters should be letters, digits or underscores
legal identifiers:
x x1 x_1 _abc sum tax_rate
illegal identifiers, why?
13 3X %change data-1 my.identifier a(3)
C++ is case sensitive:
MyVar and myvar are different identifiers
4
Rules for writing an identifier
A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.
The first letter of an identifier should be either a letter or an
underscore. However, it is discouraged to start an identifier
name with an underscore.
There is no rule on length of an identifier. However, the first 31
characters of identifiers are discriminated by the compiler.
5
Keywords
C Keywords. Keywords are predefined, reserved words used in
programming that have special meanings to the
compiler. Keywords are part of the syntax and they cannot be used as
an identifier.
keywords are identifiers reserved as part of the language
int, return, float, double,
long,switch,break,return,char,if,for etc
cannot be used by the programmer to name constructs
consist of lowercase letters only
have special meaning to the compile
6
Keywords (cont.)
asm do if return typedef
auto double inline short typeid
bool dynamic_cast int signed typename
break delete long sizeof union
case else mutable static unsigned
catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned
7
Data type
In computer science and computer programming, a data type or
simply type is a classification of data which tells the compiler or
interpreter how the programmer intends to use the data.
8
9
Int Data Type
The int data type represents inive teger values, meaning any
posititive and negative values, number or zero without decimal
point. Example of valid integers are:
4 -87 +9345 -18239
An integer consist of an optional minus or plus sign followed by a
series of digits. Commas , special symbols, such as $, or e for
exponential notation are not allowed.
10
C Qualifiers
Qualifiers alters the meaning of base data types to yield a new data type.
Size qualifiers
Size qualifiers alters the size of a basic type. There are two size
qualifiers, long and short. For example:
long double i;
The size of double is 8 bytes. However, when long keyword is used, that variable
becomes 10 bytes.
There is another keyword short which can be used if you previously know the value
of a variable will always be a small number.
Sign qualifiers
Integers and floating point variables can hold both negative and positive values.
However, if a variable needs to hold positive value only, unsigned data types are
used. For example:
// unsigned variables cannot hold negative value unsigned int positiveInteger;
There is another qualifier signed which can hold both negative and positive only.
However, it is not necessary to define variable signed since a variable is signed by
default.
An integer variable of 4 bytes can hold data from -231 to 231-1. However, if the
variable is defined as unsigned, it can hold data from 0 to 2 32-1.
It is important to note that, sign qualifiers can be applied to int and char types only.
11
Data Type Range Bytes width Format
12
Float Data Typed
The float data type represents floating-point values, meaning any
signed or unsigned number having a decimal point.
Examples of valid float variables
-78.294 395.20587 -95.0 4.39
In cannot contains special characters such as $ but can include
letter e for exponential.
Float and double can be written in exponential or scientific
notation, if needed.
See book page 76
13
Floating data type
14
Char Data Type
C also recognizes nonnumeric character data. A char represents
individual characters. Hence , the char type will generally require
only one byte of memory. A char is any valid ASCII character
enclosed in single character quotes. Examples of char are
‘m’ ‘>’ ‘H’ ‘2’ ‘[‘ ‘!’ ‘F’
Note that a char data item can also be a single numeric digit,
such as 1,2,3. but C makes a distinction between the character
‘1’ and the number 1. as character ‘1’ cannot be used in a
mathematical operation because it does not have a numeric
value.
15
Data type Bytes width Range Format
16
17
Constant
C - Constants and Literals. Constants refer to fixed values that
the program may not alter during its execution. These fixed
values are also called literals .Constants can be of any of the
basic data types like an integer constant, a floating constant, a
character constant, or a string literal.
18
19
20
Symbolic Constant
21
22
23
24
25
26
Variables y 12.5
1001
1002
1003
1004
Temperature 32 1005
1006
Letter 'c' 1007
1008
Number - 1009
variable is a named memory location
variable value is data stored in variable
variable always has a value
compiler removes variable name and assigns memory location
however, it is convenient to think that memory locations are
labeled with variable names
27
Variable Declaration
before use, every variable in C++ program needs to be declared
declaration – introduction of the name to the compiler
type – the kind of data stored in a variable
variable declaration statement specifies
type
name
1.75, -0.55
example declarations:
int numberOfBars;
double weight, totalWeight;
28
Arrays
C Programming Arrays. ... You will learn to declare, initialize
and, access array elements with the help of examples.
An array is a collection of data that holds fixed number of values
of same type. For example: if you want to store marks of 100
students, you can create an array for it.
float marks[100];
The size and type of arrays cannot be changed after its
declaration.
How to declare an array in C?
data_type array_name[array_size];
For example,
float mark[5];
Here, we declared an array, mark, of floating-point type and size
5. Meaning, it can hold 5 floating-point values.
29
Elements of an Array and How to access them?
You can access elements of an array by indices.
Suppose you declared an array mark as above. The first element
is mark[0], second element is mark[1] and so on.
Few key notes:
Suppose the starting address of mark[0] is 2120d. Then, the next
address, a[1], will be 2124d, address of a[2] will be 2128d and so
on. It's because the size of a float is 4 bytes.
How to initialize an array in C programming?
It's possible to initialize an array during declaration. For example,
int mark[5] = {19, 10, 8, 17, 9};
Another method to initialize array during declaration:
int mark[] = {19, 10, 8, 17, 9};
30