Solved

Junk character in assigning int to char data type why?

Posted on 2013-12-26
6
371 Views
Last Modified: 2013-12-26
void main()
{
         int a=5;
         char str[0];
         str[0]=a;
         cout<<str[0];
}
Doesn't print 5, why.
0
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39741035
Because you have assigned a binary 5 to str[0] and not the character '5'.  If you make the first assignment char a = '5'; then it should print '5'.
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 39741116
In declaring  
str[0]=a;
 you are saying input the character with the ASCII value of 5

If you use int
a='5';
and ouput a you will get 53 as ascii value of 5


Have a look at this ASCII table

http://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

Michael
0
 

Author Comment

by:searchsanjaysharma
ID: 39741133
I an writing a code which converts aaa to a3
Simlarly aaabbbbb as a3b5
This is why i need 5 to be appended in char array.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 23

Accepted Solution

by:
Michael Fowler earned 500 total points
ID: 39741142
To convert the int to a char you can use

str[0]='0' + a;

Michael
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39741240
What is the logic behind.
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 39741278
It works by adding the ASCII value of char '0' to the integer digit.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question