Introduction to the C programming language

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Programming Languages and Paradigms The C Programming Language.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
CS Winter 2011 Further Introduction to the C programming language.
CS Winter 2011 Introduction to the C programming language.
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
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
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.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
C++ Functions A bit of review (things we’ve covered so far)
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Pointer to an Object Can define a pointer to an object:
Structures and Classes
Procedural and Object-Oriented Programming
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Abstract Data Types and Encapsulation Concepts
Introduction to C++ Systems Programming.
Motivation and Overview
Java Primer 1: Types, Classes and Operators
C Programming Tutorial – Part I
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
C Basics.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Lecture 6 C++ Programming
Pointers and References
Introduction to the C Language
Introduction to Classes
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Abstract Data Types and Encapsulation Concepts
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Built-In (a.k.a. Native) Types in C++
Chapter 9 Objects and Classes
Dr. Bhargavi Dept of CS CHRIST
Programming in C Miscellaneous Topics.
Classes, Constructors, etc., in C++
Writing Functions.
Programming in C Miscellaneous Topics.
Java Programming Language
Defining Classes and Methods
Programming Languages and Paradigms
Chapter 9: Pointers and String
Pointers and dynamic objects
Functions Reasons Concepts Passing arguments to a function
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Introduction to Classes and Objects
Presentation transcript:

Introduction to the C programming language CS 261 - Fall 2009 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.

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]; };

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. A pointer A value 42

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 = 3.14159; p = & e; *p = 2.718281; printf(“values: %d %g %g %g\n”, p, *p, d, e); d p &d 3.14159

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 and by-reference parameters A reference parameter can be simulated by passing a pointer: void foo (double * p) { *p = 3.14159; } double d = 2.718281; foo( & d); printf(“what is d now? %g\n”, d);

Structures and by-ref params Very common idiom: struct vector vec; /* note, value, not ptr*/ vectorInit (&vec); /* pass by ref */ vectorAdd (&vec, 3.14159);

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

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.h> … /* 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);

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