1 / 66

C Programming

C Programming. Variables. What are variables?. Our way of asking the operating system to allocate memory for our C program Each piece of allocated memory receives a name Our program uses this memory to save values that it wants to use later in the program. Memory. 0. 4. 8. my_int. 32.

Download Presentation

C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Programming Variables

  2. What are variables? • Our way of asking the operating system to allocate memory for our C program • Each piece of allocated memory receives a name • Our program uses this memory to save values that it wants to use later in the program

  3. Memory 0 4 8 my_int 32 5 my_double 152 3.5 Variables in memory int my_int = 5; double my_double = 3.5;

  4. Memory 0 4 8 my_int 32 5 my_double 152 3.5 Variables in memory • Whenever we write the variable name (e.g. my_int), we ask to read the value of that variable • If we write &variable_name, we ask for the address of that variable my_int=5 &my_int=32 my_double=3.5 &my_double=152

  5. Char is also a number! • A char variable is used to store a text character: • Letters. • Digits. • Keyboard signs. • Non-printable characters. • But also small numbers (0 to 255 or -128 to 127).

  6. Text as numbers • Every character is assigned a numeric code. • There are different sets of codes: • ASCII (American Standard Code for Information Interchange) – most common. • EBCDIC – ancient, hardly used today. • Maybe others. • We will use ASCII

  7. The ASCII Table

  8. More about character encoding • You don't care what the particular numbers are • The table above shows only 128 characters (7 bits). Some are non-printable. • Extended ASCII code contains 256 characters.

  9. More about character encoding • ASCII code 0 is important – we will see it again. • Note contiguous sets of numbers, upper case and lower case characters.

  10. char as a character and a number #include <stdio.h> int main() { char c = 'b'; printf("c as a character is %c\n", c); printf("c as an integer is %d\n", c); printf("The character after %c is %c\n", c, c + 1); return 0; } c as a character is b c as an integer is 98 The character after b is c

  11. Another example /* Get the position of a letter in the abc */ #include <stdio.h> int main() { char letter; printf("Please enter a lowercase letter\n"); scanf("%c", &letter); printf("The position of this letter in the abc is %d\n", letter - 'a' + 1); return 0; }

  12. Exercise Write a program that accepts as input – • A lowercase letter and outputs – • The same letter in uppercase (e.g., if the input is ‘g’, the output should be ‘G’)

  13. Solution /* Convert a letter to uppercase */ #include <stdio.h> int main() { char letter; printf("Please enter a lowercase letter\n"); scanf("%c", &letter); printf("This letter in uppercase is %c\n", letter - 'a' + 'A'); return 0; }

  14. Arithmetic operators • An operator is an action performed on something (e.g. constants, variables). • That “something” is called an operand. • Common operators: • Assignment = • Addition + • Subtraction - • Multiplication * • Division / • Modulo %

  15. Operations with different types • When operands of two different types are involved in an operation, the operand of the ‘weaker’ type is promoted to the other type (char → int → float → double). • The result of the operation is of the higher type. • When the operands are of the same type, the result is of that type as well.

  16. Operations with different types For example - • 3 + 4 = 7 • 3.0 + 4 = 7.0 • 3 / 4 = 0 !!! • 3.0 / 4 = 0.75

  17. Example - • A program that sums the digits of a three digits number. • For example: • The input 369 yields the output 18

  18. Let’s see how it works int main() { int sum, num; sum = 0; printf("Enter a 3-digit number\n"); scanf("%d", &num); /* extract the first digit */ sum = sum + num % 10; num = num / 10; num sum 1350 9876 Arbitrary numbers (garbage)

  19. Let’s see how it works int main() { int sum, num; sum = 0; printf("Enter a 3-digit number\n"); scanf("%d", &num); /* extract the first digit */ sum = sum + num % 10; num = num / 10; num sum 1350 0

  20. Let’s see how it works int main() { int sum, num; sum = 0; printf("Enter a 3-digit number\n"); scanf("%d", &num); /* extract the first digit */ sum = sum + num % 10; num = num / 10; num sum 1350 0

  21. Let’s see how it works int main() { int sum, num; sum = 0; printf("Enter a 3-digit number\n"); scanf("%d", &num); /* extract the first digit */ sum = sum + num % 10; num = num / 10; num sum 1350 0

  22. Let’s see how it works int main() { int sum, num; sum = 0; printf("Enter a 3-digit number\n"); scanf("%d", &num); /* extract the first digit */ sum = sum + num % 10; num = num / 10; num sum 369 0

  23. Let’s see how it works int main() { int sum, num; sum = 0; printf("Enter a 3-digit number\n"); scanf("%d", &num); /* extract the first digit */ sum = sum + num % 10; num = num / 10; num sum 369 0

  24. Let’s see how it works int main() { int sum, num; sum = 0; printf("Enter a 3-digit number\n"); scanf("%d", &num); /* extract the first digit */ sum = sum + num % 10; num = num / 10; num sum 369 9

  25. Let’s see how it works int main() { int sum, num; sum = 0; printf("Enter a 3-digit number\n"); scanf("%d", &num); /* extract the first digit */ sum = sum + num % 10; num = num / 10; num sum 36 9

  26. Let’s see how it works /* extract the second digit */ sum = sum + num % 10; num = num / 10; /* extract the third digit */ sum = sum + num % 10; printf("The digits sum is %d\n", sum); return 0; } num sum 36 15

  27. Let’s see how it works /* extract the second digit */ sum = sum + num % 10; num = num / 10; /* extract the third digit */ sum = sum + num % 10; printf("The digits sum is %d\n", sum); return 0; } num sum 3 15

  28. Let’s see how it works /* extract the second digit */ sum = sum + num % 10; num = num / 10; /* extract the third digit */ sum = sum + num % 10; printf("The digits sum is %d\n", sum); return 0; } num sum 3 18

  29. Casting • Sometimes it is desirable for a variable of one type to be considered as belonging to another in an operation • We say the variable is castto the new type. • The casting operator is of the form: (type) • For example, (float)i casts the variable i to a float. • Casting (int)f will cast f from float to int. Meaning it will round it to the closest integer.

  30. Casting variables #include <stdio.h> int main() { int a=1, b=2; printf("%d / %d = %d\n", a, b, a/b); printf("%d / %d = %g\n", a, b, (float)a / b);return 0; } 1 / 2 = 0 1 / 2 = 0.5

  31. Example – find what’s wrong #include <stdio.h> int main() { int a = 10; int b = 20; printf("The average of %d and %d is %d\n", a, b, (a + b) * (1 / 2)); return 0; }

  32. Will this work? #include <stdio.h> int main() { int a = 10; int b = 20; printf ("The average of %d and %d is %d\n", a, b, (a + b)*(1.0 / 2)); return 0; }

  33. value sign (+/-) The unsigned qualifier • Normally, the last bit of a variable serves as a sign bit. • We can use all the bits to represent the value by declaring a variable as unsigned. • To declare a variable as unsigned we add the ‘unsigned’ keyword before its type. • unsigned int; • unsigned char;

  34. Unsigned range • Char (256 different values) • signed -127..128 • unsigned 0..255 • Int (4294967296 different values) • signed -2147483648.. 2147483647 • unsigned 0.. 4294967295

  35. Overflow • Happens when a variable gets assigned a value that is outside of its range • This is equivalent to saying that the number of bits required to encode the value exceeds the number of bits in the variable • The value of the variable will usually be non-sense

  36. Overflow – An example #include <stdio.h> int main() { int iA = 1000, iB = 1000000, iC = 3000000, iD = 5000000; printf ("%d * %d = %d\n", iA, iB, iA*iB); printf ("%d * %d = %d\n", iA, iC, iA*iC); printf ("%d * %d = %u\n", iA, iC, iA*iC); printf ("%d * %d = %u\n", iA, iD, iA*iD); return 0; } 1000000000 -1294967296 3000000000 705032704

  37. Introduction to CS Flow Control (Tests)

  38. Sequential Program int main() { Statement1; Statement2; … StatementN; } S1 S2 S3 S4 S5

  39. Conditional Statements • Selects statements to execute based on the value of an expression • The expression is sometimes called the controlling expression • Conditional statements: • if statement • switch statement

  40. T Expr F Statement Rest of Program Conditional statements: if • used to execute conditionally a statement or block of statements. if(expression) statement; if (grade >= 60) printf("Congratulations! You passed"); printf("Your grade is %d", grade);

  41. Absolute Value - Example int main() { double num; printf("Please enter a real number: "); scanf("%lf", &num); if (num < 0) num = -num; printf("The absolute value is %g\n", num); return 0; }

  42. Block • A sequence of statements enclosed within curly braces {} if (grade > 60){ printf("Congratulations! You passed"); printf("Hip Hip Hooray!"); } printf("Your grade is %d", grade);

  43. if-else statement F T Expr if (expression) statement1 else statement2 • if expression is true, statement1 is executed • if expression is false, statement2 is executed • both statements can be (and very often are) replaced by blocks of statements (“compound statements”) Statement2 Statement1 Rest of Program

  44. An example (fragment) int first, second, min; ... if (first < second) { min = first; printf ("The first number is smaller than the second.\n"); } else { min = second; printf ("The second number is not larger than the first\n"); } printf("The minimum is %d\n", min);

  45. Nested if • The statement in the if statement’s body can be an if statement if (expression){ if (expression)statement1elsestatement2 } If (a>b) { if (b>c) printf(“a is the largest \n”); else printf(“b is the smallest \n”); }

  46. Where does the else belong to? if (x == y) if (y == 0) printf("x == y == 0"); else printf("x == y; x,y != 0");

  47. if-else statement (cont.) if (x == y) if (y == 0) printf("x == y == 0"); else printf("x != y”); • Misleading indentation • else is associated with the closest if • use {} for clarity and to change the meaning

  48. if-else statement (cont.) if (x == y) { if (y == 0) printf("x == y == 0"); } else printf("x != y");

  49. else if • if statements distinguish between exactly 2 cases and execute different code in each case • The else-if construction allows for a multi-way decision

  50. else if if (expression) statement else if (expression) statement elseif (expression) statement else statement

More Related