C Programs and their outputs

C Programs and their outputs

Question 1:

#include<stdio.h>
int main() {
    printf("%d", sizeof('a'));
    return 0;
}

Explanation: The sizeof operator in C returns the size in bytes of a variable or a type. In this case, 'a' is a character constant, and its size is determined by the compiler. Typically, a character occupies 1 byte of memory. Therefore, the output will be 1.

Question 2:

#include<stdio.h>
int main() {
    int x = 5;
    printf("%d", sizeof(x++));
    return 0;
}

Explanation: The sizeof operator is evaluated at compile-time, and it doesn't evaluate the expression itself. The expression x++ is not executed, and its side effect (incrementing x) doesn't occur. The sizeof operator only determines the size of the expression's type. As x is an int, the output will be the size of an integer, which is typically 4 bytes.

Question 3:

#include<stdio.h>
int main() {
    int a = 5, b = 10;
    printf("%d", ++a * b++);
    return 0;
}

Explanation: The expression ++a * b++ involves both pre-increment and post-increment operators. However, the order of evaluation of operands is not defined in C. It is compiler-dependent. Therefore, the result may vary. Some compilers may evaluate ++a first, resulting in 6, and then multiply it by 10. Others may evaluate ++a * b++ from left to right, resulting in 5 * 10, which is 50. The output may vary depending on the compiler.

Question 4:

#include<stdio.h>
int main() {
    char str[] = "Hello";
    printf("%d", sizeof(str));
    return 0;
}

Explanation: The sizeof operator, when used with an array, returns the total size in bytes occupied by the array. The array str has 6 characters ('H', 'e', 'l', 'l', 'o', '\0'), so the total size will be 6 bytes. Therefore, the output will be 6.

Question 5:

#include<stdio.h>
int main() {
    int a = 0;
    if (a)
        printf("Hello");
    else
        printf("World");
    return 0;
}

Explanation: The condition if (a) checks if the value of a is non-zero. Since a is 0, the condition evaluates to false. Therefore, the code inside the else block will execute, and "World" will be printed. The output will be "World".

Question 6:

#include<stdio.h>
int main() {
    int i = 0;
    for (; i < 5; i++) {
        printf("%d ", i);
        i = i + 1;
    }
    return 0;
}

Explanation: The for loop initializes i to 0. The condition i < 5 is true as long as i is less than 5. Inside the loop, i is incremented by 1 using both i++ and i = i + 1. Therefore, the loop will execute 5 times with i taking the values 0, 2, 4, 6, and 8. The output will be "

0 2 4 6 8 ".

Question 7:

#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3};
    int *p = arr;
    printf("%d", ++*p);
    return 0;
}

Explanation: The expression ++*p dereferences the pointer p and increments the value it points to. Therefore, it increments the value at arr[0], which is 1, to become 2. The output will be 2.

Question 8:

#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3};
    int *p = arr;
    printf("%d", *++p);
    return 0;
}

Explanation: The expression *++p increments the pointer p to point to the next element in the array and then dereferences it. Therefore, it dereferences arr[1], which is 2. The output will be 2.

Question 9:

#include<stdio.h>
int main() {
    int arr[5] = {1};
    printf("%d", arr[4]);
    return 0;
}

Explanation: The array arr is initialized with the first element as 1, and the remaining elements are implicitly initialized to 0. Therefore, arr[4] will have the value 0. The output will be 0.

Question 10:

#include<stdio.h>
int main() {
    int x = 10;
    int *p = &x;
    printf("%d", *p++);
    return 0;
}

Explanation: The expression *p++ increments the pointer p to point to the next memory location and then dereferences the original value. Therefore, it dereferences x, which is 10. The output will be 10.

Question 11:

#include<stdio.h>
int main() {
    int x = 5;
    printf("%d", sizeof(x > 3 ? x++ : x--));
    return 0;
}

Explanation: The ternary operator x > 3 ? x++ : x-- evaluates the condition x > 3. Since x is 5, the condition is true. Therefore, the expression evaluates to x++. The sizeof operator determines the size of the expression's type, which is an int. So the output will be the size of an integer, typically 4 bytes.

Question 12:

#include<stdio.h>
int main() {
    int x = 5;
    int y = (x++, ++x);
    printf("%d", y);
    return 0;
}

Explanation: The expression (x++, ++x) involves the comma operator. It evaluates the sub-expressions from left to right and returns the value of the rightmost expression. The increment operators x++ and ++x both modify the value of x. The final value of x will be 7. Therefore, y will be assigned the value 7. The output will be 7.

Question 13:

#include<stdio.h>
int main() {
    int i = 1;
    int j = i++ + i++;
    printf("%d", j);
    return 0;
}

Explanation: The expression i++ + i++ involves multiple increments of i. The order of evaluation of sub-expressions is not defined in C. Therefore, the result may vary. Some compilers may evaluate `i++

  • i++` from left to right, resulting in 1 + 2, which is 3. Others may evaluate it differently. The output may vary depending on the compiler.

Question 14:

#include<stdio.h>
int main() {
    int i = 1;
    int j = ++i + ++i;
    printf("%d", j);
    return 0;
}

Explanation: The expression ++i + ++i involves multiple increments of i. The order of evaluation of sub-expressions is not defined in C. Therefore, the result may vary. Some compilers may evaluate ++i + ++i from left to right, resulting in 2 + 3, which is 5. Others may evaluate it differently. The output may vary depending on the compiler.

Question 15:

#include<stdio.h>
int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int *p = arr;
    printf("%d", *(p + 3));
    return 0;
}

Explanation: The expression *(p + 3) adds 3 to the pointer p to point to the 4th element in the array, which is 4. Then it dereferences that memory location to obtain the value. The output will be 4.

Question 16:

#include<stdio.h>
int main() {
    int x = 10;
    int *p = &x;
    *p = 5;
    printf("%d", x);
    return 0;
}

Explanation: The pointer p is assigned the memory address of x using the address-of operator &. The expression *p = 5 dereferences p and assigns the value 5 to the memory location it points to, which is x. Therefore, the value of x is modified to 5. The output will be 5.

Question 17:

#include<stdio.h>
int main() {
    int x = 5;
    int *p = &x;
    printf("%p", p);
    return 0;
}

Explanation: The pointer p is assigned the memory address of x using the address-of operator &. The %p format specifier is used to print the memory address. The output will be the memory address of x, which will be different on each run.

Question 18:

#include<stdio.h>
int main() {
    int x = 10;
    int *p = &x;
    int **q = &p;
    printf("%d", **q);
    return 0;
}

Explanation: The pointer p is assigned the memory address of x using the address-of operator &. The pointer q is assigned the memory address of p. The expression **q first dereferences q to obtain p and then dereferences p to obtain the value stored at the memory location it points to, which is x. The output will be 10.

Question 19:

#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3};
    int *p = arr;
    printf("%d", *(p + 2));
    return 0;
}

Explanation: The expression *(p + 2) adds 2 to the pointer p to point to the 3rd element in the array, which is 3. Then it dereferences that memory location to obtain the value. The output will be

Question 20:

#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3};
    int *p = arr;
    printf("%d", *p++);
    return 0;
}

Explanation: The expression *p++ first dereferences p to obtain the value at the memory location it points to, which is arr[0] with the value 1. Then it increments p to point to the next memory location. However, the result of the expression *p++ is discarded in this case. Therefore, only 1 will be printed. The output will be 1.

Question 21:

#include<stdio.h>
int main() {
    int x = 10;
    int *p = &x;
    printf("%d", ++*p);
    return 0;
}

Explanation: The expression ++*p dereferences the pointer p and increments the value it points to. Therefore, it increments the value at x, which is 10, to become 11. The output will be 11.

Question 22:

#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3, 4};
    int *p = arr + 2;
    printf("%d", *(p - 1));
    return 0;
}

Explanation: The expression arr + 2 adds 2 to the pointer arr, which points to the first element of the array. Therefore, p will point to the third element of the array, which is 3. The expression *(p - 1) subtracts 1 from p to point to the second element of the array, which is 2. Then it dereferences that memory location to obtain the value. The output will be 2.

Question 23:

#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3, 4};
    int *p = arr + 1;
    printf("%d", p[1]);
    return 0;
}

Explanation: The expression arr + 1 adds 1 to the pointer arr, which points to the first element of the array. Therefore, p will point to the second element of the array, which is 2. The expression p[1] accesses the memory location p + 1 and retrieves the value stored there, which is the third element of the array, 3. The output will be 3.

Question 24:

#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3, 4};
    printf("%d", sizeof(arr));
    return 0;
}

Explanation: The sizeof operator, when used with an array, returns the total size in bytes occupied by the array. The array arr has 4 elements, and each element is an integer. Therefore, the total size will be 4 * sizeof(int). The output will be the size of an integer multiplied by 4, typically 16 bytes.

Question 25:

#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3, 4};
    printf("%d", sizeof(arr) / sizeof(arr[0]));
    return 0;
}

Explanation: The expression sizeof(arr) returns the total size in bytes occupied by the array arr. The expression sizeof(arr[0]) returns the size in bytes of a single element in

the array. Therefore, sizeof(arr) / sizeof(arr[0]) gives the number of elements in the array. In this case, it will be 4. The output will be 4.

Question 26:

#include<stdio.h>
int main() {
    char str[10];
    printf("%d", sizeof(str));
    return 0;
}

Explanation: The sizeof operator, when used with an array, returns the total size in bytes occupied by the array. In this case, the array str has a capacity of 10 characters. Therefore, the total size will be 10 bytes. The output will be 10.

Question 27:

#include<stdio.h>
int main() {
    char str[] = "Hello";
    printf("%d", strlen(str));
    return 0;
}

Explanation: The strlen function from the string.h library returns the length of a null-terminated string. In this case, the string "Hello" has 5 characters, excluding the null character '\0'. Therefore, the output will be 5.

Question 28:

#include<stdio.h>
int main() {
    char str[] = "Hello";
    printf("%d", sizeof(str) / sizeof(str[0]));
    return 0;
}

Explanation: The expression sizeof(str) returns the total size in bytes occupied by the array str. The expression sizeof(str[0]) returns the size in bytes of a single element in the array. Therefore, sizeof(str) / sizeof(str[0]) gives the number of elements in the array. In this case, it will be 6, which includes the null character '\0'. The output will be 6.

Question 29:

#include<stdio.h>
int main() {
    char str[] = "Hello";
    printf("%c", str[5]);
    return 0;
}

Explanation: The array str contains the string "Hello" with 5 characters. Arrays in C are zero-indexed, so the index range for str is 0 to 4. Accessing str[5] goes beyond the array bounds and accesses memory that may not belong to the array. The behavior is undefined. It may result in garbage values or a segmentation fault.

Question 30:

#include<stdio.h>
int main() {
    char str[] = "Hello";
    printf("%c", str[4]);
    return 0;
}

Explanation: The array str contains the string "Hello" with 5 characters. Arrays in C are zero-indexed, so the index range for str is 0 to 4. Accessing str[4] retrieves the character 'o'. The output will be 'o'.

Question 31:

#include<stdio.h>
int main() {
    int a = 5, b = 7;
    if (a == b)
        printf("Equal");
    else if (a > b)
        printf("Greater");
    else
        printf("Smaller");
    return 0;
}

Explanation: The code compares the values of a and b using the if-else construct. Since a is less than b, the condition a > b evaluates to false. Therefore, the else block will execute, and "Smaller" will be printed. The output will be "Smaller".

Question 32:

#include<stdio.h>
int main() {
    int x = 5, y = 10;
    if (x > 0 && y > 0)
        printf("Both positive");


 else if (x > 0 || y > 0)
        printf("At least one positive");
    else
        printf("Both non-positive");
    return 0;
}

Explanation: The code checks the values of x and y using logical operators. Since both x and y are positive (greater than 0), the condition x > 0 && y > 0 evaluates to true. Therefore, the first if block will execute, and "Both positive" will be printed. The output will be "Both positive".

Question 33:

#include<stdio.h>
int main() {
    int x = 5;
    if (x > 0) {
        int y = 10;
        printf("%d", y);
    }
    printf("%d", y);
    return 0;
}

Explanation: The code declares a variable y inside the if block. This variable has block scope, meaning it is only accessible within the block it is declared in. Therefore, the first printf statement inside the if block will print the value of y, which is 10. However, the second printf statement outside the if block will cause a compilation error because y is out of scope. The code will not compile.

Question 34:

#include<stdio.h>
int main() {
    int x = 5;
    if (x > 0) {
        int y = 10;
        printf("%d", y);
    }
    else {
        int z = 15;
        printf("%d", z);
    }
    printf("%d", z);
    return 0;
}

Explanation: The code declares a variable y inside the if block and a variable z inside the else block. Both variables have block scope, meaning they are only accessible within their respective blocks. The first printf statement inside the if block will print the value of y, which is 10. The second printf statement inside the else block will print the value of z, which is 15. However, the third printf statement outside both blocks will cause a compilation error because z is out of scope. The code will not compile.

Question 35:

#include<stdio.h>
int main() {
    int x = 5;
    if (x > 0) {
        int y = 10;
        printf("%d", y);
    }
    else {
        int y = 15;
        printf("%d", y);
    }
    printf("%d", y);
    return 0;
}

Explanation: The code declares a variable y inside both the if block and the else block. Both variables have block scope, meaning they are only accessible within their respective blocks. The first printf statement inside the if block will print the value of y, which is 10. The second printf statement inside the else block will print the value of y, which is 15. However, the third printf statement outside both blocks will cause a compilation error because y is out of scope. The code will not compile.

Question 36:

#include<stdio.h>
int main() {
    int i;
    for (i = 0; i < 5; i++) {
        printf("%d ", i);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4. In each iteration, it prints the value of i followed by a space. Therefore, the output will be "0 1 2 3 4 ".

Question 37:

#include<stdio

.h>
int main() {
    int i;
    for (i = 0; i < 5; ++i) {
        printf("%d ", i);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4. In each iteration, it prints the value of i followed by a space. The expression ++i increments i before using its value in the loop. Therefore, the output will be "0 1 2 3 4 ".

Question 38:

#include<stdio.h>
int main() {
    int i;
    for (i = 0; i < 5; i += 2) {
        printf("%d ", i);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4 with a step size of 2. In each iteration, it prints the value of i followed by a space. Therefore, the output will be "0 2 4 ".

Question 39:

#include<stdio.h>
int main() {
    int i;
    for (i = 5; i >= 0; i--) {
        printf("%d ", i);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 5 to 0. In each iteration, it prints the value of i followed by a space. Therefore, the output will be "5 4 3 2 1 0 ".

Question 40:

#include<stdio.h>
int main() {
    int i = 0;
    while (i < 5) {
        printf("%d ", i);
        i++;
    }
    return 0;
}

Explanation: The code uses a while loop to iterate as long as the value of i is less than 5. In each iteration, it prints the value of i followed by a space and then increments i. Therefore, the output will be "0 1 2 3 4 ".

Question 41:

#include<stdio.h>
int main() {
    int i = 0;
    do {
        printf("%d ", i);
        i++;
    } while (i < 5);
    return 0;
}

Explanation: The code uses a do-while loop to iterate as long as the value of i is less than 5. In each iteration, it prints the value of i followed by a space and then increments i. Therefore, the output will be "0 1 2 3 4 ".

Question 42:

#include<stdio.h>
int main() {
    int i;
    for (i = 0; i < 5; i++) {
        if (i == 3)
            continue;
        printf("%d ", i);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4. In each iteration, it checks if i is equal to 3 using the if statement. If i is equal to 3, the continue statement is executed, which skips the current iteration and moves to the next one. Therefore, the number 3 will not be printed. The output will be "0 1 2 4 ".

Question 43:

#include<stdio.h>
int main() {
    int i;
    for (

i = 0; i < 5; i++) {
        if (i == 3)
            break;
        printf("%d ", i);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4. In each iteration, it checks if i is equal to 3 using the if statement. If i is equal to 3, the break statement is executed, which terminates the loop. Therefore, when i becomes 3, the loop will be terminated and the number 3 will not be printed. The output will be "0 1 2 ".

Question 44:

#include<stdio.h>
int main() {
    int i, j;
    for (i = 0, j = 5; i < j; i++, j--) {
        printf("%d %d ", i, j);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4 and j from 5 to 1. In each iteration, it prints the values of i and j followed by a space. Therefore, the output will be "0 5 1 4 2 3 ".

Question 45:

#include<stdio.h>
int main() {
    int i;
    for (i = 0; i < 5; i++) {
        printf("%d ", i);
        if (i == 2)
            break;
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4. In each iteration, it prints the value of i followed by a space. It also checks if i is equal to 2 using the if statement. If i is equal to 2, the break statement is executed, which terminates the loop. Therefore, when i becomes 2, the loop will be terminated and the numbers 3 and 4 will not be printed. The output will be "0 1 2 ".

Question 46:

#include<stdio.h>
int main() {
    int i;
    for (i = 0; i < 5; i++) {
        printf("%d ", i);
        if (i == 2)
            continue;
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4. In each iteration, it prints the value of i followed by a space. It also checks if i is equal to 2 using the if statement. If i is equal to 2, the continue statement is executed, which skips the current iteration and moves to the next one. Therefore, when i becomes 2, the number 2 will be printed, but the loop will continue to execute until i becomes 4. The output will be "0 1 2 3 4 ".

Question 47:

#include<stdio.h>
int main() {
    int i;
    for (i = 0; i < 5; i++) {
        if (i % 2 == 0)
            printf("%d ", i);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4. In each iteration, it checks if i is divisible by 2 (i %

2 == 0) using the if statement. If i is divisible by 2, it prints the value of i followed by a space. Therefore, the output will be "0 2 4 ".

Question 48:

#include<stdio.h>
int main() {
    int i;
    for (i = 0; i < 5; i++) {
        if (i % 2 == 0)
            continue;
        printf("%d ", i);
    }
    return 0;
}

Explanation: The code uses a for loop to iterate over the values of i from 0 to 4. In each iteration, it checks if i is divisible by 2 (i % 2 == 0) using the if statement. If i is divisible by 2, the continue statement is executed, which skips the current iteration and moves to the next one. Therefore, when i is 0, 2, or 4, the continue statement will be executed, and the corresponding numbers will not be printed. The output will be "1 3 ".

Question 49:

#include<stdio.h>
int main() {
    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 2; j++) {
            printf("%d %d ", i, j);
        }
    }
    return 0;
}

Explanation: The code uses nested for loops to create a 2D loop structure. The outer loop iterates over the values of i from 0 to 2, and the inner loop iterates over the values of j from 0 to 1. In each iteration, it prints the values of i and j followed by a space. Therefore, the output will be "0 0 0 1 1 0 1 1 2 0 2 1 ".

Question 50:

#include<stdio.h>
int main() {
    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < i; j++) {
            printf("%d %d ", i, j);
        }
    }
    return 0;
}

Explanation: The code uses nested for loops to create a 2D loop structure. The outer loop iterates over the values of i from 0 to 2, and the inner loop iterates over the values of j from 0 to i-1. In each iteration, it prints the values of i and j followed by a space. Therefore, the output will be an empty string because the inner loop will not execute for any value of i.