Jim's
Tutorials

Spring 2013
course
navigation

March 15 C

Read chapter 5. Switch and Cases, interesting....

Jim says

Here's the variation I showed in class, thinking about getchar, ascii, and ints.
#include <stdio.h> int main(){ int character; int number; printf("type some characters: "); character = getchar(); printf("The first one you typed was '%c' \n", character); if (character == 'a'){ printf("that was an 'a'\n"); } else { printf("that NOT was an 'a'\n"); } character = getchar(); number = character - '0'; printf("The second char you typed was '%c' which is ascii %i (or 0x%x). If that was a digit, the digit is %i. \n", character, character, character, number); return 0; }
Running it gives
$ gcc test.c -o test $ ./test type some characters: a3 The first one you typed was 'a' that was an 'a' The second char you typed was '3' which is ascii 51 (or 0x33). If that was a digit, the digit is 3.
http://cs.marlboro.edu/ courses/ spring2013/jims_tutorials/ cory/ March_15_C
last modified Friday March 15 2013 1:24 pm EDT

attachments [paper clip]

     name last modified size
[COD]grades.c Mar 15 2013 11:57 am 1.07kB