Please tell me if I've written this correctly
Hi friends,
I am beginning to practice C from K&R. Although I am only familiar with the language, this time I want to invest time learning it
thoroughly. I've written this practice excercise program to replace more than a single blank in an input with only a single blank in the
output. Here's the code.
Code:
#include <stdio.h>
/*A program that displays keyboard input to the monitor, but trimming
more than a single space to a single space I've not replaced tabs with single
spaces yet*/
#define SPACE 1
void main()
{
int c, state;
c=0;
state=SPACE;
while ((c=getchar()) != EOF)
{
if (!(state && (c==' '))) putchar(c);
if (c==' ')
{
state=SPACE;
}
else
{
state= !SPACE;
}
}
}
Although I've tested it four to five times, I don't know if I can be sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and the BITWISE AND to be the same as the LOGICAL AND (&&). Also, on purpose, I have not yet detected tabbed spaces.
Could you gurus please tell me if I've written this program correctly?
Re: Please tell me if I've written this correctly
Quote:
Originally posted by Sathyaish
Hi friends,
I am beginning to practice C from K&R.
What books do you have to use as a reference?
That's int main(), not void main()
Quote:
Although I've tested it four to five times, I don't know if I can be sure as to whether it is right, since I've made guesses about the
BITWISE operators such as NOT to be the same as the logical NOT ! and the BITWISE AND to be the same as the LOGICAL AND (&&)
Again, a good C book will point out the differences. The bitwise AND/OR is not the same as the logical AND/OR.
Basically, what you want to do is to learn the language properly, and not rely on guesses. Also, usage of a debugger is helpful in finding out what your program is doing.
Regards,
Paul McKenzie