The C Programming Language

Similar documents
How To Write Portable Programs In C

The C Programming Language course syllabus associate level

C++ Programming Language

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

1 Abstract Data Types Information Hiding

The programming language C. sws1 1

Illustration 1: Diagram of program function and data flow

Operating Systems. Privileged Instructions

CSC230 Getting Starting in C. Tyler Bletsch

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Lecture 22: C Programming 4 Embedded Systems

Comp151. Definitions & Declarations

COS 217: Introduction to Programming Systems

Sources: On the Web: Slides will be available on:

Example 1/24. Example

System Calls and Standard I/O

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

An Overview of Java. overview-1

/* File: blkcopy.c. size_t n

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

An Incomplete C++ Primer. University of Wyoming MA 5310

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Module 816. File Management in C. M. Campbell 1993 Deakin University

Binary storage of graphs and related data

ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Simple Image File Formats

MISRA-C:2012 Standards Model Summary for C / C++

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Programming languages C

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

C++ INTERVIEW QUESTIONS

El Dorado Union High School District Educational Services

UNIX, C, C++ History, Philosophy, Patterns & Influences on modern Software Development. Alexander Schatten November

Project 1: Implement a simple hosts framework using UNIX TCP Sockets to demonstrate the concept of Mobile Agents

Introduction to Java

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Informatica e Sistemi in Tempo Reale

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

SQLITE C/C++ TUTORIAL

Pemrograman Dasar. Basic Elements Of Java

Objective-C Tutorial

MatrixSSL Porting Guide

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Networks and Protocols Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Lecture Data Types and Types of a Language

public static void main(string[] args) { System.out.println("hello, world"); } }

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

System Calls Related to File Manipulation

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Chapter 13 - The Preprocessor

C++ Essentials. Sharam Hekmat PragSoft Corporation

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Moving from CS 61A Scheme to CS 61B Java

Object Oriented Software Design II

10CS35: Data Structures Using C

Java from a C perspective. Plan

Chapter One Introduction to Programming

Java programming for C/C++ developers

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Comparative Study of C, C++, C# and Java Programming Languages

C Interview Questions

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Specific Simple Network Management Tools

Simple C Programs. Goals for this Lecture. Help you learn about:

5 Arrays and Pointers

What is COM/DCOM. Distributed Object Systems 4 COM/DCOM. COM vs Corba 1. COM vs. Corba 2. Multiple inheritance vs multiple interfaces

C++ for Safety-Critical Systems. DI Günter Obiltschnig Applied Informatics Software Engineering GmbH

Embedded C Programming, Linux, and Vxworks. Synopsis

Stacks. Linear data structures

A SIMPLE WAY TO CAPTURE NETWORK TRAFFIC: THE WINDOWS PACKET CAPTURE (WINPCAP) ARCHITECTURE. Mihai Dorobanţu, M.Sc., Mihai L. Mocanu, Ph.D.

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Tutorial on C Language Programming

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Java CPD (I) Frans Coenen Department of Computer Science

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

MSP430 C/C++ CODE GENERATION TOOLS Compiler Version 3.2.X Parser Error/Warning/Remark List

Numerical Algorithms Group

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

MPLAB Harmony System Service Libraries Help

An Introduction to the C Programming Language and Software Design. Tim Bailey

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Introduction to Socket programming using C

C++ Language Tutorial

CP Lab 2: Writing programs for simple arithmetic problems

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

Chapter 13 Storage classes

C# and Other Languages

Computer Programming Tutorial

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Network Programming with Sockets. Anatomy of an Internet Connection

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Transcription:

The C Programming Language CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

The C Programming Language A high-level language for writing low-level programs Allows machine-independent systems programming Operators correspond to popular machine instructions Easy to provide efficient compilers

A Little Bit of History 1969-1972: Developed at AT&T Bell Labs by Dennis Ritchie Predecessor B, used word-based addressing. C used with development of Unix OS 1978: The C Programming Language book by Kernighan and Ritchie 1989: ANSI/ISO Standard C89 Function prototypes, void, enumerations,... 1999: ANSI/ISO Standard C99 Inline functions, complex numbers, variable length arrays,...

Comparison to Java No objects Structs, unions and enumerations instead. The three most important things in C: (1) pointers, (2) pointers, (3) pointers. Distinction between structure and pointer to structure. Pointer arithmetic. Array/pointer equivalence. Strings are arrays of small characters, null terminated. C pre-processor Setjmp/longjmp functions instead of try/throw. register, typedef, volatile, const Sizes of integers. Signed and unsigned arithmetic. Different IO Explicit memory management. Sizeof. Bit-fields.

Arithmetic There are several integer types providing different sizes of numbers and both signed and unsigned arithmetic. These are: char signed char unsigned char just a kind of integer short int unsigned short int int long int long long int unsigned int unsigned long int unsigned long long int If the word int is left out, e.g. unsigned short, then it is assumed. The sizes of these are not specified. It is merely required that sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long This allows integer sizes to match those provided by the particular machine. This illustrates C s philosophy of using flexibility to gain efficiency.

No Objects: Struct-s Instead struct s allow values to be grouped together, but provide no constructors or methods. struct farm_animal { int number_of_feet; int number_of_wings; double weight; }; struct farm_animal rover, porky, daffy; rover.number_of_feet = 4; daffy.number_of_feet = 2 rover.number_of_wings = 0; daffy.number_of_wings = 2 rover.weight = 5.8; daffy.weight = 2

Unions union s allow the same area of memory to be used for different things at different times. union some_number { int n; double d; }; union some_number mynum; mynum.n = 2; mynum.d = 3.7; /* overwrites mynum.n */ These are easy to mis-use.

Enums enum s allow symbolic names to be given to a set of integers. enum colour { RED, GREEN, BLUE = 9 }; Then RED has value 0, GREEN value 1, BLUE value 9

The Thing vs A Pointer to the Thing The & operator gives the address (location in memory) of something. The * operator returns the contents of what is at a location. These are typed. A pointer to a double vs a pointer to a byte. A pointer to a value of type T is declared with T * struct farm_animal *p_porky = &porky; struct farm_animal *p_daffy = &daffy; 4 4 5.8 2 2 2.5

The Thing vs A Pointer to the Thing Assignment of structures copies the data from the source structure to the destination structure. daffy = porky; Before: 4 4 5.8 2 2 2.5 After: 4 4 5.8 4 4 5.8

The Thing vs A Pointer to the Thing Assignment of pointers copies the data from the source pointer to the destination pointer. p_daffy = p_porky; Before: 4 4 5.8 2 2 2.5 After: 4 4 5.8 2 2 2.5

The Thing vs A Pointer to the Thing In general, assignment copies data from the source to the destination, regardless of whether it is a pointer, structure, or other type. These may be used in combination. p_porky = &daffy; /* p_porky gets the address of daffy porky = *p_daffy; /* porky gets what p_daffy points to

Arrays An array of a certain size can be declared using [ ]. int a[10]; It is not necessary to call new to create this.

Pointer Arithmetic The addresses of array elements are &(a[0]), &(a[1]), etc. You can add an integer to a pointer to get the location of another element. &(a[3]) == &(a[0]) + 3

Pointer Arithmetic You can use the expression a Then &(a[3]) == a + 3. in place of &(a[0]). a a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

Pointer Arithmetic Indeed, a[i] is really just a shorthand for *(a+i) You could write *(a+2) = *(a+5) instead of a[2] = a[5]

Pointer Arithmetic Since you can add integers to pointers, then p++ and p-- make sense. p++ means use the value of p, then afterward do p = p + 1

Strings Strings in C are null-terminated arrays of characters. hello is the array [ h, e, l, l, o, 0] It is common to see code like char *s = hello ;

The C Preprocessor Because integers and bits in integers are used for so many things, and because compiler technology was not as advanced when C was defined, the language provides a textual-substitution preprocessor to allow symbolic names to be used where integers are needed. #define READ_BIT (1<<7) #define MAX(a,b) ((a) > (b)? (a) : (b)) a = READ_BIT; c = MAX(sizeof(a), 12);

The C Preprocessor The C Preprocessor allows text from other files to be included in a program and conditional inclusion of text. #include <stdio.h> #if defined(maxpath) static char pathname[maxpath]; #else static char pathname[1000]; #endif

Type Definitions New type names may be introduced with typedef. typedef int typedef struct node hash_code; *pnode; hash_code h1, h2; pnode n1, n2, nodes[10]; These are abbreviations and are equivalent to the old types.

Header Files Typically for each.c file or library one will have a.h header file containing Function declarations #define-s of related constants typedef-s of related types

The Standard IO Library #include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { FILE *fin, *fout; char c, *infile = the-input, *outfile = the-output ; fin = fopen(infile, r ); fout = fopen(outfile, w ); if (fin == NULL fout == NULL) { } fprintf(stderr, Cannot open %s or %s\n, infile, outfile); exit(exit_failure); while ((c = getc (fin))!= EOF) putc(c, fout); } fclose(fin); fclose(fout); return EXIT_SUCCESS;

The Standard IO Library Part II Several functions. Most important: printf(fmt,...); fprintf(file *, fmt,...); sprintf(char *, fmt,...); fgets(char *buf, int buf_size, FILE *infile); Output format codes: %s : corresponding argument is a string %c: corresponding argument is an integer to be displayed as a character %d, %o, %x: corresponding argument is an integer to be displayed in decimal/octal/hex

Memory Management For dynamically managed structures use malloc and free #include <stdlib.h> void *malloc(size_t nbytes); void free(void *);

Memory Management struct cell { int value; struct cell *next; }; struct cell *makethelist(int start, int end) { struct cell *p = 0; } for ( ; end >= start; end--) { } return p; struct cell *hd = (struct cell *) malloc(sizeof(struct cell) if (hd == NULL) {... } hd->value = end; hd->next = p; p = hd; void freethelist(struct cell *p) { } while (p) { } struct cell *next = p->next; free(p); p = next;

Conclusions C lets you write efficient, machine-dependent, low-level programs that use fixed size buffers. C also lets you write efficient, portable, safe, high-level programs that manage structures of arbitrary size. It is relatively easy to write efficient C code for small or medium-sized programs. It is relatively hard to write giant programs or maintain over time. If you think you understand C, check out http://www.ioccc.org/