C++/ C question

Joined
Jan 27, 2009
Messages
8
Reaction score
0
Points
1
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?
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's 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.)
 
OP
N
Joined
Jan 27, 2009
Messages
8
Reaction score
0
Points
1
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?
 
Joined
Jan 23, 2009
Messages
162
Reaction score
1
Points
18
Location
Indiana
Your Mac's Specs
Soon to own
The 0xffff is Hexadecimal, a counting language

It converts to: 307866666666

Where is this coming from?
 
Joined
Jan 28, 2009
Messages
5
Reaction score
0
Points
1
Your Mac's Specs
macbook 2.1ghz leopard
C looks for main() not Main() to start executing. been a while, i might be wrong.
 
OP
N
Joined
Jan 27, 2009
Messages
8
Reaction score
0
Points
1
ignore the syntax errors in the beginning of each line. What does the "0x" in front of 0xffff mean?
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's 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.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top