GCC if statement where variable is not compared to anything after strcmp.
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Last edited by astrogeek; 07-11-2015 at 12:43 AM.
Reason: More precise arguments to "let's assume"... fixed typos...
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.
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
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.