Reputation: 653
My code is:
#include <unistd.h>
(void)alarm(unsigned int 0);
error: expected expression before 'unsigned'
But I'm getting the following error:
Error: expected expression before 'unsigned', due to: (void)alarm(unsigned int 0);
I'm not sure if it is my syntax or use of (void) or something else. What's going on?
I'm using Code::Blocks under Windows.
Upvotes: 0
Views: 27964
Reputation: 18492
Since you've included unistd.h
, I'm assuming you're trying to call the alarm()
function declared in that header:
unsigned int alarm(unsigned int seconds);
If you want to call alarm
with an argument of 0 seconds, simply do alarm(0)
. You don't need to cast 0
to an unsigned int
, but if you really wanted to you would just have alarm( (unsigned)0 );
.
If you don't care about the return type, then just don't assign it to a variable. You don't need to add (void)
to the start of the function call to ignore the return value. (void)alarm(0);
is perfectly legal, but also pointless.
And I'm not sure if your snippet is a chopped up example or your actual code, but you can't just call alarm
from outside of a function like that.
Also, from the man pages:
If seconds is zero, no new
alarm()
is scheduled.
But perhaps you may be wanting to cancel previous alarms:
In any event any previously set
alarm()
is canceled.
EDIT: Didn't realise until Keith pointed it out a few minutes ago that you're using Windows. alarm()
is a *nix function, if you wanted to call it from Windows you could use tools such as Cygwin (which is commonly used) to emulate a *nix environment.
Upvotes: 0
Reputation: 263177
You're not giving us enough context to figure out just what you're trying to do.
I'm going to assume that you want to call the alarm
function with an argument of 0. According to the man page (type man alarm
or man 2 alarm
, or follow this link), alarm(0)
will cancel any existing alarm without setting a new one.
On my system (Ubuntu, a Linux. i.e., Unix-like system), the following compiles, links, and executes without error:
#include <unistd.h>
int main(void) {
alarm(0);
return 0;
}
I saved the program in a file called c.c
, and I compiled and linked it with the following command:
gcc c.c -o c
and executed it with:
./c
The implementation of the alarm
function happens to be in the standard C library, which is linked by default. That might or might not be the case on your system, but if it's Linux or some other Unix-like system, it probably is.
(This isn't a particularly useful program, but it could be a starting point for something useful.
EDIT :
I see now that you're using Windows. The alarm()
function is defined by the POSIX standard, and is (mostly) specific to Unix-like systems. Windows probably doesn't provide it by default. There are Unix-like emulation layers that run under Windows, such as Cygwin.
But if you want to develop code under Windows, you might consider avoiding non-portable constructs that Windows doesn't (directly) support.
Why do you want to call alarm()
? Do you have a requirement to do what that particular function does, or are you just trying to learn the basics?
Upvotes: 1
Reputation: 27123
#include <unistd.h>
int main() {
alarm(0);
}
I think the question is to write a simple program which calls the alarm standard function. (See the comment on @MikeNakis's question).
You can't just copy the code from the man page into a program and compile it. You must make a complete program, like the one I have given here.
Upvotes: 0
Reputation: 61959
Uh, is this meant to be a function call, or is it meant to be a declaration?
If you meant it to be a declaration, then it should be:
void alarm( unsigned int i );
If you meant it to be a function call, then it should be:
(void) alarm( 0 );
(Back in my C++ days I used to cast function results to void when I wanted to document the fact that I do not care what the function returned.)
EDIT: Then again, if what you are trying to do is to just declare a variable, then try this:
unsigned int alarm = 0;
Or if you are just trying to set a variable to zero, then things are even more simple:
alarm = 0;
8-)
Upvotes: 4
Reputation: 791371
It looks like you are trying to call the POSIX alarm
function which takes an unsigned int
and returns and unsigned int
.
A correct form of the call would be:
alarm(0);
There is not normally a need to cast the return value to void
although it can silence a warning on some compilers.
There is normally no need to explicitly cast 0
to unsigned int
. The correct form would be (unsigned int)0
. It is usually simpler to use a suffix where necessary, e.g. 0U
has type unsigned int
but in this instance plain 0
will work fine.
Additionally, as a function call is not a declaration it must appear inside a function body.
E.g.
void foo()
{
alarm(0);
}
Upvotes: 1
Reputation: 3038
Agreed with Chiron that this belongs to StackOverflow.
void
just simply ignore it if you choose but better yet don'tWhy are you declaring variable 0 inside the function call? Call should be:
(void)alarm(0);
Upvotes: 0