08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 ■ Characters

H

This would print

on a line by itself. We could, of course, label our output as in the following statement:

printf("The first character is %c \n", ch);

This would print

The first character is H

Finally, we don’t even need ch. If all we want to do is print the first character in the data,

we could do so with:

printf("The first character is %c \n", getchar());

If we want to print the numeric code of the first character, we could do so by using the

specification %d instead of %c. These ideas are incorporated in Program P6.1.

Program P6.1

//read the first character in the data, print it,

//its code and the value of EOF

#include <stdio.h>

int main() {

printf("Type some data and press 'Enter' \n");

char ch = getchar();

printf("\nThe first character is %c \n", ch);

printf("Its code is %d \n", ch);

printf("Value of EOF is %d \n", EOF);

}

The following is a sample run:

Type some data and press 'Enter'

Hello

The first character is H

Its code is 72

Value of EOF is -1

146

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!