C Programming - Const - Discussion
Discussion Forum : Const - Find Output of Program (Q.No. 6)
6.
What will be the output of the program?
#include<stdio.h>
int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", *s++);
return 0;
}
Answer: Option
Explanation:
Step 1: const char *s = ""; The constant variable s is declared as an pointer to an array of characters type and initialized with an empty string.
Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and initialized with a string "Hello".
Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore str contains the text "Hello".
Step 4: 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".
Discussion:
23 comments Page 2 of 3.
Chandu said:
1 decade ago
Here they are allocating location address of characters but till it is constant pointer we can't change its values.
Pallavi said:
1 decade ago
Here s points to the empty string and it is a constant pointer, then how can we assign string 'str' to 's'?
Khushi said:
1 decade ago
Can anyone explain this in detail? how can we assign a const pointer different values each time?
Madhu said:
1 decade ago
Here *s is a constant pointer how can it's value be changed from 'NULL' to 'str' .
Am12cs said:
1 decade ago
Mohini concept is correct, while loop be excuted untill its value comes to null.
Chakri said:
1 decade ago
s is a constant pointer and it cannot be re assigned to another memory location.
Anoop said:
10 years ago
No, because postfix ++ have high precedence than * so, its equivalent to *(s++).
Mahesh napte said:
1 decade ago
Pointer can not be constant but it can store address af constant datatypes.
Bhavani said:
1 decade ago
How to change the value of constant variable s?
Please explain.
Please explain.
AVINASH said:
1 decade ago
While loop take 1 or 0 as input. How this program then correct?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers