C
Fundamentals
The basic elements of C includes c
character set, identifiers, keywords, data types, constants, variables, array,
declarations, expressions and statements.
The C character set:
C uses uppercase A to Z, the lower
case letters a to z, the digit 0 to 9 and certain special characters such as:
! ^ # % &
* ( ) ¬ - _ = + \ / [ ] { } ; : ‘ “ , < . > / ? (Blank)
Most versions
of C also allow using @ $.
It can be
combination of certain characters such as \n, \t to represent non-printable
characters new line, horizontal tab respectively. Such character combination to
print non printable character is known as escape sequence.
Identifiers:
Identifiers
are the names given to various element of program such as variables, functions
and arrays. Identifiers consist of letters and digits. Rules are to be followed
for identifiers:
- It may consist of character, digits
but first character must be an alphabet.
- No comma or blanks are allowed within
a variable name.
- No special symbols other than
underscore can be used in a variable name.
- The uppercase and lowercase letters
are distinct.
- It may begin with underscore too.
- Keywords cannot be redefined as
identifiers.
E.g. x1,
sum_temp, Table etc.
Some invalid
identifiers are
1x,”X”, -temp
etc.
Keywords:
Keywords are reserved identifiers
and they cannot be used as names for the program variables to other user
defined program elements. The meanings of the keywords have already been given
to the compiler. The keywords are also called reserved words. There are 32 keywords
available in C.
Some compilers
may also include:
ada, far,
near, asm, fortran, pascal entry, huge
Keywords must
be in lowercase
Constant:
Constant is a basic element of C
which doesn’t change its value during execution of program.
Four basic
types of constant are:
- Integer
constant
An integer constant refers
to a sequence of digits. It requires minimum 2 bytes and maximum 4 bytes. It
could either be positive, negative or zero.
e.g. 10, -30 etc.
Generally, it
is of 3 types.
- Decimal
integer
It
consists of a set of digits, 0 through 9, preceded by an optional – or + sign.
e.g.
123 0 +78 -312
Spaces,
commas and non-digit characters are invalid.
- Octal
Integer:
It
consists of any combination of digits from the set 0 through 7, with a leading
0.
e.g.
0 0234 0354 etc
c.
Hexadecimal integer
A sequence of digits preceded by Ox
or OX is considered as hexadecimal integer. They may also include alphabet
through A to F as number from a=10 to f=15
Rules for
constructing integer constant.
- It must not have a decimal point.
- It could be either positive or
negative
- If no sign precedes an integer
constant it is assumed to be positive.
- No commas or blanks are allowed within
an integer constant.
- The allowable range for integer
constants is -32768 to 32767(16)
- For 32 bit computer, range will be
larger.
2. Real constant or floating point
constants
A number with a decimal point and an
optional preceding sign represents a real constant.
e.g. 34.8, .2,
0.56 etc
The real constants could be written
in two forms, Fractional form and Exponential form.
Fractional part
It must have a decimal point. It
could be either positive or negative. No commas or blanks are lowed within a
real constant.
e.g. +325.34 ,
-32.76 etc
Exponential part
It is usually used if the value of
the constant is either too small or too large. Here, the real constant is
represented in two parts. The part appearing before’ is called mantissa, where
the part following ‘e’ is called exponent.
E.g. +3.2 e-5
4.1e8 etc
Rules for real
constants expressed in exponential form.
a.
The mantissa part and the exponent part should be
separated by a letter e.
b.
The mantissa part may have positive or negative.
c.
Default sign of mantissa part is positive.
d.
Range of real constants expressed in exponential form
is -3.4e38 to 3.4e38.
E.g. +3.2e-5, 4.1e8 etc.
3. Character Constants.
A character represented within a
single quotes denotes a character constants e.g.’A’,’2’ etc.
The maximum length of a character
constant is one character.
e.g. ‘a’ is a
character constant.
4. String constant
String constants are sequence of
characters enclosed within a double quote marks.
e.g. “hello”,
“Nepal” etc.
Variables-
A variable is a value that can
change any time. It is a memory location used to store a data value. Variables
hold different kinds of data and the same variable might hold different values
during the execution of a program. A variable name should be carefully chosen
by the programmer so that its use is reflected in a useful way in the entire
program. Variable names are case sensitive.
Any variable declared in a program
should confirm to the following.
·
They must always begin with a letter, although
some systems permit underscore as the first character.
·
The length of a variable must not be more than 8
characters.
·
White space is not allowed.
·
A variable should not be a keyword.
·
It should not contain any special characters.
examples of INVALID names are:
567
“a
(name)
*12a
A variable is an entity that has a
value and is known to the program by its name.
E.g.
f=1.8*c+32
Here, f and c
are variables.
Declaration of Variables
Every variable
used in the
- Basic
data types
char,
int float are the basic data types.
In
C, you always have to declare a variable before using it.
sizeof ()
C provides an operator named sizeof
() that gives the number of bytes associated with a specified data type or a
variable.
The sizeof ()
operator takes either a variable or a type name as an argument and returns the
number of bytes in that argument.
/*
illustrating the sizeof () operator */
#include<stdio.h>
void main ()
{
int a;
printf (“The
size of a is %d\n”, sizeof (a));
printf(“The
size of int is %d’, sizeof(int));
}
Output:
The size of a
is 2
The size of
int is 2
Const qualifier-
Whenever we
want some value of an object to be unchanged throughout the execution of the
program, we can use const qualifier. Const is a compile time concept. It just
ensures that the object is not modifiable.
/* program to
illustrate the use of const */
#include<stdio.h>
void main()
{
const float
pi=3.14;
float radius,
area;
printf(“Enter
the value of radius\n”);
scanf(“%f”,
&radius);
area=pi*radius*radius;
printf(“The
area of circle is %f”, area);
}
Output:
Enter the
value of radius
2.3
The area of
circle is 16.610600
Another way to
achieve the effect of const identifier is by the use of
#define
preprocessor directive as:
/*program to
show the use of #define */
#include<stdio.h>
#define pi
3.14
void main()
{
float radius, area;
printf(“Enter
the value of radius\n”);
scanf(“%f”,
&radius);
area=pi*radius*radius;
printf(“The
area of circle is %f”, area);
}
Output:
Enter the
value of radius
2.3
The area of
circle is 16.610600
Preprocessor
directive is not terminated by semicolon. The directive instructs all the
preprocessor to substitute 3.14 for all the occurrences of the identifier-
const- treats
as a value cannot be changed
preprocessor-
replaces all of pi by 3.14
Operator
Arithmetic Operators
The major
arithmetic operators are addition, subtraction, multiplication and division.
Each of the four arithmetic operators is sometimes called a binary operator
because it operates on two values at a time.
Integer
arithmetic
When two operands such as a and b
are declared as integers, an arithmetic operation performed on these integers
is called integer arithmetic. It gives integer value as output.
/* program
that shows integer arithmetic operator */
#include<stdio.h>
void main()
{
int a=10,
b=5,ans;
ans =a + b;
printf (“a +
b=%d\n”, ans);
ans =a-b;
printf (“a -
b=%d\n”, ans);
ans =a*b;
printf (“a *
b=%d\n”, ans);
ans =b/a;
printf (“b /
a=%d”, ans);
}
Output:
a + b=15
a - b=5
a * b=50
b / a=0
When you
divide 5 by 10, the answer is 0.5 but our output is 0.
In C, when one
integer is divided by another integer, the result is displayed as an integer.
Any fractional part is discarded. Therefore instead of printing 0.5 this
program prints out only the integer portion, 0. This process is called
truncation.
Division and
modulo division (%) have different meaning from whatever we studied in
mathematics.
Division
operator gives quotient while modulo division gives remainder after division.
/* program
that shows / and % operator */
int a=7, b=2,
ans1, ans2;
ans1=a/b;
ans2=a % b;
printf (“The
result of a/b is %d\n”, ans1);
printf (“The
result of a %%b is %d”, ans2);
}
Output:
The result of
a/b is 3
The result of
a %b is 1
Floating point
arithmetic
Floating point arithmetic involves
operands of real type in decimal notation.
/* floating
point arithmetic */
float a=19.0,
b=9.0,ans;
ans =a/b;
printf (“a /
b=%f \n”, ans);
ans =b/a;
printf (“b /
a=%f”, ans);
ans =a + b;
printf (“a +
b=%f\n”, ans);
Output:
a /b=2.111111
b /a=0.473684
a +
b=28.000000
Mixed mode arithmetic-
Operation |
Result |
7.0/2 7/2.0 7.0/2.0 7/2 |
3.5 3.5 3.5 3 |
x=5* 6.7
In this, one
operand is of type int and another is of type float. In C, this is not
considered as an error. The integer value is automatically converted to type
double for the calculation; this is known as implicit conversion.
E.g.
int a=2,ans;
float
b=12.34,result;
result =a*b;
printf(“%d*%f=%f\n”,
a, b, result);
printf)”%f in
scientific notation is %e\n”, result, result);
ans =a*b;
printf
(“%d*%f=%d\n”, a, b, ans);
}
Output:
2*12.340000
24.680000
24.680000 in
scientific notation is 2.468000E+01
2*12.340000=24
In C, there is
the choice of explicitly specifying how the values are to be converted in a
mixed mode operation. This feature is known as explicit conversion and may be
accomplished using cast operator. The name of the data type to which the
conversion is to be made (such as int or float) is enclosed in parentheses and
placed directly to the left of the value to be calculated.
E.g
int a=7;
float b;
b=(float)a+1.0;
printf(“a+1.0
is %f”,b);
}
Output:
A+1.0=8.000000
Precedence and
Associativity of Operators=
Operators have
rules of precedence and associativity that are used to determine how
expressions are evaluated.
1+2*3
In C, * has
higher precedence than +, causing the multiplication to be performed first then
the addition. Hence the value of expression is 7.
1 + (2*3)
On other hand,
expression inside parentheses are evaluated first, the expression
(1+2)*3 i.e. 9
Now, consider
the expression
1+2-3+4-5
Because the
binary operators + and – have the same precedence, the associativity rule”left
to right” is used to determine how it is evaluated.
Priority Operators Associativity
1st
( ) L
->R
2nd
* / % L ->R
3rd
+ - L
->R
4th
= R
->L
Increment and
Decrement operator
The increment
++ and decrement – are called unary operators. Since these two operators are
used with single operand.
E.g. a++ (postfix operator)
++a (prefix operator)
#++ and – as a
postfix operator
When operand
is followed by unary operator (++ or --), it is called postfix expression. In
postfix expression, assignment takes place first and then variable is
incremented or decremented by 1.
For e.g.
int a=5, b;
b=a++;
Here, the
value of variable a is assigned to b first then the value of the variable a is
incremented. Thus the value of b will be 5 and value of a will be 6 after the
evaluation of whole expression.
int a=5,b;
b=a--;
Here, the
value of variable a is assigned to b first then the value of the variable a is
decremented. Thus the value of b will be 5 and the value of a will be 4 after
the evaluation of the whole expression.
E.g.
int a=5,b;
int c=5,d;
b=a++;
printf(“The a
value is %d and b value is %d”, a, b);
d=c--;
printf(“The c
is %d and d is %d”, c, d);
}
Output:
The a value is
6 and b value is 5
The c is 4 and
d is 5
# ++ and – as
a prefix operator
When operator
precedes an operand, it is called prefix expression. In prefix expression,
increment takes place first and then the incremented value is assigned.
For. E.g.
int a=5,b;
b=++a;
Here, the
value of variable a is incremented first and then it is assigned to variables
b. The value of b will be 6 and value of a will also is 6.
int a=5,b;
b=--a;
The value of a
is decremented first and then it is assigned to variable b. Thus the value of b
will be 4 and a will also be 4 after the evaluation of whole expression.
Assignment Operator
C treats = as
an assignment operator. Its precedence is lowest among all the operators we
discussed so far and its associativity is right to left.
E.g.
a=2;
b=a*2+a;
In first, 2 is
assigned to variable a. Therefore, the value of ‘a’ becomes 2.
In second, a
is multiplied by 2 and is added with value of ‘a’ i.e. 2.
Thus the
result is 6 and is assigned to variable b with the help of assignment operator.
Lets take an
e.g. of multiple assignment.
Let i and j
are integer variables
i=j=5;
Will cause the
integer value 5 to be assigned to both i and j. To be more precise, 5 is first
assigned to j and the value of j is then assigned to i.
E.g.
1. i=5.4
2. i=j=7
i=j/3=2
- i=j=7
- if i=4*j/3 =9
- i=2*(j/3) =6
In addition to
=, there are other assignment operators such as
+ = , - = ,
* = , / = , % =
Expression Equivalent
i* = 5 i=i+5
i- =j i=i-j
j*=(i-3) j=j*(i-3)
f/=3 f=f/3
i %=(j-2) i=i%(j-2)
Relational Operators
Relational
operators are symbols that are used to test the relationship between two
variables or between a variable and a constant. C has six relational operators.
Operator Meaning
= = Equal
to
> Greater
than
< Less
than
!= Not
equal to(either greater than or less than)
>= Greater
than or equal to
<= Less
than or equal to
Note: The
assignment operator = and equality operator = = are distinctly different. The
assignment operator is used to assign a value to an identifier.
The equality
operator = = means:
e.g.(n==7)
means ‘Is the value contained in the variable n equal to 7?’ The answer to this
is either yes or no.
Logical Operators:
C has the following three logical
operators.
Operators Meaning
&& Logical AND
|| Logical OR
! Logical NOT
The logical
operators && and || are used to combine expressions containing
relational operators.
For E.g. to
program
If n is equal
to 7 and x is greater than 5. To code this, logical operators called AND are
used. The AND is represented by a symbol &&.
i.e. (n==7)
&& (x>5)
An expression
of this kind, which combines two or more relational expressions two or more
relational expressions is termed as logical expression or a compound relational
expression.
E.g. if(x>y
&& x>z) x is
highest
if (x==0 || x==1) x is binary
The logical
operator OR, is used when at least one f two conditions must be true. The OR is
represented by ||.
e.g. if n is
equal to 7 or x is greater than 5 can be written as (n==7) || (x>5)
# Precedence
of Operators
Operator Associativity
( ) L to
R
! – (unary) R to L
* / % L to R
+ - (binary) L to R
< <=
> >= L
to R
= = != L
to R
& & L to R
| | L
to R
= R
to L
Conditional Operators (ternary Operators)
The
conditional expression is represented by the? and: symbols. It is also called
ternary operator because it operates upon three values. The form of conditional
expression is
e1? e2:e3
If the value
e1 is true, the value e2 is returned by the operator? : If e1 evaluates false,
the value returned is that of e3.
E.g. j =
(i>4)?200:100
If the value of
i is 5. Since the expression (I>4) evaluates to true, the value 200(before
colon) is returned by the? : Operator and so assigned to j. If the value is say
3, the expression (i>4) evaluates false. Therefore 100 (after colon) is
returned and assigned to j.
i>4 ? 200 : 100
Comma Operator
The comma
operator is used to link the related expression together. A chain of comma in
an expression is evaluated from left to right and the value of right most
expression is the value of combined expression.
E.g
Value = (
x=10, y=5, x+y);
Bitwise Operator
These
operators operate on bit level. These can only be applied to integer operands
whether signed or unsigned.
Operator Operation performed
>> Right shift
<< Left shift
~ 1’s complement
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise Inclusive OR
Special Operators
Sizeof
operator
* and -> pointer
. and ->
structure and union
Statement
A statement is
a part of a program that can be executed. A statement causes the computer to
carry out some action. In C, there are five different classes of statements.
They are:-
- Expression statement
- Block statement
- Control statement
- Assignment statement
- Input/Output statement
- Expression statement
An
expression consists of an expression followed by a semi colon. The execution of
an expression statement causes the expression to be evaluated.
E.g.
++i;
Expression
in C can be classified as lvalues and rvalues. The lvalues refers to an
expression that has a location in memory. For example, the name of a variable
is an lvalues. Modifiable lvalues can be used on the left-hand side of an
assignment statement.
The
rvalue can be evaluated but cannot be changed. E.g. ‘5’ is rvalue. If a
variable x is previously declared, the expression x+3 is also rvalue. It is
used on right-hand side.
- Block statement
Block
statements are groups of related statements that are treated as a unit. The
statements that make up a block are logically bound together. Block statements
are also called compound statement.
A
block begins with a {and terminated by its matching}.
- Control statement
The
program makes a decision about what to do next.
The
program needs logical condition to determine if certain conditions are true or
false, they do require the repeated execution of groups of statements and they
involve the execution of individual groups of statements on a selective basis.
- Assignment statement
Statements
where results of calculation are stored. The value of the expression on the
right of the equal sign to be assigned to the variable on the left.
- Input/Output statement
Data
is read in or printed out.
0 Comments
Please Don't Post Spam Links Under Comment Section.