integers to base, and some other questions

P

pointer

I was wondering, is it better to start couple threads, or just put it
all in one. Well, i hope its ok this way...

First question is a function that puts integer n into a string s, using
the base n. The function is working correctly, but i was wondering is
there any better way of puting hex numbers in string?

sign * n%b+'7' I m using this line for storing letters for numbers
greater than 9. Isnt there a better way to put hex in there?

void itob(int n, char s[], int b)
{
int sign=1,i=0;

if (n < 0)
{
sign=-1;
}
do
{
if (n%b>9 || n%b<-9)
s[i++]= sign * n%b+'7';
else
s[i++]= sign * n%b+'0';
n /= b;
}
while(n!=0);

if (sign < 0)
{
s[i++] = '-';
}
s='\0';
reverse(s);
}


This case reports error duplicate cases. Why???

case 'a' || 'A':
case 'z' || 'Z':



this getchar is frustrating me...
while(getchar()!= EOF)

So if someone can explain this like im the third character in dumb and
dumber...
Let say im typing a line: and pressing ctr+z at the end of a line
This is a line^Z

after ctrl+z i press enter:
getchar(), is geting one character at a time and
eventually while looks like this
while (EOF != EOF)

why it doesnt exit while at that time???
What am i missing here??
 
S

Seebs

First question is a function that puts integer n into a string s, using
the base n. The function is working correctly, but i was wondering is
there any better way of puting hex numbers in string?

sprintf(..., "%x", ...)
This case reports error duplicate cases. Why???
case 'a' || 'A':
case 'z' || 'Z':

Because these are both "case 1".

Because 'a' || 'A' is 1, because either 'a' is not zero, or 'A' is not zero.

And don't bother trying 'a' | 'A'. In C, you write that as:

case 'a': case 'A':
/* code for a/A */
break;
case 'z': case 'Z':
/* code for zZ */
break;
this getchar is frustrating me...
while(getchar()!= EOF)
So if someone can explain this like im the third character in dumb and
dumber...
Let say im typing a line: and pressing ctr+z at the end of a line
This is a line^Z

^Z is not necessarily magical to C.
after ctrl+z i press enter:
getchar(), is geting one character at a time and
eventually while looks like this
while (EOF != EOF)
why it doesnt exit while at that time???
What am i missing here??

Most likely, what actually happens is that it's checking "while (26 != -1)".

Which is to say: EOF is not the value of any character. It is a
non-character value which is used to indicate that the OS thinks the input
stream has ended. ^Z is not an EOF, although some DOS variants and
derivatives treat it as one in some contexts -- but not in others.

Possibly-also-relevant: In Unix-land, there's often a magical thing you
can type which generates an EOF... *only at the beginning of a line*. (It's
more complicated than that, really, sort of, but in practice that's how it
works.)

-s
 
P

pointer

Because these are both "case 1".
Because 'a' || 'A' is 1, because either 'a' is not zero, or 'A' is not zero.

:) how did i miss that?

And don't bother trying 'a' | 'A'. In C, you write that as:

case 'a': case 'A':
/* code for a/A */
break;
case 'z': case 'Z':
/* code for zZ */
break;
And thats what i actually did...i guess i am starting to think c...lol

^Z is not necessarily magical to C.


Most likely, what actually happens is that it's checking "while (26 != -1)".

Which is to say: EOF is not the value of any character. It is a
non-character value which is used to indicate that the OS thinks the input
stream has ended. ^Z is not an EOF, although some DOS variants and
derivatives treat it as one in some contexts -- but not in others.

Possibly-also-relevant: In Unix-land, there's often a magical thing you
can type which generates an EOF... *only at the beginning of a line*. (It's
more complicated than that, really, sort of, but in practice that's how it
works.)

Isnt ^Z used when u want to get EOF??
Why would it check 26 != -1 if my string goes ...dsfuhsudfh^Z

And if ^Z is waont indicate EOF, how can i make OS think the input is over??
if i type ...jkldfhjdsahf\n and then ^Z and enter that will get me out
of getchar() why is that so?

is there any difference between ^Z alone or with some text before...
Untill now, i think i figured how it works, but i still dont have a clue
why is that so...

The thing is, i like c because its so logical, and thats why i have to
find the logic in this, before i can move on:) or i could just go to
assembly...:D
 
S

Seebs

Isnt ^Z used when u want to get EOF??

Not necessarily.
Why would it check 26 != -1 if my string goes ...dsfuhsudfh^Z

EOF is a negative value, often -1. Control characters most often have
values which are 1 for ^A, 2 for ^B, and so on up to 26 for ^Z.
And if ^Z is waont indicate EOF, how can i make OS think the input is over??

That depends on your OS.
if i type ...jkldfhjdsahf\n and then ^Z and enter that will get me out
of getchar() why is that so?

I don't know about your OS. On my OS, typing ^Z interrupts the program
and returns me to a prompt, and I can resume the program by typing "fg".

The details are basically OS-specific. What's probably happening is that
^Z is treated partially-magically, in that it can interrupt input, but if it
happens at the end of a line, you just get the line, but if you get it before
there were any characters, then it's treated differently.
is there any difference between ^Z alone or with some text before...
Probably.

The thing is, i like c because its so logical,

You're not dealing with C here, though, but the host environment's handling
of text input streams. Which may be defined independently of your C
compiler.

-s
 
N

Nick Keighley

I was wondering, is it better to start couple threads, or just put it
all in one. Well, i hope its ok this way...

First question is a function that puts integer n into a string s, using
the base n. The function is working correctly, but i was wondering is
there any better way of puting hex numbers in string?

use sprintf()

void itob(int n, char s[], int b)
{
sprintf (s, "%x", b);
}

It might overflow s of course

<snip>
 
P

Paul N

this getchar is frustrating me...
while(getchar()!= EOF)

So if someone can explain this like im the third character in dumb and
dumber...
Let say im typing a line: and pressing ctr+z at the end of a line
This is a line^Z

after ctrl+z i press enter:
getchar(), is geting one character at a time and
eventually while looks like this
while (EOF != EOF)

why it doesnt exit while at that time???
What am i missing here??

On many systems, keyboard input to getchar is buffered, that is to
say, it waits until you press ENTER before letting getchar get hold of
any of the characters. On my system (a command prompt box in Windows
XP) if I type in "asd^Zas" and press ENTER, the loop stops at that
point. Which is what I'd expect. It lets you type in a line of stuff,
then lets getchar attack it, and the loop stops when getchar gets to
the ^Z part way through.

However, while(getchar()!= EOF) is an unusual thing to do, because if
the character typed is not ^Z (or whatever your local arrangement is
for creating EOF), then you have no way of knowing what was typed. If
you do getchar() again it will return the NEXT character, not the same
one again. So there may be some difference between what you have
posted and what you are actually doing, and this might be causing you
difficulties that we aren't aware of.

Another thing to watch out for is that if you do c = getchar(), then c
should have type int rather than type char. Otherwise you could again
have difficulties with EOF, one possibility being that you get a false
end-of-file when you read a 255 byte.

And when you move from reading the keyboard to opening files and
reading then, make sure you get the right one out of text and binary
mode. Binary mode will probably not convert ^Zs to EOFs, it passes
stuff through unchanged.

Hope all this is useful.
Paul.
 
B

Barry Schwarz

I was wondering, is it better to start couple threads, or just put it
all in one. Well, i hope its ok this way...

First question is a function that puts integer n into a string s, using
the base n. The function is working correctly, but i was wondering is
there any better way of puting hex numbers in string?

Since your function does not work on mys system at all, I certainly
hope there is a better way.
sign * n%b+'7'  I m using this line for storing letters for numbers
greater than 9. Isnt there a better way to put hex in there?

void itob(int n, char s[], int b)
{
      int sign=1,i=0;

      if (n < 0)
      {
          sign=-1;
      }
      do
      {
          if (n%b>9 || n%b<-9)
             s[i++]= sign * n%b+'7';

The assumption that 'A' is equal to '7'+10 not portable. The world
consists of more than ASCII.
          else
             s[i++]= sign * n%b+'0';
          n /= b;
      }
      while(n!=0);

      if (sign < 0)
      {
            s[i++] = '-';
      }
      s='\0';
      reverse(s);

}
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top