468,512 Members | 1,518 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Unable to convert integer to character

Hi,

I am having problem in converting an integer to character in the following program. It compiles but at run time there is no value in c.

Could anyone help me with the problem. It is very urgent.


int main()
{
int a = 3;
char c = (char)a;

printf("Conversion to char c = %c \n", c);
return 0;
}

What should i do to store integer 3 as character in c?
Jan 19 '08 #1
2 3735
Savage
1,764 Expert 1GB
Hi,

I am having problem in converting an integer to character in the following program. It compiles but at run time there is no value in c.

Could anyone help me with the problem. It is very urgent.


int main()
{
int a = 3;
char c = (char)a;

printf("Conversion to char c = %c \n", c);
return 0;
}

What should i do to store integer 3 as character in c?
Add to c ASCII value of 48.(which is 0),or just add '0';
Jan 19 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
A char is an integer. However, you asked printf() to dispay the integer as a letter rather than a value with the %c format. Try %d.

Be sure to follow Savage's directions.
Jan 20 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by David Lawson | last post: by
5 posts views Thread by IamZadok | last post: by
14 posts views Thread by Drew | last post: by
20 posts views Thread by Niyazi | last post: by
10 posts views Thread by cmdolcet69 | last post: by
14 posts views Thread by rtillmore | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.