Home > OS >  What does the following mean in context of C programming language?
What does the following mean in context of C programming language?

Time:12-02

From Modern C by Jens Gustedt,

Representations of values on a computer can vary “culturally” from archi- tecture 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 repre- sentation.

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.

CodePudding user response:

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

https://en.wikipedia.org/wiki/Two's_complement vs https://en.wikipedia.org/wiki/Ones'_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 intsis invalid, overflowing addition of twounsigned` is fine. 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

  • Related