0

I am trying to execute this simple code for printing hexadecimal values in decimal and floating point format but it doesn't print anything.

Can anyone help..

#include<stdio.h>
#include<stdlib.h>
int  main()
{
    char content[]={0x24,0x00};
    int i;
    for(i=0;i<2;i++)
    {
        printf("%d",content[i]);
        printf("%.1f",content[i]/10.0);
    }
    printf("%.2f",(100*(unsigned)content[0]+(unsigned)content[1])/1000.0);
}
4
  • 1
    Then you are doing something wrong; I get 363.600.03.60, so it does print 'anything'. "Execute" ... you are aware you need to compile, and optionally link, for your OS, and then execute the thus-created executable file? Commented Jan 7, 2014 at 12:35
  • yup..I am aware of these facts. I dnt know why its not printing anything. Commented Jan 7, 2014 at 12:38
  • 2
    You need to include a "\n" on the end of the printf - I bet it's just buffered. Commented Jan 7, 2014 at 12:39
  • Thanx vicky..Putting \n actually worked Commented Jan 7, 2014 at 12:41

1 Answer 1

2

First of all, there is no such thing as "hexadecimal value".

A numeric value can be either integer or non-integer.

  • For integer values, there are several optional sizes - 1, 2, 4 or 8 bytes.

  • For non-integer values, there are two optional sizes - 4 bytes (float) or 8 bytes (double).

Your 'content' variable is an array that contains two integers, the size of each one being 1 byte.

Now that we've clarified this, let's refer to the actual question at hand.

General coding problems:

  1. Add a space or a newline character '\n' at the end of each print.

  2. You must return a value at the end of function 'main' (you may simply return 0).

  3. In the last two calls to 'printf', cast the argument to 'float', because the compiler might "send" it as 'double' by default, in which case 'printf' will treat an 8-byte value as a 4-byte value, and you will most likely get an incorrect printout.

The corrected code:

#include<stdio.h>
#include<stdlib.h>
int  main()
{
    char content[]={0x24,0x00};
    int i;
    for(i=0;i<2;i++)
    {
        printf("%d\n",content[i]);
        printf("%.1f\n",(float)(content[i]/10.0));
    }
    printf("%.2f\n",(float)((100*(unsigned)content[0]+(unsigned)content[1])/1000.0));
    return 0;
}

Having said that, I'm not really sure why you don't get any prints at all...

Sign up to request clarification or add additional context in comments.

2 Comments

Since C99 main as an exception doesn't require a return statement.
Variadic arguments of type float are automatically promoted to double, aren't they? Much like any integer narrower than int will be promoted to int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.