Print "Andrew Rocks!" - using character constants to get the ASCII codes for the characters
'A' is the C shorthand for the ASCII code for the character A (65)
Print "Andrew Rocks!" - using character constants to get the ASCII codes for the characters,
store them in array, and print the array. Note we have to track the array length.
#include <stdio.h>
#define LENGTH 14
int main(void) {
int asciiCodes[14];
asciiCodes[0] = 'A';
asciiCodes[1] = 'n';
asciiCodes[2] = 'd';
asciiCodes[3] = 'r';
asciiCodes[4] = 'e';
asciiCodes[5] = 'w';
asciiCodes[6] = ' ';
asciiCodes[7] = 'R';
asciiCodes[8] = 'o';
asciiCodes[9] = 'c';
asciiCodes[10] = 'k';
asciiCodes[11] = 's';
asciiCodes[12] = '!';
asciiCodes[13] = '\n';
int i = 0;
while (i < LENGTH) {
putchar(asciiCodes[i]);
i = i + 1;
}
return 0;
}
Print "Andrew Rocks!" - using character constants to get the ASCII codes for the characters,
initialize an array to the values , and print the array.
Note we have to track the array length.
#include <stdio.h>
#define LENGTH 14
int main(void) {
int asciiCodes[LENGTH] = {'A','n','d','r','e','w',' ','R','o','c','k','s','!','\n'};
int i = 0;
while (i < LENGTH) {
putchar(asciiCodes[i]);
i = i + 1;
}
return 0;
}
Print "Andrew Rocks!" - using character constants to get the ASCII codes for the characters,
and initialize the array to the vales array using , and print the array.
This version has a extra special value in the array (0) to indicate the end of
the sequence. This means we no longer have to keep track of how
many characters in the array (note the while loop condition)
#include <stdio.h>
int main(void) {
// if we don't specify the size of an array being initialized C will make
// it big enough to hold all the initializing elements (15 in this case)
int asciiCodes[] = {'A','n','d','r','e','w',' ','R','o','c','k','s','!','\n',0};
int i = 0;
while (asciiCodes[i] != 0) {
putchar(asciiCodes[i]);
i = i + 1;
}
return 0;
}
Print "Andrew Rocks!" - using character constants to get the ASCII codes for the characters,
and initialize the array to the vales array using , and print the array.
This version has switched to using the numeric type char. This type is almost
always 8 bits and shouldn't be used for arithmetic. It is commonly used
to hold ASCII encodings.
#include <stdio.h>
int main(void) {
// if we don't specify the size of an array being initialized C will make
// it big enough to hold all the initializing elements (15 in this case)
char asciiCodes[] = {'A','n','d','r','e','w',' ','R','o','c','k','s','!','\n',0};
int i = 0;
while (asciiCodes[i] != 0) {
putchar(asciiCodes[i]);
i = i + 1;
}
return 0;
}
Print "Andrew Rocks!" - using character constants to get the ASCII codes for the characters,
and initialize the array to the vales array using , and print the array.
C has a convenient shorthand for char arrays containing a sequence of
ASCII codes with an extra 0 value marking the end of the sequence.
Its "Andrew Rocks!";
Compare the 8 andrew_rocks?.c programs which are all equivalent
to get a better understand of how & why C encodes character sequences
#include <stdio.h>
int main(void) {
char asciiCodes[] = "Andrew Rocks!\n";
int i;
i = 0;
while (asciiCodes[i] != 0) {
putchar(asciiCodes[i]);
i = i + 1;
}
return 0;
}
Print "Andrew Rocks!" - using character constants to get the ASCII codes for the characters,
and initialize the array to the vales array using , and print the array.
C has a convenient shorthand for char arrays containing a sequence of
ASCII codes with an extra 0 value marking the end of the sequence.
Its "Andrew Rocks!";
A number of C library functions accept zero-terminated char arrays
For example printf with the "%s" specification (below)
#include <stdio.h>
int main(void) {
// getchar returns an int which will contain either
// the ASCII code of the character read or EOF
int ch = getchar();
while (ch != EOF) {
printf("you entered the character: '%c' which has ASCII code %d\n", ch, ch);
ch = getchar();
}
return 0;
}
Read characters until eof, printing them with
lower case letters convert to upper case
#include <stdio.h>
int make_upper_case(int character);
int main(void) {
// getchar returns an int which will contain either
// the ASCII code of the character read or EOF
int character = getchar();
while (character != EOF) {
int new_character = make_upper_case(character);
putchar(new_character);
character = getchar();
}
return 0;
}
// return upper case letter corresponding to lower case letter
// e.g. given 'f' return 'F'
// other characters returned unchanged
//
// library function toupper() in <ctype.h> does this task
int make_upper_case(int character) {
if (character >= 'a' && character <= 'z') {
int alphabetPosition = character - 'a';
return 'A' + alphabetPosition;
} else {
return character;
}
}
convert a string read from stdin using getchar to an int
#include <stdio.h>
int main(void) {
printf("Enter a number: ");
int c = getchar();
int n = 0;
while (c >= '0' && c <= '9') {
n = 10 * n + (c - '0');
c = getchar();
}
printf("You entered %d\n", n);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i;
printf("argc=%d\n", argc);
i = 0;
while (i < argc) {
printf("argv[%d]=%s\n", i, argv[i]);
i = i + 1;
}
return 0;
}
Simple example reading a line of input and examining characters
#include <stdio.h>
#define SIZE 8000
int main(void) {
char x[SIZE];
printf("Enter some input: ");
if (fgets(x, SIZE, stdin) == NULL) {
printf("Could not read any characters\n");
return 0;
}
// the built-in function strlen could be used here
int nCharacters = 0;
while (x[nCharacters] != '\n' && x[nCharacters] != '\0') {
nCharacters = nCharacters + 1;
}
// if we don't find a newline - the whole line can't have been read
if (x[nCharacters] != '\n') {
printf("Could not read read entire line\n");
return 0;
}
printf("That line contained %d characters\n", nCharacters);
if (nCharacters > 0) {
printf("The first character was %c\n", x[0]);
printf("The last character was %c\n", x[nCharacters-1]);
}
return 0;
}
#include <stdio.h>
int main(void) {
int num;
// scanf returns the number of items read
while (scanf("%d", &num) == 1) {
printf("you entered the number: %d\n", num);
}
return 0;
}
#include <stdio.h>
int main(void) {
// getchar returns an int which will contain either
// the ASCII code of the character read or EOF
// using an assignment in a loop/if condition is
// not recommended for novice programmers
// but is used widely by experienced C programmers
int ch;
while ((ch = getchar()) != EOF) {
printf("you entered the character: '%c' which has ASCII code %d\n", ch, ch);
}
return 0;
}
Reads lines of input until end-of-input
Print snap! if two consecutive lines are identical
See snap_line1.c for how to use functions
to produce simpler code
#include <stdio.h>
#define MAX_LINE 4096
int main(int argc, char *argv[]) {
char line[MAX_LINE];
char lastLine[MAX_LINE];
// read first line into array lastLine
printf("Enter line: ");
fgets(lastLine, MAX_LINE, stdin);
printf("Enter line: ");
while (fgets(line, MAX_LINE, stdin) != NULL) {
int i = 0;
// count how many characters differ
// between line & lastLine
int differences = 0;
while (line[i] != '\0' && lastLine[i] != 0) {
if (lastLine[i] != line[i]) {
differences = differences + 1;
}
i = i + 1;
}
if (differences == 0) {
// lines are identical
printf("Snap!\n");
}
// arrays can't be assigned so copy elements
// of lastLine to line using a loop
int j = 0;
while (line[j] != '\0') {
lastLine[j] = line[j];
j = j + 1;
}
lastLine[j] = '\0';
printf("Enter line: ");
}
return 0;
}
Reads lines of input until end-of-input
Print snap! if two consecutive lines are identical
See snap_line2.c to see how to replace compareArrays & copyArray
calls to with (strcmp & strcpy) from <string.h>
#include <stdio.h>
#define MAX_LINE 4096
int compareArrays(char array1[], char array2[]);
void copyArray(char destinationArray[], char sourceArray[]);
int main(int argc, char *argv[]) {
char line[MAX_LINE];
char lastLine[MAX_LINE];
// read first line into array lastLine
printf("Enter line: ");
fgets(lastLine, MAX_LINE, stdin);
printf("Enter line: ");
while (fgets(line, MAX_LINE, stdin) != NULL) {
if (compareArrays(line, lastLine) == 0) {
// lines are identical
printf("Snap!\n");
}
copyArray(lastLine, line);
printf("Enter line: ");
}
return 0;
}
// return 1 if array1 & array2 differ in any element, 0 otherwise
// array1 & array2 must be null-terminated char arrays
// strcmp from <string.h> performs similar function
int compareArrays(char array1[], char array2[]) {
int i = 0;
while (array1[i] != '\0') {
if (array1[i] != array2[i]) {
return 1;
}
i = i + 1;
}
if (array2[i] == '\0') {
return 0;
} else {
return 1;
}
}
// copy elements in sourceArray to destinationArray
// sourceArray must be a null-terminated char array
// destinationArray must be large enough to hold string
// strcpy from <string.h> performs the same function
void copyArray(char destinationArray[], char sourceArray[]) {
int i = 0;
while (sourceArray[i] != '\0') {
destinationArray[i] = sourceArray[i];
i = i + 1;
}
destinationArray[i] = '\0';
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i, j;
i = 0;
while (i < argc) {
j = 0;
while (argv[i][j] != '\0') {
printf("argv[%d][%d]=%c\n", i, j, argv[i][j]);
j = j + 1;
}
i = i + 1;
}
return 0;
}
Print "Andrew Rocks!" - using ASCII codes for the characters
Compare the 8 andrew_rocks?.c programs which are all equivalent to get a better understanding of how & why C encodes character sequences