Char *ptr="solo learn" ;printf(ptr+5); what is its output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

30th Jul 2020, 5:33 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
25 Answers
+ 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
30th Jul 2020, 6:10 AM
Ipang
+ 2
Ipang Sry the expression is ptr+5
30th Jul 2020, 6:15 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
+ 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.
30th Jul 2020, 6:19 AM
Ipang
+ 1
Ipang so the answer to question will be an error as there is no format specifier?
30th Jul 2020, 6:20 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
+ 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 ...
30th Jul 2020, 6:25 AM
Ipang
+ 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 ...
30th Jul 2020, 6:30 AM
Ipang
+ 1
1 It points to string 2 Index starts at 0 3 6 char is l (0..5) ,,🙏
30th Jul 2020, 10:08 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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".
31st Jul 2020, 10:33 AM
Aman kumar
Aman kumar - avatar
+ 1
Aman kumar when i run the code, output is "learn" not "l"
31st Jul 2020, 10:42 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
codemonkey the doubt is there is no format specifier, will it give an error?
30th Jul 2020, 6:19 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
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.
30th Jul 2020, 6:22 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
Ipang in challenge written as ptr+5 only
30th Jul 2020, 6:26 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
codemonkey format specifier is not compulsory for strings in c
30th Jul 2020, 6:29 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
codemonkey do you know any reason how compiler interpreted internally
30th Jul 2020, 6:32 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
codemonkey i dont know it is not compulsory
30th Jul 2020, 6:33 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
codemonkey I Am asking as question only
30th Jul 2020, 6:34 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
codemonkey oh k thanks
30th Jul 2020, 6:36 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
codemonkey int *ptr=5, printf(ptr) will give compilation error? as pointer points to integer and not char data type.
30th Jul 2020, 6:41 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
codemonkey thank you very much
30th Jul 2020, 6:46 AM
Gajjala Venkata Sai Sri Harsha Hanumantha Rao
Gajjala Venkata Sai Sri Harsha Hanumantha Rao - avatar
0
You've got type errors in this code. You have to do type casting. Also, you can't define var with uppercase.
30th Jul 2020, 1:35 PM
Gary