+ 2

Explain difference in writing same thing in two ways (considering memory space)

https://code.sololearn.com/cH8oyIJ5kn5w/?ref=app

19th Oct 2019, 3:01 PM
Pooja
Pooja - avatar
6 odpowiedzi
+ 4
There is no problem in code, i just want to know that , Here we declare a char variable ch and assiging an integer value 65(which have ACII value of A) and we know char take memory space of 1 byte While ,when we declare a integer variable ch1 and assiging a char value 'A' and we also know int take memory space of 4 byte So i want to know how this is happening in memory, for the same value
19th Oct 2019, 3:22 PM
Pooja
Pooja - avatar
+ 2
I don't understand the question. Can you elaborate more on the doubt? There's no problem with the code BTW (read: it runs).
19th Oct 2019, 3:10 PM
Ipang
+ 2
Pooja How this is happening in memory is a rather low level topic I guess. It's a work of the compiler and O/S that manages the memory allocation internally. In short, I don't know any easy way to explain that (sorry).
19th Oct 2019, 4:19 PM
Ipang
0
Pooja This is happening because you have used two different data types : one is int and basically int takes 4 bytes of memory space, while a char takes 1 byte of memory. Perhaps int can take 8 bytes as well it depends upon machine on which code is being executed. Basically on a machine, compiler knows if it is primitive data type how much memory space to be allocated. It is fixed when a compiler is designed. So memory allocation is dependant on platform, type of processor, and compiler used to compile a code. I guess this what you are looking for.
20th Oct 2019, 2:24 AM
Popat
Popat - avatar
0
Look For our convenience There are three form of primitive data types Viz.. 1. Ascii value 2. Actual value 3. Binary value Ascii value is the standard value given to all data types which be converted to binary form Actual value which we see... You declared Int ch='A' Right? At this line compiler have take its int value of 'A' so it takes 65.. You can convert 65 into binary which will definitely require memory<sizeof(int) At other hand you declare Char ch1=65 Compiler have to take char value of 65 Which is 'A'... You may check the range given for character's ascii code in ascii table <=sizeof(char) Try greater integer values for character ch1 and print ch1 You will surely clearify your doubt.... By the way nice question👌👌 Stay questioning stay learning
20th Oct 2019, 8:19 AM
Shubham
Shubham - avatar