Output of C programs | Set 60 (Constants)

Prerequisite : C Constants and Strings

Q.1 What is the output of this program?

#include<iostream>
using namespace std;
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 charactrers 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 untill 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?

#include<iostream>
using namespace std;
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?

#include<iostream>
using namespace std;
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);
    const *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:- c

Explanation : Here ptr is declared as constant integer pointer to which address of i is assigned but in function fun double pointer is passed as an argument, so it can’t convert from ‘const int **’ to ‘int **’.

Q.4 What is the output of this program?

 #include<iostream>
using namespace std;
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?

#include<iostream>
using namespace std;
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

This article is contributed by Pragya Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.