%c formatted string shows different behaviour in the shell and in C
Clash Royale CLAN TAG#URR8PPP
If i write a very simple line of code in C:
printf("Ascii char for %d is %cn",65,65);
It simply prints A
, as ascii value of 65 corresponds to character A
. But if I use the same code in shell and write the command
printf "Ascii char for %d is %cn" 65 65
it shows the output as Ascii char for 65 is 6
. I was expecting the same output as in the C language and logically also it should print the character corresponding to the given ascii code.
Why is it showing different behaviour in these cases?
shell-script c printing printf ascii
add a comment |
If i write a very simple line of code in C:
printf("Ascii char for %d is %cn",65,65);
It simply prints A
, as ascii value of 65 corresponds to character A
. But if I use the same code in shell and write the command
printf "Ascii char for %d is %cn" 65 65
it shows the output as Ascii char for 65 is 6
. I was expecting the same output as in the C language and logically also it should print the character corresponding to the given ascii code.
Why is it showing different behaviour in these cases?
shell-script c printing printf ascii
add a comment |
If i write a very simple line of code in C:
printf("Ascii char for %d is %cn",65,65);
It simply prints A
, as ascii value of 65 corresponds to character A
. But if I use the same code in shell and write the command
printf "Ascii char for %d is %cn" 65 65
it shows the output as Ascii char for 65 is 6
. I was expecting the same output as in the C language and logically also it should print the character corresponding to the given ascii code.
Why is it showing different behaviour in these cases?
shell-script c printing printf ascii
If i write a very simple line of code in C:
printf("Ascii char for %d is %cn",65,65);
It simply prints A
, as ascii value of 65 corresponds to character A
. But if I use the same code in shell and write the command
printf "Ascii char for %d is %cn" 65 65
it shows the output as Ascii char for 65 is 6
. I was expecting the same output as in the C language and logically also it should print the character corresponding to the given ascii code.
Why is it showing different behaviour in these cases?
shell-script c printing printf ascii
shell-script c printing printf ascii
edited Jan 24 at 22:22
ctrl-alt-delor
11.3k42058
11.3k42058
asked Jan 24 at 21:39
NoshiiiNoshiii
5717
5717
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The %c
takes a string and prints the first character of that string. If the string is 65
, as in your example, then it would print 6
.
This is specified by the POSIX specification for the printf
utility:
The argument to the
c
conversion specifier can be a string containing zero or more bytes. If it contains one or more bytes, the first byte shall be written and any additional bytes shall be ignored. If the argument is an empty string, it is unspecified whether nothing is written or a null byte is written.
The argument operands shall be treated as strings if the corresponding conversion specifier is
b
,c
, ors
[...]
This means that the argument of the %c
format is interpreted different in C (where a small positive integer will be converted to a char
) and in the shell (where the same integer remains a string containing several digit characters). The format itself does the same thing though; it outputs a single byte as a character.
However:
$ printf '%d %bn' 65 '101'
65 A
101 is 65 in octal. And %b
is specified in POSIX as
An additional conversion specifier character,
b
, shall be supported as follows. The argument shall be taken to be a string that can contain<backslash>
-escape sequences. [...]
ddd
, whereddd
is a zero, one, two, or three-digit octal number that shall be converted to a byte with the numeric value specified by the octal number.
It's an additional conversion specifier, since it's not available in standard C. It is however needed in the shell as we don't have typed variables (in the POSIX shell).
Also:
$ printf '%d %bn' 65 "$( printf '\0%on' 65 )"
65 A
Here we first convert 65 to an octal number in the ddd
format using %o
, before using the result of that in another printf
that uses %b
.
so it means%c
does shows a different behaviour in LINUX as compared to C ?right ?
– Noshiii
Jan 24 at 21:58
1
@Noshiii I would say that it behaves slightly differently in C than in the shell, yes. The argument that you pass in the shell is always a string, while in C it's a typed integer. The actual output is similar is nature though: A single character is outputted. It's not the same character because of how the argument is processed.
– Kusalananda
Jan 24 at 22:01
@Noshiii I've added more explanation regarding this to the answer.
– Kusalananda
Jan 24 at 22:25
%c
in C takes anint
and converts that tounsigned char
according to the book HARBISON, Samuel P. C: a reference manual. Prentice Hall, 2002.
– thrig
Jan 25 at 0:32
@thrig I have slightly amended my choice words in the sentence that I think you are referring to.
– Kusalananda
Jan 25 at 6:38
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496559%2fc-formatted-string-shows-different-behaviour-in-the-shell-and-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The %c
takes a string and prints the first character of that string. If the string is 65
, as in your example, then it would print 6
.
This is specified by the POSIX specification for the printf
utility:
The argument to the
c
conversion specifier can be a string containing zero or more bytes. If it contains one or more bytes, the first byte shall be written and any additional bytes shall be ignored. If the argument is an empty string, it is unspecified whether nothing is written or a null byte is written.
The argument operands shall be treated as strings if the corresponding conversion specifier is
b
,c
, ors
[...]
This means that the argument of the %c
format is interpreted different in C (where a small positive integer will be converted to a char
) and in the shell (where the same integer remains a string containing several digit characters). The format itself does the same thing though; it outputs a single byte as a character.
However:
$ printf '%d %bn' 65 '101'
65 A
101 is 65 in octal. And %b
is specified in POSIX as
An additional conversion specifier character,
b
, shall be supported as follows. The argument shall be taken to be a string that can contain<backslash>
-escape sequences. [...]
ddd
, whereddd
is a zero, one, two, or three-digit octal number that shall be converted to a byte with the numeric value specified by the octal number.
It's an additional conversion specifier, since it's not available in standard C. It is however needed in the shell as we don't have typed variables (in the POSIX shell).
Also:
$ printf '%d %bn' 65 "$( printf '\0%on' 65 )"
65 A
Here we first convert 65 to an octal number in the ddd
format using %o
, before using the result of that in another printf
that uses %b
.
so it means%c
does shows a different behaviour in LINUX as compared to C ?right ?
– Noshiii
Jan 24 at 21:58
1
@Noshiii I would say that it behaves slightly differently in C than in the shell, yes. The argument that you pass in the shell is always a string, while in C it's a typed integer. The actual output is similar is nature though: A single character is outputted. It's not the same character because of how the argument is processed.
– Kusalananda
Jan 24 at 22:01
@Noshiii I've added more explanation regarding this to the answer.
– Kusalananda
Jan 24 at 22:25
%c
in C takes anint
and converts that tounsigned char
according to the book HARBISON, Samuel P. C: a reference manual. Prentice Hall, 2002.
– thrig
Jan 25 at 0:32
@thrig I have slightly amended my choice words in the sentence that I think you are referring to.
– Kusalananda
Jan 25 at 6:38
add a comment |
The %c
takes a string and prints the first character of that string. If the string is 65
, as in your example, then it would print 6
.
This is specified by the POSIX specification for the printf
utility:
The argument to the
c
conversion specifier can be a string containing zero or more bytes. If it contains one or more bytes, the first byte shall be written and any additional bytes shall be ignored. If the argument is an empty string, it is unspecified whether nothing is written or a null byte is written.
The argument operands shall be treated as strings if the corresponding conversion specifier is
b
,c
, ors
[...]
This means that the argument of the %c
format is interpreted different in C (where a small positive integer will be converted to a char
) and in the shell (where the same integer remains a string containing several digit characters). The format itself does the same thing though; it outputs a single byte as a character.
However:
$ printf '%d %bn' 65 '101'
65 A
101 is 65 in octal. And %b
is specified in POSIX as
An additional conversion specifier character,
b
, shall be supported as follows. The argument shall be taken to be a string that can contain<backslash>
-escape sequences. [...]
ddd
, whereddd
is a zero, one, two, or three-digit octal number that shall be converted to a byte with the numeric value specified by the octal number.
It's an additional conversion specifier, since it's not available in standard C. It is however needed in the shell as we don't have typed variables (in the POSIX shell).
Also:
$ printf '%d %bn' 65 "$( printf '\0%on' 65 )"
65 A
Here we first convert 65 to an octal number in the ddd
format using %o
, before using the result of that in another printf
that uses %b
.
so it means%c
does shows a different behaviour in LINUX as compared to C ?right ?
– Noshiii
Jan 24 at 21:58
1
@Noshiii I would say that it behaves slightly differently in C than in the shell, yes. The argument that you pass in the shell is always a string, while in C it's a typed integer. The actual output is similar is nature though: A single character is outputted. It's not the same character because of how the argument is processed.
– Kusalananda
Jan 24 at 22:01
@Noshiii I've added more explanation regarding this to the answer.
– Kusalananda
Jan 24 at 22:25
%c
in C takes anint
and converts that tounsigned char
according to the book HARBISON, Samuel P. C: a reference manual. Prentice Hall, 2002.
– thrig
Jan 25 at 0:32
@thrig I have slightly amended my choice words in the sentence that I think you are referring to.
– Kusalananda
Jan 25 at 6:38
add a comment |
The %c
takes a string and prints the first character of that string. If the string is 65
, as in your example, then it would print 6
.
This is specified by the POSIX specification for the printf
utility:
The argument to the
c
conversion specifier can be a string containing zero or more bytes. If it contains one or more bytes, the first byte shall be written and any additional bytes shall be ignored. If the argument is an empty string, it is unspecified whether nothing is written or a null byte is written.
The argument operands shall be treated as strings if the corresponding conversion specifier is
b
,c
, ors
[...]
This means that the argument of the %c
format is interpreted different in C (where a small positive integer will be converted to a char
) and in the shell (where the same integer remains a string containing several digit characters). The format itself does the same thing though; it outputs a single byte as a character.
However:
$ printf '%d %bn' 65 '101'
65 A
101 is 65 in octal. And %b
is specified in POSIX as
An additional conversion specifier character,
b
, shall be supported as follows. The argument shall be taken to be a string that can contain<backslash>
-escape sequences. [...]
ddd
, whereddd
is a zero, one, two, or three-digit octal number that shall be converted to a byte with the numeric value specified by the octal number.
It's an additional conversion specifier, since it's not available in standard C. It is however needed in the shell as we don't have typed variables (in the POSIX shell).
Also:
$ printf '%d %bn' 65 "$( printf '\0%on' 65 )"
65 A
Here we first convert 65 to an octal number in the ddd
format using %o
, before using the result of that in another printf
that uses %b
.
The %c
takes a string and prints the first character of that string. If the string is 65
, as in your example, then it would print 6
.
This is specified by the POSIX specification for the printf
utility:
The argument to the
c
conversion specifier can be a string containing zero or more bytes. If it contains one or more bytes, the first byte shall be written and any additional bytes shall be ignored. If the argument is an empty string, it is unspecified whether nothing is written or a null byte is written.
The argument operands shall be treated as strings if the corresponding conversion specifier is
b
,c
, ors
[...]
This means that the argument of the %c
format is interpreted different in C (where a small positive integer will be converted to a char
) and in the shell (where the same integer remains a string containing several digit characters). The format itself does the same thing though; it outputs a single byte as a character.
However:
$ printf '%d %bn' 65 '101'
65 A
101 is 65 in octal. And %b
is specified in POSIX as
An additional conversion specifier character,
b
, shall be supported as follows. The argument shall be taken to be a string that can contain<backslash>
-escape sequences. [...]
ddd
, whereddd
is a zero, one, two, or three-digit octal number that shall be converted to a byte with the numeric value specified by the octal number.
It's an additional conversion specifier, since it's not available in standard C. It is however needed in the shell as we don't have typed variables (in the POSIX shell).
Also:
$ printf '%d %bn' 65 "$( printf '\0%on' 65 )"
65 A
Here we first convert 65 to an octal number in the ddd
format using %o
, before using the result of that in another printf
that uses %b
.
edited Jan 25 at 6:36
answered Jan 24 at 21:44
KusalanandaKusalananda
129k16245404
129k16245404
so it means%c
does shows a different behaviour in LINUX as compared to C ?right ?
– Noshiii
Jan 24 at 21:58
1
@Noshiii I would say that it behaves slightly differently in C than in the shell, yes. The argument that you pass in the shell is always a string, while in C it's a typed integer. The actual output is similar is nature though: A single character is outputted. It's not the same character because of how the argument is processed.
– Kusalananda
Jan 24 at 22:01
@Noshiii I've added more explanation regarding this to the answer.
– Kusalananda
Jan 24 at 22:25
%c
in C takes anint
and converts that tounsigned char
according to the book HARBISON, Samuel P. C: a reference manual. Prentice Hall, 2002.
– thrig
Jan 25 at 0:32
@thrig I have slightly amended my choice words in the sentence that I think you are referring to.
– Kusalananda
Jan 25 at 6:38
add a comment |
so it means%c
does shows a different behaviour in LINUX as compared to C ?right ?
– Noshiii
Jan 24 at 21:58
1
@Noshiii I would say that it behaves slightly differently in C than in the shell, yes. The argument that you pass in the shell is always a string, while in C it's a typed integer. The actual output is similar is nature though: A single character is outputted. It's not the same character because of how the argument is processed.
– Kusalananda
Jan 24 at 22:01
@Noshiii I've added more explanation regarding this to the answer.
– Kusalananda
Jan 24 at 22:25
%c
in C takes anint
and converts that tounsigned char
according to the book HARBISON, Samuel P. C: a reference manual. Prentice Hall, 2002.
– thrig
Jan 25 at 0:32
@thrig I have slightly amended my choice words in the sentence that I think you are referring to.
– Kusalananda
Jan 25 at 6:38
so it means
%c
does shows a different behaviour in LINUX as compared to C ?right ?– Noshiii
Jan 24 at 21:58
so it means
%c
does shows a different behaviour in LINUX as compared to C ?right ?– Noshiii
Jan 24 at 21:58
1
1
@Noshiii I would say that it behaves slightly differently in C than in the shell, yes. The argument that you pass in the shell is always a string, while in C it's a typed integer. The actual output is similar is nature though: A single character is outputted. It's not the same character because of how the argument is processed.
– Kusalananda
Jan 24 at 22:01
@Noshiii I would say that it behaves slightly differently in C than in the shell, yes. The argument that you pass in the shell is always a string, while in C it's a typed integer. The actual output is similar is nature though: A single character is outputted. It's not the same character because of how the argument is processed.
– Kusalananda
Jan 24 at 22:01
@Noshiii I've added more explanation regarding this to the answer.
– Kusalananda
Jan 24 at 22:25
@Noshiii I've added more explanation regarding this to the answer.
– Kusalananda
Jan 24 at 22:25
%c
in C takes an int
and converts that to unsigned char
according to the book HARBISON, Samuel P. C: a reference manual. Prentice Hall, 2002.– thrig
Jan 25 at 0:32
%c
in C takes an int
and converts that to unsigned char
according to the book HARBISON, Samuel P. C: a reference manual. Prentice Hall, 2002.– thrig
Jan 25 at 0:32
@thrig I have slightly amended my choice words in the sentence that I think you are referring to.
– Kusalananda
Jan 25 at 6:38
@thrig I have slightly amended my choice words in the sentence that I think you are referring to.
– Kusalananda
Jan 25 at 6:38
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496559%2fc-formatted-string-shows-different-behaviour-in-the-shell-and-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown