Exercise 1-12 in the K&R - Problem with stdout.
"Exercise 1-12. Write a program that prints its input one word per line."
I'm trying to figure out what stupidly obvious thing I've overlooked, lend me a hand? Quote:
My humble gratitude, |
Quote:
Here's a sample of the output that I got after compiling it and running it in my bash terminal. "do you know where I am going?" is the input I supplied to the program Code:
do you know where I am going? |
Code:
word += c; d + o + space = 100 + 111 + 32 = 243 It is true that you can print the character of an integer as represented by the ASCII value but you can not append them together in this fashion. One fix would be to output the character as received on the same line until a space or EOF. |
Variable 'int word' cannot store a word. Actually, you don't have to store the word, it's enough to write its chacarters to stdout as soon as you read them.
|
Thanks guys! Now it does what it's supposed to.
I cut out all the extra nonsense I had put into the program because it simple wasn't necessary. Code:
hey how's int going, man? hey how's int going, man? [/Output] While I have you guys here could you briefly explain what the difference between single and double quotes is in C? I've only ever learned Python and JavaScript before and as you guys probably know, the two are, at least to my knowledge, completely interchangable. But it seems that I've run into issues with using '' and "" as if they were the same. Is it that one set interprets what's inside them literally or numerically and the other doesn't? Thanks again you guys. |
The first if could be
Code:
if (c != ' ' && c != '\t' && c != '\n') { |
Quote:
|
You can use the strtok library function, if you don't think it would defeat the point of the exercise.
|
Quote:
Is this not as simple as grabbing the argument list and outputting those arguments? I.e. Non-compiled/tested, coded by seat of pants: Code:
int main(int argc, char **argv) |
I assume you're supposed to get the "input" with scanf or something similar.
|
Quote:
Maybe not enough of the problem was related. |
Quote:
Just dug out my old K&R first edition and will supply the reference. Or perhaps it was in THE UNIX PROGRAMMING ENVIRONMENT by Kernighan and Pike. AP |
Quote:
AP |
I agree with AP. Been a while, but from what I remember, the early chapters of the K&R book were all about throwing characters around.
|
To answer your second question, a single quote is used to identify a single character and double quotes are used for string literals. A string literal “x” is a string, containing the character ‘x’ and a null terminator ‘\0’. So “x” is two-character array in this case.
|
All times are GMT -5. The time now is 06:42 AM. |