When using the %c output conversion specifier in printf to display output

When using the %c output conversion specifier in printf to display output, the corresponding alphanumeric character for the ASCII code will be displayed on the command prompt screen, regardless of whether the variable is stored as an int or char type.






Aletha 「"Hmm, hmm-hmm~😊010101101010101001010101・・・




How is the description of a char type variable written in that world (Season 1)?


What was that again...


A char type variable can store a single character, such as an alphabet, number, or symbol, in "half-width characters.""




Aretha:"


'010000101010010101...'


Not just alphabets and numbers, but also "symbols" and other half-width characters...


Can symbols be stored in a char type variable too? How intriguing.


Well then, shall we try storing a symbol { in a char type variable 'a' and displaying it using printf output?"


#include<stdio.h>


int main(void)

{

char a='{';


printf("%c",a);


return 0;

}


Program output


{



Aretha:


"Symbols can really be stored in a char type variable and displayed using printf output.



"When storing a half-width symbol '{' in a char type variable, to make the compiler recognize it as a character, you enclose it in single quotes ' ' just like you would when writing a half-width alphabet or number such as 'a' or 'b', so it would be written as


char a = '{';


".


The ASCII code assigned to the image data of '{' was 123."




"If you use the '%c' output conversion specifier in printf to display the ASCII code 123, which corresponds to the image data of the symbol '{', it will be displayed on the command prompt screen. Here is the program."

👇


#include<stdio.h>


int main(void)

{

char a=123;


printf("%c",a);


return 0;

}


Program output


{





"Aletha: "Let's modify the program here and store 123 not in a char type variable 'a', but in an int type variable 'a'.


We will change the instruction 'char a = 123;' to 'int a = 123;'."




#include<stdio.h>


int main(void)

{

int a=123;


printf("%c",a);


return 0;

}


Program output


{




Aletha: "Like this, even if numeric data 123 is stored in a variable of type char as follows:

char a=123;

and if numeric data 123 is stored in a variable of type int as follows:

int a=123;

When outputting and displaying the numeric data 123 using the %c conversion specifier in printf,

the same { will be output."





Even if numeric data is stored in a variable of type char or int, when the data is output and displayed using the %c conversion specifier in printf, the image data of the corresponding single-byte character is output to the command prompt screen.


whether stored in a char type (1 byte) or an int type (4 bytes) in memory,

Due to data processing considerations, the numeric data is stored in memory as an int type (4 bytes) when using the %c conversion specifier in printf.



When the numeric data 1 is stored in a variable 'a' of type char (1 byte), it is stored in 1 byte of memory as:

00000001


However, when the following code is executed:

printf("%c",a);


The numeric data is converted to a 4-byte value and stored in a different memory location as:

00000000000000000000000000000001.






Next, let's try to store the value 12777777773, which exceeds the range of numeric data that can be stored in a variable of type char (-127 to 128), in a variable of type char by writing:


char a;

a = 12777777773;




Then, let's output the variable a using the %c conversion specifier in printf.



#include<stdio.h>


int main(void)

{

char a=12777777773;


printf("%c",a);


return 0;

}


Program output


m




Despite storing the value 12777777773, which exceeds the range of numeric data that can be stored in a variable of type char (-127 to 128), in a variable of type char as:


char a = 12777777773;


The program runs without displaying an error message such as "error: overflow (data is overflowing from memory)" and is executed normally.





The character "m" is being displayed on the command prompt screen. This is because, when we represent the number 1277777777 in binary, we get 001011111001100111010110101001101101. However, since a variable of type char can only store up to 8 bits, only the last 8 bits (01101101) were stored in memory. This is why this phenomenon occurs.







"01101101" represents the numerical data 109, so


char a=12777777773;


printf("%c",a);


is equivalent to


char a=109;


printf("%c",a);


when executed.



when you actually run the code instead of running:


char a = 12777777773;

printf("%c", a);


but


char a = 109;

printf("%c", a);


The output of the program is "m".


Solar: "So that's what happened. I finally understand why 'm' is displayed."



extra


Full-width characters such as Japanese (kanji, hiragana, and katakana) are called "2-byte characters."


2-byte character has a data size of 2 bytes (16 bits).


For example, it contains a sequence of 16 bits consisting of 0s and 1s, such as:


1100110011111111



The image data of 2-byte characters is assigned with data such as "1100110011111111" for each character, and this "1100110011111111" can be used to call and use the image data of 2-byte characters.


It is not possible to store a 2-byte character in a char type since it can only store up to 8 bits per byte, which is equal to 1 byte.




With the integrated development environments, EAZY IDEC


If you try to assign a 2-byte character that has 2 bytes of data to a variable of type char, like in the following program, by writing


char a='て';


and then use the "%c" conversion specifier with printf to display the value of the variable, you will encounter an error.


#include<stdio.h>


int main(void)

{

char a='て';


printf("%c\n",a);


return 0;

}


When you try to compile and run the program, you may get a warning and an error message like the following:


Warning:

An error in the code has been detected on the 5th line of the file "C:/Users/solarplexus/AppData/Local/EasyIDEC/project/11111/main.c".


Error:


multi-character character constant




The phrase "multi-character character constant" means a constant made up of multiple characters. Since a single character has a minimum data size of one byte, a constant made up of multiple characters has a data size of two bytes or more.


Inside single quotes ' ', only single-byte characters, such as alphanumeric characters and symbols (which are called one-byte characters), can be stored. However, an error occurred because an attempt was made to store multiple characters that have a data size of two bytes, such as 'て' (which is called a two-byte character).




In contrast, in the case of the Visual Studio integrated development environment, the program execution result is "ㇳ".


It seems that Visual Studio recognizes 'て' as a character data.


Since the hiragana character 'て' is a two-byte character, it is represented in the computer's memory as a two-byte data, such as:


1010101111110101


for example.


1010101111110101 is a number assigned to the image data of 'て'.



Since the char type can only store a data size of one byte, attempting to store the two-byte character 'て', which has a data size of two bytes and the value of 1010101111110101, into a char type variable will only store the last one byte, which is 11110101, from the right side of the value in Visual Studio.


Then, by using the %c output conversion specifier to print out the value of 11110101 that was stored in the memory with printf, the corresponding image data "ㇳ" will be displayed on the command prompt screen.


There are differences between the integrated development environments, EAZY IDEC and Visual Studio.









  • Xで共有
  • Facebookで共有
  • はてなブックマークでブックマーク

作者を応援しよう!

ハートをクリックで、簡単に応援の気持ちを伝えられます。(ログインが必要です)

応援したユーザー

応援すると応援コメントも書けます

新規登録で充実の読書を

マイページ
読書の状況から作品を自動で分類して簡単に管理できる
小説の未読話数がひと目でわかり前回の続きから読める
フォローしたユーザーの活動を追える
通知
小説の更新や作者の新作の情報を受け取れる
閲覧履歴
以前読んだ小説が一覧で見つけやすい
新規ユーザー登録無料

アカウントをお持ちの方はログイン

カクヨムで可能な読書体験をくわしく知る