Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   C++/ C question (http://www.mac-forums.com/forums/os-x-development-darwin/138956-c-c-question.html)

njdevilfan26 01-27-2009 04:36 PM

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?

mystic_fm 01-27-2009 05:09 PM

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.)

njdevilfan26 01-27-2009 05:13 PM

thank you very much

njdevilfan26 01-27-2009 06:46 PM

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?

deer dance 01-27-2009 10:16 PM

The 0xffff is Hexadecimal, a counting language

It converts to: 307866666666

Where is this coming from?

njdevilfan26 01-27-2009 10:20 PM

its just something from an objective c book i did not understand

daimos 01-28-2009 12:10 AM

C looks for main() not Main() to start executing. been a while, i might be wrong.

deer dance 01-28-2009 07:27 AM

No, you're right.

njdevilfan26 01-28-2009 02:21 PM

ignore the syntax errors in the beginning of each line. What does the "0x" in front of 0xffff mean?

mystic_fm 01-28-2009 05:33 PM

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.

njdevilfan26 01-28-2009 10:48 PM

thanks


All times are GMT -4. The time now is 10:27 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.