What is going to store two bytes of information? C programming.

What is going to store two bytes of information? And then we had an integer, which is fundamental. In C, right. So everything is assumed. Any integral value, by default, is assumed to be an integer in C programming. So integer was for four bytes storage. So you wanted a much bigger number to store. So it can go up to four bytes of data. So four bytes are like 32 bits, actually. So interior stores 32-bit number. And then we had situations, you know, we had a 32-bit processor for quite a long time. And then eventually we came out with also 64 bit.

When you talk about integer and long, you might have read that they are pretty much the same in terms of storing a value, which is an integral value. Right. It’s always the same. So short int long all three of them store integral value. But why long is there? Long is there because you wanted to store a long integer. Right. So when you read K&R books, you will have the references for short data type or short Int or an Int or long or long Int. Right. Short integer and an integer and a long integer.

They basically represent what is the range of value that you can store for each one of them in your programming environment. So long on a 32-bit environment, obviously will be four Byte entity, and then on a 64-bit environment, because now the fundamental environment is 64. So we are talking about 64 bits or an 8 bytes entity. So we had three different classifications in terms of primitive types for storing an integer value. And then as part of the programming language, we did not want to restrict our step to just these three in terms of integer, because we do a lot of computations and calculations where you have real numbers. You have fractional values 1.2 1.5 2.5.

They are all required to store real numbers. So we have a floating point. A floating-point is just for real numbers where you have a lot of fractional values. Right. So float data type was created to store four bytes of information, and then we have double. Double is a special situation in the earlier days because you wanted to store much higher precision. Right. So if you have 1.123456789 Right. So you have a very long, more data after the decimal dot.

How do you store, so float was created where it can store up to six digits of precision after that decimal. So one dot, 123456 So those six decimals, it can store six positions. Right. So six digit of precision and double was created to store 10 digits of precision. So after the dot, you can have around 10 digits that it can store. Now we require a different memory representation. We require different memory allocations for them. So it is a four Byte and this is very high precision. Right. Basically, double was eight Byte entity. So even in the world of 32-bit processors, which was there for quite a long time in the Intel series, actually. So we had double as an eight-byte.

The next one in terms of primitive is like a void data type. And this is where I see a lot of people get confused. Void data type really is not a data type because void means nothing. Right. So you cannot say a void x. So you are telling the compiler that please allocate me x and it is wide, which means it is nothing. So no space for it. Correct. So void was created as a special situation for a lot of programming situations where we want to return the data type of a function. Right. Sometimes we also one is the data type of the function, So sometimes we don’t know what the function does. The function does nothing. Sorry. The function returns nothing. The function will do a job, but it returns nothing. Right. And we have a lot of classical examples for that.

This is one example in terms of void understanding. Okay. Another one is void was created because many times the functions will do a job and then they want to return something to the caller, the function which has called them. So those functions, for example, the most popular one is malloc(). Right. So malloc() is going to return a piece of memory. Now, what is the usage of the piece of memory? Are you going to return just a piece of memory? And it is going to do any integer work on that or some structure work or some character work. Right. So we don’t know that memory is for what purpose? So ultimately you’re going to get.

What does malloc() return? Malloc returns a pointer. It gives you a pointer. But pointer of what data type? Yeah. So that’s the problem. Right. Because the caller wants to allocate memory to store characters or the caller wants to want a piece of memory to store integer or to store an array of structures. Right. So in those situations, it’s very difficult for the function to return a particular return value. So those things are basically void. So this is returning a pointer to avoid. So when you really see it, it’s a pointer to avoid. So what you eventually will see if I have to do a man page on that. So when you say man malloc, we do a man malloc, what do you get? You are going to get a malloc. Right. You can see the malloc man page here this particular line. And what does it return? It returns a pointer to avoid.

I think this is important to know. Many times in programming situations the function does not know what is the purpose of the piece of memory which I’m giving or what is exactly the purpose. Right. Some sort of a pointer, but we don’t know who is going to use in which way which data type. Right. So ultimately, what we do is whenever we do any programming situations, it has been a best practice and mandatory to do a malloc() and then generally for a lot of these functions, which returns a void pointer, we must typecast it. So as a Grand rule or programming.

As grand programming, what you will see here is you will see that whenever we have a character pointer, P we always do char * So we do a typecast to the malloc because Malloc is returning a void pointer. Right. So we say Malloc and suppose you are interested to allocate 50 bytes, so these 50 bytes and which will be consumed by a character pointer. Right. So you typecast. So the idea is, whatever is the data type here, what is the data type of P – it’s the char *p character pointer. So you must typecast it so that at least the Link checker gets satisfied.

Leave a Comment