Solved

Junk character in assigning int to char data type why?

Posted on 2013-12-26
6
354 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
  • 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:Michael74
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Accepted Solution

by:
Michael74 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:Michael74
ID: 39741278
It works by adding the ASCII value of char '0' to the integer digit.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now