-2

From Modern C by Jens Gustedt,

Representations of values on a computer can vary “culturally” from architecture to architecture or are determined by the type the programmer gave to the value. Therefore, we should try to reason primarily about values and not about representations if we want to write portable code.

If you already have some experience in C and in manipulating bytes and bits, you will need to make an effort to actively “forget” your knowledge for most of this section. Thinking about concrete representations of values on your computer will inhibit you more than it helps.

Takeaway - C programs primarily reason about values and not about their representation.

Question 1: What kind of 'representations' of values, is author talking about? Could I be given an example, where this 'representation' varies from architecture to architecture and also an example of how representations of values are determined by type programmer gave to value?

Question 2: What's the purpose of specifying a data type in C language, I mean that's the rule of the language but I have heard that's how a compiler knows how much memory to allocate to an object? Is that the only use, albeit crucial? I've heard there isn't a need to specify a data type in Python.

halfer
  • 19,824
  • 17
  • 99
  • 186
dexter
  • 185
  • 8
  • 1
    I'm not sure what point the author is trying to make, but: if you're thinking "representation", you might use `x >>= 1` to "efficiently" divide `x` by 2. But if you're thinking "value", you would write `x /= 2`. Another example: If you write `int i; int *ip = &i;` and if you're thinking representation, you think "`ip` contains the 32- or 64-bit address of `i` in my computer's address space." But if you're thinking value, you think "`ip` is a pointer to `int`, and currently it points to `i`." – Steve Summit Dec 01 '21 at 18:21
  • 1
    Me, I find both ways of thinking can be important — although either of them can get you into trouble, if you use it wrongly. – Steve Summit Dec 01 '21 at 18:22
  • 2
    Another example: If you declare a `struct`, and you're thinking representation, you add up the sizes of its members, and you discover that it's not the same as what `sizeof` gives you, and you ask about it on Stack Overflow, and someone closes your question as a duplicate of [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/questions/119123). But if you're thinking values, you take `sizeof(struct)` for granted, because you don't care how many bytes the structure has, that's the compiler's problem. – Steve Summit Dec 01 '21 at 18:30

1 Answers1

2

What kind of 'representations' of values, is author talking about?

https://en.wikipedia.org/wiki/Two%27s_complement vs https://en.wikipedia.org/wiki/Ones%27_complement vs https://en.wikipedia.org/wiki/Offset_binary. Generally https://en.wikipedia.org/wiki/Signed_number_representations.

But also the vast space of floating point number formats https://en.wikipedia.org/wiki/Floating-point_arithmetic#IEEE_754:_floating_point_in_modern_computers - IEEE 745, minifloat, bfloat16, etc. etc. .

Could I be given an example, where this 'representation' varies from architecture to architecture

Your PC uses twos complement vs https://superuser.com/questions/1137182/is-there-any-existing-cpu-implementation-which-uses-ones-complement .

Ach - but of course, most notably https://en.wikipedia.org/wiki/Endianness .

also an example of how representations of values are determined by type programmer gave to value?

(float)1 is represented in IEEE 745 as 0b00111111100000000000000000000000 https://www.h-schmidt.net/FloatConverter/IEEE754.html .

(unsigned)1 with 32-bit int is represented as 0b00.....0001.

What's the purpose of specifying a data type in C language,

Use computer resources efficiently. There is no point in reserving 2 gigabytes to store 8-bits of data. Type determines the range of values that can be "contained" in a variable. You communicate that "upper/lower range" of allowed values to the compiler, and the compiler generates nice and fast code. (There is also ADA where you literally specify the range of types, like type Day_type is range 1 .. 31;).

Programs are written using https://en.wikipedia.org/wiki/Harvard_architecture . Variables at block scope are put on stack https://en.wikipedia.org/wiki/Stack_(abstract_data_type)#Hardware_stack . The idea is that you have to know in advance how many bytes to reserve from the stack. Types communicate just that.

have heard that's how a compiler knows how much memory to allocate to an object?

Type communicates to the compiler how much memory to allocate for an object, but it also communicates the range of values, the representation (float vs _Float32 might be similar, but be different). Overflowing addition of two int's is invalid, overflowing addition of two unsigned is fine and wraps around. There are differences.

Is that the only use, albeit crucial?

The most important use of types is to clearly communicate the purpose of your code to other developers.

char character;
int numerical_variable;
uint_least8_t variable_with_8_bits_that_is_optimized_for_size;
uint_fast8_t variable_with_8_bits_that_is_optimized_for_speed;
wchar_t wide_character;
FILE *this_is_a_file;

I've heard there isn't a need to specify a data type in Python.

This is literally the difference between statically typed programming languages and dynamically typed programming languages. https://en.wikipedia.org/wiki/Type_system#Type_checking

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • That's very helpful. I have a follow up question, please help me out with that one as well. Its closely related. https://stackoverflow.com/questions/70193728/how-do-data-types-ensure-portability-platform-independence-answer-with-respect – dexter Dec 02 '21 at 03:19