You are on page 1of 15

Q.

what is the output

Answer:

1. int y = 1; - Variable y is declared and initialized with the value 1.


2. int z; - Variable z is declared but not initialized. It has an indeterminate value.
3. if (z = y < 0) - The expression y < 0 is evaluated, which results in false
since y is 1. Then, the result false is assigned to z. So, z becomes 0 (false).
4. x = 2; - The value of x is updated to 2 since the condition in the if statement
was false.
5. printf("%d %d %d", x, y, z); - It prints the values of x, y, and z (2 1 0) using
the format specifier %d.
6. return 0; - The main() function returns 0, indicating successful program
execution.

So, the output of this code will be: 2 1 0.

Q. What are l and r values and give example of each

In computer programming, the terms "lvalue" and "rvalue" are used to classify
expressions based on their characteristics in relation to assignment operations.

An lvalue refers to an expression that represents a memory location or an object that


can be assigned a value. An lvalue can appear on the left side of an assignment operator
(=). Examples of lvalues include variables, array elements, and dereferenced pointers.
Here's an example of an lvalue:

int x = 5; // x is an lvalue

int* p = &x; // &x is an lvalue (address of x)

int arr[5]; // arr is an lvalue

arr[2] = 10; // arr[2] is an lvalue

On the other hand, an rvalue refers to an expression that produces a value but cannot
be directly assigned to. An rvalue can appear on the right side of an assignment
operator (=). Examples of rvalues include literal values, temporary values, and the result
of a function call.

Here's an example of an rvalue:

int a = 10; // 10 is an rvalue

int b = a + 5; // (a + 5) is an rvalue

int c = func(); // func() is an rvalue (result of function call)

int d = arr[2]; // arr[2] is an rvalue

Q. what is the output


1. res = a + (b = b + a); - The expression b = b + a is evaluated first,
updating the value of b to 5. Then, a + (b) is evaluated, and the result, 8, is
assigned to res.
2. printf("%d\n", res); - It prints the value of res (8) followed by a newline
character.
3. printf("%d\n", a & b); - It prints the result of the bitwise AND operation
between a and b (2) followed by a newline character.
4. printf("%d\n", a || b); - It prints the result of the logical OR operation
between a and b (1 for true) followed by a newline character.
5. printf("%d\n", a == b); - It prints the result of the equality comparison
between a and b (0 for false) followed by a newline character.
6. printf("%d\n", (a > b) ? a : b); - It prints the larger value between a and b
(5) followed by a newline character.

Q. what is the output

Int main(){

Printf(“%lu /n”, strlen(c\nprogramming!”);

Return 0;
}

Answer: The strlen function returns the length of a string, which is the number of characters
excluding the null character ('\0'). In this case, the string "c\nprogramming!" has 14
characters.

Note: ‘\n’ is counted as one character not two – it is the new line character.

q. write a function to concatenate two strings without using builtin strcat

Q. Fill the _todo_part

int main() {

int a[]={11,22,33,44,55};

int n = sizeof(a)/_todo_;

int i=0;

while(i<n)
{

Printf(“a[%d] is %d”’_todo_);

_todo_;

Return 0;

Answer:

Q. write function to subtract two 2D matrices of size N


Q. Fill in the blanks

Struct test{

Int I; float j;

Void read_data(struct teast *d){

Prontf(“Enter value of I”\n”);

Scanf(“%d,____);

Prontf(‘Enter value of j\n”);

Scanf(______);

Int main(){
Struct test t;

Read_data(&t);

Return 0;

Answer:

Q. Program to find sum of digits using recursion


Q. struct node{

Int data;

Struct node *next}

Write a program to display contents of a linked list of this node


Q. write a function to read and display contents of a file
Q. Comment o this program

int main(){

int i =50;

const int * p=&I;

*p = 90;

printf(“%d\n”, *p);

return 0;

Answer:
The code you provided has a few issues and logical errors. Let's analyze it step
by step:

cCopy code
int main() { int i = 50 ; const int * p = &i; *p = 90 ; printf ( "%d\n" , *p); return 0 ; }
1. int i = 50; - Declares an integer variable i and initializes it with the
value 50.
2. const int* p = &i;- Declares a pointer p to a constant integer and
assigns the address of i to p. This means p is pointing to a constant
integer.
3. *p = 90; - Tries to modify the value pointed to by p to 90. However,
since p is pointing to a constant integer (const int*), this line causes a
compilation error. You cannot modify the value of a constant integer
through a pointer to a constant integer.
4. printf("%d\n", *p); - Tries to print the value pointed to by p, which is
undefined behavior due to the attempted modification of a constant
integer in the previous line. This can result in unpredictable output or a
runtime error.
5. return 0; - The main() function returns 0, indicating successful program
execution.

Overall, the code tries to modify a constant integer using a pointer to a


constant integer, which is not allowed. Additionally, trying to print the value of
the modified pointer results in undefined behavior.

To fix this code, you can modify the original variable i directly without
involving a pointer to a constant integer.

Q. Explain the following code

Answer:
1. #define mul(x,y) x*y - This line is a preprocessor directive that defines a macro
named mul which takes two parameters x and y and performs their multiplication
using the * operator. Essentially, it replaces any instance of mul(x, y) in the code
with x * y.
2. #define MAX 10 and #define MIN 5 - These lines define macros for MAX and MIN with
the respective values of 10 and 5. They allow you to use these names as symbolic
constants that get replaced with their defined values during preprocessing.
3. #include <stdio.h> - This line includes the standard input/output library for using
the printf function.
4. printf("val: %d\n", mul(MAX+1, MIN-1)); - This line uses the printf function to print
the result of mul(MAX+1, MIN-1). At this point, MAX and MIN are still defined as 10 and
5, respectively. So, it will be replaced as printf("val: %d\n", (MAX+1) * (MIN-1));.
The values of MAX and MIN are substituted into the mul macro, and the
multiplication is performed resulting in (10+1) * (5-1), which evaluates to 11 * 4
or 44.
5. #define MAX 20 - This line redefines the macro MAX with a new value of 20.
6. printf("val: %d\n", mul(MAX+1, MIN-1)); - This line again uses the printf function
to print the result of mul(MAX+1, MIN-1). Now, MAX has been redefined as 20, so it
will be replaced as printf("val: %d\n", (MAX+1) * (MIN-1));. The values of MAX (20)
and MIN (5) are substituted into the mul macro, and the multiplication is performed
resulting in (20+1) * (5-1), which evaluates to 21 * 4 or 84.

The output of this code will be:

val : 44 val : 84
It's important to note that the redefinition of macros within a program can lead to confusion
and unexpected behavior. Redefining macros can change the behavior of subsequent code, and
it's generally best practice to avoid redefining macros unless necessary and clearly documented.

Q. can macros be redefined in code

Yes, macros can be redefined in code. In C, macros defined using the #define directive
can be redefined within the same scope or in subsequent code. When a macro is
redefined, the new definition replaces the previous one.

Here's an example to demonstrate the redefinition of macros:


OUTOUT

Q. What is output of below code

Answer:

In this code, the mistake lies in the attempt to initialize both t.a and t.b simultaneously
in the union initialization.

The correct behavior is that a union can only hold one value at a time, so initializing
both t.a and t.b together will not work as intended.

To clarify, unions provide a way to store different types of data in the same memory
space. When you assign a value to one member of a union, the value of other members
becomes undefined. In your code, when you initialize both t.a and t.b simultaneously, it
results in undefined behavior.

To fix the code and ensure predictable behavior, you should initialize or access only one
member of the union at a time.
If we change the code as below

1. The union test is defined as a union type containing two members: int a and char
b.
2. In the main function, a variable t of type union test is declared.
3. The value 10 is assigned to the a member using t.a = 10;.
4. The printf statement prints the value of t.a using %d format specifier and the
value of t.b using %c format specifier.
5. Since t is a union, the a and b members share the same memory space. Accessing
the b member after assigning a value to a is implementation-dependent and can
yield unpredictable results.

The corrected code ensures that only one member of the union is initialized ( t.a = 10;),
and the output will be the value of t.a followed by the interpretation of the shared
memory as a character (t.b).

You might also like