1

I'm new to c, and this code confuses me:

pid_t getpid(void)

if what follows the type identifier pid_tis a variable (It's a variable declaration), but instead it's a function call getpid(), I don't know what why this function call is used.

FeifanZ
  • 16,250
  • 7
  • 45
  • 84
mko
  • 21,334
  • 49
  • 130
  • 191

2 Answers2

2

You're right that pid_t is a type identifier, but it's not a variable. pid_t is the return type of the function getpid().

Every function in C has a return type. Functions are declared like this:
returntype functionName(arguments)

For example, int main(int argc, const char * argv[]) returns an int and takes two arguments.

FeifanZ
  • 16,250
  • 7
  • 45
  • 84
  • it makes sense, does this only indicate the return value of the expression for documentation use only? like in php function document,`int time(void)`, but in the actual code, we don't use the return type identifier, – mko Aug 06 '12 at 02:13
  • Whether you use the return value depends on your program. But it's more than for documentation use only—it means that the function *will* return something of that type, which you can capture. For example, if you had written `$currentTime = time();` in your PHP, you're using the return value. – FeifanZ Aug 06 '12 at 02:22
  • Parameter and return types in declarations tell the compiler how to convert arguments and values automatically, and issue warnings if they aren't compatible. – Barmar Aug 06 '12 at 02:38
  • Got it, thanks. could you help me to check this test code that I try to cooperate getpid() function, https://gist.github.com/3269297 – mko Aug 06 '12 at 02:40
  • @Inspire48 I figure it out myself, I miss including some header file, which clang compiler do it for me, however throw the warning – mko Aug 06 '12 at 02:52
1
pid_t getpid(void)

this means the function named getpid doesn't take any parameters (as the argument list contains only void) and returns a value of type pid_t (so you were correct with the type specifier).

Any basic book/tutorial on C would give you this information, I recommend you work through some of this on your own to get the basics down.

This SO question might be helpful: The Definitive C Book Guide and List

Community
  • 1
  • 1
Levon
  • 138,105
  • 33
  • 200
  • 191
  • thanks for your quick answer, I'm now working on basics, is there any book you would recommend of c? – mko Aug 06 '12 at 02:07
  • @yozloy The classic book for C (though it's old) is the ["little white book" by K&R](http://en.wikipedia.org/wiki/The_C_Programming_Language) - see also [this](http://en.wikipedia.org/wiki/The_C_Programming_Language). I haven't seen any recent C texts, so I can't recommend anything specific, but almost any of them would cover the basic topics. By the way, I also just saw some PDF versions of this book floating around, so you could get it that way too :) .. a visit to your local public/university library might help too, you can browse the books to find one you like. – Levon Aug 06 '12 at 02:10
  • @yozloy No one here started out knowing everything .. everyone was a noob one time or the other (though some forget that from time to time) – Levon Aug 06 '12 at 03:37