Why is my C integer printing as a non-printable character?

  • Thread starter Thread starter jd12345
  • Start date Start date
  • Tags Tags
    Integer
Click For Summary

Discussion Overview

The discussion revolves around the behavior of printing integer values as characters in C programming, specifically focusing on the output when using the printf function with different integer values. Participants explore the implications of using ASCII values and the distinction between numeric values and character representations.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes their experience with printing an integer value of 1 using printf("%c\n", c) and notes the unexpected output of a 2x2 box.
  • Another participant explains that in C, characters are represented by their ASCII codes, and that the integer 1 corresponds to a non-printable control character.
  • A participant clarifies the difference between the numeric value 1 and the character '1', stating that '1' has an ASCII value of 49.
  • One participant confirms their testing of the code on a Linux system using GCC, reiterating the output observed.
  • Another participant suggests that the box output occurs because the ASCII value of 1 is a non-printable character and recommends using the %d format specifier to print the integer value instead of %c.

Areas of Agreement / Disagreement

Participants generally agree on the distinction between numeric values and their character representations, as well as the behavior of non-printable ASCII characters. However, there is some confusion regarding the initial description of the output, with differing interpretations of what constitutes the "box" output.

Contextual Notes

Some participants reference the ASCII table to explain the behavior of the output, but there is no consensus on the clarity of the initial description of the output as a "2x2 box." Additionally, the discussion does not resolve the confusion surrounding the terminology used to describe the output.

jd12345
Messages
251
Reaction score
2
Ok so I was trying something in C when i found something i don't understand:-
code is:-
int c = 1;
printf("%c\n",c);
(not writing the include and return 0 stuff)

so when i run this it gives an output of a 2x2 box like this:-
0 0
0 1

whats happening. When i put the 1 in commas like this int c= '1' then it gives an ouput 1. Thats good but when i don't put the comma it gives this box, why?
 
Technology news on Phys.org
jd12345 said:
Ok so I was trying something in C when i found something i don't understand:-
code is:-
int c = 1;
printf("%c\n",c);
(not writing the include and return 0 stuff)

so when i run this it gives an output of a 2x2 box like this:-
0 0
0 1

whats happening. When i put the 1 in commas like this int c= '1' then it gives an ouput 1. Thats good but when i don't put the comma it gives this box, why?

I don't understand what you're talking about with the 2x2 box...

In C a character is a number: specifically, the value of a character is its ASCII code. Some characters, such as those whose ASCII codes are in the range 0 - 31, are nonprinting control characters.

There is a difference between the number 1 and the numeric character '1'. The value of the first is 1, of course, but the value of '1' is 49.

You can convince yourself of this by running this code.
Code:
printf("%c\n", 1);  // You won't see anything on the screen.
printf("%d\n", '1'); // Should print 49.

BTW, this-- , -- is a comma. This -- ' -- is a single quote.
 
oh sorry about the single quote and comma error

printf("%c\n",1); prints the box.
I tried it - on linux (ubuntu) with GCC (if that matters)
 
The 'box' you are seeing is what happens when you print one of the non-printable ASCII chars.

int c = 1; // assign a new int the value 1
int c = '1'; // assign a new int the integer value of the character '1' (which is 49)

printf("%c", c); // print the character representation of the integer variable c
printf("%d", c); // print the integer representation of the integer variable c

Have a look at the ASCII table. Notice that the integer 1 represents the SOH (start of heading) character which is not printable. If you want to print the number one as stored in your integer variable, you should use the %d format specifier not %c.
 

Similar threads

Replies
7
Views
2K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K