LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2005, 08:23 AM   #1
cigarstub
Member
 
Registered: Sep 2005
Posts: 145

Rep: Reputation: 15
(sorry I do not intend to be insolent) ab?a:b operation


Could you understand the following C header function:
#define a (x, y) xy?x:y
 
Old 09-27-2005, 09:12 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
1. Are you sure that there's whitespace between the 'a' and the '('?
2. Have you read a book about C or C++? If not, you should definitely do that. Otherwise you should definitely read it again.
What's it you don't understand? The definition of a macro or the ?: operator?
 
Old 09-27-2005, 10:06 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
That exact syntax seams to be invalid (xy isn't defined) but maybe this will help.

<condition>?<conditons is true>:<condition is false>;

So, in the following code:

Code:
#include <stdio.h>
#define a(x, y) x>y?printf("true\n"):printf("false\n")

int main () {
    a(1,2);
    a(2,1);
    return 0;
}
The output is:

Code:
j_shaw@jshaw ~ $ ./test
false
true

Last edited by jtshaw; 09-27-2005 at 10:13 AM.
 
Old 09-29-2005, 03:12 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Your example is misquoted.

? ternary op is basically an if/else.
#define is a macro, text is substituted where found.

If you have this file:

Code:
#define largest(x, y) x > y?x:y

#define A "your question"
#define B "answer"
#define C "does this"

main()
{
   largest(10,11);
   char a[] = C B A;
}
and compiile with -E you get something like this:
Code:


main()
{
   10 > 11?10:11;
   char a[] = "does this" "answer" "your question";
}
 
  


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
Operation not permitted jakkals Linux - General 3 09-26-2005 07:30 PM
Needs help with bittorent operation... Ausar Linux - Software 3 08-28-2004 06:38 PM
If you intend to install / try MandrakeLinux 10 Community ... ernie Mandriva 1 03-21-2004 10:22 AM
Operation not permitted FrozenShadow23 Linux - Newbie 6 10-05-2003 05:13 PM
Help with 'while' loop operation ..... purpleburple Programming 13 09-04-2002 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 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