FUNDAMENTAL OF C
PROGRAMMING
Character
Set
character:-
It denotes any alphabet, digit or
special symbol used to represent information.
Use:-
These characters can be combined to form variables. C uses constants,
variables, operators, keywords and expressions as building blocks to
form a basic C program.
Character
set:-
The character set is the fundamental raw material of any language and
they are used to represent information. Like natural languages,
computer language will also have well defined character set,
which is useful to build the programs
The
characters in C are grouped into the
following two categories:
1. Source
character set
a.
Alphabets
b.
Digits
c.
Special Characters
d.
White Spaces
2. Execution
character set
a.
Escape Sequence
Source
character set
ALPHABETS
Uppercase letters
A-Z
Lowercase letters
a-z
DIGITS 0,
1, 2, 3, 4, 5, 6, 7, 8, 9
SPECIAL
CHARACTERS
~ tilde
% percent
sign
|
vertical bar @
at symbol
+ plus
sign
< less
than
_
underscore
- minus
sign
> greater
than ^
caret
# number
sign
= equal
to
&
ampersand
$ dollar
sign
/
slash
( left
parenthesis *
asterisk
\ back
slash
)
right parenthesis ′
apostrophe
:
colon
[ left
bracket
"
quotation mark
;
semicolon
]
right bracket
!
exclamation mark ,
comma
{ left flower brace
?
Question mark .
dot operator
}
right flower brace
WHITESPACE
CHARACTERS
\b
blank space
\t horizontal
tab
\v vertical
tab
\r carriage
return
\f form
feed
\n new
line
\\
Back slash
\’ Single
quote
\"
Double quote \?
Question mark
\0
Null
\a Alarm
(bell).
Execution
Character Set
Certain
ASCII characters are unprintable, which means they are not displayed
on the screen or printer. Those characters perform other functions
aside from displaying text. Examples are backspacing, moving to a
newline, or ringing a bell.
They
are used in output statements. Escape sequence usually consists of a
backslash and a letter or a combination of digits. An escape sequence
is considered as a single character but a valid character
constant.
These
are employed at the time of execution of the program. Execution
characters set are always represented by a backslash (\) followed by
a character. Note that each one of character constants represents one
character, although they consist of two characters. These characters
combinations are called as escape
sequence.
Token
Token
are classified as Keyword, Identifier, Constant, Operator.
Keywords
C
programs are constructed from a set of reserved words which provide
control and from libraries which perform special functions. The basic
instructions are built up using a reserved set of words, such
as main, for, if, while, default, double, extern, for,
and int,
etc., C demands that they are used only for giving commands or making
statements. You cannot use default,
for example, as the name of a variable. An attempt to do so will
result in a compilation error.
Keywords
have standard, predefined meanings in C. These keywords can be used
only for their intended purpose; they cannot be used as
programmer-defined identifiers. Keywords are an essential part
of a language definition. They implement specific features of the
language. Every C word is classified as either a keyword or an
identifier. A keyword is a sequence of characters that the C compiler
readily accepts and recognizes while being used in a program. Note
that the keywords are all lowercase. Since uppercase and lowercase
characters are not equivalent, it is possible to utilize an uppercase
keyword as an identifier.
The
keywords are also called ‘Reserved
words’.
Keywords
are the words whose meaning has already been explained to the C
compiler and their meanings cannot be changed.
Keywords
serve as basic building blocks for program statements.
Keywords
can be used only for their intended purpose.
Keywords
cannot be used as user-defined variables.
All
keywords must be written in lowercase.
32
keywords available in C.
Data
types
|
Qualifiers
|
User-defined
|
Storage
Classes
|
Loop
|
Others
|
Decision
|
Jump
|
Derived
|
function
|
|
Int
Char
Float
Double
|
signed
unsigned
short
long
|
typedef
enum
|
auto
extern
register
static
|
for
while
do
|
const
volatile
sizeof
|
If
else
switch
case
default
|
goto
continue
break
|
struct
union
|
void
return
|
Restrictions
apply to keywords
Keywords
are the words whose meaning has already been explained to the C
compiler and their meanings cannot be changed.
Keywords
can be used only for their intended purpose.
Keywords
cannot be used as user-defined variables.
All
keywords must be written in lowercase.
Data
type Keywords
int
Specifies the integer type of value a variable will hold
char
Specifies the character type of
value a variable will hold
float
Specifies the single-precision
floating-point of value a variable will hold
double
Specifies the double-precision floating-point type
of value a variable will
Qualifier Keywords
signed
Specifies a variable can hold positive and
negative integer type of data
unsigned
Specifies a variable can hold only the positive integer type of
data
short
Specifies a variable can hold
fairly small integer type of data
long
Specifies a variable can
hold fairly large integer type of data
Loop Control
Structure Keywords
For
Loop is used when the
number of passes is known in advance
While
Loop is used when
the number of passes is not known in advance
Do
Loop is used to
handle menu-driven programs
User-defined
type Keywords
typedef
Used to define a new name for an
existing data type
Enum
Gives an opportunity to invent
own data type and define what values the variable of this data type
can take
Jumping
Control Keywords
Break
Used to force immediate
termination of a loop, bypassing the conditional expression and any
remaining code in the body of the loop
continue
Used to take the control to the beginning of the loop bypassing the
statements inside the loop
Goto
Used to take the
control to required place in the program
Storage
Class Keywords
Storage
Classes
Storage
Default initial value
Scope
Life
auto
Memory
An unpredictable value
Local
Till the control remains within the
block
register
CPU registers
Garbage value
Local
Till the control remains within the
block
static
Memory
Zero
Local
Value of the variable persists between different
function calls
extern
Memory
Zero
Global
Till the program’s execution doesn’t come to
an end
Identifier
Identifiers
Identifiers
are names for entities in a C program, such as variables, arrays,
functions, structures, unions and labels. An identifier can be
composed only of uppercase, lowercase letters, underscore and digits,
but should start only with an alphabet or an underscore. If the
identifier is not used in an external link process, then it is called
as internal.
Example: Local variable. If the identifier is used in an external
link process, then it is called as external. Example:
Global variable
An identifier is a string of alphanumeric
characters that begins with an alphabetic character or an underscore
character that are used to represent various programming elements
such as variables, functions, arrays, structures, unions and so on.
Actually, an identifier is a user-defined word. There are 53
characters, to represent identifiers. They are 52 alphabetic
characters (i.e., both uppercase and lowercase alphabets) and the
underscore character. The underscore character is considered as a
letter in identifiers. The underscore character is usually used in
the middle of an identifier. There are 63 alphanumeric characters,
i.e., 53 alphabetic characters and 10 digits (i.e., 0-9).
Rules
for constructing identifiers
1.
The first character in an identifier must be an alphabet or an
underscore and can be followed only by any number alphabets, or
digits or underscores.
2. They must not
begin with a digit.
3. Uppercase and
lowercase letters are distinct. That is, identifiers are case
sensitive.
4. Commas or blank spaces are
not allowed within an identifier.
5.
Keywords cannot be used as an identifier.
6.
Identifiers should not be of length more than 31 characters.
7.
Identifiers must be meaningful, short, quickly and easily typed and
easily read.
Valid identifiers: total
sum average
_x y_
mark_1
x1
Invalid identifiers
1x -
begins with a digit
char
-
reserved word
x+y
- special
character
Note: Underscore
character is usually used as a link between two words in long
identifiers.
kinds
of identifiers
C defines two kinds of identifiers:
Internal
identifier
If the
identifier is used in an external link process, then it is called
as external.
These identifiers are also known as external
names;
include function names and global variable names that are shared
between source files. It has at least 63 significant
characters.
External
identifier
If the identifier is not used in an external link process, then it is
called as internal.
These identifiers are also known as internal
names;
includes the names of local variables. It has at least 31 significant
characters.
Differentiate
between Keywords words and identifiers
Keyword
Identifier
Predefined-word
User-defined
word
Must be written in lowercase only
Can written in
lowercase and uppercase
Has fixed meaning
Must be
meaningful in the program
Whose meaning has already been explained
to the C compiler
Whose meaning not explained to the C compiler
Combination of
alphabetic characters
Combination of alphanumeric
characters
Used only for it intended purpose
Used for required purpose
Underscore character is not
considered as a letter
Underscore character is considered as a letter
Constant
Constants
Constants in C are fixed value that does not change during the
execution of a program. Constants can be of any of the basic data
types. C supports several types of constants in C language as
C
constants
a. Numeric
Constants
i. Integer
Constant
1. Decimal
Integer constant
2. Octal integer
constant
3. Hexadecimal Integer
constant
ii. Real
Constant
b. Character
Constants
i. Single
Character Constant
ii. String Constant
c. Backslash
Character constants
d. Symbolic
constants
Numeric
Constant
There
are two types
of numeric constants,
1) Integer constants 2)
Real or floating-point constants
Integer constants
Any
whole number value is an integer.
An
integer constant refers to a sequence of digits without a decimal
point.
An
integer preceded by a unary minus may be considered to represent a
negative constant
Example:
0 -33
32767
There
are three types
of integer constants namely,
a)
Decimal integer constant
b) Octal integer constant
c) Hexadecimal integer constant
Decimal
Integer constant (base
10)
The
first digit must be other than 0.
Embedded
spaces, commas, and non-digit characters are not permitted between
digits.
Valid: 0
32767 -9999
-23
Invalid:
12,245
- Illegal character (,)
10 20 30
- Illegal character (blank space)
Octal
Integer Constant (base
8)
If
a constant contains two or more digits, the first digit must be 0.
In
programming, octal numbers are used.
Valid:
037
0
0435
Invalid:
0786
- Illegal
digit 8
123
- Does
not begin with zero
01.2
- Illegal
character (.)
Hexadecimal
integerconstant
The
letters a through f (or
A through F) represent the decimal
quantities 10 through 15 respectively.
This
constant must begin with either 0x or 0X.
In
programming, hexadecimal numbers are used.
Valid Hexadecimal
Integer Constant:
0x 0X1
0x7F
Invalid Hexadecimal
Integer Constant:
0xefg
- Illegal
character g
123
- Does not
begin with 0x
Unsigned
integer constant:
An unsigned integer constant specifies only positive integer value.
It is used only to count things. This constant can be identified by
appending the letter u or U to the end of the constant.
Valid:
0u 1U
65535u
0x233AU
Invalid:
-123 -
Only positive value
Long
integer constant: A
long integer constant will automatically be generated simply by
specifying a constant that exceeds the normal maximum value. It is
used only to count things. This constant can be identified by
appending the letter l or L to the end of the
constant.
Valid:
0l23456L 0x123456L
-123456l
Invalid:
0x1.2L
- Illegal
character (.)
Short
integer constant: A
short integer constant specifies small integer value. This constant
can be identified by appending the letter s or S to the end of the
constant.
Valid:
123s
-456
32767S
Invalid:
12,245
- Illegal
character (,)
10 20 30 - Illegal
character (blank space)
Note:
- A
sign qualifier can be appended at the end of the constant. Usually
suffixes(s or S, u or U, l or L) are not needed. The compiler
automatically considers small integer constants to be of
type short and
large integer constants to be of type long.
Rules
for constructing Integer constants
i.
An integer constant must have at least one digit.
ii.
It must not have a decimal point.
iii.
It can be either positive or negative.
iv.
If no sign precedes an integer constant, it is assumed to be
positive.
v.
Commas or blanks are not allowed within an integer constant.
Real
or Floating-point constant
Constants in C are fixed value that does not
change during the execution of a program. A real constant is
combination of a whole
number followed
by a decimal
point and
the fractional
part. Example:
0.0083 -0.75 .95
215.
Use
of Real or Floating-point constants
Integer numbers are inadequate to represent quantities that vary
continuously, such as distances, heights, temperatures, prices and so
on. These quantities are represented by numbers containing fractional
part. Such numbers are called real or floating
point constants.
The
Real or Floating-point constants can be written in two forms:
1. Fractional or Normal form
2. Exponential
or Scientific form
Express
a Real constant in fractional form
A real constant consists for a series of digits representing the
whole part of the number, followed by a decimal point, followed by a
series of representing the fractional part. The whole part or the
fractional part can be omitted, but both cannot be omitted. The
decimal cannot be omitted. That is, it is possible that the number
may not have digits before the decimal point or after the decimal
point.
Valid Real
constants (Fractional): 0.0 -0.1
+123.456 .2
2.
Invalid Real
constant: -
1
- a
decimal point is
missing
1, 2.3 -
Illegal character (.)
Rules
for Constructing Real Constants
in Fractional Form
1.
A real constant must have at least one digit.
2.
It must have a decimal point.
3.
It could be either positive or negative.
4.
Default sign is positive.
5.
Commas or blanks are not allowed within a real constant.
Express
a real constant in Exponential form
A real constant is combination of a whole
number followed
by a decimal
point and
the fractional
part.
If the value of a constant is either too small or too large,
exponential form of representation of real constants is usually
used.
In
exponential form, the real constant is represented
in two parts.
Mantissa
- The
part appearing before e, the
mantissa is either a real number expressed in decimal notation or an
integer.
Exponent -
The part following e, the
exponent is an integer with an optional plus or minus sign followed
by a series of digits. The letter e separating the
mantissa and the exponent can be written in either lowercase or
uppercase.
Example:
0.000342 can be represented in exponential form as
3.42e-4
7500000000 can be represented in exponential form as
7.5e9 or 75E8
Rules
for Constructing Real Constants
in Exponential Form
1.
The mantissa part and the exponential part should be separated
by letter in exponential form
2.
The mantissa part may have a positive or negative sign.
3.
Default sign of mantissa part is positive.
4.
The exponent part must have at least one digit, which must be a
positive or negative integer. Default sign is positive.
5.
Range of real constants expressed in exponential for is -3.4e38 to
3.4e38.
Character: A
character denotes an alphabet, digit or a special character.
Single
Character constants: A
single character constant or character constant is a single alphabet,
a single digit or a single special symbol enclosed within single
inverted commas. Both
the inverted commas should point to the left.
For
example, Û¥AÛ¥
is
a valid character constant whereas ‛AÛ¥
is
not. Note that the character constant Û¥
5Û¥
is
not the same as the number 5.
Valid
Character Constants:
Û¥mÛ¥
Û¥=Û¥
Û¥AÛ¥
Û¥Invalid:
Û¥123Û¥
-
Length should be 1
"A" -
Enclosed in single quotes
Note:
- Each
single character constant has an integer value that is determined by
the computer’s particular character set.
Rules
for Constructing Single
Character constants
1.
A single character constant or character constant is a single
alphabet, a single digit or a single special symbol enclosed within
single inverted commas. Both the
inverted commas should point to the left.
2.
The maximum length of a single character constant can be one
character.
3.
Each character constant has an integer value that is determined by
the computer’s particular character set.
String
Constant
and
the corresponding single character string constant "A" are
not equivalent.
Û¥AÛ¥
-
Character constant -
Û¥AÛ¥
"A"
- String
Constant
-
Û¥AÛ¥
and
Û¥
\0Û¥
(NULL)
The string
constant "A" consists of character A and \0. However,
a single character string constant does not have an equivalent
integer value. It occupies two
bytes, one
for the ASCII code of A and
another for the NULL character with a value 0,
which is used to terminate all strings.
Valid String
Constants:
- "W"
"100"
"24, Kaja Street"
Invalid String
Constants:
-
"W
the closing double quotes
missing
Raja" the beginning double
quotes missing
Rules
for Constructing String constants
1)
A string constant may consist of any combination of digits, letters,
escaped sequences and spaces enclosed in double quotes.
2)
Every string constant ends up with a NULL character
which is automatically assigned (before the closing double quotation
mark) by the compiler.
Difference
between single character constant and string constant
Character
Constant
String
Constant
A
character constant is enclosed within single inverted
commas.
A sequence of characters enclosed in double quotes
The
maximum length of a character constant can be one
character.
A string constant can be any length.
A
single character string constant has an equivalent integer
value.
A single character string constant does not have an equivalent
integer value.
The
character constant ‘A’ consists of only character A.
The string constant "A"
consists of character A and \0.
A
single character constant occupies one byte.
A single string constant occupies two bytes
Every
character constant does not end up with a NULL character.
Every string constant ends up with a NULL
character which is automatically
assigned (before the closing double quotation mark) by the compiler
Operators
Operator
An
operator is a symbol or a special character that tells the computer
to perform certain mathematical or logical manipulations which is
applied to operands to give a result. It can operate on integer and
real numbers.
Usage
operands
The
data items that operators act upon to evaluate expressions are called
as operands.
Most
operators require two or more operands while others act upon a single
operand.
Most
operators allow the individual operands to be expressions.
A
few operators permit only single variables as operands.
The
operands can be integer quantities, floating-point quantities or
characters
Various
types of Operators
C
supports a rich set of operators. C operators can classified as
List
of Arithmetic operators (Or) List
of Binary operators
Operator
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulo operation(
remainder after integer division
List
of Relational operators
Operator
Meaning
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
==
Equal to
!=
Not equal to
List
of Equality operators
Operator
Meaning
==
Equal to
!=
Not equal to
List
of Logical operators
Operator
Meaning
&&
Logical AND
||
Logical OR
!
Logical
NOT
List
of Unary operators
Operator
Meaning
-
Unary minus
++
Increment by 1
- -
Decrement by 1
sizeof()
Returns the size of the operand
List
of Conditional operators (Or) List of Ternary operators
Operator
Meaning
?:
Conditional operator
List
of Increment and Decrement operators
Operator
Meaning
++
Increment by 1
--
Decrement by 1
List
of Arithmetic assignment operators (Or) List
of Shorthand assignment operators (Or) List
of Compound assignment operators
Operator
Meaning
+=
Addition assignment
-=
Subtraction
assignment
*=
Multiplication assignment
/=
Division
assignment
%=
Modulus
assignment
List
of Bitwise operators
Operator
Meaning
~
One’s Complement
<<
Left shift
>>
Right shift
&
Bitwise AND
|
Bitwise OR
^
Bitwise X-OR
List
the Special operators
Operator
Meaning
,
Comma operator
*
Pointer operator
&
Address of operator
.
Dot operator
->
Member Access operator
=======================================
Arithmetic
operators are used in mathematical expressions in the same way that
they are used in algebra. C provides all the basic arithmetic
operators. They can operate on any built-in numeric data type of C.
They cannot be used as Boolean type, but can use them on char types,
since the char type
in C is, essentially, a subset of int.
Operator
Meaning
Example
Result (a=14, b=4)
+
Addition
a+b
18
-
Subtraction
a-b
10
*
Multiplication
a*b
56
/
Division
a/b
3
%
Modulo operation a%b
2
(remainder after integer division)
All
the above operators are called Binary operators
as they act upon two operands at a time. The operands acted upon by
arithmetic operators must represent numeric values. Thus, the
operands can be integer quantities, floating-point quantities or
characters (the character constants represent integer values, as
determined by the computer’s character set.
If
one or both operands represent negative values then the addition,
subtraction, multiplication and division operations will result in
values whose signs are determined by the usual rules of
algebra.
About
Division Operator (/): - Division
of one integer quantity by another is referred to as integer
division.
This operation always results in a truncated quotient (i.e., the
decimal portion of the quotient will be dropped). If a division
operation is carried out with two floating-point numbers or with one
floating-point number and one integer, the result will be a
floating-point quotient. The division operator (/) requires that the
second operand be nonzero.
About
Remainder Operator (%): - The
remainder operator (%) requires that both operands be integers and
the second operand be nonzero. When one of the operands is negative,
the sign of the first operand is the sign of the result
operand. Note: Beginners
should exercise care in the use of remainder operation when one of
the operands is negative.
Example: 11
% 3 = 3: -11
% 3 = -2:
11 % -3
= 2: -11
% -3
= -2
The
% operator is sometimes referred to as the modulus operator.
Among
the arithmetic operators, *, / and % fall into one precedence group
and + and – fall into another. The first group has a higher
precedence than the second. Thus, multiplication, division and
remainder operations will be carried out before addition and
subtraction. Example: a=10; b=5; c = a + b*2;
c is 20.
/*
Arithmetic operations using Arithmetic operators */
#include
<stdio.h>
#include
<conio.h>
main()
{
int a, b, c;
clrscr();
printf ("Enter a and b");
scanf ("%d %d", &a,&b);
printf ("Addition: %d\n", a+b);
printf ("Subtraction: %d\n", a-b);
printf ("Multiplication: %d\n", a*b);
printf ("Division: %d\n", a/b);
printf ("Modulus: %d ",
a%b);
getch();
}
Output:-
Enter
a and b:
10 20
Addition:
30
Subtraction:
-10
Multiplication:
200
Division:
0
Modulus:
0
Difference
between Division operator and Modulus operator
Division
Operator
(/)
Modulus Operator (%)
The
interpretation of the remainder
The
interpretation of the remainder operation is unclear when one of the
operation is
clear when one of the operands is negative
operands is negative
If
a division operation is carried out with two floating-point numbers
The remainder operator
(%) requires that both operands be integers only.
or
with one floating-point number and one integer,
the
result will be a floating-point quotient.
The
use of the division operation is determined by the usual rules of
algebra.
Beginning programmers should exercise care in the use of the
remainder
operation.
Example:
- 7 / 2 = 3
Example:-
7 % 2 = 1
========================================