Results 1 to 11 of 11

Thread: C++/ C question

  1. #1
    C++/ C question

    Member Since
    Jan 27, 2009
    Posts
    8
    C++/ C question
    What is the output of the following if its command line arguments were: sample 1 5 7

    Main(int argc, char *argv[]) {
    Int x;
    X = argv [1] + argv [2] + argv [3];
    Printf (%d, x);
    }

    I'm not sure what the argv[1] etc... does so does anyone know the output?

  2. #2
    C++/ C question

    Member Since
    Mar 15, 2007
    Posts
    161
    Specs:
    17" MacBook Pro, 2.33GHz C2D, 2GB RAM
    First off, "X" != "x", "Int" is not a defined type, "Printf" isn't a library function, etc. C is case sensitive; this won't even compile.

    Even if these compile errors were fixed, the code would end up adding together three character pointer values (i.e., addresses of places in the computer's memory), the sum of which may not even fit into an integer type, and in any event is certainly not going to yield a meaningful integer sum. It most certainly is NOT adding together the integer representations of the command line arguments (1 + 5 + 7 == 13), which is apparently what the programmer was hoping for.

    (By the way, argv[1] is addressing element #1 of a array named "argv". Element #1 is the index for the 2nd array element, since C arrays start at index 0.)

  3. #3
    C++/ C question

    Member Since
    Jan 27, 2009
    Posts
    8
    thank you very much

  4. #4
    C++/ C question

    Member Since
    Jan 27, 2009
    Posts
    8
    also I dont care about errors but can anyone explain this

    Main() {
    Unsigned int x = 0xffff;
    ~x;
    Printf (“%x”, x);
    }
    {

    what is 0xffff? and i know that ~ means one complement but what does it mean when ~x is printed?

  5. #5
    C++/ C question
    deer dance's Avatar
    Member Since
    Jan 23, 2009
    Location
    Indiana
    Posts
    162
    Specs:
    Soon to own
    The 0xffff is Hexadecimal, a counting language

    It converts to: 307866666666

    Where is this coming from?

    Proud Python Programmer/HTML Expert
    Believer in Mac OS 7

  6. #6
    C++/ C question

    Member Since
    Jan 27, 2009
    Posts
    8
    its just something from an objective c book i did not understand

  7. #7
    C++/ C question

    Member Since
    Jan 28, 2009
    Posts
    5
    Specs:
    macbook 2.1ghz leopard
    C looks for main() not Main() to start executing. been a while, i might be wrong.

  8. #8
    C++/ C question
    deer dance's Avatar
    Member Since
    Jan 23, 2009
    Location
    Indiana
    Posts
    162
    Specs:
    Soon to own
    No, you're right.

    Proud Python Programmer/HTML Expert
    Believer in Mac OS 7

  9. #9
    C++/ C question

    Member Since
    Jan 27, 2009
    Posts
    8
    ignore the syntax errors in the beginning of each line. What does the "0x" in front of 0xffff mean?

  10. #10
    C++/ C question

    Member Since
    Mar 15, 2007
    Posts
    161
    Specs:
    17" MacBook Pro, 2.33GHz C2D, 2GB RAM
    The 0x prefix tells the compiler that the number value "ffff" is given in base 16 (hexadecimal) rather than base 10 (decimal). In hexadecimal, A through F are digits just like 0 through 9.

    To understand this better, I recommend reading at least the first 1/3 or so of the Wikipedia page on hexadecimal.

  11. #11
    C++/ C question

    Member Since
    Jan 27, 2009
    Posts
    8
    thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. G3 question building question
    By OtakuboyT in forum Apple Desktops
    Replies: 1
    Last Post: 08-30-2009, 03:37 PM
  2. Noob question (MBPro question)
    By jcragg in forum Apple Notebooks
    Replies: 2
    Last Post: 01-21-2009, 07:04 AM
  3. Hardware Question + Linux Question
    By Sir Edwin CBE in forum Running Windows (or anything else) on your Mac
    Replies: 0
    Last Post: 11-20-2007, 11:53 AM
  4. Boot-Up Question Mark Folder Impending Doom Question.
    By bingwalker in forum OS X - Operating System
    Replies: 3
    Last Post: 07-10-2007, 05:19 AM
  5. Airport Question & Program Default Question
    By vicariousgeorge in forum Switcher Hangout
    Replies: 1
    Last Post: 03-01-2007, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •