LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 07-10-2015, 09:12 PM   #1
Arcosanti
Member
 
Registered: Apr 2004
Location: Mesa, AZ USA
Distribution: Slackware 14.1 kernel 4.1.13 gcc 4.8.2
Posts: 246

Rep: Reputation: 22
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.

Last edited by Arcosanti; 07-10-2015 at 09:55 PM.
 
Old 07-11-2015, 12:00 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by Arcosanti View Post
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.

Last edited by astrogeek; 07-11-2015 at 12:43 AM. Reason: More precise arguments to "let's assume"... fixed typos...
 
Old 07-11-2015, 12:23 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Arcosanti View Post

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.
 
1 members found this post helpful.
Old 07-11-2015, 07:41 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
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.

Last edited by michaelk; 07-11-2015 at 07:50 AM.
 
Old 07-11-2015, 10:57 AM   #5
Arcosanti
Member
 
Registered: Apr 2004
Location: Mesa, AZ USA
Distribution: Slackware 14.1 kernel 4.1.13 gcc 4.8.2
Posts: 246

Original Poster
Rep: Reputation: 22
Quote:
Originally Posted by berndbausch View Post
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 View Post
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 View Post
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.
 
Old 07-11-2015, 05:58 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Arcosanti View Post
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.
 
Old 07-13-2015, 03:54 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Multiple string variable comparisons in an if statement in bash buee Linux - Software 4 01-02-2019 08:21 AM
[SOLVED] inserting variable in bash printf statement divyashree Programming 8 08-16-2013 12:36 AM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 08:39 AM
bash variable loses contents in mysql statement thedude2010 Programming 3 06-02-2006 04:15 AM
How stable are GCC 2.95.3, 3.0.4, and 3.1.0 compared to each other? MatMan5959 Linux - Software 0 06-04-2002 02:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration