210 likes | 343 Views
EEE 243B Applied Computer Programming. Basic program structure. Outline. Quick comparison between C and Java Tools required to program in C main() Simple program in C Command line arguments (argc, argv) Commenting Naming conventions. Java and C.
E N D
EEE 243BApplied Computer Programming Basic program structure
Outline • Quick comparison between C and Java • Tools required to program in C • main() • Simple program in C • Command line arguments (argc, argv) • Commenting • Naming conventions Maj JGA Beaulieu & Capt MWP LeSauvage
Java and C • C and Java share similar syntax and semantics. • Java is an object oriented language, C is a structured (or procedural) language • A program in Java is interpreted, while a program in C is compiled • Java can be ported between platforms with a JVM without having to change or compile the code • C requires a platform specific compiler • C has dynamic memory allocation and pointers; Java does not. Maj JGA Beaulieu & Capt MWP LeSauvage
Tools for programming in C • You need a text editor to create and modify your code • Here you will create/modify *.c and *.h files • You need a compiler, that is platform dependant • Intel/Win, Sun SPARC/Unix… • Here you produce *.obj files • You need a linker • Here you produce *.exe files • An environment to execute programs • In the lab this will be the LEGO Mindstorms™ Maj JGA Beaulieu & Capt MWP LeSauvage
Steps to produce a C program Figure 1-9 Maj JGA Beaulieu & Capt MWP LeSauvage
Structure of a C program • All C programs must contain at least one function • This function is the main() function • There can be only one function named main() Maj JGA Beaulieu & Capt MWP LeSauvage
Structure of a C program • All C programs are written with a well defined structure: /*Preprocessor directives*/ /*Global variables declaration*/ void main(void) { /*Local variables declaration*/ /*Statements*/ } Maj JGA Beaulieu & Capt MWP LeSauvage
Structure of a C program /*Preprocessor directives*/ #include <stdio.h> void main(void) { int x,y,z; /*Local variables declaration*/ x = 2; /*Statements*/ y = 3; z = x * y; printf(“Here is the value of z: %d”, z); } Maj JGA Beaulieu & Capt MWP LeSauvage
Command line arguments • As we saw in the sample program, we can “hardcode” some numbers in a program by predefining values. • This is not very flexible, we could use some user input • We can also pass some arguments to the program. • These arguments given to the program at startup will be passed as parameters the main function. Maj JGA Beaulieu & Capt MWP LeSauvage
Command line arguments • A C program can have no arguments • void main (void) • Or exactly two arguments • void main (int argc, char *argv[]) • int argc is an integer that indicates the number of elements (count) in the argument vector (argv) Maj JGA Beaulieu & Capt MWP LeSauvage
Command line arguments Program name /0 /0 /0 6 /0 argc /0 /0 argv Maj JGA Beaulieu & Capt MWP LeSauvage
Command line arguments • So even though a C program only has two arguments, it has a lot more!!! • Lets look at an example from Forouzan and Gilbert Maj JGA Beaulieu & Capt MWP LeSauvage
Command line arguments #include <stdio.h> #include <string.h> #include <stdlib.h> int main (int argc, //Note that statements char *argv[]) //can break across lines. { //Local Definitions int i; //Statements printf ("The number of user elements: %d\n", argc); printf ("The name of the program: %s\n", argv[0]); for ( i = 1 ; i < argc ; i++ ) printf ("User Value No. %d: %s\n", i, argv[i]); return 0; } Like we just saw 2 arguments argc is defined by the command line entry argc can be used to control a loop Maj JGA Beaulieu & Capt MWP LeSauvage
Command line arguments #include <stdio.h> #include <string.h> #include <stdlib.h> int main (int argc, char *argv[]) { //Local Definitions int i; //Statements printf ("The number of user elements: %d\n", argc); printf ("The name of the program: %s\n", argv[0]); for ( i = 1 ; i < argc ; i++ ) printf ("User Value No. %d: %s\n", i, argv[i]); return 0; } argv is an array of elements Maj JGA Beaulieu & Capt MWP LeSauvage
Command line arguments • Looking at the code segment the following input strings will produce the corresponding output: • C:\cmdline Bozo The number of elements: 2 The name of the program: CMDLINE User Value No. 1: Bozo Maj JGA Beaulieu & Capt MWP LeSauvage
Command line arguments • Second example with a string: • C:\cmdline Bozo “the clown” The number of elements: 3 The name of the program: CMDLINE User Value No. 1: Bozo User Value No. 2: the clown Maj JGA Beaulieu & Capt MWP LeSauvage
Commenting • During the lab work, you will often be given code that already contain comments. • You are to comment the new code that you write or any changes that you effect to existing code. • Comments should convey the intent of a statement or a block of statements. • Do not put useless comments such as: X = 5; //here X is set to 5 • Use comments like: X = 5; //Here X is initialized to max number of elements Maj JGA Beaulieu & Capt MWP LeSauvage
Commenting • Each program or function should have a header that resembles this: ////////////////////////////////////////////////////////////// // // File: MyProgram.C // // Author: Capt LeSauvage (with some code from Forouzan) // // Description: This is the program that will display Hello // World on the robot control unit from LEGO // Mindstorms. // // Revision History: Created 1 January 2005 // Modified 10 Jan 2005 by OCDT Bloggins // OCDT Better helped with the LCD function // ////////////////////////////////////////////////////////////// Maj JGA Beaulieu & Capt MWP LeSauvage
Naming Convention • All identifiers (constants, variables and function names) should have meaningful names. • A..Z, a..z, 0..9 and the underscore (_) are valid characters • Do not start with a digit or underscore • ANSI/ISO C has 31 characters per identifier; the rest will be ignored. • So if two identifiers have 32 characters and only the last one differs, the names are equal !!! Maj JGA Beaulieu & Capt MWP LeSauvage
Naming Convention • All constants must be capitalized #define STRSIZE 20 /*Maximum string size*/ • Variables should start with a lowercase letter with an uppercase letter to start a “new” word char *customerName; //Name of the customer //or business. • Function names start with an uppercase letter with an uppercase letter for a new word int MultiplyTwoInt(int firstInt, int secondInt) {…} Maj JGA Beaulieu & Capt MWP LeSauvage
Quiz Time • What are the four tools you need to create a C program? • Name two differences between Java and C. Maj JGA Beaulieu & Capt MWP LeSauvage