Skip to main content
gamedev.net
🔒 Locked

Suggestions on console program: memory values!

Started by julioykaly 4 replies 950+ views
Original Post
julioykaly
julioykaly
Hello everyone! I just wanted to know did I solve the problem giving to me in K&R "The C Programming Language":

/* 
   Write a program to determine the ranges of char, short, int,
   and long variables, both signed and unsigned, by printing 
   appropriate values from standard headers and by direct computation.
   Harder if you compute them: determine the ranges of the various
   floating-point types. 
*/

#include <stdio.h>
#include <limits.h>
#include <float.h>

main()
{
 printf("Bits in a char: %d\n", CHAR_BIT);
 printf("Maximum value of char: %d\n", CHAR_MAX);
 printf("Minimum value of char: %d\n", CHAR_MIN);
 printf("Maximum value of int: %d\n", INT_MAX);
 printf("Minimum value of int: %d\n", INT_MIN);
 printf("Maximum value of long: %d\n", LONG_MAX);
 printf("Minimum value of long: %d\n", LONG_MIN);
 printf("Maximum value of short: %d\n", SHRT_MAX);
 printf("Minimum value of short: %d\n", SHRT_MIN);
 printf("Maximum value of signed char: %d\n", SCHAR_MAX);
 printf("Minimum value of signed char: %d\n", SCHAR_MIN);
 printf("Maximum value of unsigned int: %d\n", UINT_MAX);
 printf("Maximum value of unsigned long: %d\n", ULONG_MAX);
 printf("Maximum value of unsigned short: %d\n", USHRT_MAX);
 return 0;
}

Please and Thank you! Julio A. Cruz
programmermattc
programmermattc
I don't know all the details and I could be wrong but you could do sizeof() couldn't you? I think that's what I've seen in the book I've been reading.
=============================RhinoXNA - Easily start building 2D games in XNA!Projects
KulSeran
KulSeran
Looks like you got the first half correct.
But you dont have any of the "and by direct computation" part in there.
julioykaly
julioykaly
That's the problem I don't know what K&R mean when they say "Direct Computation"!

Thanks!

Julio A. Cruz
Zahlman
Zahlman
Calculate the value.

- How do you find out the size of the data type in bytes?
- How do you find out how many bits are in a byte? (Hint: how many bytes is a char?)
- Therefore, how do you find out the size of a data type in bits?
- What's the biggest|smallest number you can represent in N (signed|unsigned) bits? (Hint: What's the biggest number you can represent with 3 decimal digits? with 5? Etc. How do these numbers relate to powers of 10? Why should they have that relationship? What would you expect when working with numbers in base 2?)
- Therefore, what are biggest|smallest values for each data type?
vininim
vininim
I would say the other part is mainly about under/overflow detection algorithms for the diverse data types (and that's why float is hard).
sizeof can help, but you need to know the range of the unit(I believe sizeof returns everything related to char size).

(as Zahlman already answered and I shamefully misread)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.