LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-04-2004, 01:16 PM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
wacky syntax, i see it in c and java


in alot of many different lanuages i see a statement with a '?' and two statements seperated by a colon( : ), i'm not sure what this really means. I thought it means if the statement is true pick the first one, else execute the second command. but then i have a statement that threw me for a loop:

int base = skull_port_base ? skull_port_base
: SKULL_PORT_FLOOR;


i'm not sure what this means? can some explain what is happen in this line of code. thx

Last edited by djgerbavore; 08-04-2004 at 01:19 PM.
 
Old 08-04-2004, 01:35 PM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
You're right; the basic format is an (if ? then : else) statement, and the whole thing evaluates to a single expression of the same type as "then" or "else".

Quote:
int base = skull_port_base ? skull_port_base
: SKULL_PORT_FLOOR;
Let's break this down. The “int base” means to define a new integer variable called “base”, and this line of code will initialise it (set its starting value).

The initial value will be the result of the expression “skull_port_base ? skull_port_base : SKULL_PORT_FLOOR;”

“skull_port_base” is a variable that is an integer (or can be cast to an integer, such an unsigned integer or a char, or a C++ bool).

When testing to see if some expression is true or not, C (and C++) first casts it to an integer, and then checks to see if it equals zero. To veryfy this, note the following program prints 0:
Code:
#include <stdio.h>

void main() {
  printf("%d\n", 1 == 2);
}
If an expression evaluates as zero, it is considered to be false. Anything else is true.

So if “skull_port_base” is not zero, the conditional expression will evaluate as the value of “skull_port_base”. If it is zero, it will evaluate to “SKULL_PORT_FLOOR” instead.

Hope that helps,

— Robert J. Lee
 
Old 08-04-2004, 02:01 PM   #3
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
that was very clear, you explained your self very well, thanks again.
 
  


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
iPodder and it's wacky wxPython prerequisite toaster.waffle Linux - Software 2 05-03-2005 11:00 PM
wacky DNS resolution problem Rotwang Linux - Networking 3 04-04-2005 06:04 PM
Wacky errors on harddrive redbeansnrice Linux - Hardware 26 03-02-2005 09:47 AM
computer gone wacky corbintechboy Slackware 10 10-16-2003 01:22 AM
Java syntax highlighting in vim Phantomas Linux - Software 0 08-16-2003 04:34 AM

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

All times are GMT -5. The time now is 07:00 AM.

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