1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Programming Languages and Paradigms
INSTRUCTION SET ARCHITECTURES
1 C++ Syntax and Semantics The Development Process.
Homework Starting Chapter 2 K&R. Read ahead. Questions?
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Structure of a C program
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Run-time Environment and Program Organization
CS150 Introduction to Computer Science 1
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Runtime Environments Compiler Construction Chapter 7.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CPS120: Introduction to Computer Science
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Introduction to Programming
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Copyright Curt Hill Variables What are they? Why do we need them?
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Starting Chapter 2 K&R. Read ahead.. Call by Value vs Call by Reference Simple variables passed as function parameters in C are actually only copies on.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
CPS120: Introduction to Computer Science Variables and Constants.
Variables and memory addresses
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Java Programming Language Lecture27- An Introduction.
Computer Organization and Design Pointers, Arrays and Strings in C
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User-Written Functions
User Interaction and Variables
Chap. 2. Types, Operators, and Expressions
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Revision Lecture
CMSC 104, Section 4 Richard Chang
Introduction to the C Language
פרטים נוספים בסילבוס של הקורס
Homework Starting K&R Chapter 5 Good tutorial on pointers
Homework Starting Chapter 2 K&R. Read ahead. Questions?
The C Language: Intro.
C Language B. DHIVYA 17PCA140 II MCA.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Functions that return a value
Presentation transcript:

1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get a head start. Questions?

2 Call by Value vs Call by Reference Simple variables passed as function parameters in C are actually only copies on the stack. (Note: Stack pointer is a register) foo(i, j); void foo(int i, int j) { } Stack Pointer Before call and after return Unused Stack Pointer After call and before return Unusedij Return Data Stack Data Stack Data Copy of Value

3 This is known as Call by Value. Unlike some languages, which have call by reference You can't change arguments in original location within the function -- just change the stack copy To make changes, you must pass pointers to original variables. See next slide. You’re not responsible for pointers yet, but start getting used to them! Call by Value vs Call by Reference

4 Pointers as Arguments Pointer variables passed as function parameters in C are still only value on the stack but can be used to access original location indirectly foo(&i, &j); void foo(int *i, int *j) { } Stack Pointer Before call and after return Unused Stack Pointer After call and before return Unused &i&j Return Data Stack Data Stack Data Point to values

5 Pointers as Arguments The following doesn’t work!!! void exchgint (int a, int b) { int dummy; dummy = a; a = b; b = dummy; }

6 Pointers as Arguments Must be done with pointers!!! void exchgint (int *pa, int *pb) { int dummy; dummy = *pa; *pa = *pb; *pb = dummy; }

7 Pointers as Arguments An array name is automatically passed as a pointer You don't need to create a pointer yourself with the “address of” operator (&) int array1[256], array2[256]; foo(array1, array2); Unless you are passing a pointer to a specific array element (Remember printf in visitype?) foo(&array1[4*i], &array2[4*j]);

8 Scope of Variables – Local Automatic Each local variable of a function (inside { }) comes into existence only when it is called disappears when a return is performed. Local variables are said to be automatic. Memory is allocated on the stack after the calling sequence argument values Stack Pointer After local variables are allocated Stack Data Unusedij Return Data Local Variables

9 Scope of Variables – Local Static A static variable declared in a function is assigned a fixed location in memory that is NOT on the stack. Still local - can only be used inside function { } but its value is preserved between successive calls to the function E.g., the seed of a random number generator so it will have memory from one invocation to the next and not always give the same random number. int rand( ) { static int seed = 1; /* initialize to 1 and remember value between calls to rand */ }

10 Scope of Variables – Local Static Good for implementing a “Finite State Machine” –They allow a function to preserve its state value(s) But, be careful using local static variables!! –A function may behave differently when it is called with different values preserved in local static variables –Makes testing of a function more complex because you need to test its behavior with all possible values of any local static variables –Complex sequence of calls to function may be required to drive local static variables into desired test “state(s)”

11 Scope of Variables - External Alternative to local is known as external Look at maxline program on page 32 of K&R Variables declared ahead of/outside of all { }’s are available to all functions within the program These are sometimes called “global variables” Can be used to pass data between functions Values are preserved like static local variables Not a good idea – Why not?

12 Scope of Variables - External Global variables are not a good idea because: –They can be accessed from anywhere. If their value gets corrupted, it is NOT easy to figure out what code in which function caused the corruption. –They make the functions dependent on their external environment instead of being able to stand alone using arguments to get their input values and a return value to pass back an output value. Software architecture/design standards for most projects will prohibit use of “global variables” or severely restrict their use. Don’t use them!!

13 Scope of Variables – External Static Another alternative known as external static No good examples that I see in K&R Static variables declared ahead of/outside of all { }’s are available to all functions within this file only These are very similar to OOP “class variables” Can be used to pass data between functions in file only Values are preserved like static local variables These are more acceptable than global variables

14 Data Types Finished K&R Chapter 1 / Now starting Chapter 2 Variable Names Data Types: –char –int –short int –long int –float –double

15 Variable / Symbolic Constant Names Rules for generating names: –All letters and digits are allowed –Uppercase and lower case letters are different –First character must be a letter –Underscore _ counts as a letter (useful for improving readability) –But, don’t begin variable names with _ (reserved for library routines)

16 Variable / Symbolic Constant Names Rules for generating names: –Internal - at least 31 characters are significant –External - only count on 6 characters, single case –Avoid using C keywords (if, else, int, float, etc.) –Choose variable names that are related to the purpose or use of the variable

17 Data Types char (character) char has 8 bits (stored in one byte in memory) unsigned: 0 ≤ value ≤ ≤ value ≤ Overflow at 255( = 0) Underflow at 0(0 – 1 = 255) signed (if supported in the implementation): -2 7 ≤ value ≤ ≤ value ≤ Overflow at 127 ( = -128) Underflow at –128 (-128 – 1 = 127)

18 Data Types int (integer on our machines) int has 32 bits (stored in four sequential bytes in memory) unsigned: 0 ≤ value ≤ x ≤ value ≤ 0xffffffff Overflow at ( = 0) Underflow at 0(0 – 1 = ) signed: ≤ value ≤ x ≤ value ≤ 0x7fffffff Overflow at ( = – ) Underflow at – ( – 1 = )

19 Data Types short int (short integer on our machines) short int has 16 bits (stored in two sequential bytes in memory) unsigned: 0 ≤ value ≤ x0000 ≤ value ≤ 0xffff Overflow at 65535( = 0) Underflow at 0(0 – 1 = 65535) signed: ≤ value ≤ x8000 ≤ value ≤ 0x7fff Overflow at ( = –32768) Underflow at –32768 ( – 1 = 32767)

20 Data Types long int (long integer on our machines – same as int) long int has 32 bits (stored in four sequential bytes in memory) unsigned: 0 ≤ value ≤ x ≤ value ≤ 0xffffffff Overflow at ( = 0) Underflow at 0(0 – 1 = ) signed: ≤ value ≤ x ≤ value ≤ 0x7fffffff Overflow at ( = – ) Underflow at – ( – 1 = )

21 Data Types float, double –Both represent numbers with fractional parts –“double” is a “float” with more precision –Don't do much with float or double in this course In absence of a co-processor to do floating point computations, it takes a lot of compute time to do them in software. –Not often used in real time, embedded systems. –A cost versus performance tradeoff!