CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Reading: Kernighan and Ritchie, Chapter 1: A Tutorial Introduction
Please submit a question by 9 p.m. on Sunday, 30 January 2011.
Not exactly a question over anything confusing, but... How long will we be working on the "basics/building blocks" introduced in Chapter 1? Because I find it difficult to grasp all at once. It seems rather overwhelming and intimidating (like when I first saw page 29 and 32). Like, will we be getting these sorts of readings two-three times a week where the amount of "new content" is similar or more?
This is the last time you'll get a reading with so much new stuff.
Generally, we'll do one or two sections, rather than one chapter. But
I wanted you to get a big picture
of the language. Often, when
we do this much, we'll step back and talk about it. (We'll do a bit more
talking about C tomorrow.)
This is not so much a question about the reading, but about how I should be reading it. I was able to do the Raymond reading well, but I'm finding it hard to really understand anything at all with this K&R chapter. How should I be reading this?
Come visit with me and we'll step through it.
What is the difference between float and int?
float and double are for real numbers.
byte, short, int, and long
are for integers (whole numbers).
Why might one declare an integer variable long or short as opposed to just int?
The different variants of integers provide different ranges of integers.
On most of the machines I've worked on, shorts range from -32,768
to 32,767, ints range from -2,147,483,648 to
2,147,483,647, and longs range from -9,223,372,036,854,775,808
to 9,223,372,036,854,775,807. We'll talk about where those ranges come
from in a few weeks. Anyway, bigger ranges mean that the values take up
more memory. C programmers traditionally pay a lot of attention to how
much memory they use.
Can you explain the 16 bit
and 32 bit
issues?
These refer to how much memory is allocated to a value of the given type. We'll discuss these issues in more depth in week 4.
Does assigning an integer a value computed using an integer and a floating-point number automatically end up as an integer, or is this an illegal operation?
It's an illegal operation.
Are there more libraries than just the standard C library, and if so, do they have different functions? If not, why is #include &tl;stdio.h> not just a given?
There are hundreds of libraries. Not all C programs use textual input and output, and so the declarations are optional.
Aside from stdio, what libraries are primarily used?
I often use stdlib, limits, and math
in my code. When I'm writing graphical applications, I use GUI libraries.
Is there a 'man' or dictionary system for writing in C so that you can look up functions?
Most of the standard C library functions appear in section 3 of the
Linux manual system. For example, man 3 printf will give
you information on printf. Some commands might appear
in section 2.
What distinguishes a function from a library function
?
C has very few built-in functions. (See the next question for more
details.) So, function
can mean function written by the
current programmer
, built-in function
, or function taken
from a library of utility functions
.
The reading says that printf is not part of the C language.
How can C use/understand a function that's not written in C? Wouldn't the
function be in a syntax that C is unfamiliar with?
Not part of
and not written in
are different concepts.
As I mentioned, C is a small language. Dennis Ritchie seems not to
have thought that I/O was necessary to put in the core. That said,
printf quickly became part of the libraries that accompanied
every C compiler. We can write programs that use multiple language because
they all eventually get translated to the same underlying language.
In 151, we were able to save useful programs we wrote in a library file and load it whenever we wanted access to them. How does this work in C? Will we be converting our programs to a text file, and then re-opening them when we need them?
When I write a new program, how do I add it to a library like
stdio so that I can reference it in future programs?
You generally can't add to existing libraries. You can include the
code from one C source file in another with #include "file".
You can also do more complicated things with compiled code.
When would you NOT want to include info from the standard library (i.e., when would you not type #include <stdio.h>)?
If you were writing a program that interacted with the user or file system in other ways. You don't need stdio if you're writing a GUI program. You may not need stdio for reading and writing files.
What is the advantage of declaring
variables at the start of code?
We declare variables for two reasons. One is that practice suggests that programmers regularly misspell variable names. If you can only use the names you've declared, you're less likely to get odd behavior because you're accidentally working with two variable rather than the one you think you're working with.
Can you explain the differences between Formal and Actual Arguments a little more clearly? I think I might not be catching onto something.
A formal parameter is the one that appears in the function declaration.
In Scheme, it's the x in (define f (lambda (x) (+ 1 x))).
In C, it's the x in int f (int x) { return x + 1 }. The actual parameter appears in the function call. It's the 5 in
Scheme's (f 5) and C's f (5).
Instead of nesting additional if statements inside else statements, is there a way to provide a series of tests that correspond to singular consequents and then provide only one else statement, like with Scheme's cond keyword?
C does not provide anything as general as Scheme's cond
keyword. (cond is really just syntactic sugar for the
nested if statements anyway.) However, C does have another
conditional control structure, the switch statement, that
lets you choose what to do depending on the value of an expression.
We'll cover it later.
Does C (or a built in library) have a standard print function (not printf)?
The standard I/O library for C provides printf along with
some instructions for printing individual characters. That's about it.
What program do we use to write in C?
Where/how will we write/run programs?
You will write your programs using Emacs (or, perhaps, some other editor).
What is an integer? (I'm used to that referring to a whole number, but the reading also refers to characters as integers. So, is this because characters are represented as integers within the computer, or does integer
just refer to a certain type of data?)
Integers are what you think of as whole numbers (as long as you include postive, negative, and zero. You are correct that characters are represented as integers.
The code has int for fahr, celsius. Then it has
int for lower, upper, step. Why doesn't it combine all of them and say int fahr, celsius, lower, upper, step;?
To make it easier for you to change the type of fahr and
celsius.
I am still really confused about how EOF works. If main is not taking inputs then what file is this checking? When does getchar() != EOF know when to end.
When you are using the line or character count programs, what is the input? Since main is empty, doesnt that mean there are no parameters or inputs? How does the program receive the input? Likewise, when you are writing a program, how do you account for different possible parameters?
There are two kinds of inputs to a program (and to main).
There's the input that comes from the keyboard (or from an input redirection).
You access that with getchar (and some other related functions).
There's also the parameters
to the program (and to main),
such as the -d' ' we used for cut. We'll learn how
to deal with program parameters in week 3.
What does it mean when you say that you are referring to returning a value to main? If main doesn't take any parameters then how can you return a value to main?
You return a value from main. That returned value
goes to the operating system, and helps the OS figure out whether the program
succeeded or failed.
If you make a change to an external variable inside one function will it change it for the entire program?"
Yes.
What are the practical differences between Scheme's lists or vectors and C's arrays?
C's arrays are most like Scheme's vectors: They are fixed size and you can access an element in constant time. C's arrays differ from Scheme's vectors in a few ways. First, C's arrays are homogenous: They must contain all the same type. In contrast, Scheme's are heterogeneous. Second, C's arrays are not carefully checked. If you try to access element 7 of a 4 element array in Scheme, you'll get an error message. If you try to access element 7 of a 4 element array in C, you'll probably get some value, but it won't be a useful one.
Within each function do all external variables have to be declared? The book says they must be but the previous owner of my book has underlined that and written "wrong."
No. External variables do not need to be declared.
Is there any way to use an input/output interface besides the terminal?
Well, there's pipes. There are also lots of graphical libraries (none of which we'll learn this semester.
What does it mean that printf is a general-purpose output formatting function?
It means that we can use it to print all kinds of values and to describe how the values are to be printed.
How can we tell which function is part of the C language and which isn't?
If K&R have not given you a definition, it's either part of the C language or in a standard library.
Could you explain the difference between #define and
int more clearly?
#define is used when you want to define constant values
that will never change. (Well, it's used for more than that, but that's
enough for now.) When you write a type before a variable name, you declare
something that stores values, but that can change the value that is stored.
For the line count program, how does c (getchar()) advance? I understand that nl (tally of new lines) is incremented when c is the new line character, but how does c advance to recurse through the whole input? in other words, how does getchar() advance to look at every character?
getchar is a lot like Scheme's (read-character).
Each time you call it, it reads the next character. So, the
while ((c = getchar()) != EOF) { body } code:
(1) reads a character; (2) assigns it to c (stores it);
(3) checks whether it's the end-of-file character - if so, the loop
is done, if not; (4) does the body; (5) goes back to step 1.
What does the ++i mean in for (i = 1; i <= n; ++i)?
Add 1.
What are the twelve categories of input? (pg. 22 the section on Arrays)
For the problem described in that section, the categories are:
the digit 0, the digit 1, the digit 2, ..., the digit 9, whitespace,
and other
.
What are the primary differences between C and Java? Other than Java supporting OOP, of course.
C is a bit more casual in its type checking: You can easily treat a
value of one type as a value of another in C. Java makes that hard. C
gives you access to a memory address
type, which is both powerful
and dangerous. Java does not. Java provides lots of protection levels.
C does not. You also build libraries in different ways.
The reading mentions the word loop. Like what does it mean? Is it like infinite loop, where the programs run over and over? So like recursion, run a procedure until it does not fit the conditions?
We hope that our loops are not infinite. But yes, it's running some steps again and again until a condition holds (or no longer holds, depending on the kind of loop). You can also loop a fixed number of times using a counter ariable.
I do not understand the difference between = and ==. To me, it seems like they are both equal to
. The authors said that This symbol is used to distinguish the equality test from the single = that C uses for assignment.
What does that mean?
The single equals sign is used when we want to store a value
in a variable. (It's somewhat like Scheme's let or
define, except that you can change the value stored.).
The double equals sign is used for testing whether two things are equal.
It's like Scheme's eq?.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Fri Feb 25 19:30:11 2011.
The source to the document was last modified on Fri Feb 25 19:30:00 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/intro-c.html.