Understanding Data Types: Int, Float, and Unsigned Char

  • Thread starter PainterGuy
  • Start date
  • Tags
    Float
In summary: On a 32-bit PC, it's common for char to be 8 bits, short to be 16 bits, and int and long to both be 32 bits. In this case the number of bits in an int (32) is 4 times the number of bits in a char (8).As for floats and doubles, a lot depends on the hardware design. It's possible to have a floating point processor that has a 64-bit internal format for floats. In that case, the compiler might choose to use this 64-bit internal format for both float and double, and a double would still be 2x the size of a float, but a float would be 64 bits instead of 32 bits.
  • #1
PainterGuy
940
69
hello everyone, :wink:

will you help me out with some of the problems please? i will very much appreciate this kindness. :approve:

will you please have a see on this google doc:-
https://docs.google.com/viewer?a=v&...gxYzEtMzM0ZjQ4MTAzYWI1&hl=en&authkey=CJzk6IoL

please focus on highlighed parts.

to cut the long story story. in the case of "int" there are four bytes and there are 8 bits in one byte. so there are total (4 x 8) 32 bits. then if i raise 2^(32) i get 4294967296 which is equal to combined numerical range of int (2,147,483,648 + 2,147,483,647). but this formula does not work in case of "float" and double". can you tell why this is so please?

and what those digits of precision, 7 and 15, in case of "float" and "double" respectively. does this mean decimal point is followed by 7 and 15 digits respectively. tell me please.

"unsigned int" means all integers excluding -ve integers. but what are "unsigned characters"? please shed some light.

okay last question.:smile: i have seen questions asking how many bytes would be occupied by "int" and "float" on 32-bit and 16-bit system? what is this? i know you can help me here.

i am very much grateful for this help. many many thanks.

cheers
 
Technology news on Phys.org
  • #2
unsigned character - usually an 8 bit unsigned integer. There were some old machines that had 6 bit characters, and I'm not sure how C would be implemented on those machines.

float - normally these are 32 bit or 64 bit for most modern machines, and many use the IEEE standard.

http://en.wikipedia.org/wiki/IEEE_754-2008
 
  • #3
many thanks rcgldr.

sorry due to my limited knowledge it was not possible for to complely understand your kind reply.

please elaborate it a bit why the formula in my first post does not work on float and double.

is my understanding of precision correct?

are there also negative integers included in character set of ascii?

how many bytes float and int will occupy on 64 bit system?

i very much request you to help me with this stuff. many many thanks for this consideration.

cheers
 
  • #4
PainterGuy said:
many thanks rcgldr.

sorry due to my limited knowledge it was not possible for to complely understand your kind reply.

please elaborate it a bit why the formula in my first post does not work on float and double.

is my understanding of precision correct?

I'm afraid that understanding floating point numbers and how they're represented on computers will need a little research and study from you. All the information you need is in the IEEE 754-2008 link rcgldr gave you. It's not super complicated if you take your time to understand it.

Basically, no, your understanding of precision as applied to floats/doubles isn't totally correct. Nor does the same formula apply. It's stored as described in that link, and there are three parts to it. The number of bits used in the significand is the "precision". But there's also the sign and the exponent. Those take up bits too, and only together do you get a proper number.

Anyways, understand how they are represented and you will understand why the formula for integers will not work for floating point numbers. And it's all described in that link.

PainterGuy said:
are there also negative integers included in character set of ascii?
Strictly speaking, no, because ASCII goes from 0 to 127, which requires only 7 bits. So even in a signed char, none of the ASCII characters will be negative.

PainterGuy said:
how many bytes float and int will occupy on 64 bit system?
That's language dependent, and it's not defined as nicely as you'd think in C/C++. But that's a long story. In brief, an int on a 64 bit system can be 32 or 64 bits long, but will generally be 32 bits long.

Single precision floating point numbers (float) are 32 bits long, and double precision (double) are 64 bits long.

EDIT: Another page with info on floating point number representation:
http://en.wikipedia.org/wiki/Floating_point
 
Last edited:
  • #5
When I first learned C, using compilers on PC-type computers, an int was the same size as a short (also known as a short int) - 16 bits. A long (aka long int) in those days was 32 bits.

Sometime after the introduction of 32-bit processors such as the Intel 80386, the size of an int was changed to 32 bits. A short was still 16 bits, but an int and a long were both 32 bits.

If I recall correctly, there's nothing in the standards that decree what size the various integral types should be. Compiler vendors have the latitude to define the size of an int so that the size matches the width of the registers in the CPU. That's a little confusing now, because the registers on the CPUs from Intel and AMD are 64 bits wide, but have instructions that can work with 64-bit registers, 32-bit registers, and 16-bit registers.
 
  • #6
If I recall correctly, there's nothing in the standards that decree what size the various integral types should be. Compiler vendors have the latitude to define the size of an int so that the size matches the width of the registers in the CPU. That's a little confusing now, because the registers on the CPUs from Intel and AMD are 64 bits wide, but have instructions that can work with 64-bit registers, 32-bit registers, and 16-bit registers.

As I recall the only guarantees are that sizeof(char) is 1, and

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long).
 

1. What is the difference between int, float, and unsigned char?

Int, float, and unsigned char are all data types used in programming. Int is short for integer and is used to store whole numbers. Float is short for floating point and is used to store decimal numbers. Unsigned char is used to store whole numbers, but only positive values.

2. How are int, float, and unsigned char used in scientific calculations?

Int, float, and unsigned char are all used in scientific calculations to represent different types of data. Int is used for counting and indexing, float is used for precise measurements and calculations, and unsigned char is used for data that can only have positive values.

3. Can you convert between int, float, and unsigned char?

Yes, it is possible to convert between int, float, and unsigned char. However, the conversion may result in loss of precision or data. For example, converting a float to an int will remove any decimal values.

4. What is the range of values that can be stored in int, float, and unsigned char?

Int can typically store values from -2,147,483,648 to 2,147,483,647. Float can store values from approximately 1.2 x 10^-38 to 3.4 x 10^38. Unsigned char can store values from 0 to 255.

5. How do you declare and initialize variables of type int, float, and unsigned char?

To declare and initialize a variable of type int, use the keyword "int" followed by the variable name and an equal sign, followed by the value. For example: int myInt = 5. To declare and initialize a variable of type float, use the keyword "float" followed by the variable name and an equal sign, followed by the value. For example: float myFloat = 3.14. To declare and initialize a variable of type unsigned char, use the keyword "unsigned char" followed by the variable name and an equal sign, followed by the value. For example: unsigned char myChar = 255.

Similar threads

  • Programming and Computer Science
Replies
32
Views
1K
  • Programming and Computer Science
Replies
21
Views
6K
  • Programming and Computer Science
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Replies
1
Views
2K
Replies
3
Views
10K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top