Hello, I want to find out how many digits does each date type have and
how many bytes does it occupy in memory, so I wrote a program below:
#include <stdio.h>
const long double num=1123222.232121342;
main()
{
Printf("the number occupies: %i, and it is 1123222.232121342
\n",sizeof num);
printf("char: %i ,the number is %c\n",sizeof(char),(char)num);
printf("widechar: %i\n",sizeof(wchar_t));
printf("singed char: %i\n",sizeof(signed char));
printf("unsigned char: %i\n",sizeof(unsigned char));
printf("int: %i, the number is %i\n",sizeof(int),(int)num);
printf("long int: %i, the number is %li\n",sizeof(long int),(long
int)num);
printf("float: %i, the number is %f\n", sizeof(float),float(num));
printf("double: %i, the number is
%lf\n",sizeof(double),(double)num);
printf("long double: %i,the number is %Lf\n",sizeof(long
double),(long double)num);
printf("long long int: %i\n",sizeof(long long int));
printf("long long double: %i\n",sizeof(long long double));
}
But the execution result of the program is very surprising:
the number occupies: 12, and it is 1123222.232121342
char: 1 ,the number is
widechar: 2
singed char: 1
unsigned char: 1
int: 4, the number is 1123222
long int: 4, the number is 1123222
float: 4, the number is 1123222.250000
double: 8, the number is 1123222.232121
long double: 12,the number is -0.000000
long long int: 8
long long double: 8
You may notice that:
the memory occupied by int Date type and long int Date type is the
same,
print of long double number is not correct ,
and the memory long long double date type occupied is smaller (8 bytes)
than long double date type(12 bytes).
The Compiler I Use is GCC. The OS I use is Windows XP.
Can anyone tells me why do these problems happen?
Thanks a million. 5 2375
Donkey wrote: Hello, I want to find out how many digits does each date type have and how many bytes does it occupy in memory, so I wrote a program below:
Which ``date'' type are you talking about?
#include <stdio.h> const long double num=1123222.232121342; main()
int main (void) or
int main (int argc, char *argv[]) or
int main (int argc, char **argv).
Please read the newsgroup archive before posting code.
{ Printf("the number occupies: %i, and it is 1123222.232121342 \n",sizeof num);
Identifiers in C are case-sensitive. ``Printf'' is not the same as
``printf''
printf("char: %i ,the number is %c\n",sizeof(char),(char)num);
Loss of data. Why print a double value as a char?
sizeof evaluates to a value of type size_t. You can use
a cast to ``unsigned long'' here and %lu for the conversion
specifier.
printf("widechar: %i\n",sizeof(wchar_t)); printf("singed char: %i\n",sizeof(signed char)); printf("unsigned char: %i\n",sizeof(unsigned char)); printf("int: %i, the number is %i\n",sizeof(int),(int)num); printf("long int: %i, the number is %li\n",sizeof(long int),(long int)num); printf("float: %i, the number is %f\n", sizeof(float),float(num)); printf("double: %i, the number is %lf\n",sizeof(double),(double)num); printf("long double: %i,the number is %Lf\n",sizeof(long double),(long double)num); printf("long long int: %i\n",sizeof(long long int)); printf("long long double: %i\n",sizeof(long long double)); }
main returns int. But the execution result of the program is very surprising:
Not really surprising.
<snip> You may notice that: the memory occupied by int Date type and long int Date type is the same,
Again, which ``Date'' type? I don't see any such type in your code.
<snip>
Can anyone tells me why do these problems happen? Thanks a million.
They happen because you forget to read a good book on C.
Pick up K&R 2. :)
Regards,
Jonathan.
--
Meow!
"Donkey" <ne********@hotmail.com> writes: Hello, I want to find out how many digits does each date type have and how many bytes does it occupy in memory, so I wrote a program below:
#include <stdio.h> const long double num=1123222.232121342; main()
This should be "int main(void)".
{ Printf("the number occupies: %i, and it is 1123222.232121342\n",sizeof num);
printf, not Printf.
The "%i" format expects an int. sizeof yields a size_t.
To print the result of sizeof, convert it to unsigned long -- or use
"%zu" if your runtime library supports it (it's new in C99). For
example:
printf("sizeof num = %ld\n", (unsigned long)sizeof num);
printf("char: %i ,the number is %c\n",sizeof(char),(char)num); printf("widechar: %i\n",sizeof(wchar_t)); printf("singed char: %i\n",sizeof(signed char)); printf("unsigned char: %i\n",sizeof(unsigned char)); printf("int: %i, the number is %i\n",sizeof(int),(int)num); printf("long int: %i, the number is %li\n",sizeof(long int),(long int)num); printf("float: %i, the number is %f\n", sizeof(float),float(num));
float(num) is a syntax error; you mean (float)num.
printf("double: %i, the number is %lf\n",sizeof(double),(double)num); printf("long double: %i,the number is %Lf\n",sizeof(long double),(long double)num); printf("long long int: %i\n",sizeof(long long int)); printf("long long double: %i\n",sizeof(long long double));
There is no "long long double" in standard C.
}
The code you posted isn't the code you compiled (unless your compiler
is entirely too forgiving). It would also be helpful if you kept your
line length down.
But the execution result of the program is very surprising:
the number occupies: 12, and it is 1123222.232121342 char: 1 ,the number is widechar: 2 singed char: 1 unsigned char: 1 int: 4, the number is 1123222 long int: 4, the number is 1123222 float: 4, the number is 1123222.250000 double: 8, the number is 1123222.232121 long double: 12,the number is -0.000000 long long int: 8 long long double: 8
You may notice that: the memory occupied by int Date type and long int Date type is the same, print of long double number is not correct , and the memory long long double date type occupied is smaller (8 bytes) than long double date type(12 bytes). The Compiler I Use is GCC. The OS I use is Windows XP.
It's not uncommon for types int and long int to be the same size.
Possibly your runtime library (which is separate from your compiler)
doesn't handle "%Lf" properly.
Recent versions of gcc properly reject "long long double". A buggy
old version treats it as "long long".
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jonathan Burd <jb@nomail.com> writes: Donkey wrote: Hello, I want to find out how many digits does each date type have and how many bytes does it occupy in memory, so I wrote a program below:
Which ``date'' type are you talking about?
Perhaps it was a typo for "data type".
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Groovy hepcat Keith Thompson was jivin' on Fri, 31 Mar 2006 06:17:34
GMT in comp.lang.c.
Re: Something About Date Type's a cool scene! Dig it! To print the result of sizeof, convert it to unsigned long -- or use "%zu" if your runtime library supports it (it's new in C99). For example:
printf("sizeof num = %ld\n", (unsigned long)sizeof num);
No doubt Keith means this. (Note the %lu conversion specifier):
printf("sizeof num = %lu\n", (unsigned long)sizeof num);
--
Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"? ph******@alphalink.com.au.NO.SPAM (Peter "Shaggy" Haywood) writes: Groovy hepcat Keith Thompson was jivin' on Fri, 31 Mar 2006 06:17:34 GMT in comp.lang.c. Re: Something About Date Type's a cool scene! Dig it!
To print the result of sizeof, convert it to unsigned long -- or use "%zu" if your runtime library supports it (it's new in C99). For example:
printf("sizeof num = %ld\n", (unsigned long)sizeof num);
No doubt Keith means this. (Note the %lu conversion specifier):
printf("sizeof num = %lu\n", (unsigned long)sizeof num);
Yes, thanks for the correction.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dominique Javet |
last post by:
Hello,
I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this
"newbie" question... I've found no answer in the forum ...
I've a date problem with my formular. In my mysql DB...
|
by: Samir |
last post by:
Say I put a date and time for like this:
Jun-15-04 21:52:06
Here is the form I am using:
<form>
<p><input type="text" name="T1" size="20"><br>
date</p>
<p><input type="text" name="T2"...
|
by: dlx_son |
last post by:
Here is the code so far
<form name="thisform">
<h3>Enter time to add to or subtract from:</h3>
(If not entered, current time will be used)<br>
Day: <input name="d1" alt="Day of month"...
|
by: BlackFireNova |
last post by:
I have an Access 2003 mdb which contains software records. I need to
sort on a particular type of software, and then identify and count how
many copies there are per each group of that type...
|
by: oneplace |
last post by:
I have the following script that use for the following after i have made my search for a name in my database. I select the name and it bring up this from with all the information of the person. It...
|
by: bruce24444 |
last post by:
I have a database which assigns warranty claims to people with a main screen showing number of files assigned to each person. The number assigned shows day, week, month and year numbers so they can...
|
by: anagai |
last post by:
I just want to check if a date entered in a textbox is equal to the
current system date. I set the date object from the input field like
this:
dt1=new Date('10/01/2007');
the current system...
|
by: ruraldev |
last post by:
I have been trying my best to display a chosen date as dd-mm-yyyy but insert it into the mysql database as yyyy-mm-dd, I know it must be simple but no matter what I try I can't get it to work.
...
|
by: aRTx |
last post by:
I have try a couple of time but does not work for me
My files everytime are sortet by NAME.
I want to Sort my files by Date-desc.
Can anyone help me to do it?
The Script
<?
/*
ORIGJINALI
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |