0

Consider this thread What is EOF in the C programming language?

The answer was that EOF (Ctrl-D) results in that getchar returns -1

  • My question is what does Ctrl-J and Ctrl-M represent in c on OSX and why does getchar return 10 for both using the same code as in link above?
  • What other shortcuts (Ctrl-somthing / Cmd-something) results in that getcharturns a static predefined number?
Community
  • 1
  • 1
userjuicer
  • 123
  • 2
  • 9
  • What are you doing so that MacOS X returns anything? Be a bit more precise. We can't read your mind. – gnasher729 Oct 28 '14 at 13:46
  • Just testing the same code as in thread with other shortcuts. I use the shortcuts as an additional way to control the flow of a program. I am just curious where the 10 comes from and whether it Ctrl-J/M always returns 10 and if there are other shortcuts like it. – userjuicer Oct 28 '14 at 13:48
  • 2
    Please read the other answer more carefully. It doesn't say `EOF (Ctrl-D) returns -1`, in the contrary. It says that pressing the keybord sequence `Ctrl-D` signals the end of the stream, which then makes that `getchar` returns `EOF`. `EOF` is a special value to notify the caller that the end of file is reach. Often this `EOF` value is `-1` but could be any other negative value. – Jens Gustedt Oct 28 '14 at 14:06

2 Answers2

4

Ctrl-J is the shortcut for the line feed control character, having the character code 10. Here is a page with other control characters

I as of this time do not know why Ctrl-M (ASCII value 13) returns 10 but assume it is due to it being similar in function to the line feed.

The reason EOF returns -1 is because its value is -1 on most systems.

Some other defined characters:
Ctrl-G: 7
Ctrl-I: 9
...
Ctrl-V: 22

  • +1. Add more information for Ctrl-M. – ani627 Oct 28 '14 at 13:53
  • Thanks, but I primarily interested in the return. See the link above. Ctrl-D always returns -1 in c on OSX. Are there other shortcuts that ALWAYS return a predefined number? – userjuicer Oct 28 '14 at 13:54
  • @userjuice If you follow the provided link you will find the control codes and the attributed integer values of such. – Sterling Tolley Oct 28 '14 at 14:12
  • When I press Ctrl-M `getchar` returns 10 for me and not 13. `getchar` doesn't return anything when I press Ctrl-V. According to your list Also according to the list you linked to `getchar`should return 4 when Ctrl-D is pressed but it returns -1. – userjuicer Oct 28 '14 at 14:21
  • @userjuice As I have said in my answer I am unsure why it is returning ten but I believe it has something to do with the Line Feed ("\n") and the Carriage Return ("\r") character having similar behavior. Here's an article on newlines: [Newline](http://en.wikipedia.org/wiki/Newline#In_programming_languages) – Sterling Tolley Oct 28 '14 at 14:25
  • The actual value of EOF is system-dependent (but is commonly -1) -Wikipedia – Sterling Tolley Oct 28 '14 at 14:27
  • The thread I linked to states that the EOF is system-dependent, but static(?). My main question if there are others. So far I have only found Ctrl-J and Ctrl-M that oddly enough seem to have`getchar`return the same value 10. – userjuicer Oct 28 '14 at 14:33
  • `stdin` is typically in text mode. Various conversions occur per OS concerning line endings when reading/writing in text mode. Crtl-M is one of them - it is converted to 10. Had IO been in binary mode, no conversion would be expected. – chux - Reinstate Monica Oct 28 '14 at 14:43
  • @chux THANKS!! Finally some answers. But why are most Ctrl-SomeLetter blank? – userjuicer Oct 28 '14 at 14:58
  • 1
    @userjuicer Your console maps various keyboard combinations to various `char` and actions (like Ctrl-D --> EOF). The various `char` created certainly include most of the values 0 to 127. As these values are _typically_ mapped to [ASCII](http://en.wikipedia.org/wiki/ASCII), the first 32 values (Ctrl-@, CTRLA, CTRl-B, ... Ctrl-_) may have no graphical representation. – chux - Reinstate Monica Oct 28 '14 at 15:05
  • @chux Again thanks! That was the answer I was looking for. – userjuicer Oct 28 '14 at 15:17
0

stdin is typically in text mode. Various conversions occur per OS concerning line endings when reading/writing in text mode. Crtl-M is one of them - it is converted to 10. Had IO been in binary mode, no conversion would be expected.

Consoles map various keyboard combinations to various char and actions (like Ctrl-D --> EOF). The various char created certainly include most of the values 0 to 127. As these values are typically mapped to ASCII, the first 32 values (Ctrl-@, CTRL-A, CTRl-B, ... Ctrl-_), they may have no graphical representation

Note: Notice what is returned when getchar() is called again after it returned EOF. Expect it to immediately return EOF again without waiting for any additionally key presses. (Ctrl-D) set a condition, not a char.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256