Загрузка...

Understanding Why ASCII Characters Represent Differently in C: \016 vs \020

Explore the quirks of ASCII character representation in C, particularly with characters like ASCII 14 and 16, and learn why they show up as `\016` and `\020`.
---
This video is based on the question https://stackoverflow.com/q/66093614/ asked by the user 'Peabrain' ( https://stackoverflow.com/u/11853015/ ) and on the answer https://stackoverflow.com/a/66093754/ provided by the user 'chqrlie' ( https://stackoverflow.com/u/4593267/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: ASCII character 14 (and others) when assigned in string in C

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding Why ASCII Characters Represent Differently in C: \016 vs \020

When working with C programming, particularly in situations where you deal with ASCII values, you might encounter some peculiarities that could potentially confuse even experienced developers. One such situation arises when you assign values to characters but notice their representation in memory does not match our expectations. This guide delves into the case of ASCII characters 14 and 16, explaining how they are represented in C and clarifying the octal notation used for these values.

The Problem: ASCII Characters in Memory

Consider the following snippets of C code that allocate memory for a character string and assign different ASCII values:

[[See Video to Reveal this Text or Code Snippet]]

When checked in GDB (GNU Debugger), the memory representation shows:

[[See Video to Reveal this Text or Code Snippet]]

Similarly, when you use:

[[See Video to Reveal this Text or Code Snippet]]

You will see:

[[See Video to Reveal this Text or Code Snippet]]

The confusion arises: Why does ASCII character 14 map to \016 and ASCII 16 to \020? Furthermore, why do two ASCII values (16 and 20) seem to lead to the same representation in memory?

Understanding the Representation

Octal vs Decimal

The crux of the confusion lies in how C handles character representations. In C, characters can be expressed in octal notation as well as decimal.

ASCII 14 in decimal is written as 016 in octal.

ASCII 16 in decimal is written as 020 in octal.

ASCII 20 in decimal corresponds to 024 in octal.

Why Octal?

Using octal (base 8) for character representation dates back to the early days of computing. In the 1960s, some computers had 6-bit characters that could fit into 3 bits per octal digit. This made octal a convenient representation for grouping binary bits.

Other Examples:

ASCII 32 (space) is represented as 040 or \x20.

This octal representation has been inherited by C and many languages that borrow syntax from it.

Clarifying the Confusion: Two Characters, One Representation

Now, regarding the observation that both ASCII 16 and ASCII 20 print as \020, it’s important to clarify that even though they appear the same in GDB, they are not identical values. The discrepancy arises from how GDB interprets the string; it presents them in a manner that emphasizes their representation rather than their underlying binary values.

Using GDB

Utilizing the command p input in GDB can result in:

[[See Video to Reveal this Text or Code Snippet]]

This perceived duplication may lead to the assumption that the underlying data is identical, which is not necessarily the case; each ASCII value has a distinct representation and meaning.

Conclusion

Understanding the core idea of octal representation in C helps demystify why certain ASCII characters display the way they do in memory. With this knowledge, you can better navigate potential confusions when working with non-ASCII characters and their representations. Always remember, ASCII values may be straightforward in concept, but their representation can carry historical nuances and conventions that are critical for a deeper understanding of programming in C.

Next time you assign ASCII values in your code, you'll have a clearer understanding of why they appear in memory as they do!

Видео Understanding Why ASCII Characters Represent Differently in C: \016 vs \020 канала vlogize
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять