HELP!! Two questions from <<the c programming language>>

G

Guru Jois

My program of exercise 1-9

#include <stdio.h>

int main()
{
int flag=1;
int c;

while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}

}

It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?

yet smaller code..

#include<stdio.h>
main() {

int c;
int flag;

flag = 0;

while ((c = getchar ()) != EOF) {
if (c == ' ') {
if (!flag) {
flag = 1;
putchar (c);
}
}
else {
flag = 0;
putchar (c);
}
}

}

Bye
Guru Jois
 
T

Tak

How to do what? Read the following sig.

--
If you want to post a followup via groups.google.com, ensure
you quote enough for the article to make sense. Google is only
an interface to Usenet; it's not Usenet itself. Don't assume
your readers can, or ever will, see any previous articles.
More details at: <http://cfaj.freeshell.org/google/>

Sorry, I am new here.
 
A

Army1987

#include <stdio.h>

int main(void)
{
int c;
char last_char = '\n'; /* Anything that is not a space */
I would use int, or unsigned char. See below.
while ((c = getchar()) != EOF) {
if (c != ' ' || last_char != ' ')
putchar(c);
last_char = c;
What happens if char is signed, CHAR_BIT is 8, and c is 163?
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Army1987 said:
I would use int, or unsigned char. See below.
What happens if char is signed, CHAR_BIT is 8, and c is 163?

Assuming the implementation doesn't actively hinder the simplest I/O, it
gets converted to a negative value which will not compare equal to ' '.
 
B

Ben Bacarisse

Harald van Dijk said:
Assuming the implementation doesn't actively hinder the simplest I/O, it
gets converted to a negative value which will not compare equal to '
'.

Indeed. But the suggestion to use int last_char is, I think, still a
good one. It would allow the more obvious initial setting of EOF and
would simplify matters if one were to require character class testing
(if (!isspace(last_char)... and so on).

I had originally used int and changed it for no good reason as far as
I can see.
 
B

Barry Schwarz

I would use int, or unsigned char. See below.
What happens if char is signed, CHAR_BIT is 8, and c is 163?

Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?


Remove del for email
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Barry said:
Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?

getchar does not work with plain chars, it works with unsigned chars. 163
fits just fine in an unsigned char, so getchar is allowed to return 163.
 
A

Army1987

Barry Schwarz said:
Is getchar not guaranteed to return the integer value of either a
"proper" char or EOF? If 163 will not fit in a char and since EOF is
guaranteed to be negative, is it even possible for getchar to return
this value?

Assuming sizeof(int) > 1, it isn't.
getchar() will return either EOF or a valid *unsigned* char, which
can't of course equal EOF.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top