// A Demo file showing some of the things
// we can do with character variables

// Marc Chee (cs1511@cse.unsw.edu.au) June 2020

#include <stdio.h>

int main(void) {
    // we're using an int to represent a single character
    int character;
    // we can assign a character value using single quotes
    character = 'a';
    // This int representing a character can be used as either
    // a character or a number
    printf("The letter %c has the ASCII value %d.\n", character, character);

    // using getchar() to read a single character from input
    int inputChar;
    printf("Please enter a character: ");
    inputChar = getchar();
    printf("The input %c has the ASCII value %d.\n", inputChar, inputChar);
        
    // using putchar() to write a single character to output
    putchar(inputChar);
    printf("\n");
    
    
    // reading multiple characters in a loop
    printf("Type in multiple characters, ending with Ctrl-D to stop.\n");
    int readChar;
    readChar = getchar();
    while (readChar != EOF) {
        printf(
            "I read character: %c, with ASCII code: %d.\n",
            readChar, readChar
        );
        readChar = getchar();
    }


}

Resource created Tuesday 01 September 2020, 09:30:28 PM.

file: charDemo.c


Back to top

COMP1511 20T3 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G