Thread: code help

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    34

    Exclamation code help

    can any one tell how to write coding for a printf statement
    without using printf command?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You mean you want to write your own version of printf()?

    Well printf() wraps the functionality of the operating system that you have. So for Unix, printf() probably ends up calling write() or another function that ends up calling write(). For Windows, it's either WriteConsole() or WriteFile() (I think WriteConsole() calls WriteFile(), but I can't back that up at the moment).

    All in all, it depends upon the O/S you're using.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    34

    Unhappy

    can you be a littlie bit more specific
    couldnt understand what u told

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A Variable argument list perhaps?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    shuaib.akram: I think YOU need to be more specific about what you actually need to do. If you want to implement your own version of printf, that's possible to do - to make a reasonable attempt will be some 50-100 lines of fairly complex C-code [this is not a full implementation, that's more like 200-500 lines of even more complex code].

    But either one is certainly not a beginners task. The first step is to handle variable arguments to a function, which means include <stdarg.h> to your function - play with that and make that work for something simple that uses for example printf to print the 2 or more variables you've supplied to the function, making the first argument being the number of arguments.

    Once you have the idea of that, post back.

    If this isn't actually what you are intending to do, then please rephrase your question to explain better what you want to do.

    --
    Mats

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    34
    sorry for the unclear question

    we can write code for all the inbuilt functions with out using that inbuilt function
    for example
    strlen(stringname) gives length of the string
    but without using that function we can write the code to find length
    Code:
    for(i=0;i<10;i++);
    here i gives the length of string

    i wanted to know if we can write the same type of code for printf
    hope this is clear!!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Yes you can.

    Some text books show a primitive example.

    Some manual pages for stdarg.h / va_start() also show examples.

    Most of the standard C library is written in C itself, so all you really need to do is find the source code for glibc and you're done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM