C programming question (beginner)

In summary, the conversation discusses a printf statement in a C programming assignment that prints a single character. The character is produced by the ASCII code 4D, which is returned by the '\x4d' value. The conversation also mentions the convenience of using hexadecimal notation for ASCII characters. Using '\d77' would not produce the same result.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
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
  • #2
'\x4d' simply produces the character which has the ASCII code 4D (or 77 in decimal). It happens to be M.
 
  • #3
the \xhh form, where hh are two hex digits, is interpreted by the compiler into the actual byte hh.

- Warren
 
  • #4
ahhh.. I wondered if it might be ASCII.
Thanks, Muzza, you're a peach!
 
  • #5
Come again, Warren..? So I am dealing with a hex #?
 
  • #6
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.
 
  • #7
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?
 
  • #8
hmm.. I guess not. I just tried it.
 

What is C programming?

C programming is a high-level programming language used for developing operating systems, databases, and other complex programs. It was developed in the 1970s by Dennis Ritchie and is still widely used today.

What are the basic concepts of C programming?

The basic concepts of C programming include variables, data types, control structures (such as if/else statements and loops), functions, arrays, and pointers. These concepts are essential for writing programs in C.

How do I declare a variable in C?

To declare a variable in C, you must specify the data type and a name for the variable. For example, to declare an integer variable named "num", you would write: int num;

What is the difference between = and == in C?

The = symbol is used for assignment in C, meaning it assigns a value to a variable. The == symbol is used for comparison, meaning it checks if two values are equal. For example, the statement "x = 5" assigns the value 5 to the variable x, while the statement "if (x == 5)" checks if the value of x is equal to 5.

How do I print something to the screen in C?

To print something to the screen in C, you can use the printf() function. For example, to print the string "Hello World" to the screen, you would write: printf("Hello World");

Similar threads

  • Introductory Physics Homework Help
Replies
11
Views
763
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
735
  • Engineering and Comp Sci Homework Help
Replies
3
Views
883
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
504
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top