0

My assignment is to print the binary value of a decimal number, and I want to control the size of the array as I understood I should do so my program would work in all the compilers.

I don't understand briefly the operator sizeof, but I would appriciate if you can explain where should I, and why, put the sizeof in my program:

void translate_dec_bin(char s[]){
    unsigned int decNum;
    char st[MAX_LEN] = { 0 };
    int j = 0;
    sizeof(decNum, 4);
    decNum = atoi(s);

    while (decNum > 0){
        st[j] = decNum % 2;
        decNum = decNum / 2;
        j++;
    }

    while (j >=0){
        printf("%d", st[j]);
        j--;
    }
    printf("\n");

}

My thought is that when I print the number, i.e in the code:

printf("%d", st[j]);

I should put the operator. Is it right?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Assaf
  • 1,112
  • 4
  • 14
  • 35
  • 1
    `sizeof` is a unary operator: it takes only one operand, not two operands. – ouah Nov 10 '13 at 17:01
  • thank you @ouah. if i write decNum = sizeof(unsigned); will it help my goal? – Assaf Nov 10 '13 at 17:05
  • Sizeof takes an expression as operand. `expression1, expression2` is an expression. (but probably not what was intended) – wildplasser Nov 10 '13 at 23:02
  • @wildplasser: `sizeof` takes either an expression (specifically a *unary-expression*) or a parenthesized type name as its operand. (I personally wouldn't have described the later case as an operator applied to an operand, but that's how the standard describes it.) – Keith Thompson Nov 10 '13 at 23:15
  • Note: Be very, very careful when taking the `sizeof` an array. – Hot Licks Nov 10 '13 at 23:46
  • @KeithThompson: I know that. The problem is that in `sizeof(decNum, 4);` expression1,expression2 _inside_ the parentheses are still evaluated as a single expression, yielding a single operand. (that is maybe the reason why I really hate the excess parentheses in `sizeof (expression)` ) – wildplasser Nov 11 '13 at 00:58
  • @HotLicks: Applying `sizeof` to an array is perfectly safe -- as long as it's actually an array. The problem is that a function parameter that appears to be of array type is *adjusted* to pointer type. – Keith Thompson Nov 11 '13 at 07:28
  • @KeithThompson - That's basically what I said. It's very easy to think you're getting the size of the array when you're getting the size of the pointer. – Hot Licks Nov 11 '13 at 12:39

2 Answers2

1

sizeof is a unary operation, meaning it takes only one operand or argument.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
  • Thank you. @SunEric so how can it help me acheive the goal - my program will work in compilers where int the 128 bit. if i write decNum = sizeof(unsigned); will it help my goal? – Assaf Nov 10 '13 at 17:02
1

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

Sizeof is for measuring the byte-length of a datatype in C (and C++). So, if I were to write

size_t a = sizeof(int);

a will generally be equal to 4 (see Jonathan Leffler's comment). This is because a 32-bit integer requires 4 bytes of memory (32 bits/8 bits in a byte = 4).

Answering your question about portability, sizeof(int) should work on any compiler.

You might find this question useful: Is the size of C "int" 2 bytes or 4 bytes?

To set the size of your char array to the bit-size of an int, this should work:

const size_t intsize = sizeof(int) * 8;//sizeof returns size in bytes, so * 8 will give size in bits
char st[intsize] = { 0 };
Community
  • 1
  • 1
IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17
  • thank you. I understand the difference, but I can't get to understand how do I implement sizeOf in my program. @Namfuak – Assaf Nov 10 '13 at 17:11
  • Why do you need to know the size of the particular integral type your program is using? – IllusiveBrian Nov 10 '13 at 17:16
  • I am worried that my array will be too long for another compiler, so I want to set it to a certain size. – Assaf Nov 10 '13 at 17:26
  • Oh, I see what you are saying, I'll update my answer. – IllusiveBrian Nov 10 '13 at 17:52
  • 2
    Be cautious about asserting `sizeof(int) == 4`. Although that is the most common size, there have been computers where `sizeof(int) == 2` and `sizeof(int) == 8` and even `sizeof(int) == 1` (but then `CHAR_BIT == 64` so all values were 64-bit values, and `sizeof(short) == 1` too). – Jonathan Leffler Nov 10 '13 at 18:00
  • That's a good point Jonathan, especially considering the whole point of the question is handling ints of any size. – IllusiveBrian Nov 10 '13 at 18:09
  • I get an error: 3 IntelliSense: expression must have a constant value. maybe it is becuase in my main() function, the array has a constant size? MAX_LEN? @Namfuak – Assaf Nov 10 '13 at 18:12
  • Oh right, you need to use a constant in static array initialization. I'll update. – IllusiveBrian Nov 10 '13 at 22:57
  • Why not the whole portable route `const size_t intsize = sizeof(int) * CHAR_BIT;` – chux - Reinstate Monica Nov 11 '13 at 04:23
  • The 8 is bits in a byte, not char length. He's storing each binary value as a char in each array index. So, in order to store each bit in an int of machine-defined binary length, he needs to have an array that is as large as the binary length. The length of each index is already determined by the machine due to the static initialization. – IllusiveBrian Nov 11 '13 at 04:38