Thread: beginner's question about C

  1. #1
    Registered User
    Join Date
    Feb 2004

    beginner's question about C

    Hi, everyone. This is my first mail to the board. As the subject says, I'm a beginner in C programming. I got a copy of Kernighan-Ritchie's book and I'm starting to learn from it. I have no previous experience in C or Fortran or Pascal. That means you may have to bear with many silly questions from me very often.
    I'll go straight to the point. While trying one of the examples on the book (the one about converting temperatures), I decided to try this out: removing the #include <stdio.h> line while keeping the rest of the program intact. I thought that if I did that the program would not compile because the compiler would not find the library stdio.h where the printf function is defined (at least, that's what I understood). But the program got compiled anyway. That surprised me. Does any of you know why this happened?
    By the way, if you know of any book/manual/tutorial that might be useful to accompany Kernighan-Ritchie's book in order to learn aspects of C or general programming which are not covered there, I'd be glad to hear about them. I'd also be thankful, of course.

    Well, thanks in advance to you all...


  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Your understanding is correct. Printf is in the #include <stdio.h> area of your program. It should give you an error of an undefined function. Your compiler might have added it on by default or something. Stick with it C is a great language. Oh and welcome to the boards

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >This is my first mail to the board.
    Welcome. We'll do our best to make your visits as pleasant as possible, but don't forget to read the rules and "READ THIS FIRST" posts.

    >I got a copy of Kernighan-Ritchie's book
    Excellent start! You'll find that this book will answer most, if not all of your questions for some time. I still refer to it often enough to have multiple copies (one for home, one for work, one for everywhere else).

    >I decided to try this out: removing the #include <stdio.h> line while keeping the rest of the program intact.
    You've made a wonderful first impression for me.

    >Does any of you know why this happened?
    While it did compile (and usually will), did you get any warnings? You should get something along the lines of "warning: printf undefined, assuming extern returning int". This is a hint that something isn't quite right, even though most compilers will have no difficulty calling the right standard library function when you run the program. As for why it happened, just recognize that several sections of the C standard allow compilation to continue, but the behavior when the program is run will likely be undefined.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Feb 2004
    For gcc 3.2 I tried (where main.c calls printf without including stdio.h)

    gcc -ansi -pedantic main.c

    and got no warnings/errors.
    I needed to add -Wimplicit-function-declaration (or -Wall) before it would show a warning.

  5. #5
    Registered User
    Join Date
    Feb 2004

    Smile thanks, major blagger

    Thank you, major blagger. I tried compiling my code with the -Wimplicit-function-declaration option and I got a warning. The compiler performed compilation successfully, though. I had to use the option -Werror-implicit-function-declaration to get an error instead of a warning. This time I did not get any a.out bynary as a result of compilation.
    This solution triggered another doubt in me: how does the compiler manage to add the information from stdio.h without the programmer telling it to do that explicitly? I think the answer must lie in the gcc default configuration options, which I have not gone through. I'll try to see if I can figure this out.
    Thanks again,


  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    A good set of notes to accompany your K&R2 can be found here

    Just be sure you check both sets of notes - one set is more thorough than the other. Have a good look through all of his stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  2. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 05:53 PM
  3. beginner's question :D
    By kingliaho in forum C Programming
    Replies: 5
    Last Post: 10-17-2008, 05:20 PM
  4. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM