C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Character Input Output Operations

C Programming

In this tutorial we will learn to handle character input output operations in C programming language.

So far we have learned about data types, tokens and variables in C programming language. Now lets talk about how to take user input in our program and then return some output.

Character Input

To read a single character as input we use the getchar() function.

In the following example we will take a character as input from the user and will print the ASCII code of the character.

#include <stdio.h>
int main(void)
{
  char ch;
  printf("Enter any character: ");
  ch = getchar();
  printf("Entered character: %c\n", ch);
  printf("ASCII value: %d\n", ch);
  return 0;
}

Output

Enter any character: A
Entered character: A
ASCII value: 65

Character Output

To output a single character we use the putchar() function.

In the following example we take a single character as input and then output it using putchar() function.

#include <stdio.h>
int main(void)
{
  char ch;
  printf("Enter any character: ");
  ch = getchar();
  printf("Entered character: ");
  putchar(ch);
  return 0;
}

Output

Enter any character: A
Entered character: A

Testing characters

We can use the ctype.h header file from the C library to perform tests on characters.

Following are some of the functions from the ctype.h file that we can use to test characters.

All the given functions will return non-zero (true) value if the condition is satisfied by the passed argument c. If the condition fails then we will get zero (false).

FunctionDescription
isalnum(c)Is c an alphanumeric character?
isalpha(c)Is c an alphabetic character?
isdigit(c)Is c a digit?
islower(c)Is c lower case letter?
isprint(c)Is c a printable character?
ispunct(c)Is c a punctuation mark?
isspace(c)Is c a white space character?
isupper(c)Is c upper case letter?

Write a program in C to get a character input from the user and then check if it is a digit

We will first take the character as input using the getchar() function then we will use the isdigit() function to check if the entered character is a digit. If the function returns non-zero value then the character is a digit else it is not.

#include <stdio.h>
#include <ctype.h>
int main(void)
{
  char ch;
  printf("Enter character: ");
  ch = getchar();
  if (isdigit(ch)) {
    printf("Entered character is a digit.");
  }
  else {
    printf("Entered character is not digit.");
  }
  return 0;
}

Output

Enter character: 6
Entered character is a digit.

In the above code we are using if-else conditional statement. So, if isdigit(ch) returns non-zero value then the code inside the if-block printf("Entered character is a digit."); will be executed. Otherwise the else-block printf("Entered character is not digit."); will be executed.

We will learn more about if-else statement in the later tutorial.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT