0

New to Xcode and C. Feels like I have a bug in Xcode or some kind of faulty setting. I have to include \n to every line i write in printf() otherwise it will not appear on the screen when I run the program. I'll include two images so that you can see what I mean even in the simplest of coding. Has anyone encountered this? I've asked around but everyone else that uses Xcode has no problem like this.With \n it shows "Hello World!When I remove \n nothing appears both examples still runs without any errors...

  • Just fyi, I hope you're not runing Xcode 8.3.x. [There is a bug in Xcode 8.3.x](https://forums.developer.apple.com/thread/75035) whereby integrated console output (which your screenshot demonstrates you're using) doesn't honor stdout line buffering and/or direct flushes. I've confirmed this and have run into it with both C (via printf and fflush) and C++ (via std::cout and std::flush). Frankly it's a royal pain in the arse, so much so that *many* people are reverting to at-most 8.2, or even back to the 7's. I, for one, went back to 8.2.1 and the problem doesn't seem to appear there. – WhozCraig Apr 25 '17 at 19:02
  • Tried using fflush(stdout); before my printf(); but still nothing appears... thanks for the idea though! – PhallBus Apr 25 '17 at 19:10
  • After your printf, not before. – yellowantphil Apr 25 '17 at 19:12
  • THANK YOU WhozCraig!!! Since I'm so new to this I thought I'd done something wrong. Maybe my biggest fault is running on Xcode 8.3.1 . Will uninstall and try to get my hands on 8.2.1. – PhallBus Apr 25 '17 at 19:13
  • thanks yellowantphil. Tried both places, didn't work... – PhallBus Apr 25 '17 at 19:14
  • Subject to there not being an overriding bug, you need to ensure that you output the newline under normal circumstances on Unix-like systems. By default, the standard I/O library will be line buffered for output to the terminal, and probably fully buffered for output to other types of file. The behaviour is mostly implementation defined, and different systems will behave differently. Most noticeably, if you're coming from Windows, the 'no newline at the end' behaviour definitely usually works differently. I'm going to close this as a duplicate — but WhozCraig's bug is probably germane. – Jonathan Leffler Apr 25 '17 at 23:49
  • Note that the article linked by @WhozCraig says that the specific bug is fixed in XCode 8.3.2 — find references dated 2017-04-18. (I've got 8.3.2, but I never noticed the problem in 8.3.1 or 8.3 since I don't use the IDE.) – Jonathan Leffler Apr 25 '17 at 23:52
  • @JonathanLeffler Thanks for including my tag, Jonathan. I didn't know 8.3.2 fixed this issue. I was running 8.3.1 when I first noticed it, and man what a pita. Thanks again. – WhozCraig Apr 26 '17 at 02:49
  • Had some trouble downloading but here's a late update. The new version 8.3.2 fixed it for me! Thanks guys! – PhallBus Apr 26 '17 at 13:30
  • Not sure how to accept a solution but what @WhozCraig said was correct. Could not se that answer i the question that this one is marked as a duplicate from. The flush did not work. – PhallBus Apr 26 '17 at 13:32

1 Answers1

1

Depending on the implementation of stdout in your environment, the apparent behavior of printf can be different. If you are not seeing a response, it might be because the stream is being handled in a manner, such that the \n character must be seen before displaying other buffered data. On my Windows 7 implementation, it requires no \n character, but on my Linux implementation at home, it does. I used the word apparent because it is likely not the implementation of printf that is the cause of the difference in behavior, rather the implementation of your stdout console.

As suggested in the comments, if your implementation of stdout fails to display until triggered with the \n character, you can force it with a call to fflush(...):

printf("hello");
fflush(stdout);

But of course, all of this is moot if you have the bug that @WhozCraig points out in his comment. :)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ryyker
  • 22,849
  • 3
  • 43
  • 87