What's the difference between:

int once;
cout << "ok";

and this:

char twice;
cout << "ok";

Is the output for the first one just ok and the second one ok ok ?

Recommended Answers

All 10 Replies

In both cases the output is just one single "ok", you are displaying just the literal text you see there.

The declarations of the int or the char have no effect on the following output statements.

Is there more to your problem that what you're showing?

I just wanted to know the difference between int and char. Those two were practice questions for an exam that just asked for an output.
So the once and twice don''t really mean anything?

Char "is a simbol" found in ASCII code.
If you write your program like this:

char c = 'A';
int i = c;

cout << c << ", " << cout << i;

On the screen you will see:

A, 65

This is called implicit type conversion. All characters can be converted into numbers in range 0-255, that's why is char 1 byte in size (2 to the power of 8 = 256).

Same works for the oposite:

int i = 66;
char c = i;
cout << c;

On the screen you will see:

B

You can google out more really easy.
For example char array or what we use over it called string could be your next thing to explore.

Char "is a simbol" found in ASCII code.

Not really, ASCII is a way of encoding alaphbetic (and other) symbols as a number and defines numbers in the range 0 - 255. And a char is an integer data type.

All characters can be converted into numbers in range 0-255, that's why is char 1 byte in size (2 to the power of 8 = 256).

Even more wrong, not all characters (in the world) can be converted into a number in the range 0 - 255 which is why Unicode, UTF-8, UTF-16, UTF-32 (methods of encoding characters like ASCII is) and wchar_t (wide character type like char is the single character type) exist.

char and int are both integer types each have several properties unique to themselves

char
signedness: if a char is signed or unsigned is platform dependent. it is usual to consider char, signed char and unsigned char as 3 distint types. Where as int is always signed and synonymous with signed int

trap bits: a char may not have trap bits it's value is an exact representation of the memory that contains it. In addition in an array of char, or incrementing a char pointer the individual char values obtained are required to be contiguous in memory. Neither of these are true of any other integer type, they may contain trap bits and do not have to be absolutely contiguous in memory means that if you want an exact view of memory a char is the only thing you can do it with an be guaranteed to get what you want.

size: the size of a char is not necessarily 8 bits, it can be any number and there have certainly been systems in the past with 7 and 9 bit chars. This is defined in the standard library with the CHAR_BIT symbol. The size of all other types is defined in terms of the number of char required to hold them. That said I have never worked on a platform that had a CHAR_BIT that wasn't equal to 8.

int
An int is supposed to be sized such that it is the size that the processor finds easiest to work with. This means that of all the types the size of int is the most likely to change from platform to platform (although long varies to sometimes). In theory 64bit platforms could (even should) use a 64 bit int but generally most don't*. However 16bit platforms often use a 16bit int (2 char) and 32/64 bit platforms often use a 32 bit int (4 char).

The difference between char and int is all about the different properties of different sized integer types and standard definitions of exceptions and special rules that apply to them and not much to do with printable characters. It is true that cout automatically treats char as if it contained a printable character and not a numerical value which I personally find rather irritating but the standards committee had to choose one way or the other and choosing that way does make it easy (with a cast) to get either type of output which wouldn't be the case if they had chosen numerical output as default.

* Most modern 64 bit platforms are optimized to work equally well with 64 or 32 bit values, I'm not quite sure what will happen when/if we go to 128bit platforms in the future.

commented: Very complete! +14

I wanted to say that any char can be converted into int, but yeah...

The program you gave was not really dealing with int and char.Here only cout plays a picture,that is whatever typed in cout will be the answer.declarations of int and char is not used in your program.The output of your program would be ok for both the case.
The basic difference between int( keyword of integer) and char(keyword of character) is the first is integer that is includes numbers(without float values),whereas character includes (a to z and A to Z and sometimes 0 t0 9,some special chars like .,+-!?,...),char may be string also that is group of chars.

  for example:


//program in cpp
#include<iostream.h>
#include<conio.h>
void main()
{
 int a,b,c;
    clrscr();
 //for adding to num
  cout<<"enter the a and b value :";
  cin>>a>>b;
  c=a+b;
  getch();
  }

And also int requires 2 bytes of memory while char requires only one byte of memory.

The basic difference between int( keyword of integer) and char(keyword of character) is the first is integer that is includes numbers(without float values),whereas character includes (a to z and A to Z and sometimes 0 t0 9,some special chars like .,+-!?,...),char may be string also that is group of chars.

Not true. The only difference is the range of values that each can contain. An int can contain characters just as easily as char. And most of the C functions that return characters (e.g. getc()) from the keyboard return int not char. You have to look them up in limits.h header file to find out the ranges for your compiler.

And also int requires 2 bytes of memory while char requires only one byte of memory

That incorrect also. char takes only one byte but the standard say very little about the others. In ancient 16-bit Tubo C compiler an int may be 2 bytes but with most modern 32-bit compilers it is 4 bytes, which is the same as long int.

sorry sir,for mistakes i have made.

sorry sir,for mistakes i have made

Take 10 lashes with a wet noodle :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.