Lecture 3: C Programm

Similar documents
Lecture 2: C Programm

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

C'Programming' data'separate'from'methods/functions' Low'memory'overhead'compared'to'Java'

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Class Information ANNOUCEMENTS

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lectures 5-6: Introduction to C

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

In Java we have the keyword null, which is the value of an uninitialized reference type

CSC 1600 Memory Layout for Unix Processes"

CS240: Programming in C

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Programming Studio #9 ECE 190

EL2310 Scientific Programming

A Fast Review of C Essentials Part I

CS Programming In C

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

CS240: Programming in C

Technical Questions. Q 1) What are the key features in C programming language?

Lecture 3: Instruction Set Architecture

Introduction to Programming Using Java (98-388)

Memory Allocation in C

CS-201 Introduction to Programming with Java

CS 61C: Great Ideas in Computer Architecture Introduction to C

BLM2031 Structured Programming. Zeyneb KURT


Compiling and Running a C Program in Unix

Pace University. Fundamental Concepts of CS121 1

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Functions. Systems Programming Concepts

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Introduction to C++ with content from

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

OBJECTIVE QUESTIONS: Choose the correct alternative:

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Lectures 5-6: Introduction to C

Ch. 3: The C in C++ - Continued -

Functions:"" Breaking"large"computing"tasks"to" smaller"ones"

Full file at

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Final CSE 131B Spring 2004

Compiling Techniques

Computers Programming Course 5. Iulian Năstac

TYPES, VALUES AND DECLARATIONS


Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CS201 Some Important Definitions

PRINCIPLES OF OPERATING SYSTEMS

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Programming in C. What is C?... What is C?

Fundamentals of Programming

University of Kelaniya Sri Lanka

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Programming Languages

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

C Programming Review CSC 4320/6320

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

EC 413 Computer Organization

Short Notes of CS201

CS C Primer. Tyler Szepesi. January 16, 2013

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

CSE 230 Intermediate Programming in C and C++

CS558 Programming Languages

Informatica e Sistemi in Tempo Reale

EMBEDDED SYSTEMS PROGRAMMING Language Basics

M.EC201 Programming language

CS201 - Introduction to Programming Glossary By

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

a data type is Types

Java Primer 1: Types, Classes and Operators

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

Fundamental of Programming (C)

Introduction to C Language (M3-R )

ECEN 449 Microprocessor System Design. Review of C Programming

Programming in C and C++

Arrays and Pointers (part 1)

CS113: Lecture 4. Topics: Functions. Function Activation Records

CSE 333 Lecture 2 Memory

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Work relative to other classes

Flow Control. CSC215 Lecture

Run-time Environments - 3

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Programming refresher and intro to C programming

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

DECLARAING AND INITIALIZING POINTERS

Transcription:

0 3 E CS 1 Lecture 3: C Programm ing

Reading Quiz Note the intimidating red border! 2

A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory that is reserved at compile time to hold a value of particular type C. an area in memory that is reserved when the variable is declared and assigned an initial value of zero 3

A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory that is reserved at compile time to hold a value of particular type C. an area in memory that is reserved when the variable is declared and assigned an initial value of zero 4

In C, a variable is declared by A. using it in an arithmetic expression B. preceding it with one or more keywords to indicate its data type, e.g. char c; C. initializing it to a value 5

In C, a variable is declared by A. using it in an arithmetic expression B. preceding it with one or more keywords to indicate its data type, e.g. char c; C. initializing it to a value 6

A function is: A. A block of code that can be executed by invoking its name B. any block of code within curly braces- {} C. A block of code that can take multiple inputs as arguments and return multiple outputs 7

A function is: A. A block of code that can be executed by invoking its name B. any block of code within curly braces- {} C. A block of code that can take multiple inputs as arguments and return multiple outputs 8

int foo (char, int ); A. is a call to execute the function foo B. Is a declaration of the function foo which takes two inputs and returns an int value C. Is an incorrect declaration because the keywords char and int are not followed by variable names 9

int foo (char, int ); A. is a call to execute the function foo B. Is a declaration of the function foo which takes two inputs and returns an int value C. Is an incorrect declaration because the keywords char and int are not followed by variable names 10

Variables declared within a function A. can be used by other functions B. can only be used within the function C. continue to exist in memory as long as the program executes 11

Variables declared within a function A. can be used by other functions B. can only be used within the function C. continue to exist in memory as long as the program executes 12

An arithmetic expression (e.g. 2*a+6) A. Always evaluates to TRUE B. Evaluates to TRUE if the result of the expression is non zero, (in this case if the variable a is not equal to - 3) C. Evaluates to a number which is neither TRUE or FALSE 13

An arithmetic expression (e.g. 2*a+6) A. Always evaluates to TRUE B. Evaluates to TRUE if the result of the expression is non zero, (in this case if the variable a is not equal to - 3) C. Evaluates to a number which is neither TRUE or FALSE 14

What does a variable s data- type specify? A. The size of the variable and the interpretation of it value(s) when accessed B. How long the variable persists in memory C. Whether the variable stores integral or character values 15

What does a variable s data- type specify? A. The size of the variable and the interpretation of it value(s) when accessed B. How long the variable persists in memory C. Whether the variable stores integral or character values 16

# include <stdio.h> main() { A simple C program int a=30; printf( CSE %d is fun!!,a); /* Printing a statement */ } Basics of a C program: Program execution begins with main() Each statement terminates with a semi colon Libraries used by including a header _ile Comments are added using : /* */ 17

C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes Relatively fast Heap memory management manual Pointers to manipulate shared data 18

Key C concepts allow us to: Manipulate data- objects Control the _low of program execution Break large computing tasks into smaller ones 19

Manipulating data objects in C 20

Different types of data Data differ in a number of ways: q Constant vs. variable q Basic vs. structured (derived) q Data type: int, _loat, char... Data in a C program also differs based on scope and lifetime (more later) 21

How we manipulate variables depends on their data- type Data- types Basic: int, char, _loat integer data types used in arithmetic operations but not char data types Derived: Pointers Arrays Structures enumerations Derived types are accessed and manipulated differently from 22 basic types

Basic data object in memory A region in memory that contains a value and is associated with a name/identi_ier Address Attributes of a Data- object/variable: - Name/identi_ier - Value - Address: Location in memory - Size - A unique data- type - Lifetime - Scope 102 num name Value 20 4 bytes Size 23

What attributes of a variable are speci_ied in a declaration statement? A. Name, value, location, data type, lifetime, scope B. Name, data- type, value and scope C. Name, data- type, scope ; its value may or may not be speci_ied D. Name, data- type; its value, lifetime and scope may or may not be speci_ied 24

What attributes of a variable are speci_ied in a declaration statement? A. Name, value, location, data type, lifetime, scope B. Name, data- type, value and scope C. Name, data- type, scope ; its value may or may not be speci_ied D. Name, data- type; its value, lifetime and scope may or may not be speci_ied 25

Declarations and de_initions char c= a ; /* 1 byte */ short s; /* 2 bytes */ int a; /* usually 4 bytes - signed */ unsigned int a=0; /* usually 4 bytes*/ float f; /* usually 4 bytes use sizeof(_loat)*/ Scope and lifetime are often implicit but sometimes we have to use speci_ic keywords: static int a=0; /*De_ines lifetime*/ extern int a; /*Extends scope to multiple _iles*/ 26

Which of the following statements is true after a variable is declared? A. We can access and change both its value and location B. We can access and change only its value C. We can access its value and location but can only change its value 27

Which of the following statements is true after a variable is declared? A. We can access and change both its value and location B. We can access and change only its value C. We can access its value and location but can only change its value 28

Accessing value and location To access/change the value of a basic type: Use the variable name Is there another way? 102 y=x; x=10; y= x>y? x : y; To access the location/address, use the address operator & &x (is 102) 120 y x 20 55 29

Type casting

Promotion and truncation Often arithmetic expressions contain mixed integral types Promotion: In an expression all variables are promoted to the data type of the largest size char c=4; int i= c+ 10; /* c is promoted to an int */ /*What is the value in i? */ Truncation: If the value of a larger data type is assigned to a smaller one, the compiler drops the most signi_icant bits short i= 0x0104; char c =i; /*The value in i is truncated*/ /* c =0x04 Least signi_icant byte is assigned */

Type casting If we want the variables in an expression to be interpreted differently, use casting Example: int i=10; _loat f = i/3; In this case, f=3.0 If we don t want the result of the division to get trunctated to an int, use explicit casting int i=10; _loat f = (_loat)i/3; /* Explicit cast of int to _loat */

Functions: Breaking large computing tasks to smaller ones 33

C Programming Procedural thought process main () /* High level Outline */ {... get_input(arg1) /*Comment: Step 1 */ perform_step_2(arg2); perform_step_3(); store_result(); /* Print output or store in a file */ } 34

Overview of Functions Functions make code easy to Maintain Debug Reuse 35

Functions Overview void swap(int x, int y); //Declaration void swap(int x, int y) { int tmp; tmp= x; x= y; y= tmp; } tmp x swap y When swap is called 3 local variables are created: x, y, tmp the value in x and y are interchanged x y 36

Functions: Call by value main() {... 5 6 x swap(a, b); a b.... } swap y Q: Are the value of variables a and b interchanged after swap is called? A. Yes, because that s what is implemented by the swap routine B. No, because the inputs to swap are only copies of a and b 37

Functions: Call by value main() {... 5 6 x swap(a, b); a b.... } swap y Q: Are the value of variables a and b interchanged after swap is called? A. Yes, because that s what is implemented by the swap routine B. No, because the inputs to swap are only copies of a and b 38

The C runtime environment 39

What does gcc do? % gcc hello.c 40

What does gcc do? % gcc hello.c Command prompt (in linux/unix systems) 41

What does gcc do? % gcc hello.c Input to gcc: C program 42

What does gcc do? % gcc hello.c hello.c gcc a.out 43

What does gcc do? % gcc hello.c Source Program in C hello.c gcc a.out #include <stdio.h> void func1(int a, char *b) { if(a > 0) { *b = a ; } } int main() {.. func1(); printf( \abc ); } 44

What does gcc do? % gcc hello.c Source Program in C Executable : Equivalent program in machine language hello.c #include <stdio.h> void func1(int a, char *b) { if(a > 0) { *b = a ; } } int main() {.. func1(); printf( \abc ); } gcc a.out 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111! 45

What does gcc do? % gcc hello.c %./a.out (executable loaded in memory and processed) Also referred to as running the C program gcc hello.c Source : Program in C a.out Executable : Equivalent program in machine language 46

How is other code included? Include Header files (.h) that contain function declarations - the function interface The corresponding.c files contain the actual code hello.h hello.c void func1(int, char *); int func2(char *, char *); #include <stdio.h> void func1(int a, char *b) { if(a > 0) { *b = a ; } } int main() {.. func1(); printf( \abc ); } 47

What does the executable contain? Instructions or code Data What is data? Any information that instructions operate on (objects in memory) 48

C Runtime Environment Code (instructions in machine language) Data (initialized and uninitialized - static allocated) Both code and data don t change in size Heap (for dynamically allocated data) Stack (for function local variables) Heap and stack change in size as the program executes 49

C Runtime Environment Code Initialized Data Uninitialized Data Heap Stack 50

Different types of data What factor differentiates the following three categories of data in a C program: Global and static variables Local variables Dynamic variables A. Scope B. Lifetime C. Representation 51

Different types of data What factor differentiates the following three categories of data in a C program: Global and static variables Local variables Dynamic variables A. Scope B. Lifetime C. Representation 52

Data differing in lifetime Lifetime: The interval between time of creation and end of existence 53

Possible lifetimes of data 1. Lifetime = Execution time of program q Initialized or uninitialized q Must be indicated in the executable q Space is allocated with start of execution (static allocation) How much space indicated in executable 54

Possible lifetimes of data 1. Lifetime = Execution time of program 2. Lifetime = Time between explicit creation and explicit deletion q Programmer dynamically allocates memory q In C new data created when malloc( ) is called and destroyed when free( ) is called q Space in memory created during program execution (Heap allocation) How much data will be created on the heap is not known ahead of time This type of data cannot be indicated in the executable ahead of time 55

Possible lifetimes of data 1. Lifetime = Execution time of program 2. Lifetime = Time between explicit creation and explicit deletion 3. Lifetime = Execution time of a function (time between function call and function return) q Local variables of functions, parameters of function q Space is allocated when the function execution starts and is reclaimed on return from function (stack allocation) q Stack: data structure like a pile of books q Last in First Out : Adding on top (push), removing from top (pop) 56

Stack Allocation: Function local variables and parameters When program execution starts Local variables of func() Other information about func() Local variables of main() What if main calls the function func()? Stack Pointer 57

Stack Allocation: Function local variables and parameters Variables whose lifetime is the execution time of function are managed using the stack structure Local variables of main() Stack Pointer 58