Faculty Supervisor: Al Day
HTML Documentation by: Julie Sandberg
Date last updated: 9/16/95
![]()
e.g.:
scanf("%c", &initial);
printf("The initial is %c", initial);
#include < stdio.h > /* printf, scanf
definitions */
int main (void) {
char let_1, let_2, let_3; /* three letters */
int year; /* current year */
FILE *outp; /* pointer to output file */
outp = fopen("output.txt", "w");
printf("Enter a 3-letter nickname and press
return> ");
scanf ("%c%c%c", &let_1, &let_2, &let_3);
printf("Enter the current year and press
return> ");
scanf("%d", &year);
printf("Welcome, %c%c%c. %d is a great year
to study C!\n",
let_1, let_2, let_3, year);
fprintf(outp, "%c%c%c", let_1, let_2, let_3);
fclose (outp);
return (0);
}
But...How useful are single characters?
Is there a better way to manipulate words?
![]()
We have already used string constants. Consider the call:
printf("The answer is: %f", var);
The first part in the quotes is the string constant "The answer is:",
a string of 15 characters. Blank spaces ARE counted.
![]()
char string[30];This will hold a string of up to
All strings of characters are termined with the null
character '\0'. The null character marks the end of the string,
allowing the different C functions to know where the end of the string is.
All characters following the '\0' are ignored.
![]()
e.g. char str[4] = {'U', 'S', 'A', '\O';}
char str[20] = "Initial value";
%s -- type declaration for string I/OFormat: scanf("%s", pvar); printf("The string is: %s", pvar);
The & is not used with the variable name
for string input.
![]()
e.g. printf("%5s", string);
This would cause the string to be right-justified if less than
5 characters in length. If the string is longer than 5 characters, the
field would be expanded to accommodate it exactly.
![]()
e.g. printf("%-20s", vegetable_type);
This is useful whenever the situation calls for left-justification of
strings.
![]()
#include < stdio.h >
#define STRING_LEN 10
int main(void) {
char dept [STRING_LEN];
int course_num;
char days [STRING_LEN];
int time;
printf("Enter department code, course number, days ");
printf("and time like this: \n> ENGR 160 TR 1200\n> ");
scanf("%s%d%s%d", dept, &course_num, days, &time);
printf("%s %d meets %s at %d\n", dept, course_num,
days, time);
return (0);
}
Enter department code, course number, days and time like this:
![]()
#define COLUMNS 81
.
.
.
char line [COLUMNS];
char *status;
FILE *inp;
inp=fopen("sample.txt", "r");
if(status == NULL) {
printf("End-of-file encountered or read");
printf("error ocurred.\n");
}
