简体   繁体   中英

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?

  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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM