Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Size: px
Start display at page:

Download "Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary"

Transcription

1 Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016

2 ENCM 339 Fall 2016 Slide Set 5 slide 2/32 Contents Character code functions and <ctype.h> The Toolchain The C preprocessor Macros with parameters File inclusion with #include Conditional compilation

3 ENCM 339 Fall 2016 Slide Set 5 slide 3/32 Outline of Slide Set 5 Character code functions and <ctype.h> The Toolchain The C preprocessor Macros with parameters File inclusion with #include Conditional compilation

4 ENCM 339 Fall 2016 Slide Set 5 slide 4/32 Character code functions and <ctype.h> Use of #include <ctype.h> makes available a number of functions related to classifying character codes and converting character codes. An example is the isdigit function, described on Slide 29 of Slide Set 2. Another example is the toupper function, used in an example on the next slide. Let s write down a specification for the toupper function.

5 ENCM 339 Fall 2016 Slide Set 5 slide 5/32 What will be the output of this program? #include <stdio.h> #include <ctype.h> int main(void) { char *p = "Hello, ENCM 339 students!\n"; while (*p!= \0 ) { printf("%c", toupper(*p)); p++; } return 0; }

6 ENCM 339 Fall 2016 Slide Set 5 slide 6/32 Outline of Slide Set 5 Character code functions and <ctype.h> The Toolchain The C preprocessor Macros with parameters File inclusion with #include Conditional compilation

7 ENCM 339 Fall 2016 Slide Set 5 slide 7/32 The Toolchain Informally, the word compiler is often used to refer to a system that makes executable files from source code files. So, based on your experience doing Labs 1 3, you might read the command gcc foo.c as run the compiler to try to make executable file a.out from the source file foo.c. Using that informal meaning for compiler, describe what this command does: gcc bar.c -o bar

8 ENCM 339 Fall 2016 Slide Set 5 slide 8/32 Really, though, the command gcc runs several different programs these programs are called tools, and together the tools form a system called a toolchain. For development using the C programming language there are four essential tools. This a rough and incomplete summary: preprocessor (we ll cover this very soon); compiler translate C code into another language called assembly language; assembler translate assembly language into a format called machine code; linker combine pieces of machine code and pieces of other information together to make an executable file.

9 ENCM 339 Fall 2016 Slide Set 5 slide 9/32 So, what is a more precise way to describe this command from slide 7...? gcc foo.c Concepts such as assembly language, the assembler, machine code, and the linker are not ENCM 339 topics we ll look at them in detail in ENCM 369 (Computer Organization). The preprocessor, though, is an ENCM 339 topic it s what we re going to study next.

10 ENCM 339 Fall 2016 Slide Set 5 slide 10/32 Outline of Slide Set 5 Character code functions and <ctype.h> The Toolchain The C preprocessor Macros with parameters File inclusion with #include Conditional compilation

11 ENCM 339 Fall 2016 Slide Set 5 slide 11/32 The C preprocessor As the pre- part of its name suggests, the preprocessor processes C code in advance of giving C code to the compiler. C code goes in to the preprocessor, and modified C code comes out. In ENCM 339, we ll look at three main kinds of preprocessing: handling macros set up with the #define directive; file inclusion, using the #include directive; conditional compilation, using directives such as #if, #ifdef, #ifndef, #endif, and a few others. There are a few other kinds of preprocessing that we won t cover see Chapter 15 of C in a Nutshell (2nd edition) for details.

12 ENCM 339 Fall 2016 Slide Set 5 slide 12/32 Macros and the #define directive In discussion of computer programming, the word macro means, roughly, a rule for substituting a small piece of source code with another small piece of source code. The C preprocessor offers two main kinds of macros: #define macros without parameters, which are relatively simple; #define macros with parameters, which are more complicated. Probably the most common use of macros without parameters is setting up named constants, as shown on the next slide.

13 ENCM 339 Fall 2016 Slide Set 5 slide 13/32 Normally a #define directive for a macro without parameters is placed on a single line by itself, with this syntax: #define macro name replacement text Here is an example of a macro that is set up incorrectly: #define SECONDS_PER_DAY = double days2sec(double days) { return SECONDS_PER_DAY * days; } What went wrong here? How should it be fixed?

14 ENCM 339 Fall 2016 Slide Set 5 slide 14/32 Names for macros The rules are the same as the rules for names of things like variables, functions, function arguments, and so on... Start with a letter, and follow that with letters, digits, and/or underscores. By convention, most C programmers use ALL CAPS for names of macros, so that they stand out from other names, but the preprocessor will accept macro names with lowercase letters. (Warning: Do not create a name that starts with an underscore this kind of name is said to be reserved for the implementation. Roughly speaking, if you create such a name you risk getting the preprocessor or compiler very confused.)

15 ENCM 339 Fall 2016 Slide Set 5 slide 15/32 Named constants in C and C++ Where a C programmer would write something like this... #define RAD_PER_DEG ( / 180.0)... a C++ programmer would probably write... const double RAD_PER_DEG = / 180.0; What explanations are there for the difference in styles of setting up named constants? Also, why type in so many digits of π?

16 ENCM 339 Fall 2016 Slide Set 5 slide 16/32 Outline of Slide Set 5 Character code functions and <ctype.h> The Toolchain The C preprocessor Macros with parameters File inclusion with #include Conditional compilation

17 ENCM 339 Fall 2016 Slide Set 5 slide 17/32 Macros with parameters These are most easily explained by example. In the code below, x and y are macro parameters. (The example macros have one parameter each, but you re also allowed to #define a macro with two or more parameters.) #define BAD_SQUARE(x) x * x #define GOOD_SQUARE(y) ((y) * (y)) int main(void) { int a = 3, b = 4, c, d; c = BAD_SQUARE(a + b); d = GOOD_SQUARE(a + b); return 0; } What values will the variables c and d get, and why?

18 ENCM 339 Fall 2016 Slide Set 5 slide 18/32 Repeated macro expansion #define SQUARE(a) #define RECIP_SQR(b) int main(void) { double x = 2.0, y; y = RECIP_SQR(x); return 0; } ((a) * (a)) (1.0 / SQUARE(b)) What will the preprocessor do with the assignment statement? What are the general rules for cases like this?

19 ENCM 339 Fall 2016 Slide Set 5 slide 19/32 About writing macros with parameters Avoid doing it! Macros with parameters are notorious causes of hard-to-diagnose syntax errors and other mysterious program defects. Decades ago, it often made sense to write a macro that could be used like a function, but would be more efficient than a function. C compilers are much better these days, and can optimize a call to a simple function so that the function is just as efficient as a macro. There are some valid but highly specialized reasons for writing macros with parameters. Leave that work to experts (or become an expert yourself)!

20 ENCM 339 Fall 2016 Slide Set 5 slide 20/32 Macro names used in string constants #include <stdio.h> // In case we want to rename our application... #define PROGNAME "FooSonic" int main(void) { printf("welcome to the PROGNAME program!\n"); //... more code goes here... return 0; } What output will be produced by the call to printf? What are two different ways to solve the problem?

21 ENCM 339 Fall 2016 Slide Set 5 slide 21/32 Outline of Slide Set 5 Character code functions and <ctype.h> The Toolchain The C preprocessor Macros with parameters File inclusion with #include Conditional compilation

22 ENCM 339 Fall 2016 Slide Set 5 slide 22/32 File inclusion with #include A #include directive calls for inclusion of C code from another source file. There are two main forms: one uses angle brackets, and the other uses double quotes. Here is a familiar example of #include with angle brackets: #include <stdio.h> And here is an example with double quotes: #include "quux.h"

23 ENCM 339 Fall 2016 Slide Set 5 slide 23/32 #include with angle brackets This tells the preprocessor to include a library header file a file full of function prototypes, type declarations, macro definitions, and other things, all related to some portion of the library that is available to all C programs on a given platform. There is an example program on the next slide. The program shows a couple of uses of #include with angle brackets, and also demonstrates the strcat function. What is the effect of #include <stdio.h>? What is the effect of #include <string.h>? What is the program output? The array s is larger than it needs to be. What is the minimum size of the array needed for safe operation of the program?

24 ENCM 339 Fall 2016 Slide Set 5 slide 24/32 #include <stdio.h> #include <string.h> int main(void) { char s[20]; strcpy(s, "Foo"); strcat(s, "tball"); printf("%s!\n", s); return 0; }

25 ENCM 339 Fall 2016 Slide Set 5 slide 25/32 #include with double quotes Every program in Labs 1 to 3 of ENCM 339 in Fall 2016 has had a single.c file for its source code. But typical C projects involve multiple.c files and.h files, all sitting in a single directory in a file system. (Directory is another word for folder.) (Really big C projects have multiple.c files and.h files in multiple directories, but we won t look at that in ENCM 339.) #include with double quotes is used to include a file that is part of a specific project, not part of the C library. The next slide shows an example of a very tiny multi-file C project.

26 ENCM 339 Fall 2016 Slide Set 5 slide 26/32 file main.c #include "quux.h" int main(void) { quux(42); return 0; } file quux.h void quux(double x); file quux.c #include <stdio.h> #include "quux.h" void quux(double x) { printf("%f\n", x); } There are a bunch of questions about this project on the next slide...

27 ENCM 339 Fall 2016 Slide Set 5 slide 27/32 What is an easy way to use the gcc command to build an executable file called foo from the given source files? Why does main.c include quux.h? Why does quux.c include quux.h? What does the term translation unit mean, and what are the translation units produced from main.c and quux.c? What is the program output?

28 ENCM 339 Fall 2016 Slide Set 5 slide 28/32 Outline of Slide Set 5 Character code functions and <ctype.h> The Toolchain The C preprocessor Macros with parameters File inclusion with #include Conditional compilation

29 ENCM 339 Fall 2016 Slide Set 5 slide 29/32 Conditional compilation This refers to the idea that some lines of C code will make it through the preprocessor to the compiler, and other lines will not, depending on certain preprocessor directives. As a term, conditional compilation is somewhat of a misnomer, because it is really something that happens in the preprocessor, not in the compiler. (Misnomer: A word or term that suggests a meaning that is known to be wrong. [... ] A misnomer may also be simply a word that is used incorrectly or misleadingly. Source: Wikipedia.) In the next few slides, we ll look at some examples of conditional compilation, but we won t try to be comprehensive about all the different kinds of conditional compilation made available by the C preprocessor.

30 ENCM 339 Fall 2016 Slide Set 5 slide 30/32 Use of #if to comment out code void foo(int i, const char *s) { #if 1 // printf calls here to help find bugs... printf("args received by foo are...\n"); printf(" i = %d, s points to \"%s\"\n", i, s); #endif } //... foo does actual work here... By changing a single character in the above code, how could we avoid having foo print debugging information about its arguments?

31 ENCM 339 Fall 2016 Slide Set 5 slide 31/32 #ifndef: if not defined... The C standard does not specify that M_PI must be a named constant set up with #define in <math.h>, but in many C libraries, such a #define is present in <math.h>. It s an error to try to redefine a macro in a translation unit. Why exactly does the code below solve a problem? #include <stdio.h> #include <math.h> #ifndef M_PI #define M_PI #endif //... C function definitions here use M_PI...

32 ENCM 339 Fall 2016 Slide Set 5 slide 32/32 More conditional compilation Sometimes conditional compilation is used so that different pieces of C code get compiled depending on what platform the code is being compiled for. For example, one chunk of code might be right for 64-bit Linux, another chunk might be right for 32-bit Windows 7, another for 64-bit Windows 7 or 8, and so on... Yet another practical use for conditional compilation is creation of things called include guards we ll come back to that after we look at C struct types.

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 6 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide

More information

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 3 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 3 slide 2/46

More information

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 3 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01

More information

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 1 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 1 slide 2/43

More information

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng Slide Set 4 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018 Section

More information

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide

More information

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 4 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01

More information

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 9 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2018 ENCM 335 Fall 2018 Slide Set 9 slide 2/32

More information

ENCM 339 Fall 2017 Tutorial for Week 8

ENCM 339 Fall 2017 Tutorial for Week 8 ENCM 339 Fall 2017 Tutorial for Week 8 for section T01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 2 November, 2017 ENCM 339 T01 Tutorial

More information

Have examined process Creating program Have developed program Written in C Source code

Have examined process Creating program Have developed program Written in C Source code Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives

More information

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides Slide Set 1 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #54 Organizing Code in multiple files (Refer Slide Time: 00:09) In this lecture, let us look at one particular

More information

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Announcements Exam 1 (20%): Feb. 27 (Tuesday) Tentative Proposal Deadline:

More information

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 14 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2016 ENCM 339 Fall 2016 Slide Set 14 slide 2/35

More information

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 14 for ENCM 339 Fall 2015 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2015 SN s ENCM 339 Fall 2015 Slide Set 14 slide

More information

A Fast Review of C Essentials Part II

A Fast Review of C Essentials Part II A Fast Review of C Essentials Part II Structural Programming by Z. Cihan TAYSI Outline Macro processing Macro substitution Removing a macro definition Macros vs. functions Built-in macros Conditional compilation

More information

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/13 Scope Variables and functions are visible from the point they are defined until the end of the source

More information

Slide Set 1 (corrected)

Slide Set 1 (corrected) Slide Set 1 (corrected) for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018

More information

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 4 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 4 slide

More information

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. What are 3 differences between C and C++ const variables? QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

More information

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs Slide Set 2 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section

More information

Fundamentals of Programming Session 4

Fundamentals of Programming Session 4 Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc).

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 8 C: Miscellanea Control, Declarations, Preprocessor, printf/scanf 1 The story so far The low-level execution model of a process (one

More information

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember... Types Page 1 "ode to C" Monday, September 18, 2006 4:09 PM 0x0d2C ------ May your signals all trap May your references be bounded All memory aligned Floats to ints round remember... Non -zero is true ++

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #8 Feb 27 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline More c Preprocessor Bitwise operations Character handling Math/random Review for midterm Reading: k&r ch

More information

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

#include <stdio.h> int main() { printf (hello class\n); return 0; } C #include int main() printf ("hello class\n"); return 0; Working environment Linux, gcc We ll work with c9.io website, which works with ubuntu I recommend to install ubuntu too Also in tirgul

More information

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 8 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide

More information

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10 The C Pre Processor ECE2893 Lecture 18 ECE2893 The C Pre Processor Spring 2011 1 / 10 The C Pre Processor 1 The C pre processor is the very first step in any C or C++ program compilation. 2 It is a very

More information

Your first C and C++ programs

Your first C and C++ programs Your first C and C++ programs Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++,

More information

CSE 333 Lecture 7 - final C details

CSE 333 Lecture 7 - final C details CSE 333 Lecture 7 - final C details Steve Gribble Department of Computer Science & Engineering University of Washington Today s topics: - a few final C details header guards and other preprocessor tricks

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2 CS 220: Introduction to Parallel Computing Beginning C Lecture 2 Today s Schedule More C Background Differences: C vs Java/Python The C Compiler HW0 8/25/17 CS 220: Parallel Computing 2 Today s Schedule

More information

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level

More information

C and C++ 2. Functions Preprocessor. Alan Mycroft

C and C++ 2. Functions Preprocessor. Alan Mycroft C and C++ 2. Functions Preprocessor Alan Mycroft University of Cambridge (heavily based on previous years notes thanks to Alastair Beresford and Andrew Moore) Michaelmas Term 2013 2014 1 / 1 Functions

More information

Approximately a Final Exam CPSC 206

Approximately a Final Exam CPSC 206 Approximately a Final Exam CPSC 206 Sometime in History based on Kelley & Pohl Last name, First Name Last 4 digits of ID Write your section number: All parts of this exam are required unless plainly and

More information

ENCM 339 Fall 2017: Cygwin Setup Help

ENCM 339 Fall 2017: Cygwin Setup Help page 1 of 6 ENCM 339 Fall 2017: Cygwin Setup Help Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2017 Introduction This document is designed to help students

More information

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview Computer Science 322 Operating Systems Mount Holyoke College Spring 2010 Topic Notes: C and Unix Overview This course is about operating systems, but since most of our upcoming programming is in C on a

More information

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; } Modifiers unsigned. For example unsigned int would have a range of [0..2 32 1] on a 32-bit int machine. const Constant or read-only. Same as final in Java. static Similar to static in Java but not the

More information

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

ENCM 339 Fall 2017: Editing and Running Programs in the Lab page 1 of 8 ENCM 339 Fall 2017: Editing and Running Programs in the Lab Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2017 Introduction This document is a

More information

QUIZ. Source:

QUIZ. Source: QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Ch. 4: Data Abstraction The only way to get massive increases in productivity is to leverage off other people s code. That

More information

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang Topics History of Programming Languages Compilation Process Anatomy of C CMSC 104 Coding Standards Machine Code In the beginning,

More information

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 18 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary December 2017 ENCM 339 Fall 2017 Section 01

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Slide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng Slide Set 3 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018 Section

More information

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016 Oregon State University School of Electrical Engineering and Computer Science CS 261 Recitation 2 Spring 2016 Outline Programming in C o Headers o Structures o Preprocessor o Pointers Programming Assignment

More information

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009 Compiler Theory (GCC the GNU Compiler Collection) Sandro Spina 2009 GCC Probably the most used compiler. Not only a native compiler but it can also cross-compile any program, producing executables for

More information

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford. Control flow and string example C and C++ 2. Functions Preprocessor Alastair R. Beresford University of Cambridge Lent Term 2007 #include char s[]="university of Cambridge Computer Laboratory";

More information

independent compilation and Make

independent compilation and Make independent compilation and Make Geoffrey Brown David S. Wise Chris Haynes Bryce Himebaugh Computer Structures Fall 2013 Independent Compilation As a matter of style, source code files should rarely be

More information

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Fundamentals of Programming. Lecture 3: Introduction to C Programming Fundamentals of Programming Lecture 3: Introduction to C Programming Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department Outline A Simple C

More information

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3). cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion

More information

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2 page 1 of 11 ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2 Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2017 Lab instructions and

More information

Introduction to Computing Lecture 01: Introduction to C

Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering ozbek.nukhet@gmail.com Topics Introduction to C language

More information

Programming. Projects with Multiple Files

Programming. Projects with Multiple Files Programming Projects with Multiple Files Summary } GCC } Multiple source files 2 Source Code with Multiple Files } Make multiple, separate source code files which can be combined into one executable }

More information

Final C Details. CSE 333 Autumn 2018

Final C Details. CSE 333 Autumn 2018 Final C Details CSE 333 Autumn 2018 Instructor: Hal Perkins Teaching Assistants: Tarkan Al-Kazily Renshu Gu Trais McGaha Harshita Neti Thai Pham Forrest Timour Soumya Vasisht Yifan Xu Administriia Today:

More information

Unit 4 Preprocessor Directives

Unit 4 Preprocessor Directives 1 What is pre-processor? The job of C preprocessor is to process the source code before it is passed to the compiler. Source Code (test.c) Pre-Processor Intermediate Code (test.i) Compiler The pre-processor

More information

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng Slide Set 11 for ENCM 369 Winter 2015 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2015 ENCM 369 W15 Section

More information

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12 CS16 Week 2 Part 2 Kyle Dewey Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors Type Coercion / Casting Last time... Data is internally

More information

Separate Compilation of Multi-File Programs

Separate Compilation of Multi-File Programs 1 About Compiling What most people mean by the phrase "compiling a program" is actually two separate steps in the creation of that program. The rst step is proper compilation. Compilation is the translation

More information

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016 The University of Calgary ENCM 339 Programming Fundamentals Fall 2016 Instructors: S. Norman, and M. Moussavi Wednesday, November 2 7:00 to 9:00 PM The First Letter of your Last Name:! Please Print your

More information

Slide Set 15 (Complete)

Slide Set 15 (Complete) Slide Set 15 (Complete) for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2017 ENCM 339 Fall 2017

More information

The C Preprocessor (and more)!

The C Preprocessor (and more)! The C Preprocessor (and more)! Peter Kristensen 2012-11-19 Peter Kristensen The C Preprocessor (and more)! Outline 1 C Pre Processor Compiler Assembler Linker Frontend 2 Simple directives Headers Macros

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 13 C: The Rest of the Preprocessor 1 Administrivia Midterm exam Wednesday, here Topics: everything up to hw4 (including gdb concepts,

More information

ENCM 339 Fall 2017 Lecture Section 01 Lab 5 for the Week of October 16

ENCM 339 Fall 2017 Lecture Section 01 Lab 5 for the Week of October 16 page 1 of 5 ENCM 339 Fall 2017 Lecture Section 01 Lab 5 for the Week of October 16 Steve Norman Department of Electrical & Computer Engineering University of Calgary October 2017 Lab instructions and other

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

PRINCIPLES OF OPERATING SYSTEMS

PRINCIPLES OF OPERATING SYSTEMS PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Approximately a Test II CPSC 206

Approximately a Test II CPSC 206 Approximately a Test II CPSC 206 Sometime in history based on Kelly and Pohl Last name, First Name Last 5 digits of ID Write your section number(s): All parts of this exam are required unless plainly and

More information

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C. Reading. goto Considered Harmful History Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program

More information

Here's how you declare a function that returns a pointer to a character:

Here's how you declare a function that returns a pointer to a character: 23 of 40 3/28/2013 10:35 PM Violets are blue Roses are red C has been around, But it is new to you! ANALYSIS: Lines 32 and 33 in main() prompt the user for the desired sort order. The value entered is

More information

Slide Set 18. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 18. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 18 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary December 2016 ENCM 339 Fall 2016 Slide Set 18 slide 2/26

More information

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

More information

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3 Hello, World! in C Johann Myrkraverk Oskarsson October 23, 2018 Contents 1 The Quintessential Example Program 1 I Printing Text 2 II The Main Function 3 III The Header Files 4 IV Compiling and Running

More information

CSE 333 Autumn 2013 Midterm

CSE 333 Autumn 2013 Midterm CSE 333 Autumn 2013 Midterm Please do not read beyond this cover page until told to start. A question involving what could be either C or C++ is about C, unless it explicitly states that it is about C++.

More information

ENCM 501 Winter 2019 Assignment 9

ENCM 501 Winter 2019 Assignment 9 page 1 of 6 ENCM 501 Winter 2019 Assignment 9 Steve Norman Department of Electrical & Computer Engineering University of Calgary April 2019 Assignment instructions and other documents for ENCM 501 can

More information

CS2141 Software Development using C/C++ Compiling a C++ Program

CS2141 Software Development using C/C++ Compiling a C++ Program CS2141 Software Development using C/C++ Compiling a C++ Program g++ g++ is the GNU C++ compiler. A program in a file called hello.cpp: #include using namespace std; int main( ) { cout

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Gives

More information

EL6483: Brief Overview of C Programming Language

EL6483: Brief Overview of C Programming Language EL6483: Brief Overview of C Programming Language EL6483 Spring 2016 EL6483 EL6483: Brief Overview of C Programming Language Spring 2016 1 / 30 Preprocessor macros, Syntax for comments Macro definitions

More information

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems Programs CSCI 4061 Introduction to Operating Systems C Program Structure Libraries and header files Compiling and building programs Executing and debugging Instructor: Abhishek Chandra Assume familiarity

More information

1 You seek performance 3

1 You seek performance 3 Why? 2 1 You seek performance 3 1 You seek performance zero-overhead principle 4 2 You seek to interface directly with hardware 5 3 That s kinda it 6 C a nice way to avoid writing assembly language directly

More information

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world alun@debian:~$ gcc hello.c alun@debian:~$ a.out Hello, world alun@debian:~$ gcc -o hello hello.c alun@debian:~$ hello Hello, world alun@debian:~$ 1 A Quick guide to C for Networks and Operating Systems

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins) CSE 374 Programming Concepts & Tools Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins) Linked lists, trees, and friends Very, very common data structures

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 STEPS OF GCC STEPS file.c Preprocessor Compiler file1.o file2.o Assembler Linker executable file PREPROCESSOR PREPROCESSOR The C preprocessor is a macro processor that is

More information

Software Engineering /48

Software Engineering /48 Software Engineering 1 /48 Topics 1. The Compilation Process and You 2. Polymorphism and Composition 3. Small Functions 4. Comments 2 /48 The Compilation Process and You 3 / 48 1. Intro - How do you turn

More information

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

G52CPP C++ Programming Lecture 6. Dr Jason Atkin G52CPP C++ Programming Lecture 6 Dr Jason Atkin 1 Last lecture The Stack Lifetime of local variables Global variables Static local variables const (briefly) 2 Visibility is different from lifetime Just

More information

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Computers and Computation. The Modern Computer. The Operating System. The Operating System The Modern Computer Computers and Computation What is a computer? A machine that manipulates data according to instructions. Despite their apparent complexity, at the lowest level computers perform simple

More information

Chapter 7: Preprocessing Directives

Chapter 7: Preprocessing Directives Chapter 7: Preprocessing Directives Outline We will only cover these topics in Chapter 7, the remain ones are optional. Introduction Symbolic Constants and Macros Source File Inclusion Conditional Compilation

More information

ENCM 335 Fall 2018 Tutorial for Week 13

ENCM 335 Fall 2018 Tutorial for Week 13 ENCM 335 Fall 2018 Tutorial for Week 13 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 06 December, 2018 ENCM 335 Tutorial 06 Dec 2018 slide

More information

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C CSE 130: Introduction to Programming in C Stony Brook University WHY C? C is a programming lingua franca Millions of lines of C code exist Many other languages use C-like syntax C is portable

More information

C introduction: part 1

C introduction: part 1 What is C? C is a compiled language that gives the programmer maximum control and efficiency 1. 1 https://computer.howstuffworks.com/c1.htm 2 / 26 3 / 26 Outline Basic file structure Main function Compilation

More information

Lecture 10: building large projects, beginning C++, C++ and structs

Lecture 10: building large projects, beginning C++, C++ and structs CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 10:

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

2 Compiling a C program

2 Compiling a C program 2 Compiling a C program This chapter describes how to compile C programs using gcc. Programs can be compiled from a single source file or from multiple source files, and may use system libraries and header

More information

CSE2301. Functions. Functions and Compiler Directives

CSE2301. Functions. Functions and Compiler Directives Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who

More information

Assignment #5 Answers

Assignment #5 Answers Assignment #5 Answers Introductory C Programming UW Experimental College Assignment #5 ANSWERS Question 1. What's wrong with #define N 10;? The semicolon at the end of the line will become part of N's

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 CMPE 013/L Pre Processor Commands Gabriel Hugh Elkaim Spring 2013 3 Introduction Preprocessing Affect program preprocessing and execution Capabilities Inclusion of additional C source files Definition

More information

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring 2009 Topic Notes: C and Unix Overview This course is about computer organization, but since most of our programming is

More information

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng Slide Set 5 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section

More information