C
Chris Readle
Ok, I've just recently finished a beginning C class and now I'm working
through K&R2 (alongside the C99 standard) to *really* learn C.
So anyway, I'm working on an exercise in chapter one which give me
strange behavior. Here is the code I've written:
/******************************************************************************
* K&R2 Exercise 1-9
* Write a program to copy its input to its output, replacing strings
of blanks
* with a single blank.
******************************************************************************/
#include <stdio.h>
int main(void) {
int c= 0, lastc= 0;
while((c= getchar()) != EOF) {
if (c == ' ') {
if (lastc != ' ') {//if c *is* a space and lastc is
putchar(c);//*not* a space, print the char
lastc= c;
}//end inner if
}//end outer if
if (c != ' ') {//if c isn't a space don't worry about it
putchar(c);
lastc =c;
}//end if
}//end while
return 0;
}
The problem I'm having is that when I run the program, I get no
execution, just returned to a prompt. However, when I run gdb on it
(even if I just "run" the program within gdb) it works, and even does
what I expected it to. Can anyone explain this one to me?
crr
through K&R2 (alongside the C99 standard) to *really* learn C.
So anyway, I'm working on an exercise in chapter one which give me
strange behavior. Here is the code I've written:
/******************************************************************************
* K&R2 Exercise 1-9
* Write a program to copy its input to its output, replacing strings
of blanks
* with a single blank.
******************************************************************************/
#include <stdio.h>
int main(void) {
int c= 0, lastc= 0;
while((c= getchar()) != EOF) {
if (c == ' ') {
if (lastc != ' ') {//if c *is* a space and lastc is
putchar(c);//*not* a space, print the char
lastc= c;
}//end inner if
}//end outer if
if (c != ' ') {//if c isn't a space don't worry about it
putchar(c);
lastc =c;
}//end if
}//end while
return 0;
}
The problem I'm having is that when I run the program, I get no
execution, just returned to a prompt. However, when I run gdb on it
(even if I just "run" the program within gdb) it works, and even does
what I expected it to. Can anyone explain this one to me?
crr