The stdio.h header file in C is a standard input-output library that provides functions for input and output operations. It stands for “standard input-output header”. This header file declares several functions, including printf
, scanf
, fopen
, fclose
, fread
, fwrite
, etc., as well as various macros and data types.
std
means Standard andio
means Input-output statements- These statements are used to get and put data on computers
- Ex-1: Can put data to a computer via the keyboard.
- Ex-2: Can get data and see that in Monitor.
- Input-output statements are used as a function in c language
stdio.h
file contains that statements statements- So if we use these functions should include
stdio.h
, functions arescanf()
,printf()
,getchar()
,gets()
,putchar()
,puts()
.
Here’s a brief overview of some commonly used functions and macros declared in stdio.h
:
printf
: Used to print formatted output to the standard output (usually the console).scanf
: Used to read formatted input from the standard input (usually the keyboard).fprintf
,fscanf
,sprintf
,sscanf
: Variants ofprintf
andscanf
for performing formatted I/O operations on files or strings.FILE
: Data type representing a file stream used by functions likefopen
,fclose
,fread
,fwrite
, etc.stdin
,stdout
,stderr
: Predefined file streams representing standard input, standard output, and standard error respectively.
Including stdio.h
at the beginning of your C program allows you to use these functions and macros. It’s an essential header file for most C programs, as it provides basic input and output capabilities.
Contents
- 1 scanf()
- 2 printf()
- 2.1 Examples-1: How to print numbers, strings and character
- 2.2 Examples-2: print the \n using printf()
- 2.3 Example-3: print any value without using the semicolon
- 2.4 Examples-4: Different width and Different Type
- 2.5 Examples-5: printf() return type understanding
- 2.6 Example-6: Printf() and Macro
- 2.7 Example 7: Print the char pointer string
- 3 sprintf()
- 4 stdio.h Library Other Functions Syntax
- 5 NEXT
scanf()
scanf() | Description |
---|---|
syntax | int scanf(char *control_string, argument_list); scanf("control strings", &variable_1, &variable_2….&variable_n); control sting : %w -(% – conversion specification indicator, W -width of input data)& – address of the variable |
Rules | Should use comma , to separate the different variablesSame data type variable and data specifier should use. |
Returns | scanf() returns the number of data items successfully assigned a value. if an error occurs, EOF is return. |
Examples | scanf("%d", &a); scanf("%f", &a); scanf("%d %f %d", &a, &b, &c); scanf("%c", &sex); scanf("%s%c", &name, &sex); scanf("%4d", &a); scanf("%5f", &a); scanf("%20s", &name); |
printf()
- syntax :
int printf(char *control strings, argument_list);
- Data format :
%w.p
data-type %
-conversion specification indicatorw
– width of the output data (optional)p
– point (dot)- Returns:
printf()
will return the number of characters printed count values (integer values).
Examples-1: How to print numbers, strings and character
Example | Output |
---|---|
printf("Character is %c \n", "A"); | Character is A |
printf("String is %s \n" , "www.ArunEworld.com"); | String is www.ArunEworld.com |
printf("Float value is %f \n", 10.234); | Float value is 10.234000 |
printf("Integer value is %d\n" , 150); | Integer value is 150 |
printf("Double value is %lf \n", 20.123456); | Double value is 20.123456 |
printf("Octal value is %o \n", 150); | Octal value is 226 |
printf("Hexadecimal value is %x \n", 150); | Hexadecimal value is 96 |
Examples-2: print the \n using printf()
Code
#include <stdio.h>
int main()
{
printf("\\n");
return printf("\n www.ArunEworld.com");
}
Output
\n
www.ArunEworld.com
--------------------------------
Process exited after 1.403 seconds with return value 20
Press any key to continue . . .
Example-3: print any value without using the semicolon
Here below four example provided for print the value without semicolon. and going to print the ArunEworld string for all examples.
Method-1: Using if loop
Code:
#include <stdio.h>
void main()
{
if(printf("ArunEworld"))
{
}
}
Output: ArunEworld
Method-2: Using while loop
Code:
#include <stdio.h>
void main()
{
while(!printf("ArunEworld"));
}
Output: ArunEworld
Explanation:
- If you observe the while loop inside, I was mentioned
!printf("ArunEworld")
. Do you know what is the reason? - The
print()
always returns the number of characters printed on the console. - In the above code case, we are printing the ArunEworld string and the string size is 10 characters.
- When the first time executes the program in the while loop,
print()
will return 10 decimal value. so the program will continuously run like an infinite loop. - In order to avoid infinite times print the string, here mentioned
!
symbol so whatever positive value getting fromprint()
, it will be considered as the negative value of while loop after first execution and it will stop the next execution.
Method-3: Using switch case
Code:
#include <stdio.h>
void main()
{
switch(printf("ArunEworld"))
{
}
}
Output:
ArunEworld
--------------------------------
Process exited after 0.7511 seconds with return value 10
Press any key to continue . . .
Examples-4: Different width and Different Type
Print the decimal value with different width (Ex-1)
Code
#include <stdio.h> int main() { int x=8; printf("%d\n\r",x); printf("%3d\n\r",x); printf("%5d\n\r",x); printf("%-5d\n\r",x); printf("%07d\n\r",x); printf("%4d\n\r",-x); return printf("www.ArunEworld.com"); }
Output
8
8
8
8
0000008
-8
www.ArunEworld.com
--------------------------------
Process exited after 0.3254 seconds with return value 18
Press any key to continue . . .
Print the different type with different width (Ex-2)
Code:
#include <stdio.h>
int main(void)
{
printf("Strings:\n");
const char* s = "Hello";
printf("printnig S is : % ");
printf(s);
printf("\n");
printf("\t.%10s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s);
printf("Characters:\t%c %%\n", 65);
printf("Integers\n");
printf("Decimal:\t%i %d %.6i %i %.0i %+i %u\n", 1, 2, 3, 0, 0, 4, -1);
printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6);
printf("Octal:\t%o %#o %#o\n", 10, 10, 4);
printf("Floating point\n");
printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3);
printf("Padding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5);
printf("Scientific:\t%E %e\n", 1.5, 1.5);
printf("Hexadecimal:\t%a %A\n", 1.5, 1.5);
}
Output:
Strings:
printnig S is : Hello
. Hello.
.Hello .
. Hello.
Characters: A %
Integers
Decimal: 1 2 000003 0 +4 4294967295
Hexadecimal: 5 a A 0x6
Octal: 12 012 04
Floating point
Rounding: 1.500000 2 1.30000000000000000000000000000000
Padding: 01.50 1.50 1.50
Scientific: 1.500000E+000 1.500000e+000
Hexadecimal: 0x1.800000p+0 0X1.800000P+0
--------------------------------
Process exited after 2.748 seconds with return value 41
Press any key to continue . . .
Print the different type with different width (Ex-3)
Code:
#include <stdio.h>
int main()
{
printf ("Characters : %c %c \n", 'a', 65);
printf ("Decimals : %d %ld\n", 1977, 650000L);
printf ("Preceding with blanks : %10d \n", 1977);
printf ("Preceding with zeros : %010d \n", 1977);
printf ("Some different radices : %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
printf ("floats : %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
printf ("Width trick : %*d \n", 5, 10);
printf ("%s \n", "A string");
return 0;
}
Output:
Characters : a A
Decimals : 1977 650000
Preceding with blanks : 1977
Preceding with zeros : 0000001977
Some different radices : 100 64 144 0x64 0144
floats : 3.14 +3e+000 3.141600E+000
Width trick : 10
A string
--------------------------------
Process exited after 2.739 seconds with return value 0
Press any key to continue . . .
Examples-5: printf() return type understanding
Code:
#include<stdio.h>
int main()
{
printf("%x", -1<<1);
getchar();
return 0;
}
Output: Dependent on the compiler. For 32 bit compiler it would be fffffffe and for 16 bit it would be fffe.
Example-6: Printf() and Macro
# include <stdio.h>
# define display_in_macro "%s ArunEworld "
main()
{
printf(display_in_macro, display_in_macro);
getchar();
return 0;
}
Output : %s ArunEworld ArunEworld
Explanation: After a pre-processing phase of compilation, the printf
statement will become "%s ArunEworld "
in macro place.
Example 7: Print the char pointer string
#include <stdio.h>
int main(void)
{
char *p = "ArunEworld";
printf(p);
return 0;
}
Output: ArunEworld
Compiler [Warning]: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
sprintf()
- Syntax:
sprintf()
- Description: Prints the formatted output onto the character array.
Example
int main(void) { char buf[30]; int x = 1, y = 3; int z = x * y; sprintf(buf, "Mul : %d * %d = %d", x, y, z); // into buffer instead of printing on stdout printf("%s", buf); return 0; } /* Output: Mul : 1 * 3 = 3 */
stdio.h Library Other Functions Syntax
Functions | Syntax | Descriptions |
---|---|---|
fputc() | fputc(character, file_pointer); | |
getchar() | var = getchar(); | |
putchar() | putchar(var); | |
puts() | puts(var) | Example: puts(“ArunEworld “) |