Login
Login

C Input Output I/O In C

Spread the love

Last Updated on July 12, 2021 by Rahul

C Input Output I/O In C

‘Welcome in Study Learner

Today i will teach you about Input Output(I/O) in c

Here we will write C program to use scanf() function to take input from the user, printf() function to display output to the user.

Syntax:

#include<stdio.h> //include header file

int main() //main function

{ //body open

scanf() //scan value from the user

printf() // print or display value to the user

} //body close

  1. Intger Input/Output
#include<stdio.h>
int main()
{
	int testvalue;
	printf("Enter a value:");
	scanf("%d", &testvalue);
	printf("Entered value = %d", testvalue);
        return 0;
}

Output:-

Output

How will the program work? (C Input Output I/O In C)

(C Input Output I/O In C)

  1. include :- The #include is a preprocessor command that tells the compiler to include the contents of stdio.h(standred input and output) file in program.
  2. int main :- Here main() is the function name and int is return type of this function. Every C program must have this function because the executioin of program begins with the main() function.
  3. printf() :- printf() is a library function to send formatted output to the screen.
  4. scanf() :- scanf() is a another library function to take input from user.
  5. scanf(“%d”) :- %d is format specifier inside the scanf() function to take intger input from the user. When the user enters an integer, it is stored in the ‘testvalue’ variable.
  6. &testvalue :-  &testvalue gets the address of ‘testvalue’ variable  , and the value entered by the user is stored in that address.
  7. printf(“%d”) :- %d is format specifier inside the printf() which display the value stored in the ‘testvalue’ variable.
  8. return 0; :- return 0 means successful execution of main() function.

2. Float Input/Output:

#include<stdio.h>
int main()
{
	float testvalue;
	printf("Enter a float value:");
	scanf("%f", &testvalue);
	printf("Float value is %f", testvalue);
	return 0;
}

Output:-

Output

Here %f is format specifier for float

3. Double Input/Output:

#include <stdio.h>
int main()
{
    double testvalue;
    printf("Enter a value: ");
    scanf("%lf", &testvalue);
    printf("Double value is %lf", testvalue);
    return 0;
}

Output:-

Output

Here %f is format specifier for double

4. Char Input/Outpu:

#include<stdio.h>
int main()
{
	char ch;
	printf("Enter a character:");
	scanf("%c", &ch);
	printf("character is  %c", ch);
	return 0;
}

Output:-

Output

Here %c is format specifier for character

5. String Input/Output

#include<stdio.h>
int main()
{
	char name[20];
	printf("Enter a string:");
	scanf("%s", name);
	printf("String is %s", name);
        return 0;
}

Output:-

Output

Here %s is format specifier for string

(C Input Output I/O In C)


Spread the love

Leave a Comment


error: Content is protected !!