appropriate caller and print specifier

W

Wade Ward

/* C version */
static unsigned long
t,x=123456789,y=362436069,z=21288629,w=14921776,c=0;
unsigned long KISS(){
x+=545925293;
y^=(y<<13); y^=(y>>17); y^=(y<<5);
t=z+w+c; z=w; c=(t>>31); w=t&2147483647;
return(x+y+w); }
What would be an appropriate caller for this function. Am I correct
that the static specifier has the function remembering its older
variables?
unsigned long function(void)
#include <stdio.h>
int main(void)
{
unsigned long g;
g=KISS();
printf("%lu\n", g); /*looks close*/
return 0;
}
 
U

user923005

/* C version */
static unsigned long
t,x=123456789,y=362436069,z=21288629,w=14921776,c=0;
unsigned long KISS(){
x+=545925293;
y^=(y<<13); y^=(y>>17); y^=(y<<5);
t=z+w+c; z=w; c=(t>>31); w=t&2147483647;
return(x+y+w); }
What would be an appropriate caller for this function. Am I correct
that the static specifier has the function remembering its older
variables?
unsigned long function(void)
#include <stdio.h>
int main(void)
{
unsigned long g;
g=KISS();
printf("%lu\n", g); /*looks close*/
return 0;}

/* C version */
static unsigned long
t,
x = 123456789,
y = 362436069,
z = 21288629,
w = 14921776,
c = 0;
unsigned long KISS()
{
x += 545925293;
y ^= (y << 13);
y ^= (y >> 17);
y ^= (y << 5);
t = z + w + c;
z = w;
c = (t >> 31);
w = t & 2147483647;
return (x + y + w);
}
/* What would be an appropriate caller for this function?
=============================================================
Any caller needing its output, provided there is a single
thread of execution. This function modifies static data
and is therefore not reentrant.
=============================================================
Am I correct that the static specifier has the function
remembering its older variables?
=============================================================
From the ANSI/ISO C Standard ©ISO/IEC ISO/IEC 9899:1999 (E):
"5.1.2 Execution environments
1 Two execution environments are defined: freestanding and hosted. In
both cases, program startup occurs when a designated C function is
called by the execution environment. All objects with static storage
duration shall be initialized (set to their initial values) before
program startup. The manner and timing of such initialization are
otherwise unspecified. Program termination returns control to the
execution environment."

"6.2.4 Storage durations of objects
1 An object has a storage duration that determines its lifetime. There
are three storage durations: static, automatic, and allocated.
Allocated storage is described in 7.20.3.
2 The lifetime of an object is the portion of program execution during
which storage is guaranteed to be reserved for it. An object exists,
has a constant address,25) and retains its last-stored value
throughout its lifetime.26) If an object is referred to outside of its
lifetime, the behavior is undefined. The value of a pointer becomes
indeterminate when the object it points to reaches the end of its
lifetime.
3 An object whose identifier is declared with external or internal
linkage, or with the storage-class specifier static has static storage
duration. Its lifetime is the entire execution of the program and its
stored value is initialized only once, prior to program startup.
4 An object whose identifier is declared with no linkage and without
the storage-class specifier static has automatic storage duration."
=============================================================
*/

#include <stdio.h>
int main(void)
{
unsigned long g;
g = KISS();
printf("%lu\n", g); /* looks close */
return 0;
}
 
B

Barry Schwarz

/* C version */
static unsigned long
t,x=123456789,y=362436069,z=21288629,w=14921776,c=0;

If you want help, the least you could do is try to make your code
readable. Horizontal whitespace and indenting do not increase your
message cost.
unsigned long KISS(){
x+=545925293;
y^=(y<<13); y^=(y>>17); y^=(y<<5);
t=z+w+c; z=w; c=(t>>31); w=t&2147483647;
return(x+y+w); }
What would be an appropriate caller for this function. Am I correct

If you mean calling statement, then the one you have below is
adequate. You could call KISS in any statement where an expression of
type unsigned long would be allowed.
that the static specifier has the function remembering its older
variables?

The static qualifier means
1 - The objects are created and initialized only once when
your program (not function) starts.
2 - The object remain "alive" for the duration of your program
(not function).

These two statement lead to the conclusion that the only way any of
these objects change value is via an executable statement. Since you
never pass the address of any of these variables to another function
(the only way another function could access them), the values will
remain unchanged between successive calls to KISS.
unsigned long function(void)

What did you intend for this statement?
#include <stdio.h>
int main(void)
{
unsigned long g;
g=KISS();
printf("%lu\n", g); /*looks close*/

Is your question about calling KISS or printing its return value?
return 0;
}


Remove del for email
 
C

CBFalconer

Barry said:
.... snip ...


The static qualifier means
1 - The objects are created and initialized only once when
your program (not function) starts.
2 - The object remain "alive" for the duration of your program
(not function).

Not if applied to a function.
 
B

Barry Schwarz

So? This is about functions, not objects. You injected objects.

No it wasn't and I didn't. You looked at the question without looking
at the code it referred to.

If you look at the text which I quoted in my response to the original
poster, the static qualifier was applied to the definition of a bunch
of unsigned long variables. The signature of the function was simply
unsigned long with no static.


Remove del for email
 
W

Wade Ward

If you want help, the least you could do is try to make your code
readable. Horizontal whitespace and indenting do not increase your
message cost.


If you mean calling statement, then the one you have below is
adequate. You could call KISS in any statement where an expression of
type unsigned long would be allowed.


The static qualifier means
1 - The objects are created and initialized only once when
your program (not function) starts.
2 - The object remain "alive" for the duration of your program
(not function).

These two statement lead to the conclusion that the only way any of
these objects change value is via an executable statement. Since you
never pass the address of any of these variables to another function
(the only way another function could access them), the values will
remain unchanged between successive calls to KISS.


What did you intend for this statement?
Thanks for your reply. I intended this to be the declaration of the
function KISS. Since I omitted KISS altogether, it looks wrong. Is
this better?
unsigned long function KISS(void)
Is your question about calling KISS or printing its return value?
I think a caller has to call the right type and display its value in
main. So yes.
 
U

user923005

Thanks for your reply. I intended this to be the declaration of the
function KISS. Since I omitted KISS altogether, it looks wrong. Is
this better?
unsigned long function KISS(void)

Lose the word 'function'. You want:
unsigned long KISS(void);

It is not strictly necessary if the function body precedes its use.
I think a caller has to call the right type and display its value in
main. So yes.

A function should have a correct prototype in scope when it is
called. Quite often, that bit will allow the compiler to do whatever
conversions are required.
I can't really parse your sentences above very well.
 
B

Barry Schwarz

snip

Thanks for your reply. I intended this to be the declaration of the
function KISS. Since I omitted KISS altogether, it looks wrong. Is
this better?
unsigned long function KISS(void)

Well, you are getting closer. Delete the word "function" and add a
semicolon and you will have it.



Remove del for email
 
W

Wade Ward

user923005 said:
Lose the word 'function'. You want:
unsigned long KISS(void);

It is not strictly necessary if the function body precedes its use.


A function should have a correct prototype in scope when it is
called. Quite often, that bit will allow the compiler to do whatever
conversions are required.
I can't really parse your sentences above very well.
I think I was trying to say that a proper caller has the correct prototype
in scope, and--this is my own thing-- it prints out the value that it
requisitioned. I'm less interested in writing my own stuff in C nowadays,
but I don't want to get so rusty that I can't call somebody else's. My
compiler is giving me a cool one hundred errors right now for the following.
unsigned long KISS(void)
#include <stdio.h>
int main(void)
{
unsigned long g;

static unsigned long t,x=123456789,y=362436069,z=21288629,w=14921776,c=0;
g=KISS();
printf("%lu\n", g);
return 0;
}

unsigned long KISS(){
x+=545925293;
y^=(y<<13); y^=(y>>17); y^=(y<<5);
t=z+w+c; z=w; c=(t>>31); w=t&2147483647;
return(x+y+w); }
! end source
This is my first error:
6 C:\Dev-Cpp\include\stddef.h:6, from
C:\Dev-Cpp\include\stddef.h In file included from
C:/Dev-Cpp/Bin/../lib/gcc/mingw32/3.4.2/../../../../include/stddef.h:6,
from C:/Dev-Cpp/include/stddef.h
Is my compiler unable to find stdio.h ?
 
K

Keith Thompson

Wade Ward said:
My compiler is giving me a cool one hundred errors right now for the
following.
unsigned long KISS(void)
#include <stdio.h>
int main(void)
{
unsigned long g;

static unsigned long t,x=123456789,y=362436069,z=21288629,w=14921776,c=0;
g=KISS();
printf("%lu\n", g);
return 0;
}

unsigned long KISS(){
x+=545925293;
y^=(y<<13); y^=(y>>17); y^=(y<<5);
t=z+w+c; z=w; c=(t>>31); w=t&2147483647;
return(x+y+w); }
! end source
This is my first error:
6 C:\Dev-Cpp\include\stddef.h:6, from
C:\Dev-Cpp\include\stddef.h In file included from
C:/Dev-Cpp/Bin/../lib/gcc/mingw32/3.4.2/../../../../include/stddef.h:6,
from C:/Dev-Cpp/include/stddef.h
Is my compiler unable to find stdio.h ?

No, it's unable to process the declarations in stdio.h because you've
introduced a syntax error before the #include directive.

When you post code, please Please PLEASE indent it properly. It makes
it much easier to read. (Sometimes tab characters are mangled by
Usenet software; use spaces for indentation if at all possible.)
Judicious use of whitespace around operators is also very helpful.

Here's a copy of your code with whitespace added, and with no other
changes:

unsigned long KISS(void)
#include <stdio.h>
int main(void)
{
unsigned long g;

static unsigned long t, x = 123456789, y = 362436069, z = 21288629,
w = 14921776, c = 0;
g = KISS();
printf("%lu\n", g);
return 0;
}

unsigned long KISS()
{
x += 545925293;
y ^= (y<<13); y ^= (y>>17); y ^= (y<<5);
t = z + w + c; z = w; c = (t>>31); w = t&2147483647;
return (x + y + w);
}

Now take a look at your first line. There's a missing semicolon.
Declarations can span multiple lines, so as far as the compiler knows,
you just haven't finished the declaration yet; it continues looking
for a valid completion *in stdio.h*. Obviously it doesn't find one,
but it keeps looking until it can prove that there's an error. (It's
legal, but pretty much never a good idea, to begin a declaration in
one file and complete it in a nested include file.) A really smart
compiler might have guessed that the problem is the missing s

Also, your #include directives should always precede any of your own
code.

The trailing '}' of your KISS function is on the same line as the return
statement. Put it on a line by itself.

The name KISS is a poor choice; all-caps identifiers are
conventionally reserved for macros.

The prototype for KISS is inconsistent with the declaration;
unsigned long KISS(void)
and
unsigned long KISS()
mean different things.

When I fix the missing semicolon and move the #include directive to
the top of the file, I get complaints that x, y, z, t, w, and c are
undeclared in KISS. You declared these variables in main; they're not
visible in KISS. (Declaring them "static" affects their lifetime, not
their visibility.)

Since these variables are only used inside KISS, why not declare them
there?

I haven't a clue what your program is supposed to do, so I can't guess
whether it's correct or not.
 
W

Wade Ward

A function should have a correct prototype in scope when it is
called. Quite often, that bit will allow the compiler to do whatever
conversions are required.
I can't really parse your sentences above very well.
unsigned long KISS(void);
static unsigned long t, x=123456789, y=362436069, z=21288629,
w=14921776, c=0;

#include <stdio.h>
int main(void){
unsigned long g, i;
for (i=1;i<=100000;++i)
{
g=KISS();
if(i > 99996){
printf("%lu %lu\n",i, g);
}
}
// system("pause");
return 0;
}

unsigned long KISS(){
x+=545925293;
y^=(y<<13); y^=(y>>17); y^=(y<<5);
t=z+w+c; z=w; c=(t>>31); w=t&2147483647;
return(x+y+w);
}
This seems to work. I think my variables were at the wrong scope. Thanks
for your help.
 
B

Barry Schwarz

unsigned long KISS(void);
static unsigned long t, x=123456789, y=362436069, z=21288629,
w=14921776, c=0;

#include <stdio.h>
int main(void){
unsigned long g, i;
for (i=1;i<=100000;++i)
{
g=KISS();
if(i > 99996){
printf("%lu %lu\n",i, g);
}
}
// system("pause");
return 0;
}

unsigned long KISS(){
x+=545925293;
y^=(y<<13); y^=(y>>17); y^=(y<<5);
t=z+w+c; z=w; c=(t>>31); w=t&2147483647;
return(x+y+w);
}
This seems to work. I think my variables were at the wrong scope. Thanks
for your help.

All the static unsigned longs can (should) be placed in KISS.


Remove del for email
 
B

Barry Schwarz

...else why the static modifier?

All objects defined at file scope have static duration, with or
without the qualifier. The static modifier there restricts them to
internal linkage (they are not visible outside this translation unit).

The static qualifier for an object with block scope is the only(?) way
to give the object static duration. It doesn't affect visibility or
linkage at all.


Remove del for email
 
W

Wade Ward

Barry Schwarz said:
All objects defined at file scope have static duration, with or
without the qualifier. The static modifier there restricts them to
internal linkage (they are not visible outside this translation unit).

The static qualifier for an object with block scope is the only(?) way
to give the object static duration. It doesn't affect visibility or
linkage at all.
Thanks again for your help. I was pretty impressed that I got the printf
correct, but I would never have gotten the finer points.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top