Imperative Programming C. Imperative Programming Heart of program is assignment statements Aware that memory contains instructions and data values Commands:

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Informática II Prof. Dr. Gustavo Patiño MJ
CS414 C Programming Tutorial Ben Atkin
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Arrays and Pointers in C Alan L. Cox
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Class 2 CSI2172
Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C is a high level language (HLL)
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Object Oriented Programming Lecture 2: BallWorld.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
C++, OBJECT ORIENTED PROGRAMMING
C Basics.
Programming Languages and Paradigms
An Introduction to Java – Part I, language basics
Pointers, Dynamic Data, and Reference Types
Variables Title slide variables.
Outline Defining and using Pointers Operations on pointers
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Imperative Programming C

Imperative Programming Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations, loops, conditionals, procedural abstraction Commands make a flowchart

Imperative Languages FORTRAN COBOL BASIC Pascal C

Pascal Example program Loops; var i: Integer; begin for i := 1 to 10 do begin Writeln('Hello'); Writeln('This is loop ',i); end; end.

C Example /*Hello, world” */ #include main() { printf(“Howdy, earth! \n”); return 0; }

C Printing Print strings directly Other types must be converted, because they are really numbers: char c = ‘A’; printf(“Print char c: %c.\n”,c); int n = 6; printf(“Print int n: %d.\n”,n);

Increment/Decrement (P.S. Same in Java) int x,y,z; x=y=z=1; y = x++; x=1; z = ++x; printf("x++ gives %d\n",y); printf("++x gives %d\n",z);

C-isms 0 is false, 1 is true (or non-zero) Same logical operators, &&, ||, ! But there are also bitwise operators &, |, ~ Other bitwise operators: ^, >>, << x ? y : z means “if x, y, else z” (Same as Java) char c = x > 0? ‘T’ : ‘F’

Memory Addresses (C) Pointers are addresses of where values are stored in memory. &variable means “address of variable” What happens? int a = 13; printf("a is %d\n",a); printf("&a is %p\n",&a);

Pointers in C int* ptr; -> ptr is a pointer to an int (with no pointee!) What does it do? int *ptr; int num = 4; ptr = &num;

More Pointer Fun in C int *ptr; int num = 4; ptr = &num; *ptr = 5;

Pointers in Pascal program Pointers; var p: ^integer; begin new(p); p^ := 3; writeln(p^); dispose(p); end.

C Arrays int arInt[5]; int arInt2[5] = {1,4,3,2,1}; int ar[2][3] = {{1,2,3},{4,5,6}};

C Strings char str[] = “I like C.”; char *ptrStr = “I love pointers.”; Last character in String is ‘\0’

C Strings – Example: Compute length of string int myStrLen(const char* s) { int count = 0; while (*s != ‘\0’) // while(*s){ { count++; s++; } return count; }

Preprocessor Directives #define – substitute before compiling: #define MAX 1000 if(x > MAX)…

C Scope Modifiers static: don’t delete from memory - permanent duration register: suggest use of register for variable. Can’t ask for address of the variable const: make a variable constant. When pointers are const, their “pointees” are constant

Memory malloc: allocates memory. Must give number of bytes wanted. malloc function returns a pointer to the memory. (Null ptr if memory error). free: frees memory. char *ptr; char str[] = “String!”; ptr = malloc(sizeof(char)*7); free(ptr);

Defining new types Struct (record in other langs) typedef struct{ char* word; int frequency; int size; } wordInfo; wordInfo wordOne; wordOne.word = “apple”; wordOne.frequency = 0; wordOne.size = 0;

Pointers to Structs We often use pointers to structs (do you know why? wordInfo *wiPtr; Can dereference explicitly: wordInfo wi = *wiPtr; Or shortcut straight to the fields: int f = wiPtr->frequency;

What C Lacks Iterators Exception Handling Overloading Generics