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

  • Thread starter Thread starter jd12345
  • Start date Start date
  • Tags Tags
    Integer
AI Thread Summary
In C programming, the output of the code snippet `printf("%c\n", c);` where `int c = 1;` results in a non-printable character, specifically the ASCII control character for "Start of Heading" (SOH), which is why it appears as a 2x2 box. This occurs because the `%c` format specifier interprets the integer value as a character based on its ASCII code. In contrast, when using `int c = '1';`, the character '1' corresponds to the ASCII value of 49, which is printable, thus displaying the character '1' correctly. To print the integer value of `c`, the `%d` format specifier should be used instead of `%c`. Understanding the distinction between numeric values and their character representations is crucial in C programming.
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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top