Return 0; undeclared?

This is a discussion on Return 0; undeclared? within the C Programming forums, part of the General Programming Boards category; ok so my program in more or less to input a 16-bit hexadecimal number as a string of characters and ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    36

    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;
    }
    Last edited by Salem; 01-23-2011 at 11:14 PM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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); 
    
    }

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    i believe you need "%x" to read in hex number;
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    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?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by litzkrieg View Post
    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.

    http://en.wikipedia.org/wiki/Hexadecimal

    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.
    Last edited by CommonTater; 01-23-2011 at 09:11 PM.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    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!!!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by litzkrieg View Post
    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.
    Last edited by CommonTater; 01-23-2011 at 09:16 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  3. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21