- What is the value of each of the following expressions:
ch = 'd';
(a) ((ch >= 'a') && (ch <= 'z'))
(b) ((ch > 'A') && (ch < 'Z'))
(c) ((ch >= 'A') && (ch <= 'Z'))
(d) ch = ch -'a' + 'A';
(e) ch = ch - 'A' + 'a';
- What will be the output of the following:
char ch;
int d;
ch = 'd';
d = 65;
printf("ch = %c, value = %d\n", ch, ch);
printf("d = %d, d = %c\n", d, d);
- Write the header file category.h discussed in section 4.1.2.
Write the macros
IS_UPPER(),
IS_DIGIT(),
IS_PUNCT(),
IS_SPACE(),
IS_CONTROL().
- Write a code fragment to test:
- if a character is printable but not alphabetic
- if a character is alphabetic but not above 'M' or 'm'
- if a character is printable but not a digit
- Write separate loops to print out the ASCII characters and their values in
the ranges:
'a' to 'z',
'A' to 'Z',
'0' to '9'.
- Are these the same: 'a' and "a"? What is the difference between them?
- What will be the output of the source code:
#define SQ(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))
#define DIGITP(c) ((c) >= '0' && (c) <= '9')
char c = '3';
if (DIGITP(c))
printf("%d\n", CUBE(c - '0'));
else
printf("%d\n", SQ(c - '0'));
- Find the errors in the following code that was written to read characters
until end of file.
char c;
while (c = getchar())
putchar(c);
- What will be the output of the following program?
#include <stdio.h>
main()
{ int n, sum;
char ch;
ch = 'Y';
sum = 0;
scanf("%d", &n);
while (ch != 'N') {
sum = sum + n;
printf("More numbers? (Y/N) ");
scanf("%c", &ch);
scanf("%d", &n);
}
}
- What happens if scanf() is in a loop to read integers and a letter is
typed?
- What happens if scanf() reads an integer and then attempts to read a
character?
- Use a switch statement to test if a digit symbol is an even digit symbol.
- Write a single loop that reads and prints all integers as long as they are
between 1 and 100 with the following restrictions: If an input integer is
divisible by 7 terminate the loop with a break statement;
if an input integer is
divisible by 6, do not print it but continue the loop with a
continue statement.