Eval in C?

This is a discussion on Eval in C? within the C Programming forums, part of the General Programming Boards category; Hello again! Is there such a thing as eval in C? In my C-file I have this defined #define doSomething ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    15

    Question Eval in C?

    Hello again!

    Is there such a thing as eval in C?

    In my C-file I have this defined

    #define doSomething (0x123456)
    #define doSomethingElse (0x100efe2)


    Then I will like to read an imput from a file an instead of writing the hexadecimal value I would like to write doSomethingElse, in that way my user will know what he7she is doing and would not need to memeorize the hexadecimal values?

    Any tips?

    /Steffi

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Eval is not possible with C code. Though probably what you had seen was machine code "building" on runtime. I don't think that's something for beginners though...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Well simple name=value type substitutions are relatively easy to implement with a simple look up table of sorts.

    You start with say
    int doSomethingElse = 0x100efe2;

    If you then read say
    doSomethingElse
    from the file, you can use a table of names to find the corresponding symbol.
    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.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well I know C is evil, but I have no idea what you mean by "Eval"?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Thinking laterally:
    You could of course create a little file like this:
    Code:
    // myfile.p
    #define doSomething (0x123456)
    #define doSomethingElse (0x100efe2) 
    
    #include myfile
    and run that through the C-preprocessor [e.g gcc -E myfile.p]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by cpjust View Post
    Well I know C is evil, but I have no idea what you mean by "Eval"?
    Eval is found in Perl and other scripting languages (I think -- I only know Perl). It is used to evaluate a string as if it were code.

    @steffi
    Can you describe what you are trying to do?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Well I know C is evil, but I have no idea what you mean by "Eval"?
    Evaluate, ie evaluate a string as C code at runtime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. eval command
    By jas_atwal in forum Linux Programming
    Replies: 1
    Last Post: 11-09-2007, 10:32 PM
  2. codes
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-18-2005, 02:09 PM
  3. String Manipulation
    By Nitelite in forum C Programming
    Replies: 1
    Last Post: 10-05-2002, 04:05 PM
  4. using information from an array
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 04:30 PM
  5. Problems about this eval() function
    By hei in forum C Programming
    Replies: 1
    Last Post: 11-17-2001, 12:58 PM

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