ASCII code printing logic - c++

I did write this program but now I am little bit confused about it. In the code given below I did writ two different statements which print string on the screen, first one did not print anything but second one print a symbol.
#include<iostream.h>
main(){
int i = 1; char c;
cout << c << endl;
while (i <= 255){
c = i;
cout << c << endl;
i++;
}
}
Please explain me logic of this program. I am beginner of 'C' language so please explain it to me in details.
Thanks

See, as in your program you haven't initialised char c;
It must be initialised or hold some value before being printed!
int i; char c;
i = 1;
cout << c << endl; // initialsise c=something of char-type;
c = i;
cout << c << endl; //as initialised,so prints something
Second, as you have initialised this char variable c. Hence,it prints some symbol,probably an ASCII character!

First cout << c << endl; prints unitialized value c (it might be zero, so you don't see anything). But second call of this instructions prints value (char) 1 (you can see it).

When you initially printed the character 'c' then it does not have any value. When nothing is initialized in a character then it takes garbage value. Afterwards
int i=1;
char c = i;
It stores 1 in variable 'c' but as it is a character so on printing 'c' it shows the ASCII value of 1 which is ☺
It is same as doing->
cout << (char)i <<endl;
if you want to print the ASCII table then this code may help
for(int i = 1; i <= 255; i++) {
cout<<"ASCII VALUE OF "<<i<<" is "<<(char)i <<endl;
}

Related

Strange symbols printing in C++ instead of array content [duplicate]

I am working on below code:
#include<iostream>
#include<stdio.h>
using namespace std;
main() {
unsigned char a;
a=1;
printf("%d", a);
cout<<a;
}
It is printing 1 and some garbage.
Why cout is behaving so?
cout << a is printing a value which appears to be garbage to you. It is not garbage actually. It is just a non-printable ASCII character which is getting printed anyway. Note that ASCII character corresponding to 1 is non-printable. You can check whether a is printable or not using, std::isprint as:
std::cout << std::isprint(a) << std::endl;
It will print 0 (read: false) indicating the character is non-printable
--
Anyway, if you want your cout to print 1 also, then cast a to this:
cout << static_cast<unsigned>(a) << std::endl;
I had a similar issue here that I've long forgotten about. The resolution to this problem with iostream's cout can be done like this:
#include<iostream>
#include<stdio.h>
main() {
unsigned char a;
a=1;
printf("%d", a);
std::cout<< +a << std::endl;
return 0;
}
instead of casting it back to another type if you want cout to print the unsigned char value as opposed to the ascii character. You need to promote it.
If you noticed all I did was add a + before the unsigned char. This is unary addition that will promote the unsigned char to give you the actual number representation.
User Baum mit Augen is responsible for reminding me of this solution.
You need to typecast a as integer as cout<< (int)(a);. With this you will observe 1 on the output. With cout << a;, the print will be SOH (Start of Heading) corresponding to ascii value of 1 which can't be printed and hence, some special character is observed.
EDIT:
To be more accurate, the cout statement should be cout << static_cast<unsigned>(a) as Nawaz has mentioned.
The C compiler has its own way of defining the type of the printed output, because you can specify the type of the output.
Ex:
uint8_t c = 100;
printf("%d",c);
so you can also print c as an int by %d, or char %c, string %s or a hex value %x.
Where C++ has its own way too, the cout prints the 8-bit values as a char by default. So, you have to use specifiers with the output argument.
You can either use:
a + before the name of the output argument
uint8_t data_byte = 100;
cout << "val: " << +data_byte << endl;
use a function cast unsigned(var); like,
uint8_t data_byte = 100;
cout << "val: " << unsigned(data_byte) << endl;
printf("%u",a);
its so simple try it

I am trying to convert a int into a char and output the ascii value?

Before this part of code i have if else if statement to give the int value
It is also randomized, but the only problem is with this part trying to make the outputint variable to become a character ascii value which output is declared as char
right now my array shows the integer variables and not the respective ascii variables
output = static_cast<char> (outputint);
array[i][j] = output;
cout << array[i][j] << " ";
you just have to tell the compiler to print a char, like this:
cout << (char)array[i][j];
Or if you want to use C++ style casting:
cout << static_cast<char>(array[i][j]);

Code crashes. Trying to remove characters from char array C

I am basically trying to store everything after a certain index in the array.
For example, I want to store a name which is declared as char name[10]. If the user inputs in say 15 characters, it will ignore the first five characters and store the rest in the char array, however, my program crashes.
This is my code
char name[10];
cout<< "Starting position:" << endl;
cin >> startPos;
for(int i= startPos; i< startPos+10; i++)
{
cout << i << endl; // THIS WORKS
cout << i-startPos << endl; // THIS WORKS
name[i-startPos] = name[i]; // THIS CRASHES
}
For example, if my name was McStevesonse, I want the program to just store everything from the 3rd position, so the end result is Stevesonse
I would really appreciate it if someone could help me fix this crash.
Thanks
Suppose i is equal to 3. In the last iteration of the loop, i is now equal to 12, so substituting 12 in for i, your last line reads
name[12-startPos] = name[12];
name[12] is out of bounds of the array. Based on what you have shown so far, there is nothing but garbage stored in name anyway before you start doing this assignment, so all you're doing is reorganizing garbage in the array.
Please in future: post full compilable example.
A simple answer is that your array maybe is out of bound, since you don't provide full example its hard to know exactly.
Here is a working example:
#include <iostream>
using namespace std;
int main() {
int new_length, startPos;
int length = 15;
char name[15]= "McStevesonse";
cout<< "Starting position:" << endl;
cin >> startPos;
if(new_length <1){ // you need to check for negative or zero value!!!
cout << "max starting point is " <<length-1 << endl;
return -1;
}
new_length=length-startPos;
char newname[new_length];
for(int i= 0; i<new_length; i++){
newname[i] = name[i+startPos]; // THIS CRASHES
}
cout << "old name: " << name << " new name: " << newname << endl;
return 0 ;
}
To put it simply, change this:
for(int i= startPos; i< startPos+10; i++)
To this:
for(int i= startPos; i<10; i++)
You should be fine with that.
Explanation:
At some point, when you use the your old loop, this name[i-startPos] = name[i] would eventually reach an array index out of bounds and causes the crash.
Don't forget to clean up/hide the garbage:
Doing so, would cause the output to produce some kind of garbage outputs. If you got a character array of 'ABCDEFGHIJ', and have chosen 3 as the starting position, the array would be arranged to 'DEFGHIJHIJ'. In your output, you should atleast hide the excess characters, or remove by placing \0's

cout not printing unsigned char

I am working on below code:
#include<iostream>
#include<stdio.h>
using namespace std;
main() {
unsigned char a;
a=1;
printf("%d", a);
cout<<a;
}
It is printing 1 and some garbage.
Why cout is behaving so?
cout << a is printing a value which appears to be garbage to you. It is not garbage actually. It is just a non-printable ASCII character which is getting printed anyway. Note that ASCII character corresponding to 1 is non-printable. You can check whether a is printable or not using, std::isprint as:
std::cout << std::isprint(a) << std::endl;
It will print 0 (read: false) indicating the character is non-printable
--
Anyway, if you want your cout to print 1 also, then cast a to this:
cout << static_cast<unsigned>(a) << std::endl;
I had a similar issue here that I've long forgotten about. The resolution to this problem with iostream's cout can be done like this:
#include<iostream>
#include<stdio.h>
main() {
unsigned char a;
a=1;
printf("%d", a);
std::cout<< +a << std::endl;
return 0;
}
instead of casting it back to another type if you want cout to print the unsigned char value as opposed to the ascii character. You need to promote it.
If you noticed all I did was add a + before the unsigned char. This is unary addition that will promote the unsigned char to give you the actual number representation.
User Baum mit Augen is responsible for reminding me of this solution.
You need to typecast a as integer as cout<< (int)(a);. With this you will observe 1 on the output. With cout << a;, the print will be SOH (Start of Heading) corresponding to ascii value of 1 which can't be printed and hence, some special character is observed.
EDIT:
To be more accurate, the cout statement should be cout << static_cast<unsigned>(a) as Nawaz has mentioned.
The C compiler has its own way of defining the type of the printed output, because you can specify the type of the output.
Ex:
uint8_t c = 100;
printf("%d",c);
so you can also print c as an int by %d, or char %c, string %s or a hex value %x.
Where C++ has its own way too, the cout prints the 8-bit values as a char by default. So, you have to use specifiers with the output argument.
You can either use:
a + before the name of the output argument
uint8_t data_byte = 100;
cout << "val: " << +data_byte << endl;
use a function cast unsigned(var); like,
uint8_t data_byte = 100;
cout << "val: " << unsigned(data_byte) << endl;
printf("%u",a);
its so simple try it

"Ch++" or "ch+1" in C++?

While reading "C++ Primer Plus 5th edition", I saw this piece of code:
cin.get(ch);
++ch;
cout << ch;
So, this will lead to display the following character after ch. But, If I did it that way:
cin.get(ch);
cout << ch+1;
Now, cout will think ch is an int(try typecasting). So, why cout does so?
And why if I added 1 to a char it will produce a number?. And why there's a difference between:
ch++, and ch + 1.
The reason this occurs is the type of the literal 1 is int. When you add an int and a char you get an int, but when you increment a char, it remains a char.
Try this:
#include <iostream>
void print_type(char)
{
std::cout << "char\n";
}
void print_type(int)
{
std::cout << "int\n";
}
void print_type(long)
{
std::cout << "long\n";
}
int main()
{
char c = 1;
int i = 1;
long l = 1;
print_type(c); // prints "char"
print_type(i); // prints "int"
print_type(l); // prints "long"
print_type(c+i); // prints "int"
print_type(l+i); // prints "long"
print_type(c+l); // prints "long"
print_type(c++); // prints "char"
return 0;
}
Please note - this is the answe to the original question, which has since been edited.
Now, cout will think ch is an int(try
typecasting).
No it won't. It is not possible to change the type of a variable in C++.
++ch;
increments whatever is in ch.
ch + 1;
takes the value (contents) of ch, adds 1 to it and discards the result. Whatever is in ch is unchanged.
The statement ++ch; increments ch whereas ch + 1; doesn't.
Also, rememeber that '++ch' will do the increment before actually running the statement it is in, so that is why it remains a char.
int i = 0;
cout << ++i << endl;
cout << i++ << endl;
// both will print out 1.