1

I'm trying to add two numbers given as command-line arguments in C, but without any use of functions except for printf. (Cannot use functions like isDigit(), atoi(), scanf(), etc.)

I think the ASCII Table would help, but I'm unsure how to include it.

Ex: The user gives as arguments: 3 + 5 Then three arguments are 3, +, and 5. The code should print 8.

Code so far:

int main(int argc, char*argv[])
{
    char operator;
    operator = argv[2][0];

    char firstNum;
    // check if firstNum is integer (max of 999 digits)

    char secondNum;
    // check if secondNum is integer (max of 999 digits)
    
    int add;
    add = firstNum + secondNum;
    printf(sum);
}
Aniket Tiratkar
  • 798
  • 6
  • 16
  • `firstNum` and `secondNum` you declared as `char`, how do you think you store max value of 999 there, try using `char` array and search for fgets – csavvy Nov 09 '20 at 02:39
  • 1
    Do you have to deal with numbers 0..999 (1-3 digits), or do you have to deal with numbers with up to 999 digits (a good few multiples of a googol) as suggested by the "`(max of 999 digits)`" comments? The latter is a bit harder than the former, to put it politely. – Jonathan Leffler Nov 09 '20 at 03:01
  • Are you allowed to write you own function to add numbers which are represented as character strings? – Bob Jarvis - Слава Україні Nov 09 '20 at 03:07
  • @sravs - it's not a maximum value of 999 - it's a string of up to 999 digits. And one must apparently support more than just the addition operator, if I understand the comment in `main` correctly. A fine assignment - just the thing for a budding young programmer to tackle. – Bob Jarvis - Слава Україні Nov 09 '20 at 03:18
  • @BobJarvis-ReinstateMonica I cannot create a new function either. Do you know how to check if an input is an integer without functions and using ASCII? – user14552043 Nov 09 '20 at 03:54
  • 1
    If all the characters in the string are `>= '0'` and `<= '9'` then the string represents an integer - otherwise it doesn't. – Bob Jarvis - Слава Україні Nov 09 '20 at 04:02
  • You better don't even think about using ASCII values in your code. Instead just use character constants like `'0'` etc. – Gerhardh Nov 09 '20 at 10:12

1 Answers1

0

Here's a program which can (without using any external functions) convert a string into an integer (only works for positive integers). I'm sure you can generalize this to convert both command line arguments:

#include <stdio.h>

int main() {
    char *str = "12345";
    
    int num = 0;  // Store number here
    int i = 0;    // Index into string
    while (str[i] != '\0') {
        // Don't need to look up ASCII value, can just subtract
        // chars to get the difference. The compiler substitutes
        // in the ASCII value here
        int digit = str[i] - '0';

        // Accumulate the digit
        num = num*10 + digit;
        i++;
    }
    // `num` now has the integer.

    printf("number is : %d\n", num);
    return 0;
}
Mustafa Quraish
  • 692
  • 4
  • 10