migara
migara

Reputation: 55

size in bytes of a char number and int number

please tell me if I am wrong, if a number is stored as a character it will contain 1 byte per character of the number(not 4 bytes)? for example if I make an int variable of the number 8 and a char variable of '8' the int variable will have consumed more memory? and if I create an int variable as the number 12345 and a character array of "12345" the character array will have consumed more memory?

and in text files if numbers are stored are they considered as integers or characters?

thank you.

Upvotes: 3

Views: 4247

Answers (4)

Sanjeet
Sanjeet

Reputation: 31

When int is defined the memory would be allocated based on compiler option ( it can be 4 to 8 bytes). The number assigned to int is stored as is. e.g int a = 86; The number 86 would be stored at memory allocated for a.

When char is defined , there are numbers assigned to each character. When these character needs to be printed the same would print but when its stored in memory it would stored as number. These are called ASCII character, there are some more. The allocation to store is 1Byte because with 1Byte you can represent 2^8 symbols.

Upvotes: 0

Acorn
Acorn

Reputation: 26066

if a number is stored as a character it will contain 1 byte per character of the number(not 4 bytes)? for example if I make an int variable of the number 8 and a char variable of '8' the int variable will have consumed more memory?

Yes, since it is guaranteed that (assuming 8-bit bytes):

sizeof(char) == 1
sizeof(int)  >= 2    

if I create an int variable as the number 12345 and a character array of "12345" the character array will have consumed more memory?

Correct. See the different between:

strlen("12345") == 5
sizeof(12345)   >= 2

Of course, for small numbers like 7, it is not true:

strlen("7") == 1
sizeof(7)   >= 2

in text files if numbers are stored are they considered as integers or characters?

To read any data (be it in a file or in a clay tablet!) you need to know its encoding.

If it is a text file, then typically the numbers will be encoded using characters, possibly in their decimal representation.

If it is a binary file, then you may find them written as they are stored in memory for a particular computer.

In short, it depends.

Upvotes: -1

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5591

When we consider the memory location for an int or for a char we think as a whole. Integers are commonly stored using a word of memory, which is 4 bytes or 32 bits, so integers from 0 up to 4,294,967,295 (232 - 1) can be stored in an int variable. As we need total 32 bits (32/8 = 4) hence we need 4 bytes for an int variable.

But to store a ascii character we need 7 bits. The ASCII table has 128 characters, with values from 0 through 127. Thus, 7 bits are sufficient to represent a character in ASCII; (However, most computers typically reserve 1 bits more, (i.e. 8 bits), for an ASCII character

And about your question:-

and if I create an int variable as the number 12345 and a character array of "12345" the character array will have consumed more memory?

Yes from the above definition it is true. In the first case(int value) it just need 4 bytes and for the second case it need total 5 bytes. The reason is in the first case 12345 is a single integer value and in the second case "12345" are total 5 ascii characters. Even in the second case, you actually need one more byte to hold the '\0' character as a part of a string (marks end of string).

Upvotes: 0

Uku Loskit
Uku Loskit

Reputation: 42040

Yes, all of your answers are correct.

int will always take up sizeof(int) bytes, 8(int) assuming 32-bit int it will take 4 bytes, whereas 8(char) will take up one byte.

The way to think about your last question IMO is that data is stored as bytes. char and int are way of interpreting bytes, so in text files you write bytes, but if you want to write human-readable "8" into a text file, you must write this in some encoding, such as ASCII where bytes correspond to human-readable characters. So, to write "8" you would need to write the byte 0x38 (ASCII value of 8).

So, in files you have data, not int or chars.

Upvotes: 2

Related Questions