Basic Input and Output in C Language
Tags: programming, cpp, beginners
Published: 2022-06-12 08:59:40

Basic meaning behind the Input and output in programming is

- In Input, we feed data int to a program
- In Output, we feed data out from the program

The C programming language includes standard libraries that enable input and output in programs. The file pointers provide access to the file for reading and writing.

| Standard File | File Pointer | Device |
| --- | --- | --- |
| Standard input | stdin | Keyboard |
| Standard output | stdout | Screen |
| Standard error | stderr | Your screen |

The stdio.h or standard input output library in C contains input and output methods. The header file stdio.h stands for Standard Input Output. It has the information related to input/output functions.

| Function | Description |
| --- | --- |
| printf() | It is used to print the strings, integer, character etc on the output screen. |
| scanf() | It reads the character, string, integer etc from the keyboard. |
| getc() | It reads the character from the file. |
| putc() | It writes the character to the file. |
| fopen() | It opens the file and all file handling functions are defined in stdio.h header file. |
| fclose() | It closes the opened file. |
| remove() | It deletes the file. |
| fflush() | It flushes the file. |

## **putchar(), getchar() function in C**

The putchar() method in the C programming language is used to write a character to standard output/screen. The getchar() method retrieves/reads a character from keyboard input. Please see the explanation and syntax for the aforementioned file handling method below.

### putchar(0

**Declaration:** int putchar(int char)

putchar() function is used to write a character on standard output/screen. In a C program, we can use putchar function as below.

**putchar**(char);

where, char is a character variable/value.

### getchar()

**Declaration:** int getchar(void)

getchar() function is used to get/read a character from keyboard input. In a C program, we can use getchar function as below.

**getchar**(char);

where, char is a character variable/value.

```c
#include <stdio.h>
int main( ) {

   int c;

   printf( "Enter a value :");
   c = getchar( );

   printf( "\nYou entered: ");
   putchar( c );

   return 0;
}
```

According to above example, The int getchar(void) method returns the next available character from the screen as an integer. This function only reads one character at a time. If you wish to read more than one character from the screen, you may use this technique in the loop.

The int putchar(int c) method displays the character provided to it and returns the same character. This function only inserts one character at a time. If you wish to display more than one character on the screen, you may use this approach in the loop.

But when we are using more than 1 character in a program we need to use **The gets() and puts() Functions.**

```c
#include <stdio.h>
int main( ) {

   char str[100];

   printf( "Enter a value :");
   gets( str );

   printf( "\nYou entered: ");
   puts( str );

   return 0;
}
```

## **The scanf() and printf() Functions**

The scanf() method, in C, reads the value from the console as per the type specified. The scanf(const char *format,...) function receives input from the standard input stream stdin and scans it using the format specified.

The printf() method, in C, prints the value passed as the parameter to it, on the console screen. The printf(const char *format,...) function outputs to the standard output stream stdout and conforms to the format specified.

### **Using scanf() and printf() Functions with basic data types in C.**

- Integer

```c
Input: scanf("%d", &intVariable);
Output: printf("%d", intVariable);
```

Example:

```c
# include <stdio.h>

int main(void)
{
    int fahrenheit, celsius;

    printf("Please enter fahrenheit as a integer: ");
    
    scanf("%d", &fahrenheit);

    printf("You have entered: %d as the temperature in fahrenheit", fahrenheit);

    celsius = (fahrenheit - 32) /1.8 ;
    
    printf("\n %d fahrenheit is %d celsius \n", fahrenheit, celsius);
    
    return 0;
}
```

- Float

```
Input: scanf("%f", &floatVariable);
Output: printf("%f", floatVariable);
```

Example:

```c
#include <stdio.h>

int main()
{
    float speed;
    printf("\n********************************");
    printf("\nEnter Your Speed: ");
    scanf("%f", &speed);
    printf("You have entered: %f as the Speed", speed);

    if (speed <= 60)
    {
        printf("\nYour Speed is Normal");
    }
    else
    {
        printf("\nYour Speed is Too High");
    }

    return 0;
}
```

- Character

```
Input: scanf("%c", &charVariable);
Output: printf("%c", charVariable);
```

```c
#include <stdio.h>

int main()
{
    char ch;
    printf("\n\nEnter the Character: ");
    scanf("%c", &ch);
    printf("\nEntered character is: %c", ch);
}
```

- String

```
Input: scanf("%s", stringVariable);
Output: printf("%s", stringVariable);
```

Example:

```c
#include <stdio.h>

int main()
{

    char str[50];

    printf("Enter the Word: ");
    scanf("%s\n", str);

    printf("\nEntered Word is: %s", str);

    printf("\n\nEnter the Sentence: ");
    scanf("%[^\n]\ns", str);

    printf("\nEntered Sentence is: %s", str);

    return 0;
}
```

What is `%[^\n]\ns`

- `[^\n]` when we include this in `scanf` we will input the string until it encounter a new line in input.
- `%[^\n]` scans everything until a `\n`, but doesn't scan in the `\n`.
- `^` is caret operator. It helps us to ignore a particular character.
- whenever we write `^\n` in `scanf` function, we will be able to write string with spaces and also able to store in a particular variable(here, `str` variable),until `\n` (here, `\n` is Enter key) is encountered.