CS 261 - Winter 2011 Introduction to the C programming language.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Introduction to C Programming
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Programming Languages and Paradigms The C Programming Language.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Kernighan/Ritchie: Kelley/Pohl:
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Road Map Introduction to object oriented programming. Classes
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
CS Winter 2011 Further Introduction to the C programming language.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CS-2303 System Programming Concepts
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
4.1 Instance Variables, Constructors, and Methods.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS 261 – Data Structures Introduction to C Programming.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
C++ Functions A bit of review (things we’ve covered so far)
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
User-Written Functions
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
Introduction to the C programming language
C Programming Tutorial – Part I
C Basics.
Lecture 6 C++ Programming
Pointers and References
Built-In (a.k.a. Native) Types in C++
Dr. Bhargavi Dept of CS CHRIST
Java Programming Language
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Introduction to Classes and Objects
Presentation transcript:

CS Winter 2011 Introduction to the C programming language

Why C ? C is simple language, makes it easier to focus on important concepts Java is high level language, C is low level, important you learn both Lays groundwork for many other langauges

Comparing Java and C No classes, inheritance, or polymorphism Functions are the major mechanism for encapsulation Arrays and structures are the major data storage mechanisms Pointers!! Lots of pointers.

Function definitions Functions look a lot like methods you are used to in Java, but are not part of a class return-type function-name (arguments) { declarations; /* must come first!! */ function-body; }

Two level scope There are two levels of scope Variables declared outside of any function are global (use sparingly) Variables declared inside of function are local. Declarations must be listed first, before statements. You end up passing more inforation in arguments than you do in Java

Parameters are by-value Arguments to functions are passed by value Means the parameter is initialized with a COPY of the argument Will simulate by-reference using pointers

Structures Structures are like classes that have only public data fields and no methods: struct arrayBag { int count; EleType data[100]; };

Declaring a structure When you declare a variable you must use the struct keyword struct arrayBag foo; No constructors, no initialization, you must use explicit initialization routine

Access to data fields Access to data fields uses the same dot notation you are used to: struct arrayBag myData; myData.count = 3; (but often combined with pointers…)

Pointers Pointers in C are explicit (implicit in Java) A pointer is simply a value that can refer to another location in memory. 42 A valueA pointer

Pointer values vs. thing pointed to Important to distinguish between the value of the pointer and the value of the thing the pointer points to 42

Some syntax Use * to declare a pointer, also to get value of pointer. Use & to get address of a variable (address == pointer) double * p; double d, e; p = & d; *p = ; p = & e; *p = ; printf(“values: %d %g %g %g\n”, p, *p, d, e); &d p d

Pointers and Structures Pointers often combined with structures. Introduce some new syntax struct arrayBag * p; struct arrayBag g1; p = & g; p->count = 1; /* same as (*p).count */ p = 0; /* null pointer, doesn’t point to anything */

Pointers vs object pointed to When you declare a variable it is not initialized, true for both pointers and structures Always be clear whether you are talking about a pointer, or thing pointed to 90% of your programming errors will come from uninitialized pointers or values

Structures and arguments Remember, arguments are passed by value If you pass a structure, the ENTIRE structure is copied into the space for the actual argument value Wasteful, therefore we use pointers are arguments

Pointers and by-reference parameters A reference parameter can be simulated by passing a pointer: void foo (double * p) { *p = ; } double d = ; foo( & d); printf(“what is d now? %g\n”, d);

Structures and by-ref params Very common idiom: struct arrayBag a; /* note, value, not ptr*/ arrayBagInit (&a); /* pass by ref */ arrayBagAdd (&a, );

Arrays aways passed by ref void foo(double * d) { d[0] = ; } double data[4]; data[0] = 42; foo(data); /* note - NO ampresand */ printf(“what is data[0]? %g”, data[0]);

Notice, array has become a pointer Look carefully at the previous slide Notice the value was an array when it was passed as an argument But inside the function, we declared it as a pointer Pointers can be subscripted, just like arrays

Dynamic Memory No new operator. Use malloc(#bytes) instead. Use sizeof to figure how big something is struct gate * p = (struct arrayBag *) malloc(sizeof(struct arrayBag)); assert (p != 0); /* always a good idea */

Assert check conditions We will use assert to check all sorts of conditions. Halts program if condition not found. # include … /* assert checks condition is true */ assert(whatever-condition);

BTW, no boolean BTW, there is no boolean data type. Can use ordinary integer, test is zero or not zero. Can also use pointers, test is null or not null. int i; if (i != 0) if (i) /* same thing */ double * p; if (p != 0) if (p) /* same thing */

Separation of interface and implementation Interface files (*.h) have declarations and function prototypes Implementation files (*.c) have implementations prototype is function header but no body: int max(int a, int b);

Will often have interface given We will often give you the interface file, and you write the implementation

Preprocessor A Preprocessor scans C programs before they are compiled. Used to make symbolic constants, conditionally include code # define max 423 # if (max < 3) … # endif

Ensuring declarations seen only one /* interface file for foo.h */ # ifndef BigComplexName # define BigComplexName … # endif /* that way, 2nd time does nothing */

First Assignment Good news on first assignment - it doesn’t have any pointers, only arrays Major problem should be just figuring out how to compile a C program Pointers will start to show up with second assignment and beyond.

Will see more as we go along After class, Take a look at programming assignments 1 and 2 In some place (assignment 2 and later) I will give you an interface file, hand in only implementation file (whatever.c) (Note: there is no interface file needed for first assignment). Questions?

Now, review of Big Oh Now, a review of Big-Oh