# Return 0; undeclared?

• 01-23-2011
litzkrieg
Return 0; undeclared?
ok so my program in more or less to input a 16-bit hexadecimal number as a string of characters and print its decimal version to the monitor and ive worked out a ton of bugs i really am a novice but i have a good portion of the program to work, but all i get is this really large number can someone point out what i am doing wrong and tell me how i can fix it? thank you!!
heres the code:

Code:

```#include<stdio.h> int main(void) { unsigned int hex_num[4]; unsigned total;         printf("Enter Number here:_"); scanf("%C%C%C%C", &hex_num[0],&hex_num[1],&hex_num[2],&hex_num[3]);         if(hex_num[2]<(0*40)) {                 total=(hex_num[2]-0*30)*16;}         else {                         total=(hex_num[2]-0*41+10)*16;}         if(hex_num[3]<(0+40)) {                 total=(hex_num[3]-(0*30)*16);}         else {                 total=(hex_num[3]-(0*41+10)*16);} printf("number is: %i", total); return 0; }```
• 01-23-2011
tabstop
What, exactly, is the difference between "the decimal version of a 16-bit number" and "a really large number"?

Also, use code tags.

Also, 0*40 is not what you want, nor is 0*30, nor is 0*41. You should probably investigate a) the difference between 0 and '0' and b) the order of operations.
• 01-23-2011
CommonTater
WOW... hard way...

Code:

``` // hex to decimal #include <stdio.h> int main (void) { unsigned int hex; puts("Enter up to 8 hex digits: "); scanf("%x", &hex); printf ("In Decimal : %d\n\n",hex); }```
• 01-23-2011
nimitzhunter
i believe you need "%x" to read in hex number;
• 01-23-2011
litzkrieg
WOW CommonTater THANK YOU

Thank you i never knew it could really be that easy so would you could i count those 8 hex digits as a total of 16bits?
• 01-23-2011
CommonTater
Quote:

Originally Posted by litzkrieg
WOW CommonTater THANK YOU

Thank you i never knew it could really be that easy so would you could i count those 8 hex digits as a total of 16bits?

8 hex digits is 32 bits... four bits per digit.

If for some reason you want to limit it to 16 bits you can use an unsigned short for your hex variable
and then use scanf("%4x", &hex) to get the input.
• 01-23-2011
litzkrieg
PERFECT thank you CommonTater now i just have a couple other questions for my prof. tomorrow on some other stuff but Thank you every much just saved me another 3 hours of headache!!!
• 01-23-2011
CommonTater
Quote:

Originally Posted by litzkrieg
PERFECT thank you CommonTater now i just have a couple other questions for my prof. tomorrow on some other stuff but Thank you every much just saved me another 3 hours of headache!!!

Yes, well.... be shure to tell me how I did after you hand in my homework ;)

Keep in mind that the assignment might well have been for you to demonstrate that you know how scanf() makes the conversion, not simply producing the right answer.
• 01-23-2011
tabstop
We should be taking bets on which of the following two happens:
1. OP finds himself doing the three hours of pain tomorrow anyway
2. Professor finds himself rewriting the assignment before assigning it again.
• 01-23-2011
CommonTater
Quote:

Originally Posted by tabstop
We should be taking bets on which of the following two happens:
1. OP finds himself doing the three hours of pain tomorrow anyway
2. Professor finds himself rewriting the assignment before assigning it again.

3. Both.