Open In App

Output of C programs | Set 60 (Constants)

Last Updated : 08 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite: C Constants and Strings

Q.1 What is the output of this program? 

C
#include <stdio.h>
int main() {
    const char *s = "";
    char str[] = "Hello";
    s = str;
    while(*s)
        printf("%c", *s++);
    return 0;
}

Options 
a) Error 
b) H 
c) Hello 
d) Hel 

ans:- c 

Explanation

 const char *s = "";

The constant variable s is declared as an pointer to an array of characters type and initialized with an empty string. 

char str[] = "Hello"; 

The variable str is declared as an array of characters type and initialized with a string "Hello". 

s = str;

The value of the variable str is assigned to the variable s. Therefore str contains the text "Hello".

while(*s)
{ printf("%c", *s++); } 

Here the while loop got executed until the value of the variable s is available and it prints the each character of the variable s. 
Hence the output of the program is "Hello".

Q.2 What is the output of this program? 
 

C
#include <stdio.h>
int get();

int main()
{
    const int x = get();
    printf("%d", x);
    return 0;
}
int get()
{
    return 20;
} 

Options 
a) Garbage value 
b) Error 
c) 20 
d) 0 

ans:- c

Explanation

int get(); 

This is the function prototype for the function get(), it tells the compiler returns an integer value and accept no parameters. 

const int x = get(); 

The constant variable x is declared as an integer data type and initialized with the value "20". 
The function get() returns the value "20".

Q.3 What is the output of this program? 

C
#include <stdio.h>
int fun(int **ptr);

int main()
{
    int i=10;
    const int *ptr = &i;
    fun(&ptr);
    return 0;
}
int fun(int **ptr)
{
    int j = 223;
    int *temp = &j;
    printf("Before changing ptr = %5x\n", *ptr);
    *ptr = temp;
    printf("After changing ptr = %5x\n", *ptr);
    return 0;
}

Options
a) Address of i Address of j 
b) 10 223 
c) Error: cannot convert parameter 1 from 'const int **' to 'int **' 
d) Garbage value 

ans:- a

Explanation: const function parameter is allowed to have non-const values in C (only shows a warning). As ptr does not remains const in the function fun(), we will be able to change the stored address from i's to j's. It is one of the vulnerabilities of the C.

Q.4 What is the output of this program?

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

Options 
a) 10 
b) 11 
c) No output 
d) Error: ++needs a value 

ans:- d

Explanation:

const int i = 0; 

The constant variable 'i' is declared as an integer and initialized with value of '0'(zero). 

printf("%d\n", i++); 

Here the variable 'i' is incremented by 1(one). This will create an error "Cannot modify a const object". 
Because we cannot modify a const variable.

Q.5 What is the output of this program? 

C
#include <stdio.h>
int main()
{
    const int c = -11;
    const int d = 34;
    printf("%d, %d\n", c, d);
    return 0;
}

Options 
a) Error 
b) -11, 34 
c) 11, 34 
d) None of these 

ans:- b

Explanation:

const c = -11;

The constant variable 'c' is declared and initialized to value "-11". 

const int d = 34;

The constant variable 'd' is declared as an integer and initialized to value '34'. 

printf("%d, %d\n", c, d); 

The value of the variable 'c' and 'd' are printed. 
Hence the output of the program is -11, 34


Article Tags :
Practice Tags :

Similar Reads