3 Include stdio.h By Logicmojo

Unlock the complete
Logicmojo experience for FREE

1 Million +

Strong Tech Community

500 +

Questions to Practice

50 +

Jobs Referrals

Logicmojo - Updated Jan 2, 2024



Introduction

In C, a header file is identified by the term stdio.h, In order to conduct input and output operations, we use stdio.h, a header file that imports many variables, macros, and functions. We must include the stdio.h header file in our C program in order to conduct input and output operations.

One of the most often used header files in C is stdio.h. We can use the input and output features of C thanks to this file. There are many various ways to collect and display data in C due to the more than 40 functions in stdio.h that are used for input and output operations.

what is stdio.h?

Include stdio.h

the stdio.h is a header file that contains the information needed to include input/output routines in our program. For instance, printf, scanf, and so on.

The stdio.h header file should be included in our source code if we intend to utilise the printf or scanf functions in our program.

Otherwise, the software will give an error or warning stating that there was an implicit declaration of the built-in function "printf," which it doesn't know what printf or scanf is.

Include stdio.h





why we use #include?

#include is a directory for preprocessors.

It will insert the file specified by the angle brackets "<>" into the current source file.

If you use #include<stdio.h> in your Program code, it will include the stdio.h file into your source code, which contains information for all input and output functions.

Important Points in Stdio.h

  1. The system will look for the file "stdio.h" and read its whole contents, replacing this statement.

  2. Clearly, the "stdio.h" file must include valid C source statements that may be compiled as part of a program.

  3. This file is made up of numerous standard #defines that define some of the most common I/O operations.

  4. The file is known as a header file, and there are several of them on the source discs that come with your C compiler.

  5. Each of the header files has a distinct role and can be used in any program.

  6. The system will look for the file "stdio.h" and read its whole contents, replacing this statement.

  7. Clearly, the "stdio.h" file must include valid C source statements that may be compiled as part of a programme.

  8. This file is made up of numerous standard #defines that define some of the most common I/O operations.

  9. The file is known as a header file, and there are several of them on the source discs that come with your C compiler.

  10. Each of the header files has a distinct role and can be used in any program.

  11. The header file stdio.h allows us to conduct input and output operations in C. Printf() and scanf() are functions that are used to display output and receive user input, respectively. This library enables us to quickly communicate with users.

  12. Streams are used by the stdio.h library to communicate with physical devices. A stream is a channel of communication between a software and its physical input and output devices. A standard input (stdin) stream, for example, is used to read data from a keyboard.

Library Variables Of stdio.h

The variable types specified in the header stdio.h are listed below :-



S. No.Variable NameVariable Description
1FILEIt is a data object that stores details about how to control streams.
2size_tIt is a form of unsigned integral data. It is the sizeof operator's output type.
3fpos_tIt is a type of data object that can symbolise any position in a file.


Library Macros Of stdio.h

A macro is the term given to a small piece of code. The following are some of the most frequently used macros in the stdio.h library :-



S. No.Macro NameMacro Description
1NULLThis macro represents a null reference constant.
2EOFThis macro is a negative integer, indicating that the file's end has been achieved.
3BUFSIZThis macro is an integer that indicates the buffer size used by the setbuf function.
4L_tmpnamThis macro is an integer that indicates the longest length of a char array that can contain the longest possible temporary filename generated by the tmpnam function.
5 _IOFBF, _IOLBF and _IONBFThese are the macros that expand to integral constant expressions with separate values that can be used as the third argument to the setvbuf function.
6FOPEN_MAXThis macro is an integer that indicates the maximum number of files that can be opened concurrently by the system.
7FILENAME_MAXThis macro is an integer that represents the length of a char array that can contain the longest possible filename. If the implementation does not set a limit, this should be the suggested max value.
8SEEK_CUR, SEEK_END, and SEEK_SETAll such macros are used in the fseek method to find specific locations in a file.
9TMP_MAXThe maximum amount of distinct filenames that the function tmpnam can produce is this macro.
10stderr, stdin, and stdoutIThese macros are FILE type pointers that match to the standard error, input, and output streams.


Library Functions in stdio.h

In C, there are several stdio.h library methods. These functions are used to carry out various input and output tasks. The stdio.h approaches are as follows:



S. No.Function NameFunction Description
1printf()This function outputs character, string, float, integer, octal, and hexadecimal numbers to the screen.
2scanf()This function reads a character, string, or numeric data from the keypad.
3getc()It gets a sentence from the keyboard.
4gets() It reads the character from the keyboard.
5getchar()It scans the character from the keyboard.
6puts()It prints a sentence to the o/p screen.
7putchar()It creates a figure for the screen.
8clearerr()This function removes all error signs.
9f open()The stdio.h header file defines all file handling functions.
10f close()closes a file that has been viewed
11getwa file receives an integer
12putw()writes a number to a file
13f getc()a character from a file is read
14putc()writes a letter in a file.
15f putc()writes a letter in a file.
16f gets()reads a string, one line at a moment, from a file.
17f puts()writes a string to a file.
18f eof()locates the end of the file
19f getchar()reads a keyboard character
20f getc()gets a character from a file
21f printf()data is displayed to the feed.
22f scanf()reads info from the stream as input.
23v printf()data from an argument array is output to stdout.
24v scanf()Reads input data and stores it in an argument array.
25s printf()The output is written to a string.
26s scanf()Data is read from a string input.
27sn printf()Writes output to a buffer of the specified capacity.
28vsn printf()Data from an input list is displayed in a buffer of the specified size.
29vs printf()Data from an argument array is displayed as a string.
30vs scanf()Reads info from a string and stores it in an argument list.
31tmpnam()This function creates a fresh temporary file.
32tmpfile()In binary update mode, opens a temporary file.
33rename()renames the specified file
34remove()Deletes the specified file
35setbuf()Specifies the cache of a stream.
36setvbuf()A stream's buffer should be updated.
37freopen()Restarts the stream to open a new file or an alternative access mode on an existing file.
38rewind()Sets a specified stream's file position to the beginning.
39clearerr()Removes the error signs for a specific stream.
40perror()The error notice is displayed.
41ferror()Examines the error indicators for a particular stream.


Example of stdio.h

This is a simple example to understand the header file stdio.h :-

#include<stdio.h>    
int main()
{ 
   // creating a integer
   int no;    
   // using printf() from stdio.h library
   printf("enter a no:");
   // using scanf() from stdio.h library
   scanf("%d",&no);    
   printf("cube of number is:%d ",no*no*no);    
   return 0;  
}
                        

Code Explaination

It uses #include<stdio.h> to include the standard input/output library stdio.h.

It uses the syntax "int no" to define an integer variable named Number. This variable will hold the number provided by the user.

It prints the statement "enter a no" to the terminal using the printf() function as from stdio.h library. It reads an integer value from the terminal using the scanf() function as from stdio.h library. The scanf() function reads input from the console and takes two arguments: the format specifier (%d for an integer in this instance) and a pointer to the variable where the input will be stored.

Conclusions

This concludes our discussion of "Include stdio.h", after going through this article you will get to know better how the stdio.h library imports variables, functions, and macros into our program, and their need in input and output of a program.

I sincerely hope that you learned something from it and that it improved your knowledge. You can visit logicmojo to learn more about other topic related to this field.

Good luck and happy learning!







Frequently Asked Questions (FAQs)



The standard input/output (I/O) library, which offers functions for reading input from the user and showing output to the console, is included in C++ programs using the "#include < stdio.h >" directive.

The header file'stdio.h' stands for "standard input/output header." For example, "printf()" and "scanf()" are just a couple of the input/output operations functions and constants that are declared and defined in this section.

Your C++ application can use the functions and capabilities offered by the standard input/output library if you include "#include < stdio.h >" in it. By reading keyboard input and showing output to the console, these features let you communicate with the user.

The 'printf()' function from the standard input/output library, for instance, can be used to show messages or formatted output to the console:

#include < stdio.h >

int main() {
    printf("Hello, World!\n");
    return 0;
}

The phrase "Hello, World!" is displayed on the terminal in this example using the 'printf()' function. The output is printed on a new line when the newline character "n" is inserted.

You can access the functions and features defined in the standard input/output library, which are necessary for input/output operations in C++ programs, by including the'stdio.h' header file.



The preprocessor command '#include' in the C programming language is used to include header files in a C program. Functions, constants, and types that are utilized in the program are declared and defined in header files.

The '#include' directive tells the C preprocessor to add the contents of the specified header file to your C program at the point where it is put.

For instance, you would use the '#include' directive as shown below to include the standard input/output (I/O) header file'stdio.h' in a C program:

#include < stdio.h >

int main() {
    // Code goes here
    return 0;
}

You can use functions like "printf()" and "scanf()" that are defined in the "stdio.h" header file by including "#include stdio.h>." The header file's inclusion enables the C compiler to detect and comprehend those functions' declarations and definitions during compilation.

The '#include' directive is crucial to C programming because it promotes modularity and code reuse. By incorporating the corresponding header files in your C programs, you can access external libraries, such as standard libraries or user-defined libraries.




The '#include stdio.h' directive is not employed in Java. C is the only programming language where it applies. The 'java.io' package in Java has features for input and output operations.

The '#include' command seen in C is not directly equivalent in Java. Java instead employs the 'import' command to include classes or packages.

For instance, you might use the 'import' statement as follows to include the 'System' class from the 'java.lang' package in a Java program:

import java.lang.System;

public class Main {
    public static void main(String[] args) {
        // Code goes here
    }
}

You can use the 'System' class in your Java application without explicitly specifying it each time by utilizing the 'import java.lang.System' line.

The standard library for Java contains numerous packages and classes with a wide range of functionality. To use these classes, you normally use the 'import' line to include the pertinent package or class.

The 'import java.io.*' declaration imports all of the 'java.io' package's classes and subpackages in this example, enabling you to access a variety of I/O-related classes including 'InputStream', 'OutputStream', 'FileReader', and 'FileWriter', among others.

It's important to note that Java comes with a number of pre-built default packages, such 'java.lang,' which are automatically imported without the need for an explicit 'import' statement.

In conclusion, Java does not support the '#include stdio.h' directive, but you can add Java classes or packages using the 'import' expression. The 'import' statement gives Java programs access to the classes and packages required for a number of features.




To include header files that offer extra functionality or definitions in a C++ application, use the '#include' directive in C++. Here are a few '#include' directives in C++ that are frequently used:

1. "<iostream>":

This header file offers functionality for input/output streams, including the common 'cin' and 'cout' input/output streams as well as input/output manipulation routines.

2. "<vector>":

You can use the vector container class, which offers dynamic arrays with dynamic size management, through the"<vector>" header file.

3. "<string>"

The'std::string' class, which enables working with strings in C++ and performing string manipulation operations, is provided by the "<string>" header file.

4. "<fstream>":

To input and output files, use the "<fstream>" header file. It offers classes like'std::ifstream' and'std::ofstream' for reading from and writing to files, respectively.

5. '<cmath>':

Mathematical functions and constants, including trigonometric functions, logarithmic functions, and constants like PI, are available in the '<cmath>' header file.

6. '<algorithm>' :

Numerous algorithms, including ones for sorting, searching, and modifying components in containers, are included in the header file titled <algorithm>'.

7.'<ctime>':

Functions and types for working with date and time, including functions for time measurement and manipulation, are available in the '<ctime> header file.

8. "<cstdlib>>":

Standard utility functions and types, such as "std::rand()" for generating random numbers and "std::exit()" for terminating programs, are included in the "<cstdlib>>" header file.

These are only a few instances of frequently used C++ "include" directives. The C++ standard library contains a large number of additional header files, each of which offers particular functionality and definitions. Depending on the needs of your software and the functionality you desire, you must decide which header files to include.




The C function 'getch()' reads a single character from the keyboard without displaying it on the screen. It is a custom function that is not included in the C standard library. 'getch()' can behave differently depending on the operating system and compiler used.

When a program has to take input without displaying it on the screen or when it needs to read individual characters, it usually uses the 'getch()' method, which is especially useful for terminal applications with menus.

'getch()' can be used in the following ways, as an illustration:

#include < stdio.h >
#include < conio.h >  // Required for getch() function in some compilers

int main() {
    char ch;
    printf("Enter a character: ");
    ch = getch();
    printf("\nYou entered: %c\n", ch);
    return 0;
}

In this illustration, when the user is prompted to enter a character, the software uses the function getch() to read the character from the keyboard without echoing it on the screen.

'printf()' is then used to display the entered character on the screen.

As 'getch()' is not a standard C function, its availability and operation may differ between compilers and platforms. This is a crucial distinction to make. Additionally, certain compilers might include related functions that carry out related operations, such "getchar()" or "getche()." When looking for cross-platform compatibility, it is always advised to refer to the documentation of your specific compiler or use portable alternatives.




To include header files in a program, use the '#include' directive in C and C++. It has a particular syntax that must be adhered to, The '#include' directive has the following general syntax:

The syntax elements are described in the following manner:

#include < header_file >
    or
#include "header_file"

'#include' 1

In C and C++, the preprocessor is instructed to include the designated header file in the program with a preprocessor directive.

2. "header_file," also known as "header_file,"

The header file's name is specified in this section. The header file name can be specified in one of two ways:

- "header_file"

Standard library header files use this form. Angle brackets ('>') are used to encapsulate the header file name. For instance, "stdio.h" or "iostream."

the "header_file"

For user-defined header files, use this form. Double quotation marks ('""') are used to denote the header file name. '"myheader.h"' or '"utils.hpp," for instance.

The preprocessor is told to include the contents of the specified header file at the spot where the '#include' directive is put using the '#include' directive.

As a result, the program can access the definitions and declarations found in the header file that is included.

For instance, you might use the following syntax to include the standard input/output header file'stdio.h' in a C program:

#include < stdio.h >

It's vital to remember that '#include' may have slightly varied syntax and usage depending on the programming language used. You should refer to the language's own syntax rules and practices when working with that language.




The 'void main()' function declaration in C is an unconventional and ineffective approach to specify the starting point of a C program.

In C, using 'int main()' is the proper and accepted method to declare the entry point.

The'main()' function is a unique function in the C language that acts as the program's first point of execution. According to the C language specification, it must have a return type of "int."

The'main()' function in C should be written using the following syntax:

int main() {
    // Code goes here
    return 0;
}

In this syntax,

• 'int' is the return type of the'main()' function, indicating that it should return an integer value.

• The function name, "main()," is always written in lowercase characters.

• Any command-line options that can be supplied to the program are enclosed in parentheses, "()."

The code for your program, which will be executed when the program runs, can be written in the "main()" function. The'return 0;' sentence that appears at the end of the'main()' function signifies that the program has successfully ended.

It's essential to remember that the C language standard states that using 'void main()' rather than 'int main()' is invalid. The ideal course of action is to adhere to the standard and use "int main()" for portability and compliance with the C language specification, even though some compilers may allow it as a non-standard extension or for compatibility with older code.



Yes, the header file'stdio.h' is used in the C programming language. Its definitions for types, macros, and functions for typical input and output operations are included in this acronym for Standard Input Output. This header file contains definitions for file handling functions like 'fopen()' as well as functions like 'printf()' and'scanf()'. If you wanted to use these functions in a C program, you would place the '#include' directive at the start of your source code, as in the following example:

#include < stdio.h >


The C and C++ programming languages, respectively, utilize the directives "#include stdio.h" and "#include iostream" to tell the preprocessor to include the contents of a particular header file into the current code.

"#include "stdio.h" permits the programmer to carry out input/output operations and is utilized in C programming. Standard Input Output Header, or "stdio.h," contains features for handling file and console I/O, including the "printf()" function for formatted output and the "scanf()" function for formatted input.

Here is an illustration of using'stdio.h' in C:

#include < stdio.h >

int main() {
    printf("Hello, world!\n");
    return 0;
}

In contrast, "#include iostream>" is employed in C++ programming, and it introduces objects for input/output operations such as "cin," "cout," "cerr," etc.

Input Output Stream is referred to as "iostream". The use of object-oriented paradigms for input and output operations is permitted by this header file.

Using 'iostream' in C++ is demonstrated in the following example:

#include < iostream >

int main() {
    std::cout << "Hello, world!\n";
    return 0;
}

In conclusion, the main distinctions between "#include stdio.h>" and "#include iostream>" are as follows:

1. Stdio.h is used in the C language, whereas iostream is used in the C++ language.

2. Methodology: For I/O operations, "stdio.h" offers a procedural methodology, whereas "iostream" offers an object-oriented methodology.

3. Use of Operators: Unlike'stdio.h' in C, 'iostream' in C++ permits the use of the extraction operator '>>' and insertion operator '' for I/O operations.

4. Safety: The I/O operations in "iostream" are type safe, thus unlike with "printf" and "scanf," you don't need to specify the data type of the variables for I/O operations. 'stdio.h' is an exception to this rule.





Preprocessor directives '#include' and '#define' both exist in C++, but they have different functions.

'#include'

Using this directive, the contents of a different file are added to the current file. It's frequently used to include pre-defined or user-defined libraries that have helpful code you want to reuse, such as function declarations and constants.

For instance, you would include the 'iostream' library at the head of your C++ program if you wanted to use the'std::cout' and'std::endl' objects for console output:

#include < iostream >

Use double quotations around the file name rather than angle brackets if the file you wish to include is in the same directory as your current file. For instance:

#include "myheaderfile.h"

'#define'

Using this directive, you can create a macro, which is a kind of name for a piece of code. This might be a constant, a macro that performs a function, or even a piece of code.

Here is a '#define' statement that defines a constant:

#define PI 3.14159

Here's an example of a "#define" statement that turns a macro into a function:

#define SQUARE(x) ((x) * (x))

In the first example, the preprocessor will substitute '3.14159' for any instances of 'PI' in the code before the code is compiled. In the second case, 'SQUARE(5)' would be changed to '((5) * (5))'.

As the 'const' keyword and inline functions are typically safer and more versatile, it is crucial to note that the use of '#define' for constants and inline functions is less popular in modern C++. But utilizing the '#ifdef', '#ifndef', and '#endif' directives, conditional compilation is still done using the '#define' command.

'#include' is used to include external files, and '#define' is used to define macros. In C++, these two commands have different functions.





In C programming, the Standard Input/Output library, often known as "stdio.h," is included using the "#include stdio.h>" directive. This library offers a variety of features for input/output tasks, and many C programs depend on its use. Here is a thorough breakdown of its applications:

1. Standard I/O Operations

Standard input/output operation routines are available in the'stdio.h' library. For instance, "printf()" and "scanf()" are used to produce a string or other data to the console, respectively.

2. File I/O operations

'stdio.h' contains functions for handling files in addition to normal I/O routines. These functions include 'fopen()' for opening files, 'fclose()' for closing them, 'fprintf()' and 'fscanf()' for file output and input, among others.

3. Error Handling

The'stdio.h' file also contains functions like 'perror()' and 'ferror()' for managing I/O-related errors.

4. Data Types and Macros,

In file and console I/O operations,'stdio.h' provides a number of data types including 'FILE' and 'fpos_t' as well as macros like 'EOF' (End Of File).





The output function in the C programming language is called "printf()." Its name, "print formatted," denotes that it has the ability to output formatted strings.

It is a component of the library'stdio.h'.

The format string that the 'printf()' function uses can contain format specifiers, escape sequences, and regular characters. Here is an illustration of how it can be applied:

#include < stdio.h >

int main() {
    int num = 100;
    printf("Hello, world! The number is %d\n", num);
    return 0;
}

For instance, "Hello, world! The format string reads, "The number is%dn." The various components of this format string have the following meanings:

• "Hello, universe! The quantity is ": This string of common characters will be output precisely as written.

• '%d': This format specifier is used. It suggests that an integer value ought to be reported in this location. After the format string, the argument list determines the value that will be produced in place of '%d'. 'num' is printed in this instance instead of '%d'.

• "n": This is an escape key combination. It denotes a newline character, which advances output to the following line.

The 'printf()' function is quite flexible and can deal with a wide variety of output. It recognizes a variety of format specifiers, such as '%d' (integer), '%f' (floating point number), '%c' (character), and '%s' (string).



Logicmojo Learning Library