Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 1 ECE15: Introduction to Computer Programming Using the C Language
1: Introduction
A. Orlitsky and A. Vardy, based in part on slides by S. Arzi, G. Ruckenstein, E. Avior, S. Asmir, M. Elad. ECE15 – Engineering Computation – Fall 15
Whe(n/re) Class WLH 2001 TTh 2:00–3:20 Sections Peter 102 M 4–4:50 Peter 103 M, W, F 3–3:50 O ce hours Jacobs 4307 M 9-1; Tu 11-2, 3:30-6; W 9-1, 5-8; Th 11-2; F 11-2 Instructor Alon Orlitsky Atk 4109 M 2-3
TA’s David Hall, Raghav Abboy, Neha Ahlawat, Vijay Chintalapudi, Moein Falahatgar, Shengyao Guo, Pranav Markani, Anton Qiu, Sitanan (Spipe) Tanyasakulkit, Hans Yu, Allen Zhao
Grading 30% Weekly homework sets, worst omitted 20-30% Midterm, Th 11/5 2-3:20 40-50% Final, Th 12/10 3-6 Midterm / final weight chosen to maximize your score
Homework Homework is crucial to understanding the material. Please take it seriously. Up to 4 problems will be assigned weekly except before the midterm and final. Please upload your solutions online by noon the following Thursday. You may generally discuss the homework with others, but do not look at anyone else’s code, nor show them yours.
Regrades Regrade requests must be submitted with explanation in writing within two weeks from the day the grade was assigned.
Website HW’s, solutions, announcements: http://ece15.info Grades: http://ted.ucsd.edu Discussions: http://piazza.com
Feedback Send all email correspondence to ecefi[email protected].
Textbooks Programming in C, 3rd Edition, Stephen G. Kochan, Developer’s Library The C Programming Language, 2nd Ed., Kernighan and Ritchie, Prentice Hall, 1988
Syllabus Approximate topics, order covered, and corresponding material in Kochan 4th Edition. For 3rd Ed. just increment chapter by one (e.g. 2 becomes 3) pages are essentially the same. Lecture Topics Chapter: Pages 1 introduction, first program, compiling 2: 11-19 2 variables, types, input, output 3: 21-30, 15: 345-362 3 math: operations, expressions, logic 3: 30-39 4 control flow: conditionals, loops 5: 65-92, 4: 43-62 5 algorithms (maybe move to later) review 6 arrays, multi-dimensional arrays 6: 95-116, 9: 193-215 7 functions, scope, modular design 7: 119-158 8 pointers, dynamic memory allocation 10: 233-274
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 3 Why Grades
❖ Necessary evil Hello Hello! ❖ Non-reasons ‣Judge ‣Easy ‣Cool ❖ Top 8 reasons ‣Without: show up only at graduation ‣Worse: not just class empty! ‣Helps you know what you know ‣Helps us know what you know ‣Encourage to perform, excel ‣Helps you in future ‣Evolution
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 4 Expectations
❖ High-school GPA?
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 5 Calibration
❖ What grades stand for Generic High Amazing! Average A Autstanding! Acceptable Anbelievable! B Bummer Brilliant
C Catastrophe Cool Drama Detraction D Disaster Do it again Death ❖ Conclusion: Don’t panic if you get a B or a C!
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 6 Homework
❖ Teach you the most ❖ Weekly assignments ❖ 3-4 problems ❖ To help you: Website, Ted, Piazza ❖ Daily recitation sections and office hours ❖ If not enough, let us know ❖ Discuss with friends ❖ Don’t copy - write your own ❖ We’ll check, and follow up!
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 7 Gradual
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 8 How Hard is the Course?
❖ Easy ‣Introductory ‣Different backgrounds ‣From scratch ‣No prior programming assumed ‣Everything should be clear
❖ Difficult ‣First course ‣Learn a new language ‣New way of thinking ‣Homework
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 9 Ready?
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 10 What’s the Course About?
Programming - Making computers do what we want
C - Powerful multi-purpose programming language, most popular, and basis for C++, Java, Python, other languages
Modular design - Philosophy of breaking tasks into components, implementing each, then combining
Programming used everywhere
One of the most useful classes you will take!
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 11 First Lecture Outline
❖ Operating systems ‣Unix
❖ Editors ‣Emacs ❖ Compiling and running ‣Create, compile, run (crash) ❖ The first C program ‣Hello world ❖ Programming style ‣White space, indentation, comments
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 12 Operating Systems
❖ Oversee computer operations ❖ Interface computer, peripherals, humans ❖ Common varieties ‣DOS ๏Windows + XP + Vista + 7 + 8 ‣Unix + Linux ๏Mac OS ๏... iOS, Android, ... ❖ And the winner (at least in ECE15) is: ‣Unix
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 13 Unix Commands
❖ Prompt (here > ) means ready for next command ❖ Run command by typing its name > ls info story ‣Type arguments after name > more info Observing Files I can write ❖ ls - list files in current directory (almost) anything ❖ more - print files on screen > more story ‣more name - prints file called name Once upon a time... > cp story tale Moving and Removing Files > ls ❖ cp - copy (duplicate) file info story tale ‣ cp name1 name2 - copy name1 to name2 > more tale ‣ both will have identical contents Once upon a time... ❖ mv - move (rename) file > rm story ‣ mv name1 name2 - move name1 to name2 remove story? y ‣name1 disappears, contents in name2 - remove files > ls ❖ rm info tale ‣rm name - remove file called name Lecture 1 ECE15: Introduction to Computer Programming Using the C Language > 14 More UNIX Commands
Directories (Folders) ❖ mkdir - create a subdirectory in current directory ‣mkdir name - create a directory called name ❖ cd - change directory ‣cd name - change to directory name ‣. current directory, .. parent directory, ~ home directory, - previous directory ❖ rmdir - remove directory ‣rmdir name - remove directory name ❖ pwd - path to preset working directory Other Commands ❖ date, cal ❖ Even more commands? See Website ❖ What’s missing? Create a file! Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 15 First Lecture Outline
❖ Operating systems ‣Unix
❖ Editors ‣Emacs ❖ Compiling and running ‣Create, compile, run (crash) ❖ The first C program ‣Hello world ❖ Programming style ‣White space, indentation, comments
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 16 Creating a File
❖ Text editor ❖ Not rich text format editors: word ❖ Plain text editor And ‣wordpad the winner ‣VI/VIM is..... ‣Emacs
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 17 Emacs Commands
❖ Run by typing: > emacs “filename”
❖ Start typing
❖ Move around: arrow keys
❖ Save: ctrl-x ctrl-s
❖ Exit: ctrl-x ctrl-c
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 18 Example
> ls > emacs test > ls test > more test This is an emacs text editor
I can type anything I wish
So easy
At the end I: * save (ctrl-x ctrl-s) * exit (ctrl-x ctrl-c) >
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 19 First Lecture Outline
❖ Operating systems ‣Unix
❖ Editors ‣Emacs ❖ Compiling and running ‣Create, compile, run (crash) ❖ The first C program ‣Hello world ❖ Programming style ‣White space, indentation, comments
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 20 C Program Files
❖ Source-code file ‣We create ‣Almost English
❖ Executable file ‣Generated from source file ‣By a compiler ‣Gibberish to us ‣Computer understands
❖ Need to know ‣Create source ‣Compile it to create executable ‣Run executable Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 21 First Lecture Outline
❖ Operating systems ‣Unix
❖ Editors ‣Emacs ❖ Compiling and running ‣Create, compile, run (crash) ❖ The first C program ‣Hello world ❖ Programming style ‣White space, indentation, comments
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 22 “First C Program”
First C tutorial, 1974: Its first program:
Programming in C A Tutorial main() { ߝ printf(“hello, world”); } Brian W. Kernighan
Bell Laboratories, Murray Hill, N. J. Nowadays:
#include
2. A Simple C Program main( ) { printf("hello, world"); }
A C program consists of one or more functions, which are similar to the functions and subroutines of a Fortran program or the procedures of PL/I, and perhaps some external data definitions. main is such a function, and in fact all C programs must have a main. Execution of the program begins at the first state- ment of main. main will usually invoke other functions to perform its job, some coming from the same program, and others from libraries. One method of communicating data between functions is by arguments. The parentheses following the function name surround the argument list; here main is a function of no arguments, indicated by ( ). The {} enclose the statements of the function. Individual statements end with a semicolon but are other- wise free-format. printf is a library function which will format and print output on the terminal (unless some other des- tination is specified). In this case it prints Create hello.c
❖ Name of source-code file ‣ filename.c ‣.c extension indicates it’s a c program (like .jpg, .html, .pdf) ‣filename: descriptive - print.c, add.c, primes.c ‣Starts with letter or digit, can have letters, digits, _, -, no spaces ❖ Create hello.c > ls test > rm test > ls > emacs hello.c > ls hello.c >
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 24 Compile
❖ Compiler - program that takes source file and creates executable ❖ Several compilers ‣C compiler - cc ‣We will use gnu C compiler - gcc ❖ Run gcc on source-code file ‣gcc filename.c ❖ Creates executable file: a.out > ls regardless of source file ‣a.out hello.c > gcc hello.c > ls a.out hello.c >
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 25 Run
❖ a.out is an executable file ❖ Can run it Hint If a.out exists in your ❖ To run a.out type its name current directory, but typing a.out doesn’t > a.out work, tell unix to look for > hello world it in the current directory > by typing: ./a.out For a permanent fix, add PATH=$PATH:. to the .bashrc file in your That’s it! home directory. Then type: source .bashrc
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 26 Compilation Options
❖ Normal compilation: gcc (creates a.out) ❖ Options ‣-o filename - places executable in filename (instead of a.out) > gcc -o hello.out hello.c > gcc hello.c -o hello.out ‣-Wall - prints all compilation warnings > gcc -Wall hello.c ‣-std=c99 - Uses ANSI C99 compiler > gcc -std=c99 hello.c ‣--help - shows all command line arguments > gcc --help ‣As always, when in doubt: Google “gcc options”
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 27 Summary
❖ Use any editor (emacs, vi, notepad, wordpad) ❖ Create source-code file (hello.c) ❖ Compile file using Gnu C Compiler (gcc hello.c) ❖ Compiler creates executable file a.out What’s next? ❖ Run executable by typing its name (a.out) The finer things in life! > ls > emacs hello.c > ls > hello.c > gcc hello.c > ls > a.out hello.c > a.out Hello world! > Done! Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 28 First Lecture Outline
❖ Operating systems ‣Unix
❖ Editors ‣Emacs ❖ Compiling and running ‣Create, compile, run (crash) ❖ The first C program ‣Hello world ❖ Programming style ‣White space, indentation, comments
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 29 Programming Style
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 30 White Space
❖ One or more ‣ Spaces, tabs, newlines ‣ Treated as one ❖ Same: int main() { printf("Hello, world!\n"); return 0; }
int main(){printf("Hello, world!\n");return 0;}
❖ Purpose ‣ Clarity - one command per line, reveals structure ‣ Appearance (style) ❖ Similar to indentation and comments (next)
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language Common Indentation Styles
#include
Les goûts et les couleurs ne se discutent pas
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 32 Comments
❖ Ignored by computer, very useful for humans ❖ Text from // till end of line printf("Hello, world!\n"); // I am a comment ❖ Text between /* and the next */ printf("Hello, world!\n"); /* Yo tambien */ ‣Useful for multiple-line comments Useful for: /* Je suis /* Je suis Understanding un long ➧ ➧ * un long Co-workers commentaire */ * commentaire */ ‣Cannot be nested You later Grading... /* I am /* an invalid comment */ lastima */ Just
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language do it! 33 Example
❖ Use to explain code #include
❖ Like comments ❖ without - dull ❖ With right amount - dandy
But don’t overdo!
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 35 First Lecture Outline
❖ Operating systems ‣Unix
❖ Editors ‣Emacs ❖ Compiling and running ‣Create, compile, run (crash) ❖ The first C program ‣Hello world ❖ Programming style ‣White space, indentation, comments
Lecture 1 ECE15: Introduction to Computer Programming Using the C Language 36 Who Gets?
❖ Most
❖ Some
❖ None
Lecture 2 ECE15: Introduction to Computer Programming Using the C Language 37