0% found this document useful (0 votes)
29 views8 pages

Unit 2

1. A variable is declared with a data type and initialized by assigning a value. Values can be assigned during declaration, with an assignment statement, or by user input. 2. Formatted input in C uses scanf() with a control string to read input according to data type into variables. 3. Bitwise operators perform operations on individual bits of data. Logical bitwise operators include &, |, and ^. Shift operators move bits left or right. The ~ operator inverts all bits.

Uploaded by

eltonlewis1025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

Unit 2

1. A variable is declared with a data type and initialized by assigning a value. Values can be assigned during declaration, with an assignment statement, or by user input. 2. Formatted input in C uses scanf() with a control string to read input according to data type into variables. 3. Bitwise operators perform operations on individual bits of data. Logical bitwise operators include &, |, and ^. Shift operators move bits left or right. The ~ operator inverts all bits.

Uploaded by

eltonlewis1025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit-2

1.Explain with example declaring, initializing and assigning values to the variable.
A variable is a data name that may be used to store a data value.
Declaration Of Variables
Variables must be declared before they are used. Declaration serves two purposes
I. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.
Primary Type Declaration
A variable can be used to store a value of any data type. The syntax for declaring a variable
is as follows:
data-type vl,v2, .... vn;
vl, v2, ....vn are the names of variables. Variables are separated by commas. A declaration
statement must end with a semicolon. Some valid declarations are:
int count ;
int number, total;
double ratio;
int and double are the keywords to represent integer type and real type data values
respectively. Table shows various data types and their keywords.
Note that the qualifiers short, long, or unsigned are by default int. If we want to declare
a character variable as unsigned, then we must use both the terms like unsigned char.
Assigning values to the variables:
Assigning values can be done in three ways- They are : Using assignment statement,
Initialization and inputting using scanf
Assignment statement : Values can be assigned to variables using the assignment
operator = as follows :
variable_name=constant;
Examples : initial_value=0;
Balance=78.23;
C permits multiple assignments in one line. For example, the following is valid.
initial_value=0; Balance=78.23;
An assignment statement calculates the value of the expression on the right hand side
and assigns it to the variable on the left hand side. Hence following are valid :
ch=ch+32; fact=fact*i;
Initialization : It is the process of assigning values to variables at the time of declaration.
The general form is :
data-type variable_name=constant;
Examples : int final_value=100; float bal=23.67;
C permits initialization of more than one variable in one statement Example :
int p=q=r=10;
External variables are initialized to zero by default. Local variables that are not initialized
contain garbage value.
Reading data from keyboard :
Scanf is used to read data from keyboard. It has the following general form :
scanf(“control string”,&variable1, &variable2,….);
Example
/*program for assigning value of variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2;
float a, b;
chat ch;
clrscr() ;
num1=10;
num2=2000;
a=87.56;
b=34567.21367;
ch='M';
printf("num1=%d\n", num1);
printf("num2=%d\n", num2);
printf("a=%f\n",a);
printf("b=%5.2f\n", b);
printf("ch=%c\n", ch);
getch();
}

2. Explain with example the formatted input in C.


Formatted input refers to an input data that has been arranged in a particular format.
For example, consider the following data contains three pieces of data, arranged in a
particular form such as 15.73 123 John
Such data has to be read conforming to the format of its appearance, that is, the
first part of the data should be read into a variable float, the second into int, and the
third part into char. This is possible in C using the scanf function. The general form of
scanf is
scanf("control string", arg1, arg2, .......argn);
The control string specifies the field format in which the data is to be entered and
the arguments arg1 ,arg2, .....argn specify the address of locations where the data is
stored . Control string and arguments are separated by commas.
Control string includes field (or format ) specifications, consisting of the conversion
character %, a data type character (or type specifier), and an optional number, specifying
the field width. Blanks, tabs, or new lines are ignored.

3. Write a note on bitwise operator with example.


C supports special operators known as bitwise operators for manipulation of data at
bit level. A bitwise operator operates on each bit of data. Those operators are used for
testing bits, complementing or shifting bits to the right or left. Bitwise operators cannot
be applied to a float or double. The bit wise operators can be divided into :
▪ The logical bit wise operators.
▪ The shift operators
▪ The one’s complement operators.
Bitwise Logical Operators : There are 3 logical bit wise operators. They are:
- Bit wise AND(&) - Bit wise OR(|) -Bit wise exclusive OR(^) or (XOR)
These are binary operators and require two integer type operands.
Bit wise AND
The bit wise AND operator is represented by a single ampersand (&) and is surrounded
on both sides by integer expressions. The result of anding operation is 1if the both
the bits have a value of 1; otherwise it is 0.
Bit wise OR
The bit wise OR is represented by the symbol | (vertical bar) and is surrounded by two
integer operands. The result of OR operation is 1 if at least one of the bits has a value
of 1; otherwise it is zero.
Bit wise Exclusive OR (XOR)
The bit wise exclusive of is represented by the symbol ^. The result of exclusive OR is1
if only one of the bits is 1; otherwise it is 0.
Bitwise Shift operators
The shift operators are used to move bit patterns either to the left or to the right. Shift
operators are represented by the symbols << and >> and are used in the following
form:
Left Shift: op << n
Right Shift: op >> n
Op is the integer expression that is to be shifted and n the number of bit positions to
be shifted.
The bitwise ones Complement operator (~)
It is a unary operator that causes the bits of its operand to be inverted i.e. 1s becomes
0s and 0s becomes 1s. This operator always precedes its operand. The operand must be
an integer type quantity (including integer, long, short, unsigned, or char). This operator is
often combined with the bit wise AND operator to turn off a particular bit.

4. Explain token in C.
C Tokens
In a passage of text, individual words
and punctuation marks are called
tokens. Similarly, in a c program the
smallest individual units are knownas
C tokens. C has six types of tokens as
shown in Fig. C programs are written using these tokens and the syntax of the language.

5. Explain the concept of type conversion in C with example.


Automatic or Implicit Type Conversion
C permits the mixing of constants and variables of different types in an expression. C
automatically converts any intermediate values to proper type so that the expression
can be evaluated without losing any significance. This automatic conversion is known
as implicit type conversion.
If the operands are of different types, the lower type is automatically converted to higher
type before the operation proceeds. The result is of the higher type.
The final result of an expression is converted to the type of the variable on the left of
the assignment sign before assigning value to it. The following changes are introduced
during final assignment :
● Float to double causes truncation of fractional part
● Double to float converts rounding of digits
● Long int to int causes dropping of higher order bits
Explicit conversion (Casting a Value)
C performs type conversion automatically. However, there are instances when we
want to force a type conversion in a way that is different from the automatic
conversion. Consider for example, the calculation of ratio of females to males in a town.
ratio = female_number / male_number;
Since female_number and male_number are declared as integers the ratio would
represent a wrong figure. Hence it should be converted to float.
ratio = (float) female_number / male_number;
The operator (float) converts the female_number to floating point for the purpose of
evaluation of the expression. Then using the rule of automatic conversion, the division
is performed in floating point mode, thus retaining the fractional part of result.
Note that in no way does the operator (float) affect the value of the variable
female_number. And also, the type of female_number remains as int in the other parts
of the program.
The process of such a local conversion is known as explicit type conversion or casting
a value. The general form of a cast is: (type-name)expression where type-name is one of the
standard C data types. The expression may be a constant,variable or an expression.

6. What is ternary operator explain with example.


A ternary operator pair “?:” is available in C to construct conditional expression of the
form: exp1 ? exp2 : exp3;
Here exp1 is evaluated first. If it is true then the expression exp2 is evaluated and
becomes the value of the expression. If exp1 is false then exp3 is evaluated and its
value becomes the value of the expression. For example, suppose a = 10; b = 15;
x = (a > b) ? a : b ;
Here x will be assigned to the value of b.
/*odd or even using Conditional operators*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter an integer number :");
scanf("%d",&num);
num%2==0?printf("Even"):printf("Odd");
getch();
}
7. What is an identifier. Write the rules for identifier.
Identifiers : refer to the names of variables, functions and arrays. These are user- defined
names and consist of a sequence of letters and digits, with a letter as a first character.
Both uppercase and lowercase letters are permitted, although lowercaseletters are
commonly used. The underscore character is also permitted in identifiers. It is usually
used as a link between two words in long identifiers.
Rules for identifiers
● First char must be alphabet or underscore.
● Must consist of only letters, digits or underscore.
● Only first 31 characters are significant.
● Cannot use a keyword.
● Must not contain white space.

8. Explain operator precedence in C with example.


Each operator in C has precedence associated to it. This precedence is used to determine
how an expression involving more than one operator is evaluated. Thereare distinct
levels of precedence and an operator belongs to one of these levels. The operators of
higher precedence are evaluated first.
The operators of same precedence are evaluated from either ‘left to right’ or ‘right to
left’ depending on their level. This is known as associativity property of an operator.It is very
important to note carefully, the order of precedence and associativity of
operators. Consider the following conditional statement:
if (x == 10 + 15&&y< 10)
The precedence rules say that the addition operator has a higher priority than the
logical operator (&&) and the relational operators (== and <). Therefore, the additionof
10 and 15 is executed first. This is equivalent to:
If (x == 25&&y<10)
The next step is to determine whether x is equal to 25 and y is less than 10. If we
assume a value of 20 for x and 5 for y, then
x = = 25 is FALSE (0)
Y < 10 is TRUE (1)
Note that since the operator < enjoys a higher priority compared to ==, y < 10 is
tested first and then x == 25 is tested.
Finally we get: If (FALSE && TRUE)
Because one of the conditions is FALSE, the complex condition is FALSE.
In the case of &&, it is guaranteed that the second operand will not be evaluated if the first is
zero and in the case of ||, the second operand will not be evaluated if the
first is non-zero.
Rules of Precedence and Associativity
(1)Precedence rules decides the order in which different operators are applied.
(2)Associativity rule decide the order in which multiple occurrences of the same level
operator are applied.

9. Explain the basic data type in C with example.


C language is rich in its data types. The variety of data types allow the programmer to
select the type appropriate to the needs of the application. C supports three classes of
data types:
1. Primary (or fundamental) data types
2. Derived data types
3. User-defined data types
Derived data types include arrays, functions, structures and pointers. Users can create
their own data types using typedef and enum.
Primary datatypes
All C compilers support five fundamental data types, namely integer (int), character
(char), floating point (float), double-precision floating point (double) and void. They
also offer extended data types such as long int and long double.
Data Type Range of values
char -128 to 127
int -32,768 to 32,767
float 3.4e-38 to 3.4e+38
double 1.7e-308 to 1.7e+308
Integer Types
Integers are whole numbers and occupy one word of storage. Since the word sizes of
machines vary (16 or 32 bits) the size of an integer depends on the computer. If we use
a 16 bit word length, the size of the integer value is limited to the range -32768 to 32767
A signed integer uses one bit for sign and 15 bits for the magnitude of the number.
Similarly, a 32 bit word length can store an integer ranging from -2,147,483,648 to
2,147,483,647.
To provide control over the range of numbers and storage space, C has three classes of
integer storage, namely short int, int, and long int, in both signed and unsigned forms.
Short int represents small integer values and requires half the amount of storage as a
regular int number uses.
Unlike signed integers, unsigned integers use all the bits for the magnitude of thenumber
and are always positive. Therefore, for a 16 bit machine, the range of unsigned integer
numbers will be from 0 to 65,535.
Long and unsigned integers are used to increase the range of values. The use of qualifier
signed on integers is optional because integers are signed by default. Table shows basic
types with qualifiers and their size and range on a 16-bit machine.
Floating Point Types
Floating point (or real) numbers are stored in 32 bits with 6 digits of precision. Floating
point numbers are defined in C by the keyword float. When the accuracy provided by
a float number is not sufficient, the type double can be used to define the number.
A double data type number uses 64 bits giving a precision of 14 digits. These are known
as double precision numbers. To extend the precision further, we may use long double
which uses 80 bits.
Void Types
The void type has no values. This is usually used to specify the type of function. A function
is said to be void when it does not return any value to the calling function. Void can also
be used to specify a generic type that can represent any of the other standard types.
Character Types
A single character can be defined as a character (char) type data. Characters are usually
stored in 8 bits (one byte) of internal storage. Qualifiers signed or unsigned can be
applied to char. The range of unsigned chars is between 0 and 255 and of signed chars
is from - 128 to 127.
10. Explain the relational operator with example.
Relational operators are used to compare two operands, and depending on their relation,
certain decisions are taken. For example, it can be used to compare the ageof two
persons, price of two items and so on. There are 6 types of relational operators. They are:
Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
A simple relational expression contains only one relational operator and takes the
following form:
ae-1 relational operator ae-2
where ae-1 and ae-2 are arithmetic expressions, which may be simple constants,
variables or combination of them. Some examples of relational expressions and their
evaluated values are listed below:
3.5 <= 12 TRUE
-6 > 0 FALSE
10 < 7 + 5 TRUE
• When arithmetic expressions are used on either side of a relational operator, the
arithmetic expressions will be evaluated first and then the results are compared.
• Relational expressions are used in decision making statements of C language such
as if, while and for statements to decide the course of action of a running program.
/*example for relational operator*/
#include<stdio.h>
#include<conio.h>
void main()
{
int x=12, y=13;
clrscr();
printf("x=%d\n", x);
printf("y=%d\n\n", y);
//x is greater than y
printf("x>y:%d\n", x>y);
//x is greater than or equal to y
printf("x>=y:%d\n", x>=y);
//x is smaller than y
printf("x<y:%d\n", x<y);
//x is smaller than or equal to y
printf("x<=y:%d\n", x<=y);
//x is equal to y
printf("x==y:%d\n", x==y);
//x is not equal to y
printf("x!=y:%d\n", x!=y);
getch();
}
11. Difference between getchar and putchar.

Getchar Putchar

Getchar is used to Putchar is used to


read a single character. output the values of
character variables.

It can be used Putchar can be used


repeatedly to read a repeatedly to output a
sequence string of characters
of successive single stored in an array
characters and store it using a loop.
in the array.

You might also like