Go Back   Forums > Community Chatterbox > Tech Corner
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 24-08-2013, 06:15 PM   #1
Mighty Midget
Pox Vobiscum
 
Mighty Midget's Avatar


 
Join Date: Mar 2006
Location: Krakeroy, Norway
Posts: 3,014
Default C code noob's corner

I am (trying???) to learn C as part of my studies. Things are going fairly ok in programming but extending one task is giving me some grey hairs.

This code will output the size in bytes for various types of variables, as well as the value range. However, things are tricky when it comes to the
float
double
both signed and unsigned, long and short, if it's there, I want to include it in the program.

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

int main()
{
    printf("char... \tsize: %d byte \t", sizeof(char));
    printf("%d to %d \n", CHAR_MIN, CHAR_MAX);
    printf("unsgn char... \tsize: %d byte \t", sizeof(unsigned char));
    printf("0 to %d \n", UCHAR_MAX);
    printf("short int... \tsize: %d bytes \t", sizeof(short int));
    printf("%d to %d \n", SHRT_MIN, SHRT_MAX);
    printf("unsgn s int... \tsize: %d bytes \t", sizeof(unsigned short int));
    printf("0 to %d \n", USHRT_MAX);
    printf("long int... \tsize: %d bytes \t", sizeof(long int));
    printf("%ld to %ld \n", LONG_MIN, LONG_MAX);
    printf("unsgn l int... \tsize: %d bytes \t", sizeof(unsigned long int));
    printf("0 to %u \n", ULONG_MAX);                                            
    printf("float... \tsize: %d bytes \n", sizeof(float));
    printf("double... \tsize: %d bytes \n)", sizeof(double));
return 0;
}
As you can see, the MIN and MAX are missing for the last two, the float and double, and I haven't figured out if there are any long/short/signed/unsigned versions of these two.

The float header was included as I messed around with different float constants not getting anywhere.

Any input, so to speak, would be greatly appreciated!
__________________
Je Suis Charlie
Mighty Midget is offline                         Send a private message to Mighty Midget
Reply With Quote
Old 25-08-2013, 07:25 PM   #2
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,613
Default

The short answer is that, if you haven't found those constants in the same place as the rest, they probably don't exist. Although I'm not sure, and there's always a way to find those values out. Such constants aren't required by the C specifications; whether they're defined or not depends on the implementation (compiler) that you're using.

In C, not even the bit sizes and ranges of data types are defined uniquely. For example an int will be 32 bits long in a 32-bit environment, and 64 bits long in a 64-bit one. Most more recent languages do specify fixed sizes and ranges for all data types, but C is different in this regard.

PS: there's a sub board for this:
http://www.abandonia.com/vbullet/forumdisplay.php?f=25
__________________
Life starts every day anew. Prospects not so good...

Last edited by Japo; 25-08-2013 at 07:33 PM.
Japo is offline                         Send a private message to Japo
Reply With Quote
Old 27-08-2013, 05:40 AM   #3
jonh_sabugs
Abandonia nerd

 
Join Date: May 2010
Location: Brazil
Posts: 91
Default

Actually, float.h contains the constants FLT_MAX, FLT_MIN, DBL_MAX and DBL_MIN, which are the largest and smallest numbers you can represent in those types. These values are ANSI, and must exist in any standard C compiler. It seems, though, that these values are just the minimum/maximum values that the compiler must accept (i.e. a float could have numbers larger than FLT_MAX, but it must be able to represent at least numbers up to FLT_MAX).
jonh_sabugs is offline                         Send a private message to jonh_sabugs
Reply With Quote
Old 27-08-2013, 03:34 PM   #4
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,613
Default

Thanks John and sorry MM if I was misleading. I'm not familiar with the different C standards, but I know that data types in C are dependent on the platform (bitness), although the compiler takes care of parsing the correct #define's depending on the environment.

Look what I found:
http://en.wikipedia.org/wiki/C_data_...he_basic_types
__________________
Life starts every day anew. Prospects not so good...
Japo is offline                         Send a private message to Japo
Reply With Quote
Old 27-08-2013, 03:53 PM   #5
jonh_sabugs
Abandonia nerd

 
Join Date: May 2010
Location: Brazil
Posts: 91
Default

Hello Japo. You are correct, C data types may have different sizes in different implementations, which can be annoying, but the ANSI standard defines a minimum range the types must represent. Take a look here:

http://www.acm.uiuc.edu/webmonkeys/b...guide/2.4.html

Which was actually a good idea from the guys who created the standard, otherwise portability would be a pain.
jonh_sabugs is offline                         Send a private message to jonh_sabugs
Reply With Quote
Old 27-08-2013, 04:27 PM   #6
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,613
Default

Thanks I didn't know there was a minimum irrespective of platform. By "the standard" I suppose you mean C89?

Anyway MM keep the questions coming, I hope you stick to learning! BTW to be real standard C you should define "int main(void)", not "int main()"
__________________
Life starts every day anew. Prospects not so good...

Last edited by Japo; 27-08-2013 at 04:33 PM.
Japo is offline                         Send a private message to Japo
Reply With Quote
Old 27-08-2013, 04:37 PM   #7
jonh_sabugs
Abandonia nerd

 
Join Date: May 2010
Location: Brazil
Posts: 91
Default

That's a good question. I originally learned about the types restrictions as being ANSI, but I don't know which of the standards issued it.
jonh_sabugs is offline                         Send a private message to jonh_sabugs
Reply With Quote
Old 27-08-2013, 05:07 PM   #8
Mighty Midget
Pox Vobiscum
 
Mighty Midget's Avatar


 
Join Date: Mar 2006
Location: Krakeroy, Norway
Posts: 3,014
Default

Thanks tons to both of you! And don't worry, Japo, I will keep asking I will need all the inputs I can get. This school isn't wasting time or waiting for stragglers!
__________________
Je Suis Charlie
Mighty Midget is offline                         Send a private message to Mighty Midget
Reply With Quote
Old 27-08-2013, 07:44 PM   #9
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default

Quote:
As you can see, the MIN and MAX are missing for the last two, the float and double, and I haven't figured out if there are any long/short/signed/unsigned versions of these two.
IIRC, long double is a thing. Float is what you'd describe as "short double", really.
As per the IEEE-754 specification, all floating point numbers are signed. See http://en.wikipedia.org/wiki/IEEE_floating_point for more details. And thank your deity of choice that you don't have to learn that in order to implement floating point operations in assembly... completely on CPU.

Also, you might be missing wchar_t but I don't remember for sure if it's a C or C++ thing.
Quote:
Originally Posted by Japo View Post
In C, not even the bit sizes and ranges of data types are defined uniquely. For example an int will be 32 bits long in a 32-bit environment, and 64 bits long in a 64-bit one. Most more recent languages do specify fixed sizes and ranges for all data types, but C is different in this regard.
IIRC, there is an include for that sort of thing (though my memory fails to provide the name).
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote
Old 27-08-2013, 09:23 PM   #10
Japo
Autonomous human
 
Japo's Avatar


 
Join Date: Mar 2006
Location: ,
Posts: 4,613
Default

Quote:
Originally Posted by The Fifth Horseman View Post
IIRC, there is an include for that sort of thing (though my memory fails to provide the name).
But I don't think it's ANSI... Microsoft has its own extended data type system at least since they created the Windows API:
http://msdn.microsoft.com/en-us/library/aa383751.aspx

I think wchar_t is C++ (never used C++ a lot) but I don't think it's C, unless it was introduced in C99, or C11 (most popular compilers don't follow even C99).
__________________
Life starts every day anew. Prospects not so good...
Japo is offline                         Send a private message to Japo
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
EoF Corner Eagle of Fire Offers 6 03-02-2010 05:01 PM
The end of the file corner Eagle of Fire Music, Art, Movies 8 08-12-2009 12:09 AM
Spoonman's Art Corner Spoonman Music, Art, Movies 122 15-01-2009 09:35 PM
Spoonman's Music Corner Spoonman Music, Art, Movies 3 25-06-2005 05:18 PM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 01:11 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.