Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   stdio.h what language is it (http://www.programmingforums.org/showthread.php?t=15658)

tyros Apr 19th, 2008 10:44 AM

stdio.h what language is it
 
I m curious abt the header files used in C
i tried reading the code in stdio,h for the function scanf
but i cant understand anything suggest any source for some info regarding this

Jessehk Apr 19th, 2008 11:15 AM

Re: stdio.h what language is it
 
Any header files used by C (stdio.h, math.h, stdlib.h, string.h, etc) are all written in C. Where you might be getting confused is that most implementations of the C standard library that I've seen (especially on POSIX systems where there is a lot of added functionality) are extremely complicated.

There are LOTS of macro tricks, compiler tricks (and extensions) and it is generally unreadable.
For example, the character class functions are fairly easy to use:
:

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. int main( void ) {
  5.     int ch = 'A';
  6.  
  7.     /* is ch an alphabetic character? */
  8.     if ( isalpha( ch ) )
  9.         puts( "\'A\' is alphabetic!" );
  10.  
  11.     return 0;
  12. }


But if you check its preprocessed output (with gcc -E in my particular case), you'll see a massive file where the entire contents of the stdio.h file and the ctype.h file (as well as some other stuff that I don't pretend to understand) have been included.

The actual call to isalpha() was replaced with this:

:

  1. int main( void ) {
  2.     int ch = 'A';
  3.  
  4.  
  5.     if ( ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISalpha) )
  6.         puts( "\'A\' is alphabetic!" );
  7.  
  8.     return 0;
  9. }


I'm pretty sure that the implementation of the character class functions in glibc (the GNU standard C library) is actually a table lookup.

If you think that's bad, try looking at the C++ headers. :icon_eek:

EDIT: I should also mention that the definition for functions will not be in the header files. Header files declare functions so that the compiler and linker will be aware of them. The actual source for a function like scanf will likely not be present on your system. Depending on which platform you're on, you can download the source to your C library and find it. It probably won't be easy to find, though.

Narue Apr 19th, 2008 12:15 PM

Re: stdio.h what language is it
 
>suggest any source for some info regarding this
http://www.amazon.com/Standard-Libra.../dp/0131315099

If you'd like, I can also give you a sample implementation for Windows. It's greatly simplified just so that beginners will have an easier time than with vendor quality implementations.

tyros Apr 21st, 2008 10:12 AM

Re: stdio.h what language is it
 
thnks a lot ppl
and narue plz giv me tht windows impementation u were talking abt

Narue Apr 21st, 2008 11:15 AM

Re: stdio.h what language is it
 
1 Attachment(s)
Attached are the source files. Note that all of the standard names that I redefine have been prefixed with 'j' or 'J' to avoid conflicts with your compiler. Other parts of the standard library (such as malloc or the ctype functions) are used as-is with the implementation; I don't redefine them. Finally, I haven't had time to deck out the whole thing with detailed comments and documentation, so you'll have to suffer with the current minimal commenting.

If you don't understand the what, why, or how of any piece of code, feel free to start a thread on it. :)


All times are GMT -5. The time now is 3:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC