473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

c & time problem

RVO
Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.
Would someone kindly explain how this works and/or correct my understanding?

#include <stdio.h>
#include <time.h>
// Gets System Date and Time
int main(void)
{
int i;
i = mytim();
printf("D/T= %s", i);
}

mytim()
{
time_t now;
if( (now = time(NULL)) == (time_t) - 1 )
puts("FAIL: Unable to get time");
else
printf("Date & time: %s", ctime(&now));

// Either of following works equally well
return (ctime(&now));
}
Oct 22 '06 #1
10 1315
RVO wrote:
Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.
Why do you assume 16 bit int? The function name is main, not MAIN.
Would someone kindly explain how this works and/or correct my
understanding?

#include <stdio.h>
#include <time.h>
// Gets System Date and Time
int main(void)
{
int i;
i = mytim();
printf("D/T= %s", i);
}

mytim()
Don't rely on implicit int return type.
{
time_t now;
if( (now = time(NULL)) == (time_t) - 1 )
puts("FAIL: Unable to get time");
else
printf("Date & time: %s", ctime(&now));
// Either of following works equally well
return (ctime(&now));
}
Did you take note of all of the compiler warnings emitted when you
compiled this?

A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.

--
Ian Collins.
Oct 22 '06 #2
RVO
Ian Collins wrote:
>RVO wrote:

>>Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.


Why do you assume 16 bit int? The function name is main, not MAIN.

1) because it is.
2) MAIN for emphasis.
>
>>Would someone kindly explain how this works and/or correct my
understanding?

#include <stdio.h>
#include <time.h>
// Gets System Date and Time
int main(void)
{
int i;
i = mytim();
printf("D/T= %s", i);
}

mytim()


Don't rely on implicit int return type.

3) Got code from "C" expert on web.
>
>>{
time_t now;
if( (now = time(NULL)) == (time_t) - 1 )
puts("FAIL: Unable to get time");
else
printf("Date & time: %s", ctime(&now));
// Either of following works equally well
return (ctime(&now));
}


Did you take note of all of the compiler warnings emitted when you
compiled this?

4) the warning was not related to this as far as I can tell.
>A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.
5) But if "C" is so unique why does it not flag the %s usage with the
integer?
Oct 22 '06 #3
RVO wrote:
Ian Collins wrote:
>RVO wrote:

>>Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.


Why do you assume 16 bit int? The function name is main, not MAIN.

1) because it is.
On what?
>>
Don't rely on implicit int return type.

3) Got code from "C" expert on web.
One to avoid then.
>>

Did you take note of all of the compiler warnings emitted when you
compiled this?

4) the warning was not related to this as far as I can tell.
/tmp/x.c: In function `main':
/tmp/x.c:7: warning: implicit declaration of function `mytim'
/tmp/x.c:8: warning: format argument is not a pointer (arg 2)
/tmp/x.c:9: warning: control reaches end of non-void function
/tmp/x.c: At top level:
/tmp/x.c:12: warning: return type defaults to `int'
/tmp/x.c: In function `mytim':
/tmp/x.c:19: warning: return makes integer from pointer without a cast

Which one? The warning for line 8 looks plain enough. Perhaps you
didn't invoke your compiler in conforming mode.
>A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.
5) But if "C" is so unique why does it not flag the %s usage with the
integer?
gcc did, see above.

--
Ian Collins.
Oct 22 '06 #4
Ian Collins wrote:
>
gcc did, see above.
I should point out that this warning is (possibly) unique to gcc. The
compiler is not required (or able without knowledge of the passed
arguments) to type check arguments passed to a variadic function like
printf. Doing so requires compile time parsing of the argument list.

--
Ian Collins.
Oct 22 '06 #5
RVO wrote:
Ian Collins wrote:
>RVO wrote:
>>I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In
main the returned "string" is assigned to an integer (16 bit)
and is printed with a string format in MAIN.

Why do you assume 16 bit int? The function name is main, not
MAIN.
1) because it is.
No it isn't necessarily. You can draw some conclusions from the
value of INT_MAX in <limits.h>
2) MAIN for emphasis.
Silly.
>
>>Would someone kindly explain how this works and/or correct my
understanding?

#include <stdio.h>
#include <time.h>
// Gets System Date and Time
int main(void)
{
int i;
i = mytim();
printf("D/T= %s", i);
}

mytim()

Don't rely on implicit int return type.
3) Got code from "C" expert on web.
He wasn't an expert.
.... snip ...

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 22 '06 #6
On Sun, 2006-10-22 at 02:39 -0700, RVO wrote:
Ian Collins wrote:
RVO wrote:
>Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.
Why do you assume 16 bit int? The function name is main, not MAIN.
1) because it is.
No, it isn't. You need to understand that to use C.
2) MAIN for emphasis.
Just write main(). It's significance in your program implies emphasis.

<<code snipped>>
Don't rely on implicit int return type.
3) Got code from "C" expert on web.
So? If everyone on the web who claimed to know C actually did, this
group wouldn't have nearly as much traffic, would it?

I should also mention that you shouldn't use // commments. They aren't
valid in C90, and (more importantly) will not display correctly on
Usenet unless great care is taken.

<<code snipped>>
Did you take note of all of the compiler warnings emitted when you
compiled this?
4) the warning was not related to this as far as I can tell.
So you ignored it? You need to take note of /all/ warnings, /always/.
Always. I cannot stress that enough.
>
A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.
5) But if "C" is so unique why does it not flag the %s usage with the
integer?
Because it's a language? Not many languages have the capacity for verbs
like "flag". They fail to even really exist unless someone takes the
time to write them down.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 22 '06 #7
RVO
Ian Collins wrote:
>RVO wrote:

>>Ian Collins wrote:
>>>RVO wrote:


Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.

Why do you assume 16 bit int? The function name is main, not MAIN.

1) because it is.


On what?
>>>Don't rely on implicit int return type.

3) Got code from "C" expert on web.
One to avoid then.
>>>Did you take note of all of the compiler warnings emitted when you
compiled this?

4) the warning was not related to this as far as I can tell.
/tmp/x.c: In function `main':
/tmp/x.c:7: warning: implicit declaration of function `mytim'
/tmp/x.c:8: warning: format argument is not a pointer (arg 2)
/tmp/x.c:9: warning: control reaches end of non-void function
/tmp/x.c: At top level:
/tmp/x.c:12: warning: return type defaults to `int'
/tmp/x.c: In function `mytim':
/tmp/x.c:19: warning: return makes integer from pointer without a cast

Which one? The warning for line 8 looks plain enough. Perhaps you
didn't invoke your compiler in conforming mode.
>>>A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.


5) But if "C" is so unique why does it not flag the %s usage with the
integer?


gcc did, see above.
Really wish that when people respond to a query they would address the
points made. I do not intend to carry this conversation further with
your bombastic supercilious remarks.
Oct 23 '06 #8
RVO
Andrew Poelstra wrote:
>On Sun, 2006-10-22 at 02:39 -0700, RVO wrote:

>>Ian Collins wrote:

>>>RVO wrote:
Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.

Why do you assume 16 bit int? The function name is main, not MAIN.
1) because it is.


No, it isn't. You need to understand that to use C.
>>2) MAIN for emphasis.


Just write main(). It's significance in your program implies emphasis.

<<code snipped>>

>>>Don't rely on implicit int return type.

3) Got code from "C" expert on web.

So? If everyone on the web who claimed to know C actually did, this
group wouldn't have nearly as much traffic, would it?

I should also mention that you shouldn't use // commments. They aren't
valid in C90, and (more importantly) will not display correctly on
Usenet unless great care is taken.

<<code snipped>>

>>>Did you take note of all of the compiler warnings emitted when you
compiled this?
4) the warning was not related to this as far as I can tell.


So you ignored it? You need to take note of /all/ warnings, /always/.
Always. I cannot stress that enough.
>>>A classic case of undefined behaviour, sizeof char* happens to equal
sizeof int.
5) But if "C" is so unique why does it not flag the %s usage with the
integer?


Because it's a language? Not many languages have the capacity for verbs
like "flag". They fail to even really exist unless someone takes the
time to write them down.
I have little to learn from this group. Thank you for wasting my time.
Oct 23 '06 #9
RVO said:
Hi,
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.
Would someone kindly explain how this works and/or correct my
understanding?
The code "works" (in the loosest sense of the word) because the platform you
are using happens to convert from char * to int without loss of information
or corruption of representation. Not all platforms do so, so your instincts
are dead right - the code is broken, even though it "works" on your
platform.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 23 '06 #10
RVO wrote:
I am having a problem understanding why this code works.
As I understand it the "ctime" function returns a string. In main the
returned "string" is assigned to an integer (16 bit) and is printed with
a string format in MAIN.
Would someone kindly explain how this works and/or correct my understanding?

#include <stdio.h>
#include <time.h>
// Gets System Date and Time
int main(void)
{
int i;
i = mytim();
printf("D/T= %s", i);
}

mytim()
{
time_t now;
if( (now = time(NULL)) == (time_t) - 1 )
puts("FAIL: Unable to get time");
else
printf("Date & time: %s", ctime(&now));

// Either of following works equally well
return (ctime(&now));
}
this is absolutely awful code that simply crawls with Undefined
Behaviour. That
is the C standard does not specify the semantics of programs that have
these
constructs, consequently the implementer can do what he damn well
pleases.

Hence "why does this work?" is not a meaningful question.

Why does it seem to "work" then? Well if mytim returns a string in C
it actually returns a char* (ptr-to-char). A pointer is often
implemented as
a machine address. The machine address gets stuffed into an int. You
claim ints are16-bit on your platform. So I guess addresses are 16-bit
(unusual...) on your platform. You then tell printf() to print an int
as if it
were a char*, and printf() does its best with the mutilated machine
address.
This motley collection of UB then produces what you think is sensible
output. On you platform. This time. Don't rely on UB.

Tip for the future- if you ask for help don't be rude to those who try
to
help you. I'm giving you the benefit of the doubt this time.

--
Nick Keighley

Oct 23 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
12
by: ~~~ .NET Ed ~~~ | last post by:
Hi, I have a standalone XML file (with the appropriate xml document header) that works fine when I load it using XmlDocument. I can have child elements like this without problems: ...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
0
by: SeanGallavan | last post by:
Our Environment: Two network load balanced (using Microsoft NLB software) webservers with session maintained in a SQL Server database. NLB is configured with no affinity settings and two...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
7
by: Kirt | last post by:
i have walked a directory and have written the foll xml document. one of the folder had "&" character so i replaced it by "&amp;" #------------------test1.xml <Directory> <dirname>C:\Documents and...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.