[C] Character Input/Output

  • getchar : Read character
#include <stdio.h>

int main(void)
{
    char btype;
    puts("----------------------");
    puts(" ** Character ** ");
    puts("----------------------");
    
    printf(" Enter Blood type : ");
    scanf("%c", &btype);
    
//    btype = getchar();
    
    printf(" Your blood type is %c \n", btype);
    puts("----------------------");
    puts(" scanf, getchar : two way for input character");
    puts("----------------------");
    
    return 0;

    /*
    ----------------------
    ** Character ** 
    ----------------------
    Enter Blood type : a
    Your blood type is a 
    ----------------------
    scanf, getchar : two way for input character
    ----------------------
    */
}
#include <stdio.h>

int main(void)
{
    char a, b;
    puts("----------------------");
    puts(" ** Two character ** ");
    puts("----------------------");
    
    printf(" Enter two character : ");
    scanf("%c%c", &a, &b);
    
    printf(" a = %c, b = %c \n", a, b);
    puts("----------------------");
    puts(" There is problem if you click enter key after first character");
    puts("----------------------");
    
    return 0;

    /*
    ----------------------
    ** Two character ** 
    ----------------------
    Enter two character : AB 
    a = A, b = B 
    ----------------------
    There is problem if you click enter key after first character
    ----------------------
    */
}
#include <stdio.h>

// scanf
int main(void)
{
    char a, b;
    puts("----------------------");
    puts(" ** Two character ** ");
    puts("----------------------");
    
    printf(" Enter First character : ");
    scanf("%c", &a);
    
    printf(" Enter Second character : ");
    while(getchar() != '\n');
    scanf("%c", &b);
    
    printf(" a = %c, b = %c \n", a, b);
    puts("----------------------");
    
    return 0;

    /*
    ----------------------
    ** Two character **
    ----------------------
    Enter First character : A
    Enter Second character : B
    a = A, b = B 
    ----------------------
    */
}
#include <stdio.h>

// getchar()
int main(void)
{
    char a, b;
    puts("----------------------");
    puts(" ** Two character ** ");
    puts("----------------------");
    
    printf(" Enter First character : ");
    a = getchar();
    
    printf(" Enter Second character : ");
    while(getchar() != '\n');
    b = getchar();
    
    printf(" a = %c, b = %c \n", a, b);
    puts("----------------------");
    
    return 0;

    /*
    ----------------------
    ** Two character **
    ----------------------
    Enter First character : A
    Enter Second character : B
    a = A, b = B 
    ----------------------
    */
}

Reference

Tags:

Categories:

Updated: