Search code examples
cstringif-statementscanfstrcmp

Is it possible to input a variable of some sort and compare it with another variable at the same time in C?


I was wondering if there was possibly a way where one could scan a variable and then compare it all in the same line (same time).

So far I tried this:

if(strcmp((scanf("create.%s",comp)),comp)==0)          //Please do not mind any missed parentheses or something like that...

I know ^that doesn't work because I've tried it and it ended up with an error...

So how would one achieve such a task? Or is it impossible?


Solution

    1. It didn't work since scanf returns length, not char pointer
    2. The fact that you write it in the same line have nothing to do with the execution time, you might as well separate it into two parts.
    3. If you really really want to do that (and I see no reason to), you may do the following:

      char *superScanfWithString(const char *format, char * str) {
          scanf(format,str);
          return str;
      }
      ...
      if(strcmp((superScanfWithString("create.%s",comp)),comp)==0)