strange operator

This is a discussion on strange operator within the C++ Programming forums, part of the General Programming Boards category; hello everyone. i would like to know what this line of code means and what is the operator used over ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    18

    strange operator

    hello everyone.

    i would like to know what this line of code means and what is the operator used over here ??? is it a compund assignment or what is it ??

    and also how this code works ??


    ans<?=c;

    where ans and c are some integer variables.

    thanks in advance.

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeĺ, Sweden
    Posts
    555
    It's use is to cause syntax errors.

  3. #3
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Code:
    ans<?=c;
    Maybe, it was supposed to be written as:

    Code:
    ans<=c;
    Testing if the value of 'ans' is less than or equal to value of 'c'.
    (I dunno, im still new here though)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    It might be some variation of this
    http://gcc.gnu.org/onlinedocs/gcc-4....l#Conditionals

    The OP should
    - state the compiler in question
    - post more than one line to estabish the context for the question.

    Hell, it might not even be C code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    18
    OOPS i am really sorry .

    its actually a C++ piece of code. it does give a compile error on gcc.

    but anyway if anybody knows C++ then i would appreciate their help very much

    here is the code........................

    # include <iostream>

    // I use Dev-Cpp .So <conio.h> is allowed
    # include <conio.h>

    using namespace std;

    int main()
    {
    int ans=10,c=35;

    ans<?=c;

    cout<<ans<<endl;


    ans>?=c;

    cout<<ans<<endl;

    getch();
    return 0;
    }


    here is the output

    10
    35

    Oh and I am very sorry by the way. I should have seen the code clearly before posting it and it should have been posted in the right place.
    Last edited by abhi_86; 08-21-2006 at 05:38 AM.

  6. #6
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    This is C forum, you may find your answer to C++ forum

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Moved to right forum.

    > it does give a compile error on gcc.
    gcc is a C compiler
    g++ is a C++ compiler.
    Did you save your file as prog.c or prog.cpp ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Probably compiled with g++ and for me there are no diffrences if prog.c or prog.cpp
    Last edited by andor; 08-21-2006 at 06:26 AM.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Where did you get that code, abhi_86?

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    18
    the code is purely mine. but i had seen those operators somewhere. (i dont remember where exactly).

    but as Salem has pointed it out it must be a variation of ternary operator (combined with the assignment operator i think ). but i am not very sure..........

    Did you save your file as prog.c or prog.cpp ?
    i have saved the file as prog.cpp
    i get compile error if i save it as prog.c

    i definitely think its a valid expression because it produces two different answers when used with '<' and '>' and it doesnt give any compile error.

    but i would like to know how exactly this works ........

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Read more of the manual which I linked in an earlier post (I guess).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    These operators are GNU extensions to C++, they do not exist in standard C++.

    This code:
    a <?= b;

    Will compare a and b and assign the smaller value to a.

    Or it will generate compiler errors on compilers that don't support these operators.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Pfff, and some people give out about getch() from conio being non standard. Why not just go with the good olde ternary: a<=b ? a=b : a=a; or something better? <- Rhetorical
    Last edited by twomers; 08-21-2006 at 12:19 PM.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Language extensions are pretty much common across compilers. The idea behind some of them illudes me. But the curious thing is that you always find them to be useful. The above operation is very common, so it was decided that <? and >? would be added to the available operators. This is just syntactic sugar. But other extensions are that have a more meaningful existence.

    For example, GNU C++ allows the $ sign to be used in identifiers. This is so for compatibility with previous C code. So, some extensions may be added to a compiler to guarantee some form of backwards compatibility.

    Other type of extensions, probably the most interesting too, are those which provide functionality described in C99.

    Other reason why a compiler may offer extensions is to provide functionality that, if otherwise inexistent, would involve a lenghty piece of code. I'm sure there are many more reasons.

    But the important thing to retain is that in no way a language extension is a measurement of how well a compiler implements the standards. The use of the -pedantic switch will reduce extensions to errors.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Yeah, but in this case it's not a lengthly piece of code that's replaced. Either of these work:

    if (b < a) a = b;
    a = (a < b) ? a : b;

    Even the also nonstandard:
    a = min(a,b);

    is better in my opinion. Someone who had learned only standard C++ and no extensions would easily infer what a= min(a,b) meant, while a<?=b is not nearly so obvious.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange problem with operator overloading
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2008, 10:18 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. strange linking error with operator overloading
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 07-03-2006, 07:32 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21