1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
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.
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 –
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Basic Input/Output and Variables Ethan Cerami New York
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Chapter 2: Using Data.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 C Programming Week 2 Variables, flow control and the Debugger.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Introduction to Programming
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Copyright © – Curt Hill Types What they do.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Computer Programming for Engineers
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture2.
User Interaction and Variables
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Computing Fundamentals
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Intro to C Tutorial 4: Arithmetic and Logical expressions
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.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
INPUT & OUTPUT scanf & printf.
פרטים נוספים בסילבוס של הקורס
C Programming Variables.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
Week 2 Variables, flow control and the Debugger
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

2 Why do We Need Variables? Computer programs manipulate data Data is given as input or calculated throughout the program To be later accessed, variables must be remembered Thus, variables are stored in the memory Variable name  memory address

3 What is a Variable? A memory chunk that is linked to a name Stores data (of a predefined type) Read/write Definition: type name Assignment: name = value Definition + assignment: type name = value

4 Why do we Need Different Types? Saving memory Execution speed Makes compiler “life” easier

5 Data representation Bit – a binary digit: 0 or 1 Bits are always grouped! Byte – a group of 8 bits Therefore, a byte can represent up to 2 8 =256 values The values can range from 0 to 255 or from -128 to 127 The fundamental data unit of a computer Word – a group of (usually) 4 (or 8) bytes 4 bytes = 32 bits Value range: 0 to (4,294,967,295 ) Or, more often: to (-2,147,483,648 to +2,147,483,647)

6 Data Types char – a single byte character Integer: short int (or short) – usually 2 bytes (rarely used) int - usually 4 bytes long int (or long) – 4 or 8 bytes (rarely used) Real: float – a single precision real number – usually 4 bytes double – a double precision real number – usually 8 bytes long double - a double precision real number – usually 8 bytes (rarely used) Signed vs. unsigned

7 Variable Naming Rules Letters, digits, underscores Legal: i, CSE_5a, a_very_long_name_that_isnt_very_useful, fahrenheit Illegal: 5a_CSE (first character cannot be a digit) my-var Case sensitive CSE_5a is different from cse_5a Variables names should have a meaning

8 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

9 Example: Swap Two Variables /* Swap1 – swaps two variables a,b using a temporary variable */ #include int main() { int a = 2, b = 5, temp; temp = a; a = b; b = temp; printf("a = %d, b = %d\n",a,b); return 0; }

10 Example: Swap Two Variables How to swap two variables without using a temporary variable?

11 Example: Swap Two Variables int a, b; // User is requested to enter a and b printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); a = a + b; // this way b is still "alive" b = a - b; // a + b - b = a a = a - b; // a + b - a = b printf("a = %d, b = %d\n", a, b);

12 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

13 printf and scanf printf – prints to the screen. Can also accept variables and print their values scanf – gets values from the standard input and assigns to corresponding variables

14 printf can Print Variable Values printf ("z=%d\n", z ); The sequence %d is a special sequence and is not printed It indicates to printf to print the value of an integer variable written after the printed string

15 scanf Gets Input from the User scanf("%lf", &var); Wait for the user to type in a double value, and store it in the variable ‘var’ ‘&var’ – “address” of var (we shall deal with it later on the course) To get 2 doubles from the user, use – scanf("%lf %lf", &var1, &var2);

16 prinft/scanf Conversion Codes A % in the printf/scanf string is replaced by a respective variable %c – a character %d – an integer, %u – an unsigned integer %f – a float %lf – a double %g – a nicer way to show a double (in printf) % - the ‘%’ character (in printf)

17 Example int a = 2, b = 3, c, e; double h; c = a * b; printf(“%d\n”,c); // 6 printf(“%d * %d = %d\n”,a,b,c); // 2 * 3 = 6 printf(“%d * %d = %d\n”,a,b,a*b); // 2 * 3 = 6 printf(“%d\n”,2*3); // 6 scanf(“%d %lf”, &e, &h);

18 What does the ‘&’ means? scanf("%d", &var); var – value of var &var – address of var Our intension is to insert the input to the var’s “cell” in memory What happens if we discard the ‘&’?

19 Agenda Variables (Review) Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

20 Arithmetic operators An operator is a mathematical action performed on constants and/or variables (operands) Some operators: Braces () Assignment = Addition + Subtraction - Multiplication * Division / Modulo %

21 int number, newNumber; int ones, tens, hundreds; printf("Please enter a 3-digit number "); scanf("%d", &number); // divide number to ones, tens, hundreds ones = number % 10; tens = (number%100)/10; hundreds = number/100; // calculate the new number newNumber = ones * tens * 10 + hundreds; printf("The reversed number is %d\n", newNumber); Example: Reverse a 3-digit Number

22 Operations on Different Types Operation on two different types (e.g., ) The result is of the generalized operand (e.g., int is a float is a double) 5/2.0  2.5 When the operands are of the same type, the result is of that type as well 5/2  2

23 Examples = = / 4 = / 4 = 0.75

24 Agenda Variables (Review) Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

25 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 Syntax: (type)operation/variable

26 Example – find what’s wrong #include 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; }

27 Alternatives #include int main() { int a = 10; int b = 20; printf(“Avg of %d and %d is %g\n", a, b, (a + b) * ((double)1 / 2)); printf(“Avg of %d and %d is %g\n", a, b, (a + b) * (1.0 / 2)); // will the following work? printf(“Avg of %d and %d is %g\n", a, b, (a + b) * (double)(1 / 2)); return 0; }

28 More Examples int a = 11; int b = 20; double x = 4.5; int y = x; // implicit cast printf("y = %g\n",y); // ERROR - do not relay on implicit cast in printf printf("y = %d\n",y); printf ("The average of %d and %d is %g\n", a, b, ((a+b) * (1.0/2))); printf ("The average of %d and %d is %d\n", a, b, (int)((a+b) * (1.0/2))); printf ("The average of %d and %d is %d\n", a,b, (a + b)*(1.0 / 2)); // ERROR printf ("The average of %d and %d is %d\n", a,b, (int)((a + b)*(1.0 / 2)));

29 Will this Work? #include 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; }

30 Summary on Implicit Cast (Q&A) int a = 4.5; // implicit cast from double (4.5) to int (4), it is legal but might cause problem, thus usually results with a compilation warning (possible lose of data) double x = 4; // implicit cast from int to double, no compilation warning since "int is a double" relation hold printf("%g\n", a); // printf/scanf is "dumb", in this case there is no compilation warnings, but the output will be wrong printf("%d \n", 2.5); // same here: no warnings, wrong output

31 Agenda Variables (Review) Input / Output Arithmetic Operations Casting Char as a Number (if time allow)

32 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).

33 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 The ASCII table.

34 More about character encoding most of the time, 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

35 More about character encoding ASCII code 0 (NULL character) is important – we will see it again Note contiguous sets of numbers, upper case and lower case characters

36 Example of char as both a character and a small number #include int main(void) { char i = 'b'; printf("i as a character is %c\n", i); printf("i as an integer is %d\n", i); printf("The character after %c is %c\n", i, i + 1); return 0; }

37 Another example /* Get the position of a letter in the abc */ #include int main(void) { 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; }

38 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’)

39 Solution /* Convert a letter to uppercase */ #include int main(void) { 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; }