160 likes | 501 Views
Program Development Cycle. Edit program Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied, go back to #1. My First C Program. /* Arup Guha My First C Program 8/23/07 COP 3223 Rocks! */ #include <stdio.h>
E N D
Program Development Cycle Edit program Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied, go back to #1.
My First C Program /* Arup Guha My First C Program 8/23/07 COP 3223 Rocks! */ #include <stdio.h> int main(void) { printf(“COP 3223 Rocks!\n”); return 0; }
Purpose of Comments • To tell the reader basic information about the program • Who wrote it • When they wrote it • What it does • Any other useful information about it • Inside code, they inform the reader about the specific function of a group of statements.
#includes • Tells the compiler that you may be using prewritten C functions for a particular library. • We will always use stdio.h – it allows us to do standard input and output. • When you call a function for a C library, it will look to all of the libraries you have included to find out HOW to run the function.
Function main • The only code directly executed is the code inside of main • The beginning and ending are delineated by { and }, respectively. • Everything else is run in sequential order, as it is listed. • Other functions (like printf) ONLY run if they are called from main, OR from another function called by main, etc.
printf • Prewritten C function in stdio.h • Prints out everything inside of “” exactly with these exceptions • A backslash and the character after it • A percent sign and the character after it • Prints go in order, and the cursor starts where the last print ended.
Exceptions for prints • Backslash – Escape Sequence • \n is a newline • \t is a tab • \\ is a backslash • \” is a double quote • Percent Codes (Will be explained later) • %d (for integer variable) • %lf (for double variable) • %c (for character)
Variables • Types we will commonly use • int • char • double • How to Declare a Variable • <type> <name>; • Examples • int value; • char my_letter, your_letter; • double money;
Assignment Statement ( = ) • ONE EQUAL SIGN, NOT TWO • Syntax: <variable> = <expression> • What it does: • Calculates the value of <expression> at that point in time. • Changes the value of <variable> to this NEW value.
/* Arup Guha My Second C Program 8/23/07 Computes the number of feet in a mile */ #include <stdio.h> int main(void) { // Declare variables int feet_in_mile, yards_in_mile; int feet_in_yard; // Make calculation yards_in_mile = 1760; feet_in_yard = 3; feet_in_mile = yards_in_mile*feet_in_yard; // Output the result printf(“Mile = %d Feet.\n”, feet_in_mile); return 0; }
% code explanation • If %d or a similar percent code is encountered inside “” of a printf, this is what happens • An expression must be specified after the end of the double quote, separated by a comma • Value of this expression is printed in the FORMAT of the type specified by the percent code. • Experiment to see what happens if you use the wrong percent code.