Thread: printf without include??

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    printf without include??

    i want to
    Code:
    printf("a");
    without using
    Code:
    #include<stdio.h>
    but i have no idea to start.
    i already asked my teacher how about making my own
    Code:
    #include "printf"
    but he said that i can't use any include at all
    can any body help me??
    thx for the reply

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He wants you to write your own console IO without using any standard headers? Good luck.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by quzah
    He wants you to write your own console IO without using any standard headers? Good luck.

    Quzah.
    Actually, it's not to hard.

    There's some code over at http://www.osdever.net about writing C functions without the RTL (Run-Time Library).

    Basically, all you do is access the memory for the video, and then put your text onto the screen.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you don't know how to start, you're probably mistunderstanding the assignment, because writing your own I/O library (no matter how simplistic) without any headers is an exercise in VERY low level programming. Try this:
    Code:
    int main(void)
    {
      printf("a");
    
      return 0;
    }
    It's wrong on so many levels to be scary, but it may happen to work. It sounds like your teacher can't recognize undefined behavior and wants to show you a "neat trick".
    My best code is written with the delete key.

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    That sounds difficult in my view, because I was first thinking:
    "Well that would be easy", but you can't use putchar() or even strlen()!? There must be magic in C Programming!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >"Well that would be easy", but you can't use putchar() or even strlen()!?
    Nope, you have to write them yourself if you want them. strlen is a no brainer, but putchar is surprisingly difficult to implement. Especially if you aren't allowed to include any headers.
    My best code is written with the delete key.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by asun
    i want to
    Code:
    printf("a");
    without using
    Code:
    #include<stdio.h>
    Code:
    int printf(const char *format, ... );
    
    int main(void)
    {
       printf("a");
       return 0;
    }
    ...Provided the proper library is linked?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    No includes? I'm curious as to what your teacher's actual answer is. :S

  9. #9
    Deleting... VOX's Avatar
    Join Date
    Oct 2004
    Location
    VA
    Posts
    94
    I don't know how you would do it but maybe you could use assembly? I know you can print stuff on the screen with that. And don't tell me it's off topic because you can put assembly into your C code.
    Boy you stink, go take a shower before you continue to code. Better do your laundry and spray your chair too.

    The one and only resource for finding information.

    Next version of windows released!!!!

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Wouldn't you at least need write()? I mean, the kernel doesn't actually let you send stuff to the console without passing it through it, does it?
    If you understand what you're doing, you're not learning anything.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Furthermore, as suggested in the first reply to mine, most kernels now don't simply let you grab ahold of the hardware. So I suspect that you fiddling with the video memory directly wouldn't be appreciated either. Could be wrong, but that's what I'm thinking. (Oh, and that link was dead when I went to look at it.)

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    vvv The Red Means Happy!
    Kleido-0, time to update your title thingy. Unless you're hoping for some negative rep
    If you understand what you're doing, you're not learning anything.

  13. #13
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I could see people scrolling down the screen and saying themselves: "Where's the red!?" lol.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And don't tell me it's off topic because you can put assembly into your C code.
    It's off-topic because you can't do it portably. First, embedding assembly code into C source is a common extension, but not blessed by the standard. Second, assembly itself is dreadfully nonportable. Now, if the question mentioned a compiler, operating system, and architecture then we could get into mentioning assembly. Of course, it would still be off-topic because it isn't C.

    >I was hoping you would do that :P
    I have done that, just not with the code you linked to. In a previous life I implemented a large portion of the C run-time and standard library to work with a very broken OS.
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Code:
    int printf(const char *format, ... );
    
    int main(void)
    {
       printf("a");
       return 0;
    }
    this code does work.
    but i don't understand about
    Code:
    int printf(const char *format, ... );
    what is this code do??
    what does the format do??

    thx dave_sinkula

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  4. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  5. Replies: 4
    Last Post: 04-01-2003, 12:49 AM