COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### A Minor Problem With atoi() And Negative Numbers

• Email
• Follow

```Hi!

I have implemented a simple program to convert temperatures from
Celsius to Fahrenheit and vice-versa, printing them on a simple table.

The max temperature (that is, the maximum value until which the table
must be printed) is passed through command line to the program:

../temp_convert 100

This command tells the program to print a table of temperatures until
the max value 100 is reached.

When passing negative values, such as:

../temp_convert -100

The program does not work at all.

For getting the value through command line, I used the "atoi()"
function in this way:

max_temp = atoi(argv[1]);

Does this problem have something to do with the "atoi()" function?

Best Regards!

Marcelo
```
 0

See related articles to this posting

```Marcelo De Brito wrote:
> Hi!
>
> I have implemented a simple program to convert temperatures from
> Celsius to Fahrenheit and vice-versa, printing them on a simple table.
>
> The max temperature (that is, the maximum value until which the table
> must be printed) is passed through command line to the program:
>
> ../temp_convert 100
>
> This command tells the program to print a table of temperatures until
> the max value 100 is reached.
>
> When passing negative values, such as:
>
> ../temp_convert -100
>
> The program does not work at all.
>
> For getting the value through command line, I used the "atoi()"
> function in this way:
>
> max_temp = atoi(argv[1]);
>
> Does this problem have something to do with the "atoi()" function?

You should post a working example - atoi will work fine with negative
numbers.  What type is max_temp?

You should look at using strtol rather than atoi, the latter doesn't
give you any indication of failure.

--
Ian Collins
```
 0

```Hi!

I have fixed the code!

By the way... Here you are the code:

#include<stdio.h>
#include<stdlib.h>

double farh2celsius(double tf);
double celsius2farh(double tc);

int main(int argc, char *argv[])
{
int i, max_temp;

if(argc < 2)
{
fprintf(stderr, "ERROR!! Usage: %s 'max temperature'\n", argv[0]);
exit(0);
}

max_temp = atoi(argv[1]);
printf("\n");
printf("Temperature(C)  Temperature(F)  |  Temperature(F)
Temperature(C)\n");

if(max_temp >= 0)
for(i = 0; i <= max_temp; ++i)
printf("%7d\t\t%11.2f\t|\t%7d\t\t%11.2f\n", i, farh2celsius(i),
i, celsius2farh(i));
else
for(i = 0; i >= max_temp; --i)
printf("%7d\t\t%11.2f\t|\t%7d\t\t%11.2f\n", i, farh2celsius(i),
i, celsius2farh(i));
printf("\n");

return 0;
}

double farh2celsius(double tf)
{
return(9*tf/5 - 32);
}

double celsius2farh(double tc)
{
return(5*(tc - 32)/9);
}

Just another question: How should I proceed to pass strings (through
command line) as a program input?

Thank You!

Marcelo
```
 0

```On 3/7/2010 8:18 PM, Marcelo De Brito wrote:
> Hi!
>
> I have fixed the code!
> [...]
>
> double farh2celsius(double tf)
> {
>    return(9*tf/5 - 32);
> }

Let's see: On the Fahrenheit scale, water freezes at 32
degrees.  So, 9 * 32 / 5 - 32 = 25.6; we conclude that the
Celsius freezing point is 25.6 degrees, right?

> double celsius2farh(double tc)
> {
>    return(5*(tc - 32)/9);
> }

Again: Water freezes at 0 on the Celsius scale.  Then
5 * (0 - 32) / 9 = -160 / 9 ~= -17.78; we conclude that the
Fahrenheit freezing point is -17.78 degrees, right?

Putting these two results together, we find 32 == -17.78,
0 == 25.6, and Hell has frozen.

> Just another question: How should I proceed to pass strings (through
> command line) as a program input?

It's system-specific, but typically you'll type the program
name, one or more spaces or tabs, the string you want to pass,
and the ENTER or RETURN key.  On some systems you may need to
precede the whole thing with a "verb" like RUN or MCR.  Consult

--
Eric Sosman
esosman@ieee-dot-org.invalid
```
 0

```Hi!

Eric, you are right. I committed a mistake while implementing it. But
I have fixed it. Thank you for your observation.

>> It's system-specific, but typically you'll type the program
>>name, one or more spaces or tabs, the string you want to pass,
>>and the ENTER or RETURN key.  On some systems you may need to
>>precede the whole thing with a "verb" like RUN or MCR.

Thank you so much, Eric! Please, don't feel offended, but It reminded
me of the famous USENET personality BIFF. :-)

See here:
http://bit.ly/cEjn3K

Best Regards!

Marcelo
```
 0

```Marcelo De Brito <nosophorus@gmail.com> writes:
<snip>
> By the way... Here you are the code:
<snip>
>   if(max_temp >= 0)
>     for(i = 0; i <= max_temp; ++i)
>       printf("%7d\t\t%11.2f\t|\t%7d\t\t%11.2f\n", i, farh2celsius(i),
> i, celsius2farh(i));
>   else
>     for(i = 0; i >= max_temp; --i)
>       printf("%7d\t\t%11.2f\t|\t%7d\t\t%11.2f\n", i, farh2celsius(i),
> i, celsius2farh(i));

A small point: this duplication is unfortunate.  It hardly matters in
this small program but part of the art of programming is to find ways
of abstracting common features out of duplicated code.

I'd strive to find a way to write one loop that worked for both
directions.  I won't say how I'd do it because it is reasonable
exercise to try for yourself.

<snip>
--
Ben.
```
 0

```Hi!

Ben, I know the code is ill implemented and it is even worse when it
comes to the Art Of Programming. The sole objective of the code above
has nothing to do with temperatures and/or good programming style. I
implemented it just to learn a little bit more the C language and how
to pass parameters through command line. At least I already know how
to pass simple numeric parameters. The next objective is strings.

Best regards!

Marcelo
```
 0

```On 8 Mar, 03:12, Marcelo De Brito <nosopho...@gmail.com> wrote:

> Ben, I know the code is ill implemented and it is even worse when it
> comes to the Art Of Programming. The sole objective of the code above
> has nothing to do with temperatures and/or good programming style.

how can any program not have something to do with programming style!
:-)

> I
> implemented it just to learn a little bit more the C language and how
> to pass parameters through command line. At least I already know how
> to pass simple numeric parameters. The next objective is strings.

I'm not sure I understand. The parameters passed on the command line
*are* strings.

#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv [])
{
if (argc < 2)
{
fprintf (stderr, "ERROR!! Usage: %s 'string'\n", argv [0]);
exit (EXIT_FAILURE);
}

printf ("command line argument is %s\n", argv [1]);

return 0;
}

```
 0

```Nick Keighley wrote:
>
> On 8 Mar, 03:12, Marcelo De Brito <nosopho...@gmail.com> wrote:

> > I
> > implemented it just to learn a little
> > bit more the C language and how
> > to pass parameters through command line. At least I already know how
> > to pass simple numeric parameters. The next objective is strings.
>
> I'm not sure I understand. The parameters passed on the command line
> *are* strings.
>
> #include <stdlib.h>
> #include <stdio.h>
>
> int main (int argc, char *argv [])

Yes.
argv is a NULL terminated array of pointers to strings.

--
pete
```
 0

```Hi!

Thank you so much for your replies! :-)

I would like to know how a program would take a string of chars as its
input through command line. For example, let's say we have simple
program that takes a string of chars and returns us the length of it.
See:

../my_program my dromedrario is cool

How to implement that? What should I do to pass the string "my
dromedrario is cool" as the program input? What functions should I use
to implement that?

Please, avoid BIFFisms ( http://bit.ly/cEjn3K ) as replies! :-)

Best Regards!

Marcelo
```
 0

```In article <ce0fbca9-4bb7-4e3e-b560-
>
> Hi!
>
> Thank you so much for your replies! :-)
>
> I would like to know how a program would take a string of chars as its
> input through command line. For example, let's say we have simple
> program that takes a string of chars and returns us the length of it.
> See:
>
> ./my_program my dromedrario is cool

int main(int argc, char **argv)
{
if (argc > 1)
{
/* Examine command line arguments here... */

/*
Note that we will not be able to tell the difference between:
my dromedrario is cool
and
my              dromedrario       is      cool
unless we use quoted arguments like this:
"my dromedrario is cool"
and
"my              dromedrario       is      cool"
*/
}
return 0;
}

> How to implement that? What should I do to pass the string "my
> dromedrario is cool" as the program input? What functions should I use
> to implement that?
>
> Please, avoid BIFFisms ( http://bit.ly/cEjn3K ) as replies! :-)

Now where did I put my Atomic Napalm Neutralizer?
```
 0

```In article <ce0fbca9-4bb7-4e3e-b560-f40a238e259a@x22g2000yqx.googlegroups.com>,
Marcelo De Brito  <nosophorus@gmail.com> wrote:
>I would like to know how a program would take a string of chars as its
>input through command line. For example, let's say we have simple
>program that takes a string of chars and returns us the length of it.
>See:
>
>./my_program my dromedrario is cool
>
>How to implement that? What should I do to pass the string "my
>dromedrario is cool" as the program input? What functions should I use
>to implement that?
>

\$ cat a.c
#include <stdio.h>
int main(int argc, char **argv)
{
if (argc==2) puts(argv[1]);
return 0;
}
\$ cc a.c
\$ ./a.out "Please, avoid BIFFisms ( http://bit.ly/cEjn3K ) as replies! :-)"
Please, avoid BIFFisms ( http://bit.ly/cEjn3K ) as replies! :-)
```
 0

```Marcelo De Brito wrote:
> Hi!
>
> Thank you so much for your replies! :-)
>
> I would like to know how a program would take a string of chars as its
> input through command line. For example, let's say we have simple
> program that takes a string of chars and returns us the length of it.
> See:
>
> ./my_program my dromedrario is cool
>
> How to implement that? What should I do to pass the string "my
> dromedrario is cool" as the program input? What functions should I use
> to implement that?
>

You can do that with argc and argv, but aren't you mispelling "dromedario?"

> Please, avoid BIFFisms ( http://bit.ly/cEjn3K ) as replies! :-)

LIKE REDNECKS TALKING?
--
fred
```
 0

```On 8 Mar, 23:22, Marcelo De Brito <nosopho...@gmail.com> wrote:
> Hi!
>
> Thank you so much for your replies! :-)
>
> I would like to know how a program would take a string of chars as its
> input through command line. For example, let's say we have simple
> program that takes a string of chars and returns us the length of it.
> See:
>
> ./my_program my dromedrario is cool
>
> How to implement that? What should I do to pass the string "my
> dromedrario is cool" as the program input? What functions should I use
> to implement that?

what's wrong with the example I gave you?

> Please, avoid BIFFisms (http://bit.ly/cEjn3K) as replies! :-)
```
 0