CMPSC 311- Introduction to Systems Programming Module: Introduction to C

Similar documents
CSE 333 Lecture 2 - gentle re-introduction to C

CSE 333. Lecture 1 - Intro, C refresher. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSE 333 Lecture 3 - arrays, memory, pointers

CSE 333 Lecture 2 - arrays, memory, pointers

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

CMPSC 311- Introduction to Systems Programming Module: Arrays and Pointers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSE 333 Lecture 2 Memory

Lecture 7: file I/O, more Unix

PRINCIPLES OF OPERATING SYSTEMS

Intro, C refresher. CSE 333 Summer Instructor: Hal Perkins. Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

Lecture 03 Bits, Bytes and Data Types

Lecture 2: C Programm

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS61, Fall 2012 Section 2 Notes

CSE 374 Programming Concepts & Tools

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CSE 303: Concepts and Tools for Software Development

Kurt Schmidt. October 30, 2018

Systems Programming and Computer Architecture ( )

Contents. A Review of C language. Visual C Visual C++ 6.0

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CSE 12 Spring 2016 Week One, Lecture Two

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Lecture 3: C Programm

Lecture 8: Structs & File I/O

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Processes. Johan Montelius KTH

Pointers, Pointers, Pointers

A process. the stack

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

Arrays and Memory Management

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

CSE 303 Lecture 8. Intro to C programming

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Pointers, Pointers, Pointers

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

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

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

Important From Last Time


CMPSC 311- Introduction to Systems Programming Module: Build Processing

CSE 333 Midterm Exam July 24, Name UW ID#

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Final CSE 131B Spring 2004

Important From Last Time

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Lectures 5-6: Introduction to C

Tutorial 1 C Tutorial: Pointers, Strings, Exec

CSC 2400: Computing Systems. X86 Assembly: Function Calls

HW1 due Monday by 9:30am Assignment online, submission details to come

Chapter 11 Introduction to Programming in C

CS 261 Fall Mike Lam, Professor. Structs and I/O

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

CSCI 171 Chapter Outlines

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

C Fundamentals! C vs. Java: Design Goals. q Java design goals! q Implications for Java! language, machine language, hardware!

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Dynamic memory allocation (malloc)

Memory and Arrays. CSE 333 Spring Instructor: Justin Hsia

ch = argv[i][++j]; /* why does ++j but j++ does not? */

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

211: Computer Architecture Summer 2016

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

18-642: Code Style for Compilers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Lecture 9: Potpourri: Call by reference vs call by value Enum / struct / union Advanced Unix

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

Memory Allocation in C

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

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Programming in C First meeting Tiina Niklander

CSE 12 Spring 2018 Week One, Lecture Two

Exercise Session 2 Systems Programming and Computer Architecture

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Objectives. External declarations Pointers Dynamic data structures using pointers Function pointers

Programming in C First meeting

Transcription:

CMPSC 311- Introduction to Systems Programming Module: Introduction to C Professor Patrick McDaniel Fall 2015

Building HW1 You are provided with a Makefile that will assist in making your program. You don t have to call the compiler directly, the utliity will do it for you. Just type make at the command line: @ubuntu:~/siis/courses/cmpsc311-f15/project/assign1/src$ make gcc -I. -c -g -Wall -I. cmpsc311-f15-assign1.c -o cmpsc311-f15-assign1.o gcc -I. -c -g -Wall -I. a1support.c -o a1support.o gcc -lm cmpsc311-f15-assign1.o a1support.o -o cmpsc311-f15-assign1 -lm mcdaniel@ubuntu:~/siis/courses/cmpsc311-f15/project/assign1/src$ We will cover Makefiles in a later lecture, just use this for now. Page

C workflow Editor (emacs, vi) or IDE (eclipse) edit source files (.c,.h) foo.h foo.c bar.c compile object files (.o) foo.o bar.o link executable load link bar execute, debug, profile,... process link bar libz.a statically linked libraries libc.s o shared libraries Page 3

From C to machine code C source file (dosum.c) int dosum(int i, int j) { return i+j; C compiler (gcc -S) machine code (dosum.o) assembly source file (dosum.s) 80483b0: 55 89 e5 8b 45 0c 03 45 08 5d c3 dosum: pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax addl 8(%ebp), %eax popl %ebp ret assembler (as) Page 4

Skipping assembly language Most C compilers generate object.o" files directly i.e., without actually saving the readable.s assembly file dosum.c gcc -S dosum.s as dosum.o gcc -c Note: Object code is is re-locatable machine code, but generally cannot be executed without some manipulation (e.g., via a linker) Page 5

Multi-file C programs C source file (dosum.c) C source file (sumnum.c) int dosum(int i, int j) { return i+j; #include <stdio.h> int dosum(int i, int j); int main(int argc, char **argv) { printf("%d\n", dosum(1,2)); this prototype of dosum( ) tells gcc about the types of dosum s arguments and its return value dosum( ) is implemented in sumnum.c Page 6

Multi-file C programs C source file (dosum.c) C source file (sumnum.c) int dosum(int i, int j) { return i+j; #include <stdio.h> int dosum(int i, int j); int main(int argc, char **argv) { printf("%d\n", dosum(1,2)); why do we need this #include? where is the implementation of printf? Page 7

Compiling multi-file programs Multiple object files are linked to produce an executable standard libraries (libc, crt1,...) are usually also linked in a library is just a pre-assembled collection of.o files dosum.c gcc -c dosum.o ld (or gcc) sumnum sumnum.c gcc -c sumnum.o libraries (e.g., libc) Page 8

Object files revisted sumnum.o, dosum.o are object files each contains machine code produced by the compiler each might contain references to external symbols variables and functions not defined in the associated.c file e.g., sumnum.o contains code that relies on printf( ) and dosum( ), but these are defined in libc.a and dosum.o, respectively linking resolves these external symbols while smooshing together object files and libraries Page 9

Let s dive into C itself Things that are the same as Java syntax for statements, control structures, function calls types: int, double, char, long, float type-casting syntax: float x = (float) 5 / 3; expressions, operators, precedence + - * / % ++ -- = += -= *= /= %= < <= ==!= > >= &&! scope (local scope is within a set of { braces) comments: /* comment */ or // comment *to EOL* Page 10

Primitive types in C integer types char, int floating point float, double modifiers short [int] long [int, double] signed [char, int] unsigned [char, int] type bytes (32 bit) bytes (64 bit) 32 bit range printf char 1 1 [0, 255] %c short int 2 2 [-32768,32767] %hd unsigned short int 2 2 [0, 65535] %hu int 4 4 [-214748648, 2147483647] %d unsigned int 4 4 [0, 4294967295] %u long int 4 8 long long int 8 8 [-2147483648, 2147483647] %ld [-9223372036854775808, 9223372036854775807] %lld float 4 4 approx [10-38, 10 38 ] %f double 8 8 approx [10-308, 10 308 ] %lf long double 12 16 approx [10-4932, 10 4932 ] %Lf pointer 4 8 [0, 4294967295] %p Page 11

C99 extended integer types Solve the conundrum of how big is a long int? #include <stdint.h> void foo(void) { int8_t w; // exactly 8 bits, signed int16_t x; // exactly 16 bits, signed int32_t y; // exactly 32 bits, signed int64_t z; // exactly 64 bits, signed uint8_t w;...etc. // exactly 8 bits, unsigned Page 12

Similar to Java... variables must declare at the start of a function or block (changed in C99) need not be initialized before use (gcc -Wall will warn) #include <stdio.h> int main(int argc, char **argv) { int x, y = 5; // note x is uninitialized! long z = x+y; printf("z is '%ld'\n", z); // what s printed? { int y = 10; printf("y is '%d'\n", y); int w = 20; // ok in c99 printf("y is '%d', w is '%d'\n", y, w); Page 13

Similar to Java... const a qualifier that indicates the variable s value cannot change compiler will issue an error if you try to violate this why is this qualifier useful? #include <stdio.h> int main(int argc, char **argv) { const double MAX_GPA = 4.0; printf("max_gpa: %g\n", MAX_GPA); MAX_GPA = 5.0; // illegal! Page 14

Similar to Java... for loops can t declare variables in the loop header (changed in c99) if/else, while, and do/while loops no boolean type (changed in c99) any type can be used; 0 means false, everything else true int i; for (i=0; i<100; i++) { if (i % 10 == 0) { printf("i: %d\n", i); Page 15

Similar to Java/C++... C always passes arguments by value value is copied into function any local modification change is not reflecting in original value passed pointers lets you pass by reference pass memory location of variable passed more on these soon least intuitive part of C very dangerous part of C void add_pbv(int c) { c += 10; printf("pbv c: %d\n", c); void add_pbr(int *c) { *c += 10; printf("pbr *c: %d\n", *c); int main(int argc, char **argv) { int x = 1; printf("x: %d\n", x); add_pbv(x); printf("x: %d\n", x); add_pbr(&x); printf("x: %d\n", x); Page 16

Pass-by-value C passes arguments by value callee receives a copy of the argument void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; swap(a, b); printf("a: %d, b: %d\n", a, b); if the callee (function that is called) modifies an argument, caller s copy isn t modified Page 17

Pass-by-value (stack) OS kernel [protected] stack main a 42 b -7 void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(a, b); printf("a: %d, b: %d\n", a, b); Page 18

Pass-by-value (stack) OS kernel [protected] stack main a 42 b -7 a? b? swap tmp? void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(a, b); printf("a: %d, b: %d\n", a, b); Page 19

Pass-by-value (stack) OS kernel [protected] stack main a 42 b -7 a 42 b -7 swap tmp? void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(a, b); printf("a: %d, b: %d\n", a, b); Page 20

Pass-by-value (stack) OS kernel [protected] stack main a 42 b -7 a 42 b -7 swap tmp 42 void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(a, b); printf("a: %d, b: %d\n", a, b); Page 21

Pass-by-value (stack) OS kernel [protected] stack main a 42 b -7 a -7 b -7 swap tmp 42 void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(a, b); printf("a: %d, b: %d\n", a, b); Page 22

Pass-by-value (stack) OS kernel [protected] stack main a 42 b -7 a -7 b 42 swap tmp 42 void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(a, b); printf("a: %d, b: %d\n", a, b); Page 23

Pass-by-value (stack) OS kernel [protected] stack main a 42 b -7 void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(a, b); printf("a: %d, b: %d\n", a, b); Page 24

Pass-by-value (stack) OS kernel [protected] stack main a 42 b -7 void swap(int a, int b) { int tmp = a; a = b; b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(a, b); printf("a: %d, b: %d\n", a, b); Page 25

Pass-by-reference You can use pointers to pass by reference callee still receives a copy of the argument but, the argument is a pointer the pointer s value points-to the variable in the scope of the caller this gives the callee a way to modify a variable that s in the scope of the caller void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 26

Pass-by-reference (stack) OS kernel [protected] stack main a 42 b -7 void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 27

Pass-by-reference (stack) OS kernel [protected] stack main a 42 b -7 a? b? swap tmp? void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 28

Pass-by-reference (stack) OS kernel [protected] stack main a 42 b -7 a b swap tmp? void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 29

Pass-by-reference (stack) OS kernel [protected] stack main a 42 b -7 a b swap tmp 42 void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 30

Pass-by-reference (stack) OS kernel [protected] stack main a -7 b -7 a b swap tmp 42 void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 31

Pass-by-reference (stack) OS kernel [protected] stack main a -7 b 42 a b swap tmp 42 void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 32

Pass-by-reference (stack) OS kernel [protected] stack main a -7 b 42 void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 33

Pass-by-reference (stack) OS kernel [protected] stack main a -7 b 42 void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; int main(int argc, char **argv) { int a = 42, b = -7; heap (malloc/free) read/write segment.data,.bss read-only segment.text,.rodata swap(&a, &b); printf("a: %d, b: %d\n", a, b); Page 34

Very different than Java/C++ arrays just a bare, contiguous block of memory of the correct size array of 6 ints requires 6 x 4 bytes = 24 bytes of memory arrays have no methods, do not know their own length (no bounds checking) C doesn t stop you from overstepping the end of an array!! many, many security bugs come from this Page 35

Very different than Java/C++ arrays just a bare, contiguous block of memory of the correct size array of 6 ints requires 6 x 4 bytes = 24 bytes of memory arrays have no methods, do not know their own length C doesn t stop you from overstepping the end of an array!! many, many security bugs come from this Page 36

Very different than Java/C++ strings array of char terminated by the NULL character \0 are not objects, have no methods; string.h has helpful utilities (see strings lecture coming soon!) x h e l l o \n \0 char *x = hello\n ; Page 37

Very different than Java/C++ errors and exceptions C has no exceptions (no try / catch) errors are returned as integer error codes from functions Sometimes makes error handling ugly and inelegant crashes if you do something bad, you ll end up spraying bytes around memory, hopefully causing a segmentation fault and crash objects there aren t any; struct is closest feature (set of fields) Page 38

Very different than Java/C++ memory management there is no garbage collector anything you allocate you have to free (memory leaks) local variables are allocated off of the stack freed when you return from the function global and static variables are allocated in a data segment are freed when your program exits you can allocate memory in the heap segment using malloc( ) you must free malloc ed memory with free( ) failing to free is a leak, double-freeing is an error (hopefully crash) Page 39

Very different than Java/C++ console I/O C library (libc) has portable routines for reading/writing scanf(), printf() file I/O C library has portable routines for reading/writing fopen(), fread(), fwrite(), fclose(), etc. does buffering by default, is blocking by default OS provides (less portable) routines we ll be using these: more control over buffering, blocking Low level binary reads and writes read(), write(), open(), close() Page 40

Very different than Java/C++ network I/O C standard library has no notion of network I/O OS provides (somewhat portable) routines lots of complexity lies here errors: network can fail performance: network can be slow concurrency: servers speak to thousands of clients simultaneously Page 41

Very different than Java/C++ Libraries you can count on C has very few compared to most other languages no built-in trees, hash tables, linked lists, sort, etc. you have to write many things on your own particularly data structures error prone, tedious, hard to build efficiently and portably this is one of the main reasons C is a much less productive language than Java, C++, python, or others Page 42

Defining a function returntype name(type name,..., type name) { statements; // sum integers from 1 to max int sumto(int max) { int i, sum = 0; for (i=1; i<=max; i++) { sum += i; return sum; Page 43

Problem: ordering Don t call a function that hasn t been declared yet: #include <stdio.h> int main(int argc, char **argv) { printf("sumto(5) is: %d\n", sumto(5)); // sum integers from 1 to max int sumto(int max) { int i, sum = 0; for (i=1; i<=max; i++) { sum += i; return sum; Page 44

Problem: ordering Solution 1: reverse order of definition #include <stdio.h> // sum integers from 1 to max int sumto(int max) { int i, sum = 0; for (i=1; i<=max; i++) { sum += i; return sum; int main(int argc, char **argv) { printf("sumto(5) is: %d\n", sumto(5)); Page 45

Problem: ordering Solution 2: provide a declaration of the function teaches the compiler the argument and return types of the function #include <stdio.h> // this function prototype is a // declaration of sumto int sumto(int); int main(int argc, char **argv) { printf("sumto(5) is: %d\n", sumto(5)); // sum integers from 1 to max int sumto(int max) { int i, sum = 0; for (i=1; i<=max; i++) { sum += i; return sum; The body-less function declaration is called a functional prototype. Page 46

UNIX Std* There are three input types provided to all UNIX programs Standard input (STDIN) Standard output (STDOUT) By default printf always goes to STDOUT Standard error (STDERR) Page 47

UNIX Environment variables Environment variables are variable set within the user terminal session (called a shell) used to setup the environment (we will see this more later) use export <VARNAME>=value to create use echo $<VARNAME> to print use printevn to print all values mcdaniel@ubuntu:~/tmp/helloworld$ export VARIABLE=mine mcdaniel@ubuntu:~/tmp/helloworld$ echo $VARIABLE mine mcdaniel@ubuntu:~/tmp/helloworld$ printenv VARIABLE=mine SSH_AGENT_PID=2122 GIO_LAUNCHED_DESKTOP_FILE_PID=2638 TERM=xterm SHELL=/bin/bash... Page 48

Running a program After building a program mcdaniel@ubuntu:~/tmp/helloworld$ vim helloworld.c mcdaniel@ubuntu:~/tmp/helloworld$ gcc helloworld.c -o helloworld mcdaniel@ubuntu:~/tmp/helloworld$ helloworld helloworld: command not found mcdaniel@ubuntu:~/tmp/helloworld$ echo $PATH /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/ bin:/sbin:/bin:/usr/games:/usr/local/games mcdaniel@ubuntu:~/tmp/helloworld$./helloworld Hello world! mcdaniel@ubuntu:~/tmp/helloworld UNIX looks for a program in all of the directories listed by the PATH environment variable to add to search path just add more : separated paths, export PATH=$PATH:/new/path Page 49