CompOrg Fall 2005 - HW1 FAQ

Click on a question to expand it (for the details).
Click on the question title again to hide the details.

+ argc, argv

Question:

How do I read in the list of numbers from the command line?


Answer:

argc is the total number of command line parameters (including the name of the program itself), so argc-1 is the number of integers you need to put in an array.

argv[1] is the address of the ASCII representation of the first number, you can convert this to an intger with the atoi() function.

We've looked at programs that deal with command line parameters, here is one example: add2nums.c

+ binary output

Question:

How do I print an int in binary? printf("%b",x)?


Answer:

printf does not support printing in binary, so you have to write the code yourself. Basically you need to look at the MS bit (we did this in lab!), if it's a 1 call printf("1") otherwise printf("0"). Now shift the int 1 place to the left, and repeat (do this 32 times for an int).