Thread: printf without include??

  1. #16
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by quzah
    #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>
    asun, prepare to do A LOT of copying and pasting ;P lol

  2. #17
    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.

  3. #18
    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.

  4. #19
    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.

  5. #20
    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

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is this code do??
    It declares printf, which is approximately what including <stdio.h> would do.
    My best code is written with the delete key.

  7. #22
    ---
    Join Date
    May 2004
    Posts
    1,379
    Approximately? As far as printf() is concerned, isnt that all it would do?

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >As far as printf() is concerned, isnt that all it would do?
    How the standard headers are implemented isn't specified.
    My best code is written with the delete key.

  9. #24
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by asun
    Code:
    int printf(const char *format, ... );
    what is this code do??
    what does the format do??

    thx dave_sinkula
    >What does this code do?
    It's called a prototype function. It lets int main know there is a function called int printf(const char *) somewhere. If you put the full printf function on top of the int main, then you don't need a prototype function. You could do this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void bird(void) {
    	puts("*whistle sound*");
    }
    
    int main(void) {
    	bird();
    	return 0;
    }
    OR
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void bird(void);
    
    int main(void) {
    	bird();
    	return 0;
    }
    
    void bird(void) {
    	puts("*whistle sound*");
    }
    But you CAN'T do this
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void) {
    	bird();
    	return 0;
    }
    
    void bird(void) {
    	puts("*whistle sound*");
    }
    Because you get this on compiling:
    Code:
    kleid@Shiva:~/Programming/Laboratory$ gcc prototype.c
    prototype.c:9: warning: type mismatch with previous implicit declaration
    prototype.c:5: warning: previous implicit declaration of `bird'
    prototype.c:9: warning: `bird' was previously implicitly declared to return `int'
    Because int main does not know where the void bird(void) function is.
    >What does the format do?
    The const char *format says that the function printf() has an argument that's a char array that won't be changed (hence the const). format is a string filled with %i's %d's %g's %f's %s's. The %whatever is a way of formatting your string output. That's why the char array is named format. You could even like this:
    Code:
    int printf(const char *JDFLkjserjsdflJER, ... );
    There's my 2 cents.

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