See this page as a slide show
CS156 HowToProgram: How to write a program
Hello World #1
- A simple hello world example
/*
* Purpose: Say hello to the world.
*/
#include <stdio.h>
int main() {
// Say hello
printf("Hello, world.\n");
return 0;
}
Hello World #2
#include <stdio.h>
int main(){
int x=0,y[14],*z=&y;*(z++)=0x48;*(z++)=y[x++]+0x1D;
*(z++)=y[x++]+0x07;*(z++)=y[x++]+0x00;*(z++)=y[x++]+0x03;
*(z++)=y[x++]-0x43;*(z++)=y[x++]-0x0C;*(z++)=y[x++]+0x57;
*(z++)=y[x++]-0x08;*(z++)=y[x++]+0x03;*(z++)=y[x++]-0x06;
*(z++)=y[x++]-0x08;*(z++)=y[x++]-0x43;*(z++)=y[x]-0x21;
x=*(--z);while(y[x]!=NULL)putchar(y[x++]);
}
- Many programs will do what we want, but few will be good programs.
Signs of a Good Program
- Effective - The first goal of a program is to be correct.
- Simple — The program should be only as complex as necessary
to solve the problem.
- Documented — A programmer unfamiliar with the program should be
able to easily understand what it does and how.
- Robust — The program should handle unexpected events gracefully.
Signs of a Bad Program
- Lack of useful comments and documentation
- Overly complex, e.g. sorting a list to find the maximum element
- Clever code is hard to interpret and modify.
- Many things can be done in multiple ways in C, but
choosing the shortest way is not always the best idea.
Why does good programming matter?
- Programs
often nearly always outlive their expected lifetime.
- Programs often outlive the programmers’ short term memory.
- Most useful code will end up being modified
by someone who did not write it.
- Always program as if your code was to be maintained by
a dangerous psychopath who knows where you live.
A Few Style Tips
- Break your program into blocks
- Document the purpose of each block and how it works.
- Use meaningful variable names.
- Use the simplest effective code.
Practical Program Writing: What to do.
- Understand what the program should do.
- Describe the program in pseudocode.
- Write the code from the top down.
- Work in steps. Write/Compile/Test/Repeat
Practical Program Writing: What not to do.
- Don’t try to write the program without any planning of forethought.
- Don’t try to write the program all at once.
- Don’t send me your code and ask me to tell you what is wrong with it.
- Don’t wait until the last minute.
Bugs and Errors
- Bug is a programmer’s euphemism for error
- Syntax errors occur when a program does
not follow the rules defined by the C language.
Syntax highlighting helps.
- Semantic or logic errors occur when the program does
not perform as expected. Planning and pseudocode helps.
Correcting Syntax Errors
- The compiler detects and describes syntax
errors during compilation.
- Often, a single error will cause the compiler
to detect many errors later in the code.
- It is best to try to correct the errors one at a time.
This will keep you from spending time trying to correct phantom errors.
Example
// a bad example
#include <stdio.h>
int main() {
float friction;
foat number;
// assign some values
friction = .04;
number = friction 3.2 * 2;
// print number
printf("The final number was %f\n" number);
printf("The value of \"friction\" was %f.\n",friction);
return 0;
}
Example
% c11 c.c
c.c: In function ‘main’:
c.c:5: error: ‘foat’ undeclared (first use in this function)
c.c:5: error: (Each undeclared identifier is reported only once
c.c:5: error: for each function it appears in.)
c.c:5: error: expected ‘;’ before ‘number’
c.c:9: error: ‘number’ undeclared (first use in this function)
c.c:9: error: expected ‘;’ before numeric constant
c.c:12: error: expected ‘)’ before ‘number’
c.c:12: warning: too few arguments for format
- The error messages give the file, function, and line number
of the error. A description of the error is also given.
- Sometimes, an error is not detected until several lines after
it actually occurred. The line number can be misleading.
Finding and Correcting Logical Errors
- Writing a program in stages help in avoiding logical errors.
- To correct logical errors, try to localize the error to
a small portion of code.
- Test just this piece by commenting out other parts
of the code or making a separate test program.
- Use printf to give you the values of variables at
various points in the program.
- Use your brain; don’t work at random.