Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Junk character in assigning int to char data type why?

Posted on 2013-12-26
6
Medium Priority
?
377 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 84

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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

715 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