Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez

Similar presentations


Presentation on theme: "COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez"— Presentation transcript:

1 COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez djrg@cise.ufl.edudjrg@cise.ufl.edu http://cise.ufl.edu/~djrg/http://cise.ufl.edu/~djrg/ https://ufcprog2015.wordpress.com/

2 ADMINISTRATIVE STUFF New rule: whenever you participate tell me your name! A quick word on file extensions! The code file extension should always be “.c”. This tells the gcc compiler what programming language you are using! The output/executable extension doesn’t matter! a.out (Default!) a.o

3 ADMINISTRATIVE STUFF Reminder: First quiz will be on Friday (5/22). It will cover everything we’ve covered until this Wednesday (5/20) Homework percentages (these will generally apply for every homework) 15% correct compilation. No errors or warnings! 75% actual functionality (will be divided depending on the homework) For HW1: Each line of output is 25% 10% good coding practices! For HW1: you should get these points by default, for other homework these will include using good variable names and including good comments.

4 LAST FRIDAY Defining a variable: ; Assigning a value: = ; Combined definition and assignment: = ;

5 QUICK WORD ON FUNCTIONS We will cover a lot more on functions in a couple weeks. What is a function? A piece of code that we can easily reuse. What do they consist of? An identifier or name! (same rules as the ones for variables!) Parameters Return Type. We have been using two functions so far. Guesses? printf main

6 QUICK WORD ON FUNCTIONS For now we care about calling functions. The general pattern looks like this: ( ); Similar to how we do printf("Hello world!\n"); printf("I hope to get a %i in the HW", grade); Notice that when a function has more than 1 parameter, we separate the parameters by commas (‘,’)

7 PRINTING A VARIABLE We will use, surprise surprise printf How? By using a special character in the string… Each type uses a different character, we will need to learn them… TypePrintf characters int%i, %x, %o float%f, %e, %g, %a double%f, %e, %g, %a char%c _Bool%i, %u

8 PRINTING A VARIABLE #include int main(void) { int grade = 100; printf("I will get a %i in my first C HW\n", grade); return 0; }

9 PRINTING A VARIABLE #include int main(void) { char letter = 'D'; printf("I hope not to get a %c in my first C HW\n", letter); return 0; }

10 PRINTING TWO OR MORE VARIABLES #include int main(void) { char letter = 100; printf("I will get a %i in my first C HW\n“, sum); return 0; } #include int main(void) { char letter = 'D'; int grade = 100; printf("With a %i in my HW I won't get a %c in my C programming class\n", grade, letter); return 0; }

11 READING A VARIABLE FROM CONSOLE So far, all the programs are deterministic. They always return the same value. No user input. How do we change that? What if I want to be able to alter the result? We can use a powerful function called scanf. scanf comes from the same stdio.h library that printf comes from. It uses the same % characters. Let’s see an example of its usage.

12 READING A VARIABLE #include int main(void) { int grade; scanf("%i", &grade ); printf("I will get a %i in my first C HW\n", sum); return 0; }

13 READING A VARIABLE #include int main(void) { int grade; scanf("%i", &grade ); printf("I will get a %i in my first C HW\n", sum); return 0; }

14 SLIGHTLY BETTER: READING A VARIABLE #include int main(void) { int grade; printf("Input the grade you want to get: "); scanf("%i", &grade ); printf("I will get a %i in my first C HW\n", sum); return 0; }

15 ARITHMETIC OPERATORS What would you expect? '+', '-', '/', '*' We can also use parenthesis to force order of operations And a few others! So for example: int a = 4 + 2; int b = a * 2 + 3; float c = b/a; Let’s actually try this one! And see how it goes!

16 FLOATING POINT CONVERSIONS The previous example doesn’t work as expected Instead of a 2.5 for the last float, we get a 2. Why? How do we get the right result? Internally a and b are integers. The division between to integers is treated as an integer division (one that ignores the remainder)

17 FLOATING POINT CONVERSIONS The fix? Easy. We need to make an explicit conversion to float Instead of: float c = b/a; We do one of: float c = (float)b/a; float c = b/(float)a; float c = (float)b/(float)a; This technique is called casting. It doesn’t affect a or b! It just changes the value interpretation for the operation!

18 MODULUS Sometimes we actually want the remainder of a division. For that we use the modulus operator % int a = 4 + 2; int b = a * 2 + 3; int c = b % a; Remember that if the modulus is 0, then the second number exactly divides the first one.

19 ASSIGNMENT AND OPERATIONS Sometimes we want to do a quick operation on a variable and save the result in the variable itself. For example: int a = 8; a = a * 2; // get the double of 8. A quick shortcut for this is: a *= 2; //same as a = a * 2; This also works for other operators!

20 ASSIGNMENT AND OPERATIONS An even more specific common operation that has it’s own operand is: Adding or subtracting 1 from a variable! For example: int a = 8; a = a + 1; // adding 1 Of course we could do: a += 1; // adding 1 However an even easier way is a++; // adding 1 Same story for subtracting 1 (--).

21 TYPE SPECIFIERS Specifier long short unsigned signed So, can I have a infinitely large number stored in a variable of type int?

22 SIZE IN MEMORY OF DIFFERENT TYPES There is an operator called sizeof. One parameter: a type. Returns an unsigned value that uses the %zu value to printf. sizeof For example: Sizeof(int);


Download ppt "COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez"

Similar presentations


Ads by Google