28 October 2012

Part 5 - Assignments, Expressions and Function Calls

Topics Covered:

Combining assignments, expressions and function calls
Integer types: 16-bit and 32-bit
Counting characters in the input stream



Watch video >>


Downloadable Files

countchars.c


Exercises

1. Compile and run the program countchars.c on your computer. Confirm that the input 
hello
, followed by a newline and an end-of-file signal results in a value of 6. Does the EOF signal itself count towards the resulting character count?


2. In a previous part, we made the program fahr LOWER UPPER STEP, which converts temperatures in Fahrenheit from LOWER to UPPER in steps of STEP degrees Fahrenheit. Write the program cels, which does the same thing, but which starts with Celsius temperatures and converts them to Fahrenheit.


Questions

1. Different systems and text editors will use different representations of the newline, depending on the context. Using various text editors, and (if possible) various operating systems, produce a series of test text files, all of which contain the same number of characters and newlines, but which differ only as to which system they were produced on. What is the file size in bytes reported for each file? What is the character count of each file reported by countchars ? Are there any discrepancies?

2. Consider the following program:

#include <stdio.h>
int main(int argc, char *argv[])
{
    if (argc < 2) {
        printf("Usage: %s NAME\n", argv[0]);
        return 1;
    }
    if (printf("%s\n", argv[1]) < 2) {
        printf("NAME is too short!\n");
        return 2;
    }
    return 0;
}
 

a. For what values of  argv[1] does the program produce the message  NAME is too short! ? The man page entry for printf(3) may prove helpful in determining what the return value of printf should be.


b. Notice that even such a short program can quickly have many "magic numbers" such as 0, 1, 2. Rewrite the expressions which use magic numbers to use appropriately named symbolic constants which are to be defined at the top of the program with #define directives.


Quick reference

16-bit signed integer

A 16-bit signed integer can store any of the values (-32768, -32767, ..., -2, -1, 0, 1, 2, ..., 32766, 32767). In C, the int type is guaranteed to be at least 16 bits. However, the exact width depends on the machine implementation.

32-bit signed integer

A 32-bit signed integer can store any of the values (-2147483648, -2147483647, ..., -2, -1, 0, 1, 2, ..., 2147483646, 2147483647). In C, the long type is defined to be at least 32 bits. However, the exact width depends on the machine implementation.

3 comments:

  1. It's -32767 to 32767, because of a potential trap representation on negative zero. Check your standard.

    ReplyDelete
  2. Thanks for the clarification. You are correct about int. Referencing C99 §5.2.4.2, INT_MIN has a minimum value of -32767 (but it may be lower per the implementation). The other integer types similarly exclude the lowest negative value from the requirement.

    However a 16-bit int as defined in §7.18 still requires that INT16_MIN is exactly -32768 and INT16_MAX is 32767. This is what I meant by a "16-bit signed integer".

    ReplyDelete
  3. Please show me, where on this page were you mentioned the type int16_t? I see int mentioned (with relevance to the error previously noted), but not int16_t.

    You're not teaching people C99, but a language that differs subtly from C99.

    ReplyDelete