









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










CSSE 120—Rose Hulman Institute of Technology
char myChar; myChar = 'C'; printf("%c\n", myChar); /* %c is format spec. for char */ printf("%d\n", myChar); /* can print char as a decimal */ printf("%c\n", 67); /* can print int as char */ myChar++; printf("%c\n", myChar); /* What prose do you suppose? */
char c = 'A'; int i = 'A'; printf("A"); printf("%c", 'A'); printf("%c", 'B'-1); printf("%c", c); printf("%c", i); putchar('A'); /* can "push" single characters to output */ putchar('C' - 2); putchar(toupper('a')); /* Need to #include <ctype.h> */ putchar(c); putchar(i);
getchar() Caveat: getchar() returns an int , either a char value or EOF (end of file) void getSomeChars() { int inChar; int count = 0; printf("\n\nType some text, then press 'Enter': "); fflush(stdout); inChar = getchar(); while (inChar != '\n' && inChar != EOF) { count++; inChar = getchar(); } printf("\nYou entered %d characters.", count); }
Note: most operating systems only pass characters to your program after the user presses the enter key
int tolower(int c); int toupper(int c);
print each character entered print the upper-case version of each character
isdigit(c) isalpha(c) islower(c) isupper(c) isspace(c)
See the C Library Reference link on ANGEL under Course Resources for more functions.
Signature: int getline(char s[], int lim); Spec: Gets a line of characters from the user and puts up to lim characters of it in the given array. Returns the length of the string.
char lname[MAX_NAME]; int len; len = getline(lname, MAX_NAME); printf("\n\n'%s', has %d characters.\n", lname, len);
Function Purpose char *strcpy(char s, char ct) copy string ct to string s, including '\0'; return s char *strcat(char s, char ct) concatenate string ct to end of string s; return s int strcmp(char *cs, char *ct) compare string cs to string ct, return a negative number if cs<ct, zero if cs==ct, or positive otherwise char *strstr(char *cs, char *ct) return a pointer to first occurrence of ct in cs, or NULL if not present size_t strlen(char *cs) return length of cs (size_t is an typedef for int on most systems)
Descriptions from K&R, p. 249. See the C Library Reference link on ANGEL for more.
char lname[10]; or *char fname = "Lou";
Key Points!
int x; // an element int xA[4]; or *int xA; // an array of ints
char c; //an element char str[4]; or *char str; // a string, i.e., an array of chars
*char str; // an element *char strA[4] or **char strA; // an array of strings