Thread: What is #include and why we use?

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    4

    Post What is #include and why we use?

    What is #include <stdio.h> in a C program and why we use this in the program? How many "#include" we use in a C program. Can anyone explain me?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What is #include <stdio.h> in a C program and why we use this in the program?
    Including a file simply copies its contents into that position in the containing file.

    Header (.h) files generally contain function prototypes; structs, unions, enums, and typedefs; and macros. These declare identifiers that you can then use in a particular source file without declaring them directly in that source file.

    For example, when you use printf, you haven't defined that function yourself, so where does it come from? It is declared in stdio.h, and it's actual code comes from the standard library, which is automatically linked with your program when you compile.

    How many "#include" we use in a C program?
    You can use as many as you want. The current C standard library defines 29 header files. Other libraries come with their own header files.

    Can anyone explain me?
    This is a deep psychological/philosophical question far beyond the scope of this forum.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What is #include <stdio.h> in a C program
    Include directive - Wikipedia

    > and why we use this in the program?
    It saves having to manually type
    extern int printf(const char *format, ... );
    every time you want to print anything.


    > How many "#include" we use in a C program
    As many as you like.
    Though you should restrict yourself to the minimum required to successfully compile your program. Unnecessary inclusion of header files will make compile times longer.


    > Can anyone explain me?
    Ask your parents.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    If you're learning programming, sometimes it's best to treat parts of the program as 'boilerplate' and concentrate on other things. For example, one possible 'Hello, world' program for C is

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	printf("Hello, world!\n");
    	return 0;
    }
    Of the 6 non-blank lines, the only one that you really have to understand is the printf line. Treat the rest as boilerplate and then heed your compiler warnings. For example, if you forgot the #include line, you will get a warning about printf. If you forgot which header file you need for printf, look it up.

  6. #6
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    Thanks algorism,

    It is damn simple. Now, i understood what it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does #include indirectly include the source file too?
    By Lord Asriel in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 08:20 AM
  2. Replies: 1
    Last Post: 11-06-2011, 06:20 PM
  3. basic difference between #include<> and #include""
    By gunjansethi in forum C Programming
    Replies: 1
    Last Post: 03-26-2010, 12:53 AM
  4. #include <windows.h> and #include <wininet.h>
    By steve1_rm in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2009, 11:14 AM
  5. Which came first? #include <stdio.h> or #include <stdlib.h> ?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-14-2002, 10:58 PM

Tags for this Thread