LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   GCC if statement where variable is not compared to anything after strcmp. (https://www.linuxquestions.org/questions/linux-software-2/gcc-if-statement-where-variable-is-not-compared-to-anything-after-strcmp-4175547727/)

Arcosanti 07-10-2015 09:12 PM

GCC if statement where variable is not compared to anything after strcmp.
 
How does the GCC compiler interpret this:

Code:

returnval  = strcmp(buffer, BUFLEN)
if(returnval)
    {
    returnval = strcmp(buffer, BUFLEN2)
    }

I can't find anything online about this.

astrogeek 07-11-2015 12:00 AM

Quote:

Originally Posted by Arcosanti (Post 5389917)
How does the GCC compiler interpret this:

Code:

returnval  = strcmp(buffer, BUFLEN)
if(returnval)
    {
    returnval = strcmp(buffer, BUFLEN2)
    }

I can't find anything online about this.

I am not sure I understand the point of the question, but your edits since I first read the post help, so my comments for what they are worth...

First, strcmp takes two const char pointers to NULL terminated strings as arguments, so if your variable names indicate their types, BUFLEN and BUFLEN2 are not likely to be valid strings for comparison to buffer.

We will ignore absence of statement ending ;'s and treat this as pseudo code.

But let's assume that buffer, BUFLEN and BUFLEN2 are all valid pointers to null terminated strings, then I would read the result as follows:

If buffer == BUFLEN, returnval == 0, then the second strcmp() will not be executed.
If buffer != BUFLEN, returnval != 0, then the second strcmp() will be executed.

berndbausch 07-11-2015 12:23 AM

Quote:

Originally Posted by Arcosanti (Post 5389917)

I can't find anything online about this.

http://linux.die.net/man/3/strcmp

Or do you mean how this is translated? Use the gcc -S option.

michaelk 07-11-2015 07:41 AM

Basically an if statement expression i.e. if (expression) evaluates as either true of false with true being a non zero value. Therefore if returnval (in this case it is a valid expression) is not zero the statements within the if block are executed.

Arcosanti 07-11-2015 10:57 AM

Quote:

Originally Posted by berndbausch (Post 5389952)
http://linux.die.net/man/3/strcmp

Or do you mean how this is translated? Use the gcc -S option.

No, I am wanting to know about how the if statement is being used in this case. Usually it is used to compare a value against another value. In this particular case it is not. I am trying to understand how it works in this case. I just added in the strcmp in as I think it is central to understanding how the if statement is being used. However I was not familiar with the -S option, so I'll look into that.

Quote:

Originally Posted by astrogeek (Post 5389948)
I am not sure I understand the point of the question, but your edits since I first read the post help, so my comments for what they are worth...

First, strcmp takes two const char pointers to NULL terminated strings as arguments, so if your variable names indicate their types, BUFLEN and BUFLEN2 are not likely to be valid strings for comparison to buffer.

We will ignore absence of statement ending ;'s and treat this as pseudo code.

But let's assume that buffer, BUFLEN and BUFLEN2 are all valid pointers to null terminated strings, then I would read the result as follows:

If buffer == BUFLEN, returnval == 0, then the second strcmp() will not be executed.
If buffer != BUFLEN, returnval != 0, then the second strcmp() will be executed.

You are correct on BUFLEN and BUFLEN2. I should have called them STRING and STRING2. Yes I did forget the ';'. :-)

Quote:

Originally Posted by michaelk (Post 5390042)
Basically an if statement expression i.e. if (expression) evaluates as either true of false with true being a non zero value. Therefore if returnval (in this case it is a valid expression) is not zero the statements within the if block are executed.

That's what I was wanting to know. So, it behaves like a boolean operation then.

berndbausch 07-11-2015 05:58 PM

Quote:

Originally Posted by Arcosanti (Post 5390081)
No, I am wanting to know about how the if statement is being used in this case. Usually it is used to compare a value against another value.
(...)
So, it behaves like a boolean operation then.

I would say that in any programming language, an if statement is not usually used to compare two values. It is used to execute code conditionally. The condition can be an expression of any complexity containing any number of values. C in particular accepts any expression, as the language has no "boolean" type.

sundialsvcs 07-13-2015 03:54 PM

Actually, it's a bug. (At least, "as written.")

The first two arguments to the strcmp() function must be char *s. In the examples shown, neither second-argument is likely to be such a pointer.

If we "glide over the probable typos" here, the intent of this logic is to compare two strings using one of two possible lengths. If the first attempt results in a value that is "true-thy" (i.e. the result is "not-equal"), a second compare is done using a different maximum length.


All times are GMT -5. The time now is 10:09 PM.