First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Programming for Engineers: Introducing C
14 January...
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
first.c (cont’d) int main(int argc, char* argv[]) { int num; num=1; printf(”I am a simple ”); printf(”computer.∖n”); printf(”My favorite number is %d because it is first.∖n”, num); printf(”∖n∖nPress any key to continue...”); while(!kbhit()); return 0; } Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
#pragma
▶ ▶
#pragma sets compiler options. hrdstop ▶ ▶
▶
argsused ▶
▶
specifies the location of the precompiled including standard c libraries is done before #pragma disables the warning message:“Parameter name is never used in function func-name.”
These are part of the application console template.
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
int main(int argc, char* argv[])
▶
main() is the entry-point function for console applications
▶
argc and argv[] are command-line interface Returns integer number to OS
▶
▶ ▶
It returns 0 if there is no error. It returns a non-zero number if there is an error.
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Blocks of Statements ▶
Curly braces are used to specify that a block of code is to be associated.
▶
The block is treated as a single line of code.
▶
{ indicates the beginning of the block
▶
} indicates the end of the block
▶
Example int main(int argc, char* argv[]) { ⋅⋅⋅ return 0; } Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Declaration and Initialization of Variables
▶
Declaration: allocate space in memory for the varable int num;
▶
Initialization: assign the variable a useful value num=1;
▶
Note that assignment uses only a single equal sign.
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Data Types
▶ ▶
Name variables with a meaningful name. Reasons to declare variables ▶ ▶ ▶
▶ ▶
Without declaration, the program will not compile. Declaration allocates space for the variable. Declaration prevents similarly but mispelled variables from being created. Declaration forces planning before programming. Declaration makes it easier to grasp the purpose of the program.
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Output Two types of output: ▶
Text printed only printf(”I am a simple ”); printf(”computer.∖n”); ▶
▶
∖n starts a new line.
Text printed with user-supplied data printf(”My favorite number is %d because it is first.∖n”, num); ▶
▶
%d specifies the format; it is discussed in more depth in Chapter 4. %d displays integers, positive or negative whole numbers.
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Pause for Any Key
▶
Prompt user to wait for any key to be pressed printf(”∖n∖nPress any key to continue...”);
▶
Wait while no key is hit and then continue while(!kbhit());
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
return 0
▶
Returns a value to calling program
▶
This value may be used by the scripting language that called program such as DOS batch file The convention is
▶
▶ ▶
It returns 0 if there is no error. It returns a non-zero number if there is an error.
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Basic Structure
▶
Header Files ▶ ▶
▶
stdio.h: standard I/O functions conio.h: console I/O functions
Body ▶
main(⋅){⋅}
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Another Program // fathm ft.c – converts 2 fathoms to feet #include int main(void) { int feet, fathoms; fathoms = 2; feet = 6 * fathoms; printf(”There are %d feet in %d fathoms!∖n”, feet, fathoms); printf(”Yes, I said %d feet!∖n”, 6 * fathoms); return 0; } Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Basic Structure Expanded
▶
Comment documents purpose of program
▶
Declarations reserve memory
▶
Initialization changes to initial value of memory
▶
Input: none in this program
▶
Filter or algorithm: multiplication in this program
▶
Output: display results
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Sample Program /* two func.c: a program using two functions in one file */ #include void butler(void); /* ISO/ANSI C function prototyping */ int main(void) { printf(”I will summon the butler function.∖n”); butler(); printf(”Yes. Bring me some tea and writeable CD-ROMS.∖n”); return 0; }
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Sample Program (cont’d)
void butler(void) /* start of function definition */ { printf(”You rang, sir?∖n”); }
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Observations
▶
The butler function appears three times ▶ ▶ ▶
▶
Advantages ▶ ▶
▶
void butler(void); //prototype butler(); //invocation (call) of function void butler(void) //definition of function Modularizes code Allows for reuse of code with minimal overhead
Disadvantage ▶
Slightly slower than in-line code
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Debugging
▶
▶
Good debuggins skills make the difference between hating PENG and finding it not so bad Types of errors ▶
▶
Syntax errors: Not knowing keywords, operators or language lexicon Semantic or logical error: Errors in meaning
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Syntax Errors
▶
Syntax error are caught by compiler and linker.
▶
Semantic errors require one to track state of program, values variables transition through
▶
Example: nogood.c
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
ACLU #2
Programming for Engineers: Introducing C
First Program Program Structure Two (or More) Functions Debugging ACLU #2 Programming Assignment
Programming Assignment
Complete Chapter 2 Exercise 2 Complete Chapter 2 Exercise 5 Due Date: 26 January 2009