1627703042
1627703042
Fundamentals of C Language
Identifiers:
Identifiers: An identifiers is the name given to program elements such
as variables, functions and arrays, etc. Identifiers are also the names of
objects, which can take different values but only one value at a time.
Example: The following names are valid identifiers:
x y12 sum_1 _temperature
names area tax_rate TABLE
Tokens:
Cprogram has punctuation marks, keywords and operators as the
smallest individual units and are referred to as C tockens Following six
types of tockens are used in C language.
C tokens Exs
1) keywords auto, float etc
2) constants -23, 4,16 etc
3) identifiers sum, average, etc
4) string literals “Total marks”, “sum=” etc
5) operators +, -, * etc
6) separators ; , : “
constant
The constant is a value, written into a program instruction that
does not change during the execution of a program.
There are four basic types of constants in C. They are integer
constants, floating-point constants, character constants and string
constants.
data-types:
4
Variables:
A variable is a name that C language compiler associates with a
storage location in the main memory of the computer. A variable is an
identfifier that is used to represent some specified type of information
within a designated portion of a program.Variable holds data that can be
modified during program execution.
Every variable needs to be declared before it is used in a program.
A variable declaration consists of a data type followed by one or more
variable names, ending with a semicolon.
The general format of the variable declaration is:
data_type identifier 1,2, …, n
For example to declare j as an integer and x as a floating point variable,
we should write:
int j;
float x;
As a shorthand, we can declare j and k in a single line as:
int j, k;
which is the same as the declaration of j and k as:
int j;
int k;
Solution:
data-types: There are various types of data that a variable may contain
in a programming language. All such types are named as data type. The
basic data types are: int, char, float and double. The basic data types can
be augmented by the use of the data type qualifiers short, long, signed
and unsigned.
data-types: There are various types of data that a variable may contain
in a programming language. All such types are named as data type. The
basic data types are: int, char, float and double. The basic data types can
be augmented by the use of the data type qualifiers short, long, signed
and unsigned.
For example:
int a,b,c;
which state that a, b and c are integer variables.
For example: the following are the examples of valid string constants:
“The Total Marks=”
“Rs 2000.00”
“34.557”
Q. Name and describe the four basic data types in C with their memory
requirements.
Solution: There are various types of data that a variable may contain in a
programming language. All such types are named as Data Type.
C supports several different types of data, each of which may be
represented differently within computer‟s memory. The basic data types
are listed below. Typical memory requirements are also given.
word
char Single character 1 byte -128 127
-38
float Floating-point 1 word ( 4 3.4 X 10 3.4 X 1038
number (i.e. a bytes)
number containing a
decimal point and /or
an exponent
double Double-precision 2 words (8 1.7 X 10-308 1.7 X 10308
floating-point number bytes)
(i.e., more significant
figures, and an
exponent which may
be larger in
magnitude)
long Whole numbers 1 word ( 4 - 2,147,438,647
bytes) 2,147,438,648
long double Fractional numbers 10 bytes 3.4 X 10- 3.4 X 104932
4932
Arithmetic operators:
Operator Meaning
< Less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to
Q. Write down the logical operators available in C and show how they
work giving examples.
!i
This is true only if the value of i is zero.
Solution: The increment and decrement operators (there are two, a pre-
and a post- operator for each operation) are unary operators.
The post-increment operator ++ is written to the right of its operand:
n++;
The effect of the statement above is to add one to the current value of n;
its action is equivalent to:
n = n+1;
Besides being more concise, the post-increment operation on some
computers may be executed faster than this latter form of assignment.
13
Assignment operators:
Bitwise operators.
Additional operators:
Sizeof operator:
Comma ( , ) operator:
Solution:
Unary operator:
1. Operators that act on one operand are referred to as unary
operators.
2. Associativity of unary operators are from right to left.
3. Examples of unary operators are ++ and – called increment and
decrement operators, respectively, -(unary minus) one‟s
complement (~), Negation (!), Address of (&), value at address
(*), size in bytes (size of) etc.
Binary operator:
17
Ex: How does the type float differ from double in C language?
Solution: float data type refers real number in single precision and has 6
decimal digits. It takes 4 bytes in memory to refer values ranging from
3.4e-38 to 3.4e+38.
double data type also refers to real number but in double precision and
has 12 decimal digits. It takes 8 bytes of memory to refer values ranging
from 1.7e-308 to 1.7e+308.
Solution:
To store a fraction, we need a floating point type of data variable,
called data type float. It occupies 4 bytes in memory. It can hold numbers
in the range from –3.4e38 to +3.4e38 with six and seven digits of
precision.
A double data type can store a floating point value with a greater
accuracy than a float type. Double data type occupies 8 bytes in memory
and can hold numbers in the range from –1.7e308 to +1.7e308 with 15
digits of precision.
A variable of type double can be declared as:
double a, population;
A variable of type float can be declared as:
float a, population;
Operator Meaning
& bitwise AND
| bitwise OR
^ bitwise XOR
<< shift left
>> shift right
~ bitwise
complement
Left shift operator: The left bit-shift operator, “<<” is a binary operator.
For example consider the statement,
c = a << 3;
The value in the integer a is shifted to the left by three bit positions. The
result is assigned to the integer c. Since the value of a is 0000 0000 0000
20
1101 the value of c after the execution of the above statement is 0000
0000 0110 1000 (104 in decimal) and is illustrated below:
Left-Shift <<
drop off 0000 0000 0000 1101 insert 0‟s
The three left-most bits drop off due to the left shift (i.e., they are
not present in the result). Three zeros are inserted in the right. The effect
of shifting a variable to the left by one bit position is to multiply it by 2.
In the above example, a is shifted left by 3 positions, which is equivalent
to multiplying a by 2*2*2, i.e., 23. Since the initial value of a is 13, the
value assigned to c is 13 * 8 = 104.
While multiplying a number by a power of 2, considerable saving
in execution time can be achieved by using the left bit-shift operator
instead of the multiplication operator, since shifting is faster than
multiplication.
Right-Shift >>
The 2 right-most bits drop off ( are not present in the result), and
zeros are inserted in the left. The effect of shifting a variable to the right
by one bit position is to perform integer division by 2 (i.e., divide by 2
21
and truncate the result). Hence, shifting to the right by 2 bit positions has
the effect of integer division by 2*2 = 4. Since the initial value of a is 13,
shifting to the right by 2 bit positions yields the value 3 ( the result of
dividing 13 by 4 and truncating the result).
float x;
char c;
Determine the data type of each of the following expressions.
i) i+c
ii) x+c
iii) i+x
iv) j+x
Q. How can you determine the maximum value that a numeric variable
can hold?
printf(“%d\n”, „A‟);
Solution: The ASCII value 65 for character „A‟ will be printed.
&&&&&&&&&&