help with int

why do i get a garbage value when i execute the statement
1
2
3
4
5
6
#include <iostream>
int main()
{
int i='hell';
cout<<i;
}

and plz tell me the difference between int i='hell' and int i="hell"
Any help is highly appreciated
hey man... actually the variable i is an integer data type and u cannot assign a string! and moreover, its not a string! the difference between int i='hell' and int i="hell" is that the characters enclosed in double quotes is called STRING... whereas its not valid for putting 'hell' as only ONE character can be enclosed in a sing quotes and this is called as a character constant...
actually both of them is rubbish! the rubbish value u r getting is the ASCII value of h...
if u want the correct program:
1
2
3
4
5
6
#include <iostream.h>
void main()  //Sorry am used with void main :P
{
int i='h';
cout<<i;
}

the ASCII value of h will be stored in i and that is printed on the console...
think i explained what i can :)
I'm surprised that even compiles. But anyway, int is short for integer, which means number. 'hell' isn't a number, it's a word. The syntax is also incorrect, as only single characters get sourounded with apostrophes. More then one character strings such as 'hell' should be surrounded with quotation mArks.

The correct method for displaying a string like this is:
 
char i[] = "hell";
Hey man, I think you should look into data types a bit. Data types are the different ways you can represent variables.
int stands for integer, and an integer is a number and only a number. If you want to make a variable that is able to handle words then you might be interested in learning about the char data type(stands for character) or string types.
But to answer your second question. When you use something like ' ' you're actually getting the ASCII equivalence of whatever is between the two ' . ASCII is a standard for representing letters with numbers. That's why you're able to do 'A' and get a number as an output(64).

Now to explain the " ". " " in a nut shell it can be thought of as a literal way of representing that may or may not changes. It's a bit confusing at first glance but one can think of it as a version of ' ' that can accept more than one character at a time.
I tried my best to explain. And let me point out that cout<<i; wouldn't work because cout hasn't been declared to be able to use in the program although you can use std::cout to call that function. In order to use cout normally, you'll have to do using namespace std; before main.
Last edited on
Topic archived. No new replies allowed.