last day (20 days later) » 

10:52 PM
2
A: Printf output of pointer string explanation from an interview

Vlad from MoscowThis call printf("%d\n", s); has undefined behaviour because an invalid format specifier is used with a pointer. This call printf("%d\n", *s); outputs internal code (for example ASCII code) of the character '1'. This call printf("%c\n", s); has undefined behaviour due to using an inva...

 
Careful with those UB remarks buddy. This is code that can be tested and behavior that can be checked and verified on or off line.
Did you execute these commands? Did you check the values? Please provide a use case in-code example.
 
@RannLifshitz If a call of printf outputs a value it does not meann that the call does not have undefined behavior.
 
Really? Then why did you delete your previous comment? Because you KNEW it would be flagged as rude or abusive.
 
@RannLifshitz I have not deleted my comments. It is not me. It is somebody else.
 
@Vlad from Moscow I didn't want to cause any conflict with my question. But for number 1 - printf("%d\n", s); this is not an undefined behavior. What if part of the code was to know the address of the first pointer?
 
10:52 PM
@Rami It is undefined behavior because an incorrect format specifier is used for the argument. So it can be outputted even a trap number. The selected answer is totally incorrect.
 
@VladfromMoscow : "So it can be outputted even a trap number." - please provide a code + execution example showing this behavior which can be reproduced. In this case you are misleading others. "The selected answer is totally incorrect" - wrong. Even if you are correct, which is disputed, the only section in dispute is printf #3.
 
@RannLifshitz The code example is already provided in the question printf("%d\n", s); . Please read at last the description of the function fprintf in the C Standard.
 
@VladfromMoscow : Execute the code please. Then inspect the pointer values. I worked in a company doing these sort of things for 3.5 years. There is a concept of truncation which I displayed, in an example which can be reproduced.
@VladfromMoscow : Your remarks are abusive. WHY ??? I have no idea who you are but I did not address you disrespectfully. We can disagree in a professional manner. Please do so.
@VladfromMoscow : What are you trying to do here ? The point is to help Rami out. This has escalated into something ugly. I have no ill will towards you.
 
@RannLifshitz Careful with those UB remarks buddy. This is code that can be tested and behavior that can be checked and verified on or off line. And that comment demonstrates that you don't understand undefined behavior at all.
 
@AndrewHenle : Please see edit to the answer.
 
10:52 PM
@RannLifshitz Your answer has nothing to do with your statement that "[undefined] behavior that can be checked and verified". It's a bit hard to unring that bell.
 
@AndrewHenle : If the code has never been executed on a system - I agree - this is indeed UB. However - the code in question was executed on a specific system, with given outputs in the question itself. These outputs allowed to deduce what exactly happened under that system. Therefore the behavior is definable under that specific system.
 
M.M
@RannLifshitz it seems you do not know what "undefined behavior" means. This terminology is defined in section 3.4.3 of the C Standard (which you obviously haven't read). You will become a better programmer by familiarizing yourself with the standard instead of insisting that behaviour you saw on some system is all there is to it
 
@M.M : But I have read it mate. And the final note on this whole sordid affair I would like to state is - UB is the case WHEN YOU HAVE NOT RUN THE CODE ON SPECIFIC "HW" SYSTEM. When you have run the code - you have learned how that specific system behaves - which make the behavior definable on that system. On other systems? It is still UB untill you have tried it on these other systems. Think abstract VS concrete.
 
M.M
@RannLifshitz UB is defined as "behaviour for which the C Standard imposes no requirements". Please indicate which page of the C Standard defines the behaviour on any specific system you have tried it on. (Don't bother replying if you can't)
 
11:38 PM
@RannLifshitz In general, running some undefined code on a particular system and "learning" how it behaves is among the worst methods of determining how undefined behavior behaves. Since there are no guarantees about undefined behavior, there is no guarantee that the behavior you observe today will be observed again tomorrow -- even under the same compiler.
For the case of passing a pointer value to printf and printing it with the %c format, it's true, what you learn about the behavior (under a particular compiler) is likely to persist for a while. But it's still essentially useless information; it doesn't teach you much of anything you need to know about writing correct programs.
 

  last day (20 days later) »