C API - does it exist

drwoo123

Member
Apr 3, 2002
195
0
0
Coming from the world of java I'm used to having access to an API. Does one exist for ANSI C. For example there is header called stdio.h . How do I find out what functions (kinda like methods I imagine) are available in this "library".

Forgive me b/c I'm just starting out in C - but I have the following analogies in my mind.


Headers are like classes in java - only you cant create an instance of them since objects dont exist.

Functions are like the methods that would be available to a class, only you don't have a calling object but rather just call them directly.

So my question is where is the documentation of these headers and functions - what they return, what they take as parameters and perhaps even code snipped examples.

Or am I wayy off track here.

An example is using printf("%22.7e" , variable);

How would I find out what the format %22.7e stands for - must be documented somewhere right.

thanks
 

Burnsy

Member
Dec 30, 2001
90
0
0
You can find a reference for the C standard libraries at cplusplus.com.

If you're coming straight from Java you'd probably feel more at home with C++ than with C :)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
If you're on a unix system, use man pages. man 2 printf.

A header isn't really like a class. A header is basically a bunch of definitions. When you #include it, you literally dump its contents into your file right at that point. There are no namespaces, so you have to make sure you use function names that aren't already used. A class is meant to be instantiated and become an object. "Objects" don't really even exist in C.

Yeah, functions are basically the same as methods -- no, it probably makes more sense the other way around -- methods are functions that are tied to a class/object.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
I'll expound a bit on what BBWF said...

Originally posted by: drwoo123

Headers are like classes in java

No. A header file is based on the idea that the declaration should be separate from the definition. In C and C++ you can have only one definition; however, you can have literally an infinite number of declarations. You could certainly declare something in every translation unit; however, that violates basic principles of orthogonality (DRY--don't repeat yourself, OAOO-once and only once). The solution is to offload this onto the preprocessor with includes as BBWF said. The key conceptual difference with classes is that the declaration and definition are essentially coalesced into a single abstraction. These languages (e.g. Java, C#, ...) do not have a concept of declarations independent of definitions.

Functions are like the methods that would be available to a class, only you don't have a calling object but rather just call them directly.

Not to be pedantic, but a you can't call methods on a class; rather, you call methods on an object. This might seem a trivial point to make, but it's very important. When you compile code that makes use of a class' methods it will, depending on the compiler, wire up the call to jmp to the code wherever it resides in the resultant executable. There is generally no level of indirection between the caller and the callee unless you making use of facilities only available at run-time (function pointers, delegates, events, etc.). This isn't a rule, and different compilers might handle it differently (e.g. the use of vtables).

So my question is where is the documentation of these headers and functions - what they return, what they take as parameters and perhaps even code snipped examples.

Or am I wayy off track here.

An example is using printf("%22.7e" , variable);

How would I find out what the format %22.7e stands for - must be documented somewhere right.

thanks

The functions declared in stdio.h are part of ANSI C, so any documentation covering ANSI C will discuss these functions. Any other library you employ will generally have documentation available for it as well. The %22.7e you refer to is simply known as a format specifier. As you can see, I prefer using MSDN for my documentation, but there are many available.

HTH.