C Programming - Strings - Discussion
Discussion Forum : Strings - Find Output of Program (Q.No. 35)
35.
What will be the output of the following program in 16 bit platform assuming that 1022 is memory address of the string "Hello1" (in Turbo C under DOS) ?
#include<stdio.h>
int main()
{
printf("%u %s\n", &"Hello1", &"Hello2");
return 0;
}
Answer: Option
Explanation:
In printf("%u %s\n", &"Hello", &"Hello");.
The %u format specifier tells the compiler to print the memory address of the "Hello1".
The %s format specifier tells the compiler to print the string "Hello2".
Hence the output of the program is "1022 Hello2".
Discussion:
18 comments Page 1 of 2.
Shivaleela said:
3 years ago
@Ashu:
char str[]="hello" stored in stack segment as it is a local variable and simply "hello" in printif stored in code/text segment. So both the addresses are different.
char str[]="hello" stored in stack segment as it is a local variable and simply "hello" in printif stored in code/text segment. So both the addresses are different.
Balaji said:
5 years ago
&hello1 gives the address.
&"hello1" gives the values of the string in %u unsigned int.
If you use %s it gives value in a string.
&"hello1" gives the values of the string in %u unsigned int.
If you use %s it gives value in a string.
Aananth said:
9 years ago
& is an address operator and we are passing the starting address of hello2 to %s , so it works.
Abhi said:
9 years ago
In printf(&"Hello2") statement.
It will print Hello2 only because the printf() accepts the char * in its parameter list where the printf() is defined in the system.
So, it is correct.
It will print Hello2 only because the printf() accepts the char * in its parameter list where the printf() is defined in the system.
So, it is correct.
Akshitha said:
10 years ago
We can not use symbol & in printf but how to use?
Gunish said:
1 decade ago
@Ramratan Kumar.
There is no difference.
There is no difference.
Phanendra yarra said:
1 decade ago
@Ashu.
void main() {
char str[]="hello";
clrscr();
printf("str= %u",str);
printf("\nstr= %u","hello");
getch();
}
Output-
str=65520
str=193
String itself is pointer to its first character. So where ever we are writing string will have different address.
void main() {
char str[]="hello";
clrscr();
printf("str= %u",str);
printf("\nstr= %u","hello");
getch();
}
Output-
str=65520
str=193
String itself is pointer to its first character. So where ever we are writing string will have different address.
Ramratan kumar said:
1 decade ago
What are the difference between printf("%s",&"hello2"); and printf("%s","hello2")?
Ozlemsen said:
1 decade ago
Did you try this one? I have tried and received error message.
(1)
DunkerDonuts said:
1 decade ago
%u is the format specifier for unsigned, not pointer address. Use %p if you want to print an address.
The fact that a pointer address can be represented as an unsigned int is only machine dependent and should not be relied upon.
The fact that a pointer address can be represented as an unsigned int is only machine dependent and should not be relied upon.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers