C programming question (beginner)

Click For Summary

Homework Help Overview

The discussion revolves around a C programming assignment focused on understanding the output of a printf statement that uses a hexadecimal escape sequence. Participants are exploring the implications of using hexadecimal notation in character representation.

Discussion Character

  • Conceptual clarification, Assumption checking

Approaches and Questions Raised

  • Participants discuss the meaning of the hexadecimal escape sequence '\x4d' and its relation to ASCII values. Questions arise regarding the interpretation of hexadecimal numbers and their conversion to decimal.

Discussion Status

Some participants have provided clarifications about the hexadecimal representation and its connection to ASCII characters. There is an ongoing exploration of related concepts, such as the difference between hexadecimal and decimal representations, but no explicit consensus has been reached.

Contextual Notes

Participants mention the need for understanding hexadecimal notation and its application in programming, indicating a potential gap in knowledge regarding number systems.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
Here's a question I am working on for a c programming assignment.

If the following printf statement was part of a complete program, what would it print?

printf("%c",'\x4d');

Now, I know this prints M because I ran it, and I know the output is a single character because %c is a single character conversion spec. But I don't understand what value '\x4d' is returning. Is it just producing the character M and if so why?

Thanks for your help!
 
Physics news on Phys.org
'\x4d' simply produces the character which has the ASCII code 4D (or 77 in decimal). It happens to be M.
 
the \xhh form, where hh are two hex digits, is interpreted by the compiler into the actual byte hh.

- Warren
 
ahhh.. I wondered if it might be ASCII.
Thanks, Muzza, you're a peach!
 
Come again, Warren..? So I am dealing with a hex #?
 
Math Is Hard said:
Come again, Warren..? So I am dealing with a hex #?
All ASCII characters are encoded as a binary number using eight binary digits. Out of conveniance, it is easier to write the equivalent hexadecimal number rather than the binary. It's just maths.
 
OK, I think I get it. I have forgotten hexadecimal notation and had to punch it in a calculator for hex to base 10 translation.

If i had used '\d77' would I have gotten the same result?
 
hmm.. I guess not. I just tried it.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
69
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K