Questions and Answers of 2nd CPU Assignment are given below...
CPU - Computer Programming and Utilization | GECR | 1st SEM
Any Doubts or questions, please comment below.
List of Questions
(2)Explain C Tokens.
(3)Define following:
1.Variable 2.Identifier 3.Constant 4.Keyword
(4)Explain the basic structure of C program.
(5)List and explain fundamental data types of “C language
(6)What is Type conversion and Type casting? State difference between them.
(7)Explain printf and scanf with proper syntax.
(8)Explain getchar() and putchar() with proper example.
Answers
(1) Explain
different types of operators with examples
Sol: Arithmetic
Operators:
There are following arithmetic
operators supported by C language:
Assume variable A holds 10 and variable
B holds 20 then:
Operator
|
Description
|
Example
|
+
|
Adds two operands
|
A + B will give 30
|
-
|
Subtracts second operand from the
first
|
A - B will give -10
|
*
|
Multiply both operands
|
A * B will give 200
|
/
|
Divide numerator by denumerator
|
B / A will give 2
|
%
|
Modulus Operator and remainder of
after an integer division
|
B % A will give 0
|
++
|
Increment operator, increases integer
value by one
|
A++ will give 11
|
--
|
Decrement operator, decreases integer
value by one
|
A-- will give 9
|
Logical (or Relational) Operators:
There are following logical operators
supported by C languageAssume variable A holds 10 and variable B holds 20 then:
Operator
|
Description
|
Example
|
==
|
Checks if the value of two operands
is equal or not, if yes then condition becomes true.
|
(A == B) is not true.
|
!=
|
Checks if the value of two operands
is equal or not, if values are not equal then condition becomes true.
|
(A != B) is true.
|
>
|
Checks if the value of left operand
is greater than the value of right operand, if yes then condition becomes
true.
|
(A > B) is not true.
|
<
|
Checks if the value of left operand
is less than the value of right operand, if yes then condition becomes true.
|
(A < B) is true.
|
>=
|
Checks if the value of left operand
is greater than or equal to the value of right operand, if yes then condition
becomes true.
|
(A >= B) is not true.
|
<=
|
Checks if the value of left operand
is less than or equal to the value of right operand, if yes then condition
becomes true.
|
(A <= B) is true.
|
&&
|
Called Logical AND operator. If both
the operands are non zero then then condition becomes true.
|
(A && B) is true.
|
||
|
Called Logical OR Operator. If any of
the two operands is non zero then then condition becomes true.
|
(A || B) is true.
|
!
|
Called Logical NOT Operator. Use to
reverses the logical state of its operand. If a condition is true then
Logical NOT operator will make false.
|
!(A && B) is false.
|
Bitwise Operators:
Bitwise operator works on bits and perform
bit by bit operation.Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
Operator
|
Description
|
Example
|
&
|
Binary AND Operator copies a bit to
the result if it exists in both operands.
|
(A & B) will give 12 which is
0000 1100
|
|
|
Binary OR Operator copies a bit if it
exists in eather operand.
|
(A | B) will give 61 which is 0011
1101
|
^
|
Binary XOR Operator copies the bit if
it is set in one operand but not both.
|
(A ^ B) will give 49 which is 0011
0001
|
~
|
Binary Ones Complement Operator is
unary and has the efect of 'flipping' bits.
|
(~A ) will give -60 which is 1100
0011
|
<<
|
Binary Left Shift Operator. The left
operands value is moved left by the number of bits specified by the right
operand.
|
A << 2 will give 240 which is
1111 0000
|
>>
|
Binary Right Shift Operator. The left
operands value is moved right by the number of bits specified by the right
operand.
|
A >> 2 will give 15 which is
0000 1111
|
Assignment Operators:
Operator
|
Description
|
Example
|
=
|
Simple assignment operator, Assigns values
from right side operands to left side operand
|
C = A + B will assigne value of A + B
into C
|
+=
|
Add AND assignment operator, It adds
right operand to the left operand and assign the result to left operand
|
C += A is equivalent to C = C + A
|
-=
|
Subtract AND assignment operator, It
subtracts right operand from the left operand and assign the result to left
operand
|
C -= A is equivalent to C = C - A
|
*=
|
Multiply AND assignment operator, It
multiplies right operand with the left operand and assign the result to left
operand
|
C *= A is equivalent to C = C * A
|
/=
|
Divide AND assignment operator, It
divides left operand with the right operand and assign the result to left
operand
|
C /= A is equivalent to C = C / A
|
%=
|
Modulus AND assignment operator, It takes
modulus using two operands and assign the result to left operand
|
C %= A is equivalent to C = C % A
|
<<=
|
Left shift AND assignment operator
|
C <<= 2 is same as C = C
<< 2
|
>>=
|
Right shift AND assignment operator
|
C >>= 2 is same as C = C
>> 2
|
&=
|
Bitwise AND assignment operator
|
C &= 2 is same as C = C & 2
|
^=
|
bitwise exclusive OR and assignment
operator
|
C ^= 2 is same as C = C ^ 2
|
|=
|
bitwise inclusive OR and assignment
operator
|
C |= 2 is same as C = C | 2
|
Misc Operators
Operator
|
Description
|
Example
|
sizeof()
|
Returns the size of an variable.
|
sizeof(a), where a is interger, will
return 4.
|
&
|
Returns the address of an variable.
|
&a; will give actaul address of
the variable.
|
*
|
Pointer to a variable.
|
*a; will pointer to a variable.
|
? :
|
Conditional Expression
|
If Condition is true ? Then value X :
Otherwise value Y
|
(2) Explain
C Tokens.
Sol :
- C tokens are the basic
buildings blocks in C language which are constructed together to write a C
program.
- Each and every smallest individual units in a C program are known as C tokens.
- C tokens are of six types. They are,
- Keywords (eg: int, while),
- Identifiers (eg: main, total),
- Constants (eg: 10, 20),
- Strings (eg: “total”, “hello”),
- Special symbols (eg: (), {}),
- Operators
(eg: +, /,-,*)
C tokens example program:
int main()
{ int x, y, total; x = 10, y = 20; total = x + y; Printf (“Total = %d \n”, total); } . |
- main – identifier
- {,}, (,) – delimiter
- int – keyword
- x, y, total – identifier
- main, {, }, (, ), int, x, y, total – tokens
(3) Define
following:
(1)Variable (2)
Identifier (3) Constant (4) Keyword
Sol:
(1) a variable : is a storage location paired with an associated symbolic
name (an identifier), which contains some known or unknown quantity of
information referred to as a value.
(2) an identifier is a combination of alphanumeric characters, the first
being a letter of the alphabet or an underline, and the remaining being any
letter of the alphabet, any numeric digit, or the underline.
(3) In programming, a constant is a value that never changes.
A constant can be. a number, like 25 or 3.6. a character, like a or $.
(4) In programming, a keyword is a word that is reserved by a
program because the word has a special meaning. Keywords can be commands or
parameters. Every programming language has a set of keywords that cannot be
used as variable names.
(4)
Explain the
basic structure of C program.
A C program may contain one or more
sections. Basic structure of C program is given below.
1.
Documentation
section : The
documentation section consists of a set of comment lines giving the name of the
program, the author and other details, which the programmer would like to use
later.
2.
Link
section : The
link section provides instructions to the compiler to link functions from the
system library.
3.
Definition
section : The
definition section defines all symbolic constants.
4.
Global
declaration section : There
are some variables that are used in more than one function. Such variables are
called global variables and are declared in the global declaration section that
is outside of all the functions. This section also declares all the
user-defined functions.
5.
main ()
function section : Every
C program must have one main function section. This section contains two parts;
declaration part and executable part
6.
Declaration
part : The declaration
part declares all the variables used in the executable part.
7.
Executable
part : There is at
least one statement in the executable part. These two parts must appear
between the opening and closing braces. The program execution begins at the
opening brace and ends at the closing brace. The closing brace of the main
function is the logical end of the program. All statements in the declaration
and executable part end with a semicolon.
8.
Subprogram
section : The
subprogram section contains all the user-defined functions that are called in
the main () function.
User-defined functions are generally placed immediately after the main () function,
although they may appear in any order.
All section, except the main () function
section may be absent when they are not required.
(5)
List and
explain fundamental data types of “C language
Sol: C has the following basic built-in datatypes.
·
int
·
float
·
double
·
char
1.int
- data type
·
int is used to define integer numbers.
{
int
Count;
Count
= 5;
}
|
2.float
- data type
·
float is used to define floating point
numbers.
{
float
Miles;
Miles
= 5.6;
}
|
3.double
- data type
·
double is used to define BIG floating point
numbers. It reserves twice the storage for the number. On PCs this is likely to
be 8 bytes.
{
double
Atoms;
Atoms
= 2500000;
}
|
4.char
- data type
·
char defines characters.
{
char
Letter;
Letter
= 'x';
}
|
5.void
- data type
·
1.
To declare generic pointer
·
2.
As a function return type
·
3.
As a function parameter.
(6)
What is Type
conversion and Type casting? State difference between them.
Sol: Type Conversion is that which
converts to data type into another for example converting a int into float
converting a float into double.The Type Conversion is that which automatically converts the one data type into another but remember we can store a large data type into the other for example we can't store a float into int because a float is greater than int.
The type Conversion is performed by the compiler but a casting is done by the user for example converting a float into int
(7)
Explain
printf and scanf with proper syntax.
Sol:
1. C printf() function:
·
printf()
function is used to print the “character, string, float, integer, octal and
hexadecimal values” onto the output screen.
·
We
use printf() function with %d format specifier to display the value of an
integer variable.
·
Similarly
%c is used to display character, %f for float variable, %s for string variable,
%lf for double and %x for hexadecimal variable.
·
To
generate a newline,we use “\n” in C printf() statement.
·
Syntax : printf("user
defined message");
prinf("Format
specifers",value1,value2,..);
Note: C language is case sensitive. For
example, printf() and scanf() are different from Printf() and Scanf(). All
characters in printf() and scanf() functions must be in lower case.
2. Scan(f)
·
scanf() function is used to read
character, string, numeric data from keyboard.
·
Syntax : scanf("format
specifiers",&value1,&value2,.....);
·
Example of scanf function:
int a; float b;
scanf("%d%f",&a,&b);
int a; float b;
scanf("%d%f",&a,&b);
Format
specifier:
Format
specifier
|
Type
of value
|
%d
|
Integer
|
%f
|
Float
|
%lf
|
Double
|
%c
|
Single character
|
%s
|
String
|
%u
|
Unsigned int
|
%ld
|
Long int
|
%lf
|
Long double
|
(8)
Explain
getchar() and putchar() with proper example.
Sol:
-getch() and getchar() are used to read a
character from screen.
-putch() and putchar() are used to write a character to screen.
-putch() and putchar() are used to write a character to screen.
-getch() and putch() are non-standard
functions defined in conio.h, mostly used in turbo C/dev C++ environement.
getchar() are putchar() are standard functions defined in C standard and they
can be used in all environments.
a) getchar() Function:
This Function reads a single character
from the standard input device. There is no parameter within the parentheses.
Its syntax is:
char_var = getchar();
|
where char_var is a character type variable to which an accepted character is assigned.
Example:
main()
{
char letter;
letter = getchar();
}
b) putchar() Function:
This Function prints a single character
on the screen. The character to be displayed is of type char. Its syntax is:
putchar(ch_var);
|
where ch_var is a character variable which is enclosed within the parenthesis.
Example:
main()
{
char ch;
putchar(ch);
}
Stay Amazed | Stay Geeky | Jay Akbari | Pixmercy
THANK YOU!!!!!!!
ReplyDelete:-) :-) :-)
I have found that this site is very informative, interesting and very well written. keep up the nice high quality writing. Drugstore Dupes
ReplyDeleteI got here much interesting stuff. The post is great! Thanks for sharing it! HPAT Sample Paper
ReplyDelete