Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming Variables.

Similar presentations


Presentation on theme: "C Programming Variables."— 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 Variables in memory int my_int = 5; double my_double = 3.5; Memory
32 152 4 8

4 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 5 3.5 my_int my_double Memory 32 152 4 8 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; } The character after b is c c as a character is b c as an integer is 98

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 – and outputs –
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 = 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

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

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

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

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

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 */ 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 */ 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 */ 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 cast to 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.5 1 / 2 = 0

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 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; sign (+/-) value

34 Unsigned range Char (256 different values)
Int ( different values) signed unsigned

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 = , iC = , iD = ; 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; }

37 Introduction to CS Flow Control (Tests)

38 Sequential Program int main() { S1 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 Conditional statements: if
used to execute conditionally a statement or block of statements. if (expression) statement; Expr Statement Rest of Program T F 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 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) statement1 else statement2 } 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) else

51 An example – grade.c if (grade >= 90) { printf ("A\n");
} else if (grade >= 80) { printf ("B\n"); } else if (grade >= 70) { printf ("C\n"); } else if (grade >= 60) { printf ("D\n"); } else { printf ("F\n"); }

52 What would happen if we changed the order of the ifs?
if (grade >= 60) { printf (“D\n"); } else if (grade >= 70) { printf (“C\n"); } else if (grade >= 80) { printf (“B\n"); } else if (grade >= 90) { printf (“A\n"); } else { printf ("F\n"); } Order Matters!!

53 True or False In C, every expression has a numeric value
An expression is ‘true’ when its value is non-zero If it is zero, it is false Therefore, in the following – if (expression) statement statement is executed if expression evaluates to non-zero.

54 More about operators In C, every expression has a numeric value
When using arithmetic operators (+, -, *, /) this is straightforward The value of A+B is the sum of A and B And so on…

55 More about operators Expressions with relational operators (<, <=, >, >=, etc.) have values as well (intuitively, we are used to thinking about them as ‘true’ or ‘false’) A < B evaluates to zero if A is larger than or equal to B, and some non-zero value if A is smaller than B The exact non-zero value varies (and is not important for that matter)

56 Relational operators They are:
A == B (Note the difference from A = B!!!!!) A != B A < B A > B A <= B A >= B The value of the expression is non-zero if it’s true, zero if it’s false

57 Example – val.c int a, b; printf("Enter two numbers\n");
scanf("%d%d", &a, &b); if (a == b) { printf("The numbers equal %d\n", a); printf("The expression a == b is %d\n", a == b); } else printf("The numbers are not equal\n");

58 The assignment operator =
The assignment operator is also an operator. Hence, expressions involving it have a numeric value. This value equals to whatever appears on the right of the assignment operator For example: (x = 4) evaluates to 4 (y = 0) evaluates to 0

59 A very common mistake Very often a programmer might confuse between the equality operator and the assignment operator: if (x==4) … if (x=4) … The second is usually a mistake, but legal in C so the compiler doesn’t warn us about it!

60 Example int main() { int i = 2; printf("i = %d\n", i);
}

61 Example int main() { int i = 2; printf("i = %d\n", i);
} i = 2

62 Example int main() { int i = 2; printf("i = %d\n", i);
} (i==4) = 0

63 Example int main() { int i = 2; printf("i = %d\n", i);
} i = 2

64 Example int main() { int i = 2; printf("i = %d\n", i);
} (i=4) = 4

65 Example int main() { int i = 2; printf("i = %d\n", i);
} i = 4

66 Rule of Thumb When comparing to a constant it is better to write the constant first how does this help us? (4 == i) is the same as (i == 4) BUT (4 = i) is a compilation error


Download ppt "C Programming Variables."

Similar presentations


Ads by Google