C has logical operators
||, in decreasing
order of precedence
otherwise - NOT
1 if both
b are non-zero and is
0 otherwise - AND
0 if both
0 and is
1 otherwise - OR
How can you use logical operators to change an
if it is false and
1 if it is true? - !!c
What is the value of
!1||0&&1? - 0
|| are short-circuiting:
C has various functions to classify different sorts of characters.
These functions are declared in
#include<ctype.h> int isdigit(int c);
This function returns a non-zero value if the character
c is a
'0'-'9') and zero otherwise.
c must be either in the range of an
unsigned char or
EOF (otherwise behaviour is undefined)
c have type
How could we implement it?
int isalnum(int c); alphanumeric
int isalpha(int c); alphabetic character
int isblank(int c); space or tab character
int iscntrl(int c); control character (
int isgraph(int c); printable character other than space
int islower(int c); lowercase character
int isprint(int c); printable character including space
int ispunct(int c); printable character other than space or an
int isspace(int c); whitespace character (
int isupper(int c); uppercase character
int isxdigit(int c); hexadecimal digit
We’ve previously seen
scanf(), along with
sprintf() puts a formatted string into the array pointed to by
int sprintf(char *str, const char *format, ...);
snprintf() does the same, but puts at most
characters into the array. Why might you want this?
int snprintf(char *str, size_t size, const char *format, ...);
Aside: variants of these functions with bounds checking were
introduced as an optional feature in C11 (see Annex K of the C
printf_s() etc. They work in
subtely different ways to the normal variants and NO compilers
implement them entirely correctly. These functions are likely to be
deprecated/removed in the future, so avoid using them.
feof() returns a non-zero value if the end of file has been
reached and zero if not. This only happens if you have tried to read
past the last character in the file, not if you have only read
the last character.
int feof(FILE *stream);
fgets() reads in at most
size-1 characters from the file
stream and stores them in the
char *fgets(char *s, int size, FILE *stream);
fgets() keeps reading until it either reaches a newline
'\n' (which will be included in the string
s) or it
reaches the end of the file.
s on success and
NULL on failure (if end-of-file
has been reached and no bytes are read, this counts as failure)
fputs() writes every character from the null-terminated string
s into the file stream
int fputs(const char *s, FILE *stream);
s to the screen followed by a newline
int puts(const char *s);
fputc() writes the character
c (cast to an
unsigned char) to the file stream
int fputc(int c, FILE *stream);
putchar() writes the character
c to the screen
int putchar(int c);
Aside: printing to the screen is often buffered. Once you are done writing, it is often good to print a newline, at which point the buffer will be flushed.
fgetc() gets a single character from the input stream (i.e.
file) and returns it as an
unsigned char cast to an
EOF on error.
int fgetc(FILE *stream);
getchar() works equivalently to
ungetc() lets you push back one character onto the stream.
This will be the first character read on future reads from the
stream. You are only guaranteed to be able to push back one
character, but it does NOT have to be the character you read
from the stream.
int ungetc(int c, FILE *stream);
rand() generates random numbers between
srand() sets the seed of the random number generator. If
srand() is given the same seed, it should produce the same
sequence of random numbers from
The random-number generator is not cryptographically secure!
Typical practice is to use the current time as the seed:
#include <stdio.h> #include <stdlib.h> #include <time.h> ... srand(time(NULL)); int random_number=rand();