Hello EE209 students, Here are my answers to your questions. I had to leave out some questions that I did not fully understand, so if your questions are not addressed here, please see me in person. In general, please frame the questions more specifically instead of saying I don't know X. That way, I can write more targeted answers to better suit your questions. Anyway, I hope this email is helpful. As I said in the class, please bring your own blank sheet each time so that you can submit your question sheet after each class. I won't bring the sheets next time. Also, please feel free to follow up with questions to this mailing list. Thanks, KyoungSoo Q & A 1. What is redirection? => It means it changes the default direction of input or output(or error) stream. Without redirection, standard input is from your keyboard, and the standard output and error streams go to the display. You can change the source or sink of these streams by using redirection (>, <, |) on a unix shell. '>' changes the standard output to a file followed by '>'. '<' changes the source of standard input to a file followed by it. '|' connects the standard output of one program to the standard input of the next program. For example, cat file1 | ./echo means 'cat' program reads the file1 and prints the content of file1 to the standard input of 'echo' program. 2. Why do you need to type "enter" to feed the standard input into the program? Why not getchar() gets each character as soon as you type it? => Good question, and it's a tricky one. :-) There are three types of buffering for stream operations: unbuffered, block buffered, and line buffered modes. stdin's default buffering (when you use a terminal) is line buffered mode. That is, getchar() has to wait until one whole line (with '\n' at the end) is available before extracting any character from stdin. You can change the default behavior to "unbuffered" by changing the terminal setting (refer to tcsetattr()). block buffered mode reads/outputs the stream by a unit of disk I/O block. No need to know that at this point. 3. What's the difference between EOF and Ctrl+D ? => Ctrl+D is end-of-transmission character (EOT) with ASCII value 4. When you type this character, you notify the terminal (that's waiting for the next character from your keyboard) that you're done with feeding the input. All characters up until that point are accepted as valid input and fed to the program as stdin. From the perspective of the program, when getchar() gets passed the last character and knows that it does not have any more character to read in, it returns a special value (failure to read more character), which is EOF (-1). Note that EOF is not part of ASCII code. It's simply a special return value meaning it reached the end of the input. 4. Call-by-value vs. Call-by-reference? => It's a mode of passing parameters to functions. If you pass the parameters by call-by-value, even if you modify the value inside the function, it won't change the original value. Call-by-reference is the opposite: when you change the value, that changes the value of the original variable. For example, int x = 3; f (x); printf("x=%d\n", x); where void f(int x) { x = 2;} Call-by-reference would print out 2 whereas call-by-value would print out 3. In C, parameter passing is done by call-by-value, but if you use pointers, you can imitate call-by-reference. 5. Why is it important to make the source code clean, readable and modular? => Eventually to maintain the source code better. If you write a program just to run it only once, you won't need to pay much attention for future maintenance. But often time, you need to manage the source code for a long time so that you can add new features, change the behavior, fix bugs, etc. And also, you may need to work together in a team and share your source code with others (not in our programming assignments!!). Then, your source code becomes a medium for communication. 6. I don't know what DFA is? => I'll just give you the context why we use DFA. DFA is one way of characterizing a complex problem. By converting the problem into (simpler) states and transitions, you identify the algorithm for the solution by checking the relationship between the states. DFA is a conceptual framework where you put the meaning to each state, and define the interaction between the states by figuring out actions for possible input/events for each state. If you want more rigorous definitions, please refer to the books on automata theory or theory of computation. 7. Should we always write DFA? => No. Certain problems can be solved more easily with DFA. That is, once you have a DFA diagram, you can easily convert it into a program. Of course, not every problem can be solved with DFA, but any problem that can be explained as states and transitions between them can be solved with DFA. 8. Can you explain again why we use "int" for getchar(), putchar() as return value not "char"? => To have a special value to notify the caller of the function of an error/failure. EOF is the special value, and "char" is not wide enough to hold any character + this value. 9. What's modularity? => A program with modularity means the program consists of components that are separable and independently combined. In a C program, such a component is typically represented as function. A function should have logically-related statements that collectively do one thing well with the right set of input and output. 10. What's the side effect? => If you change the value of an operand while you're evaluating the main expression, you cause the side effect to the operand. In general, when you (intentionally or unintentionally) change some behavior while your main purpose is something else, you are referred to cause side effect. In while ( (c = getchar()) != EOF), your main purpose is to check whether the input is unequal to EOF, but in doing so, you change the value of c. That's the side effect. 11. What is isalpha(c)? => It returns non-zero if c is an alphabet, zero if not. 12. What do you mean by "maintainable"? => There's no one correct definition, but conceptually, the source code is maintainable if it consists of a set of well-defined modules and the interaction between them so that you can easily understand the logic of the program even if you read it after a long while. 13. What is the role of (void) in int main(void)? => It simply means that the function does not have input arguments. 14. Is there a specific command or value for EOF? => $ grep EOF /usr/include/stdio.h #ifndef EOF # define EOF (-1) It's defined as macro to be (-1). 15. What are getchar() and putchar()? Where did they appear? => They are library functions prepared and written by someone else. All you need is to consult a man page for these functions (e.g., man getchar) and see what they do and how to use them. You can even find the source code by googling them, but you don't need to understand how they work for now. 16. What's the difference between getchar(), scanf(), gets(), getc()? => scanf accepts formatted input from stdin while getchar()/getc() accepts a character at a time. gets() accepts a whole string (ended by '\n') into a buffer. Please use 'man' for more information. 17. upper < upper.c > junk.c ? => Instead of using default stdin (keyboard) and stdout (display), this command feeds "upper.c" as stdin and uses "junk.c" as stdout. From the perspective of upper, it does not know whether the input is physically typed by a human or fed by a file. All it sees is a stream of input characters. At the same time, whatever is sent to the stdout from upper goes to the file, "junk.c", instead of the display. 18. int c; ... if (c < 'A') Does the character type gets converted into an integer when you enclose the letter with ''? (Not sure if I understand the question) => 'x' means the character value for the letter x. 'A' is 65, so what you're doing is the same as if (c < 65). 19. As modularity increases, the complexity also (seems to) increase? => If you're saying that the size of the code gets larger, that's true, but if your code gets more modular, then you can understand the program more easily. By focusing on what each component does, you can quickly get the big picture of what the entire program does. 20. How do I adjust the size of an array for input? => I did not understand your question, but let's defer the discussion until we deal with arrays. 21. What is SSH protocol? => It's Secure Shell. Simply put, it's an encrypted version of telnet which is used to log into a machine remotely. 22. What is GNU? => GNU is originally a name of an operating system that is more or less like unix. http://en.wikipedia.org/wiki/GNU 23. There are many editors such as vi and emacs. What's the most common editor? => You're right. vi, emacs, gedit, vc++ editor, ... If you are already used to one of these, you can stick to it. If not, I recommend emacs since it's easier to learn it if you're used to windows environment. vi and emacs are the most popular as far as I know, but people tend to have developed their own favorite ones. 24. What is the meaning of the DFA (deterministic finite automata)? => It is called finite since # of states is finite. Automata simply refers to a self-operating machine or a system. Deterministic means that the state transition is deterministic for a given input /event. That is, for a given event, you can move to at most one state at a time. There is also NFA (Non-deterministic FA) where you can move to multiple states for a given input/event. Nevertheless, it's proven that the expressive power of DFA and NFA is the same. (You can convert any NFA to DFA)