delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/26/15:51:23

From: "John M. Aldrich" <fighteerNO AT SPAMcs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: argv[], char, and int
Date: Sat, 26 Apr 1997 02:15:41 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 47
Message-ID: <3361654D.5150@SPAMcs.com>
References: <5jtgo3$74r$1 AT excalibur DOT flash DOT net>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp101.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Ryan Crumley wrote:
> 
> I am realitivly new to C/C++ and have a few questions. I am hoping someone
> can give me some guidance. This is a simplified version of what I am trying
> to do but hopefully it will get the point across. What I would like to do
> is take argv[1], and argv[2] and preform math operations on them. This
> brings up a problem because I am using "char *argv" How would I go about
> changing these to an int? If anyone would be willing to help me could you
> email me at the address below.

/* add.c */
#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
    int a, b;

    if ( argc < 3 )
    {
        fprintf( stderr, "Two numeric arguments required.\n" );
        exit( 1 );
    }

    a = atoi( argv[1] );
    b = atoi( argv[2] );

    printf( "%d + %d = %d\n", a, b, a + b );
    /* etc. */

    return 0;
}

D:\CPROGS>gcc -Wall -O -g -o add.exe add.c

D:\CPROGS>add 44 55
44 + 55 = 99

D:\CPROGS>

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I      |   mailto:fighteer AT cs DOT com   |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| <<< This tagline under >>> |
|                      - Douglas Adams | <<<    construction    >>> |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019