0
Char *ptr="solo learn" ;printf(ptr+5); what is its output?
What my doubt is in printf, there is no format specifier. Will give error for not mentioning? Please explain.
25 Respostas
+ 6
There are problems with that "code"
1. Char is unknown type, it could be auto-caps feature, but still a problem.
2. printf() call is missing format specifier (you said so yourself).
3. ptr *+ 5 isn't a valid expression (edited).
(Edit)
The question had been edited
+ 2
Ipang Sry the expression is ptr+5
+ 1
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Oh I see ...
Well then you may see 'learn' as output, of course you'd need "%s" specifier.
(Edit)
The C-string itself (ptr) is the format specifier.
+ 1
Ipang so the answer to question will be an error as there is no format specifier?
+ 1
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Error primarily because no format specifier, the ptr *+ 5 (if that's how it was written in challenge). And the 'Char' typo also ...
+ 1
Guys, in that case then, I'd assume the <ptr> is used as format specifier by printf(), it is possible. But I'd set <ptr> const char* instead to match the printf() prototype ...
+ 1
1 It points to string
2 Index starts at 0
3 6 char is l (0..5) ,,š
+ 1
Here , In the character pointer "ptr" , Base address of the given string is assigned. So pointer ptr stores the base address of "s" also. Now as given in the Printing statement 'printf(ptr+5)' , 5 is added to ptr , so pointer ptr will point 5 characters ahead now and the Character will be "l".
Hence , The output of the given code will be "l".
+ 1
Aman kumar when i run the code, output is "learn" not "l"
0
codemonkey the doubt is there is no format specifier, will it give an error?
0
codemonkey OK thank you .i got the question in challenge and answer to it is learn. I got a doubt as there is no format specifier, it would give an error and asked a doubt.
0
Ipang in challenge written as ptr+5 only
0
codemonkey format specifier is not compulsory for strings in c
0
codemonkey do you know any reason how compiler interpreted internally
0
codemonkey i dont know it is not compulsory
0
codemonkey I Am asking as question only
0
codemonkey oh k thanks
0
codemonkey int *ptr=5, printf(ptr) will give compilation error? as pointer points to integer and not char data type.
0
codemonkey thank you very much
0
You've got type errors in this code. You have to do type casting. Also, you can't define var with uppercase.