convert int counter to char in for loop in c
convert int counter to char in for loop in c
I can't seem to find an answer as to why I am not able to type cast a counting integer into a char type within a for loop. Below is a simple instance in which I have tried to declare the type case before and after the loop begins but have not gotten any where. Fundamentally, I don't understand why this is not allowed and can't, or am too stupid to find any information on this.
#include <stdio.h>
int
main(void)
int i;
//char j; <-If i declare here
//char j=(char)i; <-or here there is no profound affect
for(i=0;i<9;++i)
char j=(char)i;
printf("i=%d, j=%cn", i, j); <--Case A: j=%c, Case B: j=%d
return 0;
OUTPUT
Case A Output printf("%c",j) (j initialized as char & typecast as char)
i=0, j=
i=1, j=
i=2, j=
i=3, j=
i=4, j=
i=5, j=
i=6, j=
i=7, j=
i=8, j=
Case B Output printf("%d",j) (j initialized as char & typecast as char)
i=0, j=0
i=1, j=1 <- if typecast from int to char worked these values should not
i=2, j=2 <- show up when using printf("%d",j)
i=3, j=3
i=4, j=4
i=5, j=5
i=6, j=6
i=7, j=7
i=8, j=8
I was expecting a printed value of the the number as a char instead of blank values. There could be an issue with terminal or ascii values not being represented properly, but that does not answer the root of my question. ** Case B shows numeric values for the j column. However, these values were printed as using a double place holder. If my type case from double to char had worked, these values would be either the associated ascii values for the numbers or garbage.** I'm not looking for a way around this, just to understand what is going on when the code is compiled or in runtime. Thanks!
local vars are not initialized, so with the current code, the value of i can be anything. And yeah, you really need to explain what is your expectation and the actual result.
– khachik
Sep 13 '18 at 1:44
If you want to see the integer values held by
j, print with printf("i=%d, j=%dn", i, j);. As @ShadowRanger said, you won't see anything with %c for small values, because these characters are unprintable.– David Bowling
Sep 13 '18 at 3:11
j
printf("i=%d, j=%dn", i, j);
%c
@khachik:
i is the loop variable, and gets initialized to 0 at the beginning of the for loop. No undefined behavior there.– ShadowRanger
Sep 13 '18 at 10:33
i
0
for
2 Answers
2
The Terminal has maybe a problem with the NUL character when displayed with %c in the formated string. I changed the Program a bit..
#include <stdio.h>
int main(void)
int i, x=123456789;
//char j;
char j=(char)i;
printf("int i: %dn",i);
printf("int x: %dn",x);
printf("char j: %dn",j);
printf("hex j: 0x%02xn",j);
for(i=32;i<128;++i)
char j=(char)i;
printf("i=%d, j=0x%02x, j= %d, j =%cn", i, j, j, j);
printf("char j: %cn",j);
// Problems is the output as char?
// Programmers Notepad Consol Specific!
printf("Terminal Stops before this in PN :Dn");
return 0;
I think the Program does what you want when printing with %d or %x. But %c makes problems.
I used Programmers Notepad. Other Programs may not have my problem :D
Importand is that Printable characters start at decimal 32 with space. See ASCII Printable. Before that it is normal to get no output. %c tries to convert the decimal value in a printable character according to the ASCII table. Also have a look on the C reference.The %d is for signed decimal Integer.
Because the number you are trying to convert is bigger than a char. Char can only be 1 character, hence you can't fit 9 digits/characters into a single char. This would need to be a char/string to do what you are trying.
Or try changing x=0->9 but not more than a single digit.
The number being converted is between 0 and 8, which fits in every
char that exists. x isn't being used anywhere.– ShadowRanger
Sep 13 '18 at 1:37
char
x
(char)7 is not '7' if you meant that.– khachik
Sep 13 '18 at 1:45
(char)7
'7'
Yea, sorry I should of left out var x and its values. I was going going to extend the code to using the i value to pull each of the 9 "char numbers" (1,2...8,9) in then sum them but decided the simpler the better.
– Matthew Dawson
Sep 13 '18 at 2:31
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What you mean by "not able"? Is there a warning from the compiler? An error? Does it misbehave in some way? Part of a Minimal, Complete, and Verifiable example is the input, expected output, and actual output; saying "doesn't work" or words to that effect is not helpful. Are you expecting the result to be a printable character? The ASCII values from 0-8 aren't printable (bell and backspace are the closest you get), so you wouldn't see much of use in the terminal.
– ShadowRanger
Sep 13 '18 at 1:38