270 likes | 535 Views
CS156: More Introduction to C Variables, Input/Output, Expressions. Lecture 2. Basic C Program Structure. Your simplest C programs will have the following simple structure. /***************************************** * Comment section * ******************************************/
E N D
CS156: More Introduction to CVariables, Input/Output, Expressions Lecture 2
Basic C Program Structure • Your simplest C programs will have the following simple structure. /***************************************** * Comment section * ******************************************/ …Preprocessor directives… …Variable Declarations… int main( ) { …Local Variable Declarations… // Comment …Executable Statements… return 0; } Lecture 2: More Introduction to C
C Elements: Comments • Comments are used to document the functionality of a piece of code. • Comments are ignored by the compiler. • In-line comments - // comment • Multi line comments - /* … */ • You should use block comments at the top of all your .c files to document its purpose, its use, and the functions it contains. /***************************************** * Author: CS155 Instructor * * Purpose: Demo of first program * * Inputs: none ** Outputs: prints Hello World * Functions: main * ******************************************/ Lecture 2: More Introduction to C
Careful!! • Just like in Java, /* comments */ can't be nested. • Example below: inner comment /* ignored, so outer comment close with first */ then trouble with the latter */ Lecture 2: More Introduction to C
C Elements: #include • The #include <file.h> syntax tells the compiler to include a set of variable and function definitions from another file. • A standard library of useful functions exists and is accessed through a set of standard .h files. • stdio.h – basic input and output (e.g. printf, scanf ) • stdlib.h – many utility functions (e.g. rand, malloc, strod ) • math.h – mathematical functions (e.g. sqrt, pow ) Lecture 2: More Introduction to C
C Elements: Variables • A variable is a named placeholder for a piece of information. • Each variable has a unique name. • Must start with [a-zA-Z_] • Should start with [a-zA-Z] • Can only contain [a-zA-Z0-9_] • Names are case sensitive (e.g. var1, Var1) • All variables have an associated type which determines what kind of information it can hold. Lecture 2: More Introduction to C
Data types Lecture 2: More Introduction to C
C Elements: Variable Declarations • Variables must be declared before they are used. Declarations take the form DataType name; • Examplesint zip_code; char grade; • All variables have an associated scope that determines where in the program they are accessible. • Variables declared outside any function are called global variables. They are accessible anywhere. Lecture 2: More Introduction to C
Basic Variable Types • int – an integer type • int zip_code; // a zip code • float – a floating point type (real number) • float friction; // a friction coeff • char – a single character • char grade; // letter grade: ABCDF Lecture 2: More Introduction to C
C Elements: Simple Expressions • Simple expressions can be formed using the operators • a + b – Add • a - b – Subtract • a * b – Multiply • a / b – Divide • a % b – Modulus (return remainder) • Here a and b can be literals, variables or expressions. • e.g. 1.4*2 or var1*3.14 or 2*(var1+2) • If a has the value 12 and b = 7, what is a % b Lecture 2: More Introduction to C
C Elements: Assignment Statements • For variables to be useful we must assign them a value. var = expr; • The variable var must be declared before we attempt to assign it a value. • Attempting to assign a value to an undeclared variable results in a syntax error. The compiler will notify you of this. Lecture 2: More Introduction to C
Example /***************************************** * example2-1.c : a simple program * ******************************************/ #include <stdio.h> int zip_code; // a global variable int main() { float friction; // local variable for amt of friction float number; // local variable for calculation // assign some values zip_code = 44455; friction = 0.04; number = (zip_code * friction) – 3.2; // print number printf("The final number was %f\n", number); return 0; } Lecture 2: More Introduction to C
Input and Output Lecture 2: More Introduction to C
stdio.h: printf • The printf function allows us to print to the screen. • Special formatting symbols allow us to print the values of variables. • %d – print an int variable • %f – print a float variable • %c – print a char variable • Special characters can be printed with escape sequences. • \n – the newline character • \t – the tab character • \" – double quote Lecture 2: More Introduction to C
Example 2 /***************************************** * example2-2.c : a printf example * ******************************************/ #include <stdio.h> int main() { float friction; float number; int numPeople; // assign some values friction = 0.04; number = friction + 3.2 * 2; numPeople = 3; // print number printf("The final number was %f\n", number); printf("The value of \"friction\" was %f.\n",friction ); printf("There are %d people here.\n", numPeople ); return 0; } Lecture 2: More Introduction to C
Expressions and Operator Precedence • Consider, a + b * c. This expression is ambiguous unless we specify an order for the operations to be performed in. • C defines a set of precedence relations for operators. Highest Precedence – First Evaluated Lowest Precedence – Last Evaluated Lecture 2: More Introduction to C
Example 3 /***************************************** * example2-3.c : a precedence example * ******************************************/ #include <stdio.h> int main() { // print number printf("1+2*3 = %d\n", 1+2*3 ); printf("(1+2)*3 = %d\n", (1+2)*3 ); printf("2*3+4*5 = %d\n", 2*3+4*5 ); printf("2*(3+4)*5 = %d\n", 2*(3+4)*5 ); return 0; } preisner> ./example2-3 1+2*3 = 7 (1+2)*3 = 9 2*3+4*5 = 26 2*(4+3)*5 = 70 Lecture 2: More Introduction to C
Type Conversion and Division • C automatically converts between types when mixed types are used in an expression. i.e. int →float • 3.0 / 1.5 → 2.0 (float) • 3 / 1.5 → 3.0 / 1.5 → 2.0 (float) • Integer division return the integer part of the result (remainder is discarded). Remember this. • 3 / 2 → 1 (int) • 21 / 8 → 2 (int) Lecture 2: More Introduction to C
A brief note about floating-point constants How to confuse your readers: x = 3. + .4; Was that .4 or 4? Don’t make me squint to see a leading or trailing decimal point—help me out with a zero: x = 3.0 + 0.4; Lecture 2: More Introduction to C
Reading input • Input can be read from the keyboard using scanf. • The usage is similar to printf. The formatting symbols are the same. • Should first prompt the user for what type of information is expected • printf( "Enter a number between 1 and 10: " ); int zipcode; float weight; scanf("%d",&zipcode); scanf("%f",&weight); scanf("%d %f", &zipcode, &weight); Lecture 2: More Introduction to C
Example reading in from stdin /***************************************** * example2-2b.c : a printf example * ******************************************/ #include <stdio.h> int main() { float friction; float number; int numPeople; // read in some values printf("Enter the amount of friction: "); // prompt stays on line scanf("%f", &friction ); printf("Enter the number of people:\n"); // prompt goes to next line scanf("%d", &numPeople); // assign some values number = friction + 3.2 * 2; // print number printf("The final number was %f\n", number); printf("The value of \"friction\" was %f.\n", friction ); printf("There are %d people here.\n", numPeople); return 0; } Lecture 2: More Introduction to C
What is this &? • The value of a variable is stored in a particular place in the computers memory. • The variable name is a way of referencing the variable’s value. • The & in front of the variable’s name tells the program to use the variable’s address in memory rather than its value. Lecture 2: More Introduction to C
Memory 9278 9279 9280 9281 9282 9283 9284 9285 9286 Main memory is divided into many memory locations (or cells) Each memory cell has a numeric address, which uniquely identifies it Slide from Lewis & Loftus Java Software Solutions Lecture 2: More Introduction to C
Address • Each house has an address (like memory does) • The person who lives at that address is the value held at that address • People may move, but the house and address remain the same (value varies, hence we call them variables) • If you want to know who lives at address 0x5FA70, we want the value at the address, so we reference the variable and we’ll get “Mary” • If you want to know the address of the house, we use the & to designate we want the address, not the value inside Slide info from Neil Obremski Lecture 2: More Introduction to C
scanf reads values IN… • scanf needs to know where to store them… • It reads in what you tell it • converts it from whatever base you tell it • converts it to whatever type you tell it • and puts the result WHERE you tell it • Where = address • Why doesn’t printf need the address too? Lecture 2: More Introduction to C
printf Lecture 2: More Introduction to C
scanf Lecture 2: More Introduction to C