CS 201 A Whirlwind Tour of C Programming

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

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files.
CSE 303 Lecture 16 Multi-file (larger) programs
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
SPL – Practical Session 2 Topics: – Makefile – C++ Memory Management – Pointers.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
The Makefile utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
Introduction: C Pointers Day 2 These Slides NOT From Text.
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
Introduction to Make Updated by Prasad Spring 2000.
Guide To UNIX Using Linux Third Edition
Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Scons Writing Solid Code Overview What is scons? scons Basics Other cools scons stuff Resources.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
(language, compilation and debugging) David 09/16/2011.
Data Display Debugger (DDD)
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
Navigating the C++ Development Environment
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Makefile Script file to automate program compilation and linking (making) 1. Write the "makefile" 2. Write your programs 3. Run "make" or "make -f makefile"
GNU Make Computer Organization II 1 © McQuain What is make ? make is a system utility for managing the build process (compilation/linking/etc).
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
C++ Functions A bit of review (things we’ve covered so far)
1 Building a program in C: Preprocessor, Compilation and Linkage.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
CSE 374 Programming Concepts & Tools
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
The Machine Model Memory
Computer Systems MTSU CSCI 3240 Spring 2016 Dr. Hyrum D. Carroll
Executable program (p1)
Brief Intro to Make CST494/ Gannod.
CSE 374 Programming Concepts & Tools
A bit of C programming Lecture 3 Uli Raich.
Command Line Arguments
Command line arguments
Agenda Make Utility Command Line Arguments in Unix
Makefiles Caryl Rahn.
CSE 303 Concepts and Tools for Software Development
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
C Basics.
SPL – Practical Session 2
Computer Systems and Networks
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Makefiles and the make utility
Exam 4 review Copyright © 2008 W. W. Norton & Company.
CSc 352 An Introduction to the C Preprocessor
Appendix F C Programming Environment on UNIX Systems
Makefiles and the make utility
Compiler vs linker The compiler translates one .c file into a .o file
Executable program (p1)
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
SPL – PS1 Introduction to C++.
What is make? make is a system utility for managing the build process (compilation/linking/etc). There are various versions of make; these notes discuss.
Chapter 11 Introduction to Programming in C
The make utility (original presentation courtesy of Alark Joshi)
Presentation transcript:

CS 201 A Whirlwind Tour of C Programming Gerson Robboy Portland State University

A brute simple Makefile all: sd test1 t1check test2 sd: sd.c cc -g -o sd sd.c test1: test1.c cc -o test1 test1.c test2: test2.c cc -o test2 test2.c t1check: t1check.c cc -o t1check t1check.c clean: rm sd test1 t1check test2 t1sort

Some things about Makefiles Call it makefile or Makefile (big or little M) The “make” utility will use that by default The first rule in the Makefile is used by default if you just say “make” with no arguments The second line of each rule (the command) must start with a tab, not spaces!

A slightly more complex makefile CC = gcc CFLAGS = -Wall -O2 LIBS = -lm OBJS = driver.o kernels.o fcyc.o clock.o all: driver driver: $(OBJS) config.h defs.h fcyc.h $(CC) $(CFLAGS) $(OBJS) $(LIBS) -o driver driver.o: driver.c defs.h kernels.o: kernels.c ... h fcyc.o: fcyc.c clock.o: clock.c

A brute simple shell script test1 sd t1file t1sort t1check echo test2 sd t2file t1sort t1check echo A plain text file containing commands Known as a batch file in some O. S.’s Make sure the file has “executable” permission Invoke it by name, just like a binary program

argc and argv main has two arguments: main(int argc, char *argv[]) argc tells how many command line arguments there are, including the command itself argv is a pointer to an array of pointers to characters Example: find . –print argc = 3 argv[0] = “find” argv[1] = “.” argv[2] = “-print”

The C preprocessor bass> gcc -O2 -v -o p m.c a.c cpp [args] m.c /tmp/cca07630.i cc1 /tmp/cca07630.i m.c -O2 [args] -o /tmp/cca07630.s as [args] -o /tmp/cca076301.o /tmp/cca07630.s <similar process for a.c> ld -o p [system obj files] /tmp/cca076301.o /tmp/cca076302.o bass> cc or gcc, the compiler driver, invokes a bunch of things The first one is cpp, the preprocessor After that is cc1, the translator cpp converts the C source file to another C source file expands #defines, #includes, etc.

Things you can tell the preprocessor Included files: #include <foo.h> #include “bar.h” Defined constants: #define MAXVAL 40000000 Macros: #define MIN(x,y) ((x)<(y) ? (x):(y)) #define RIDX(i, j, n) ((i) * (n) + (j)) Conditional compilation: #ifdef … #if defined( … ) #endif

What do you use conditional compilation for? Debug print statements By defining or undefining one constant, you can include or exclude many scattered pieces of code Code you think you may need again #ifdefs are more readable than commenting code out Portability To multiple operating systems To multiple processors Compilers have “built in” constants defined #if defined(__i386__) || defined(WIN32) || … To different compilers for the same processor

Pointers Pointers can point to any data type, including structures pixel foo[32000]; // An array of pixels // p is a pointer to a pixel, initialized to foo pixel *p = foo; &(foo[99]) is the same thing as (p+99) You can use subscripts with pointers Incrementing a pointer adds the increment times the size of the data type that it points to. foo[99] is the same as *(p+99) or p[99] foo[0].blue is the same thing as p->blue foo[99].red is the same thing as (p+99)->red

Pointer example Following a linked list struct zilch listhead; struct zilch *p = listhead; while (p != NULL) { ... // Do stuff to *p p = p->next; }

What’s the problem with this? int zarray[34000]; char *zp = zarray; int i; for(i=0; i<34000; i++){ *zp++ = i; }

C pointer declarations int *p p is a pointer to int int *p[13] p is an array[13] of pointer to int int *(p[13]) p is an array[13] of pointer to int int **p p is a pointer to a pointer to an int int (*p)[13] p is a pointer to an array[13] of int int *f() f is a function returning a pointer to int int (*f)() f is a pointer to a function returning int int (*(*f())[13])() f is a function returning ptr to an array[13] of pointers to functions returning int int (*(*x[3])())[5] x is an array[3] of pointers to functions returning pointers to array[5] of ints