0

I'm a beginner to C and I'm trying to accept input from zsh terminal using

./filename 1234 5678

So, instead of using scanf where you compile and run then get prompt to input numbers, I want to write the above command and use 1234 and 5678 directly into my code.

How do I do that?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    those are called command line arguments, take a look at `argc` and `argv` in your `main` signature `int main(int argc, char* argv[]){ ... }`. – yano Feb 21 '23 at 20:07
  • Related C++ question, which mostly applies to C: [What does int argc, char *argv\[\] mean?](https://stackoverflow.com/q/3024197/12149471) – Andreas Wenzel Feb 21 '23 at 20:14
  • You may also want to take a look at [this documentation](https://en.cppreference.com/w/c/language/main_function) of the function `main`. However, that page is a reference, not a tutorial. Any good C book for beginners will explain how to use command-line arguments. You may want to take a look at [this list](https://stackoverflow.com/q/562303/12149471) of books intented for beginners. – Andreas Wenzel Feb 21 '23 at 20:22
  • If you want a web tutorial on this topic, then you may want to look [here](https://www.tutorialspoint.com/cprogramming/c_command_line_arguments.htm), but I generally recommend a good book instead. – Andreas Wenzel Feb 21 '23 at 20:27

1 Answers1

-3

try below once :

#define INT_CONVERTED       (1 << 0)
#define FLOAT_CONVERTED     (1 << 1)

char *strlwr(char *str)
{
    char *ptr = str;

    while (*ptr)
    {
        *ptr = tolower(*ptr);
        ptr++;
    }
    return str;
}


int NumberOfDots(char *s)
{
    int dots = 0;
    while (*s)
        dots += *s++ == '.';
    return dots;
}

int NOTstrcasechr(char *str, int ch)
{
    return strchr(str, ch) == NULL && strchr(str, toupper(ch)) == NULL;
}

int ReadNumber(double *db, int *in)
{
    int result = 0;

    do
    {
        char str[100];
        int dots;

        result = 0;
        printf("Enter number: ");
        fgets(str, 100, stdin);
        if ((dots = NumberOfDots(str)) > 1) str[0] = '\0';

        if (sscanf(str, "%lf", db) == 1)
        {
            result |= FLOAT_CONVERTED;
        }
        if (!result || (!dots && NOTstrcasechr(str, 'e')))
            if (NOTstrcasechr(str, 'x'))
            {
                if (sscanf(str, "%d", in) == 1)
                {
                    result |= INT_CONVERTED;
                }
            }
            else 
                if(result)
                {
                    result |= INT_CONVERTED;
                    *in = (int)*db;
                }
        if (strstr(strlwr(str), "exit") != NULL) result = -1;

    } while (!result);
    return result;
}


int main(int argc, char **argv)
{
    double db;
    int in;
    int result;

    while ((result = ReadNumber(&db, &in)) != -1)
    {

        if (result & FLOAT_CONVERTED) printf("Float = %lf ", db);
        if (result & INT_CONVERTED) printf("Integer = %d ", in);
        printf("\n\r");

    }
    return 0;
}

Enter number: xfdsfdsfdsf
Enter number: rthdgfhghg
Enter number: 0x4567
Float = 17767.000000 Integer = 17767
Enter number: 3e67
Float = 30000000000000000978680950144401383192292617328216608963406365458432.000000
Enter number: 54567
Float = 54567.000000 Integer = 54567
Enter number: dfgdfgdfgdfgdgg
Enter number: 3456
Float = 3456.000000 Integer = 3456
Enter number: 12354654465454654654565567567576
Float = 12354654465454653961713368432640.000000 Integer = -1
Enter number: exit
Sandesh
  • 1,190
  • 3
  • 23
  • 41