Thread: printf without include??

  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
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Ok maybe not as bad as I thought lol
    Code:
    int mystrlen(char *sz) {
    	int c, count=0;
    	while((c=sz[count++]) != '\0');
    	return --count;
    }
    Ok the putchar... I think I found something, check it out http://www.uclibc.org/cgi-bin/cvsweb....1&view=markup

    For those who don't know, try googling:
    filetype:c putchar

    That's what I used to find the page above just in case you all wanted to know.
    Last edited by Kleid-0; 01-08-2005 at 09:00 PM.

  9. #9
    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

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Ok maybe not as bad as I thought lol
    Yes, most of the string functions are trivial.

    >Ok the putchar... I think I found something
    Have you walked all of the way through the macro maze to see what's really happening?
    My best code is written with the delete key.

  11. #11
    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!!!!

  12. #12
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Prelude
    Have you walked all of the way through the macro maze to see what's really happening? :)
    I was hoping you would do that :P . Prelude, the macromaze master! lol

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Kleid-0
    Ok maybe not as bad as I thought lol
    ...snip...
    Ok the putchar... I think I found something, check it out http://www.uclibc.org/cgi-bin/cvsweb....1&view=markup

    For those who don't know, try googling:
    filetype:c putchar

    That's what I used to find the page above just in case you all wanted to know.
    The linked code wouldn't be allowed by the OP's definition. They include their own header file, "_stdio.h", which includes who knows what. Oh, wait, here it is:
    Code:
    /* Copyright (C) 2004       Manuel Novoa III    <[email protected]>
     *
     * GNU Library General Public License (LGPL) version 2 or later.
     *
     * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
     */
    
    #define _GNU_SOURCE
    
    #include <assert.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <signal.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    #ifdef __UCLIBC_HAS_WC
    ...snip...
    So yeah, that's not going to cut it.

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

  14. #14
    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.

  15. #15
    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.

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