cs3251 UNIT I QBANK
cs3251 UNIT I QBANK
UNIT I
PART A
Local variables are declared within a specific block of code, such as a function or method, and
have limited scope and lifetime, existing only within that block.
Global variables are declared outside of any function and can be accessed from any part of the
program, persisting throughout its execution.
Example,
#include <stdio.h>
int x = 5; // global variable
int main() {
int y = 10; // local variable
return 0;
}
2. How many bytes are occupied by the int, char, float, long int and double?
U
int 4 %d
float 4 %f
double 8 %lf
char 1 %c
#include , #define
#undef , #ifdef
#ifndef, #if
#else, #elif
The ternary operator is a conditional operator that takes three operands: a condition, a
value to be returned if the condition is true, and a value to be returned if the condition is false.
It evaluates the condition and returns one of the two specified values based on whether the
condition is true or false.
#include <stdio.h>
int main()
{
int x = 10, y = 5;
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
6. Find the output for following C program.
AP
int main(){
int x = 20, y = 35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d n", x, y); }
output : 57 94
{x=y++ + x++;} :
x=y+x;//35+20
x++; //56
y++; //36
y=++y + ++x; :
++y;//37
++x;//57
y=y+x;//37+57
So x=57 y=94
Memory representation : Array elements are always stored in consecutive memory location
A[10] = 0 1 2 3 4 5 6 7 8 9
101 102 103 104 105 106 107 108 109 110
arr[0] arr[2] arr[6] arr[9]
lower bound upper bound
length of an array : upper bound – lower bound +1
= 9-0+1
=10
10. Write an example for compile time initialization of character arrays [strings]. AP
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[] = {'c', 'h', 'r', 'i', 's', 't', 'o', 'p', 'h', 'e', 'r'};
char name2[] = "christopher";
while (1){} Here 1 is a non zero, value so the condition is always true. So it is an infinite loop.
++a means do the increment before the operation (pre increment)a++ means do the increment
Example:
4. strcmp(s1,s2) Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater
than 0 if s1>s2.
The declaration is preceded by the word static. The initial values are enclosed in braces,
Example
One dimensional array can be used as database where the elements are the records.
Strings, stacks, queue, heaps and hash tables can be implemented using arrays.
Sorting of elements into ascending order and descending order can be performed using arrays.
Multi-dimensioned arrays have two or more index values which specify the element in the
array.
multi[i][j];
Declaration:
Here, the size of the data type int is 2.The value for w=2Address of salary[4] =1000+2(4-
0)=1000+8=1008
PART – B