Reading and writing a character

Reading and writing a character

Published by: Nuru

Published date: 21 Jun 2021

Reading and writing a character Photo

Reading and writing a character

The scanf() and printf() functions are mostly used for reading and writing a character:

char c= ‘$’;               /*declaration of char variable c */
printf(“%c”,c);         /*Prints character $ */
scanf(“%c”,&c);       for input a character in c
printf(“%c”,c);         for output a character in c

getc() and putc() character I/O Function

getc() and putc() are also used for reading and writing a character.

Function getc(): It reads a single character from the input and returns an integer value. If it fails, it returns EOF. The syntax of getc() in C language is,

int getc(FILE *stream);

Function putc(): It prints the passed character in screen
Example of getc() in C language

#include
int main ()
{
char ch;
printf("Enter the character: ");
ch = getc(stdin);
printf("Character entered: ");
putc(ch, stdout);
return(0);
}

The getch() Function

getch(): The function getch() is a non-standard function. It is declared in “conio.h” header file.

  • Mostly it is used by Turbo C. It is not a part of the C standard library.
  • It immediately returns the entered character without even waiting for the enter key.
  • Here is an example of getch() in C language:

#include
#include
int main()
{
char c;
printf("Enter the character : ");
c= getch();
printf("Entered character : %c", c);
return 0;
}

The getche() Function

getche(): Like getch(), the getche() function is also a non-standard function and declared in “conio.h” header file.

  • It reads a single character from the keyboard and returns it immediately without even waiting for enter key.
  • Below is an example of getche() in C language.
    #include
    #include
    int main()
    {
    char val;
    printf("Enter the character : ");
    val = getche();
    printf(“ Entered character : %c", val);
    return 0;
    }

Input data with scanf()

  • Most programs need to input data from the keyboard.
  • The most flexible way our program can read numeric data from the keyboard is by using the scanf() library function.
  • The scanf() function reads data from the keyboard according to a specified format and assigns the input data to one or more program variables.
  • Like printf(), scanf() uses the same format string to describe the format of the input as
  • For example, the statement scanf("%d", &x); reads a decimal integer from the keyboard and assigns it to the integer variable x.
  • Likewise, the following statement reads a floating-point value from the keyboard and assigns it to the variable-rate: scanf("%f", &rate);

scanf()

  • The & symbol is C's address-of operator, which specifies the address of the variable stated
  • The scanf() requires the & symbol before each numeric variable name in its argument list
  •  A single scanf() can input more than one value if we include multiple conversion specifiers in the format string and variable names (again, each preceded by & in the argument list).
  • The following statement inputs an integer value and a floating-point value and assigns them to the variables x and rate, respectively: scanf("%d %f", &x, &rate);
  • When multiple variables are entered, scanf() uses white space to separate the input into fields. White space can be spaces, tabs, or new lines.
  • Each conversion specifier in the scanf() format string is matched with an input field and the end of each input field is identified by white space.
  • This gives us considerable flexibility. In the preceding scanf(), we could enter    10 12.45
  • Or this: 10
    12.45

An example of printf()/scanf()

#include
Int main()
{
float y;
int x;
puts( "Enter a float, then an int" );
scanf( "%f %d", &y, &x);
printf( "\nYou entered %f and %d ", y, x );
return 0;
}

String Input with gets()

gets(): The gets() function reads a line from stdin(standard input) into the buffer pointed to by string pointer until either a terminating newline or EOF (end of file) occurs.

e.g.

#include
int main()
{
/* character array of length 100 */
char str[100]; /* Will discussed later in chapter array in detail */
printf("Enter a string : ");
gets( str );
printf(“You Entered String: ”);
puts( str );
return 0;
}

Input strings with scanf()

We can use scanf() function to read text string from standard input. The syntax of scanf() for reading string literal is as below:
scanf(“%s”, string_variable);
Example:
#include
int main()
{
/* character array of length 100 */
char str[100];    /* Will discussed later in chapter array in detail */
printf("Enter a string : ");
scanf("%s",str) ;
printf("You Entered String: %s",str);
return 0;
}
Note: Unlike gets(), scanf () reads string until any white space
character is encountered

  • In previous example if we give the input with white space, it takes input only up to that white space character , rest is ignored.
  • To read string with white space up to one line, scanf() function can be written as:
    scanf("%[^\n]", string_variable) ;
    Example:
    #include
    int main()
    {
    /* character array of length 100 */
    char str[100]; /* Will discussed later in chapter array in detail */
    printf("Enter a string : ");
    scanf("%[^\n]",str) ;
    printf("You Entered String: %s",str);
    return 0;
    }