What is a constant? Explain the constants in C.
Any unchanged value in a program during the program execution is called constant.
In C language, constants are divided into two types Numeric and String or character:
What is a variable? How are the variables declared in C?
A variable is an identifier or a name which is used to refer to a value and this value varies or changes during the program execution. Variables used in a C program are declared with appropriate datatypes along with their name.
What is an expression? What are the operators in C?
An expression consists of variable and constants separated by operators.
What is a library function? Mention its use.
Library functions are the built-in programs that are available with the compiler. It is used to perform standard mathematical operations.
eg. sqrt(), fabs(), log(), etc.
Convert the following mathematical expressions into C expressions.
i. (a + b) / (c + d)
ii. (a * b) / (pow(c, 2) + d) * g
iii. sqrt(1 + x) * (log(cos(2 * x)) / (1 + fabs(y)))
iv. z = exp(x) + log(y) + p * q * r * (s - t);
v. T = sin(a) * cos(b) - fabs(g - h) + sqrt(a * b);
vi. z * (alpha + beta) / sin(x * M_PI / 180) + (pow(a, b) + pow(c, d)) / (a + b)
vii. y = sin(omega * M_PI) * cos((omega * M_PI) / t);
Explain the program structure in C language.
<Header files>
<Global declaration of variables>
main()
{
<Local declaration of variables>
--------------
<Statements>
--------------
}
<Sub programs - function blocks>
1. The header files or preprocessor directives gives instructions to the compiler to include compiler options (#include), macro substitution (#define) to substitute a constant for an identifier and conditional (#ifdef) directives.
2. The main statement block, function block and other blocks used in C program are enclosed in braces {}.
3. Variables declared outside main() are called global variables, and they can be used in the main program block and sub program block.
4. Variables declared inside main() are called local variables, and they are used only in the block in which they are declared. Sub programs/functions can also have local variables.
5. Any C program has coding in the form of letters and symbols. Normally documentation to the program is made by adding remarks or comment lines enclosed in /* and */ whenever necessary.
Explain the different data types in C.
int
:
refers to integer. It can hold a signed or unsigned whole
number within specific range.
char
:
refers to character. It can hold one letter/symbol. In fact,
char in C language is associated with integers refers to a
letter/symbol as per ASCII which has assigned integer value
for all letters/symbol used in programming.
float
: refers to floating point or real number. It
can hold a real number like 3.174813 or 4.53e6 with six decimal
digits in decimal or exponential form.
double
: also refers to floating or real number. It
can hold a real number in double precision. A double precision
number uses 12 decimal digit like 3.42134421232 or
4.2324452423e12.
Discuss increment and decrement operators available on C and the rules associated with them.
Increment operator(++) is used to increase the value of an integer or char variable by 1. Decrement operator(--) is used to reduce the value of an integer or char by 1. m++ and m-- are referring the post-fix increment and decrement operation, and ++m and --m are referring the prefix increment and decrement operation.
Post-fix increment first assign the value of the variable then increment it. Whereas, the prefix increment first increase the value and then assign the value of variable.
Explain arithmetic and logical operators in C with suitable examples.
Arithmetic operators are used to perform arithmetic operations while assigning a value to a variable.
example: m += 10, this is evaluated as m = m + 10, it increase the value of m then assigns the new value of m to m.
Logical operators are used to connect more relational operations to form a complex expression called logical expression. A value obtained by evaluating a logical expression is always logical, i.e either true or false.
example: (5 > 2) && (5<7) = True, this check the both condition if 5 is greater than 2 and 5 is less than 7 if both are true then the result is also true.
Explain bitwise logical operators available in C. Discuss the order of evaluation.
Bitwise operators work on individual bits (0s and 1s) of integer values. They perform operations at the binary level.
Common bitwise operators:
&
(AND): 1 if both bits are 1|
(OR): 1 if at least one bit is 1^
(XOR): 1 if only one of the bits is 1~
(NOT): Flips all bits<<
(Left Shift): Shifts bits left>>
(Right Shift): Shifts bits rightThe order of evaluation follows operator precedence rules, with parentheses having the highest precedence.
List the different types of operators in C. Discuss the following with examples: (a) conditional operators (b) relational operators.
Types of operators in C:
(condition) ? expr1 : expr2
max = (a > b) ? a : b;
>, <, >=, <=, ==, !=
if (age >= 18) { printf("Adult"); }
_
(underscore).
(True/False)
long int
occupies _________ bytes
of memory.
float
differ from
double
in C language?