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 1 of 3.
Rahul Kant said:
1 decade ago
In my opinion code to print "Hello" should be like this:
int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", *s++);
return 0;
}++);
return 0;
}
int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", *s++);
return 0;
}++);
return 0;
}
Jayp said:
1 decade ago
s is a pointer and you are not changing the value of some constant, so here pointer s is just doing what a pointer requires to do that is pointing to str and accessing the data stored in str.
So you can change cont pointer points to some other address but not directly the value.
So you can change cont pointer points to some other address but not directly the value.
Suresh Kumar said:
1 decade ago
Yes, s is a pointer to a constant character, and is initialized by "", thus the location to which s points can be changed, but the location cannot be changed using s, the while loop simply iterates over the string to give output "hello".
Mohini said:
1 decade ago
while(*s)
this condition says that while loop will get executed till there is no NULL,i.e end of string.
so it will go on printing each character till the end of string.
thus whole string gets printed
this condition says that while loop will get executed till there is no NULL,i.e end of string.
so it will go on printing each character till the end of string.
thus whole string gets printed
Shyam said:
1 decade ago
Can anyone tell me how can we copy whole string by just one equal to sign. I mean we suppose to run a loop to copy a array from one to another but hear they just put one equal to and it is done how ?
Deepthi said:
7 years ago
Here s is a pointer to a constant. That means we can change the address but we can't change the value. If it's constant to a pointer then we can change the value but address can't be changed.
Vinay Dixit said:
1 decade ago
Code should be like this:
int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", s++);
return 0;
}
int main()
{
const char *s = "";
char str[] = "Hello";
s = str;
while(*s)
printf("%c", s++);
return 0;
}
Anon said:
1 decade ago
Guys const char * is a pointer to a constant and not constant pointer. You can never alter the value it is pointing at but you can change the address it is holding.
(1)
Ckbrave13 said:
2 decades ago
Here 'str' points to location of 'H'.When s = str ,'s' contains location of 'H'.'*s' points to 'H' and each time it increments it prints "HELLO".
Jayesh monani said:
10 years ago
We can't directly assign s=str, it should give error, for that strcpy(s, str) should be used.
Somebody please explain?
Somebody please explain?
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers