1 C Programming Week 2 Variables, flow control and the Debugger.

Slides:



Advertisements
Similar presentations
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Advertisements

For loops For loops are controlled by a counter variable. for( c =init_value;c
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Programming The Development Environment and Your First C Program.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
1 Software John Sum Institute of Technology Management National Chung Hsing University.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Lecture2.
EKT120 COMPUTER PROGRAMMING
Chapter 2 - Introduction to C Programming
Input/output.
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Formatted Input/Output
Looping.
Chapter 2 - Introduction to C Programming
Software John Sum Institute of Technology Management
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
C Programming Variables.
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Lecture3.
Week 2 Variables, flow control and the Debugger
Formatted Input/Output
Introduction to C Topics Compilation Using the gcc Compiler
Formatted Input/Output
EECE.2160 ECE Application Programming
Chapter 2 - Introduction to C Programming
EECE.2160 ECE Application Programming
Introduction to C Programming
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

1 C Programming Week 2 Variables, flow control and the Debugger

Example 1: ‘scanf’ use #include int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); printf("Enter an integer\n"); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; }

#include int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); printf("Enter an integer\n"); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Example 1: Summing user variables We read the first variable from the user (5) We read the second value from the user (7) Output: Answer: = 12 Output: Answer: = 12

#include int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); printf("Enter an integer\n"); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Example 1: Summing user variables ??? bsuma ??? Memory

#include int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); printf("Enter an integer\n"); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Example 1: Summing user variables ??? bsuma ??? Memory

#include int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); printf("Enter an integer\n"); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Example 1: Summing user variables 5 bsuma ??? Memory

#include int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); printf("Enter an integer\n"); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Example 1: Summing user variables 5 bsuma 7??? Memory

#include int main() { int a, b, sum; printf("Enter an integer\n"); scanf("%d", &a); printf("Enter an integer\n"); scanf("%d", &b); sum = a + b; printf("Answer: %d + %d = %d\n", a, b, sum); return 0; } Example 1: Summing user variables 5 bsuma 712 Memory

The & sign refers to the memory address of the variable – where it should be stored. scanf( "%d %lf", student_num, average); Why we need & in scanf? 17 student_num address: average address: 7510 Error!! Will store in location 17 and 85 Error!! Will store in location 17 and 85

The & sign refers to the memory address of the variable – where it should be stored. scanf( "%d %lf", &student_num, &average); Why we need & in scanf? 17 student_num address: average address: 7510 OK!! Will store in location 1760 and 7510 OK!! Will store in location 1760 and 7510

scanf When reading numbers: The computer expects white space between numbers White space: space, tab, new line. scanf ignores the white space Numbers separated with white space scanf("%d%d",&i,&j); Numbers separated with white space scanf("%d %d",&i,&j); 12,34 Numbers separated with “,” scanf("%d, %d",&i,&j); Numbers separated with “+” scanf("%d + %d",&i,&j);

scanf When reading characters: White spaces are considered as part of the input. abConsecutive characters scanf("%c%c",&i,&j); a b Characters separated with space scanf("%c %c",&i,&j);

We first perform the mathematical operation with the variable (=) and then evaluate it (++). We first evaluate the variable (++) and then perform the mathematical operation with it (=). Shorter lines e = e + 1; e++;  f = f + 1; ++f;  j=5 i=6 j=5 i=6 i = 5; j = i++; i = 5; j = i++; i=j=6 i = 5; j = ++i; i = 5; j = ++i;

We first perform the mathematical operation with the variable (=) and then evaluate it (--). We first evaluate the variable (--) and then perform the mathematical operation with it (=). Shorter lines e = e - 1; e--;  f = f - 1; --f;  j=5 i=4 j=5 i=4 i = 5; j = i--; i = 5; j = i--; i=j=4 i = 5; j = --i; i = 5; j = --i;

Exercise Write a program that gets from the user the amount of US $, the current exchange rate and converts it to NIS. 15

Solution Exercise_Exchange.c

17 Understanding Errors Error: error LNK2019: unresolved external symbol referenced in function _WinMainCRTStartup Problem: You did not define your project as a console application Solution: Start over, create a new project, this time remember to define it as a console application

18 Understanding Errors Error: fatal error C1083: Cannot open include file: 'stdioh': No such file or directory Problem: The file you’re trying to include does not exist Solution: Make sure the file name is spelled correctly

19 Understanding Errors Error: warning C4013: 'print' undefined; assuming extern returning int Problem: The command you’re trying to use does not exist Solution: Make sure you spelled the command’s name correctly (upper/lower case, omitting letters etc.)

20 Understanding Errors Error: error C2146: syntax error : missing ';' before identifier 'print' Problem: Hmm..., missing ‘;’? Solution: What are you waiting for, add it!

scanf warning In visual 2008 when you use scanf you might get the following warning: “warning C4996: ‘scanf’ was declared deprecated …” You can disregard the warning.

22 The Debugger Some programs may compile correctly, yet not produce the desirable results These programs are valid and correct C programs, yet not the programs we meant to write! The debugger can be used to follow the program step by step and may help detecting bugs in an already compiled program

23 Example (for debugging) A program that sums the digits of a number with three digits. For example: The input 369 yields the output 18

24 #include int main() { int sum = 0, num; /* Read a 3-digit number from the user */ printf("Enter 3-digits number\n"); scanf("%d", &num); sum = sum + num % 10; num = num / 10; sum = sum + num % 10; num = num / 10; sum = sum + num % 10; /* Print the result */ printf("The sum of the digits is %d\n", sum); return 0; } Example 2: Sum Digits

25 Exercise Copy the above program Run in the debugger and see how it works

26 Insert Breakpoint

27 Start Debugging (F5)

28 Debugger

29 Step Over (F10)

30 Other Commands

31 Update Variables

Example 3 Write a program that gets from the user a number and returns its absolute value 32

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

Example 3 - Absolute Value /* The absolute value of a number */ int main() { double num = 0.0; printf("Please enter a real number: "); scanf("%lf", &num); if (num < 0) num = -num; printf("The absolute value is %g\n", num); return 0; } What happens if we change %lf to %g?

From %lf to %g 35

Example 4 Write a program that gets from the user a grade of a student in an exam and prints the grade, adding if the student passed or failed the exam. If the grade is 60 or above – the student passed the exam. 36

Example 4 – student grade // The grade of a student – did he pass the exam? int main() { int grade = 0; printf("Please enter a the grade of the student: "); scanf("%d", &grade); if (grade >=60) printf ("The grade is %d, the student passed the exam\n",grade); else printf ("The grade is %d, the student failed the exam\n",grade); return 0; }

Example 5 Write a program that is given a grade and translates it into A-F grade Conversion: : A 80-89: B 70-79: C 60-69: D 0-59: F 38

Example 5 – grade conversion // Converting a grade from to A-F int main() { int grade = 0; printf("Please enter a the grade of the student: "); scanf("%d", &grade); printf ("The grade is "); 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"); } return 0; }