CS110_01b_2pgms - EECS 110: 1b Two Simple C Programs Jack...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS 110: 1b Two Simple C Programs Jack Tumblin jet@cs.northwestern.edu 1 C is just one of many ... Machine language, Assembly language `symbolic language' (book) x86 or PPC assembler, AltiVec, MMX, SSE,... Interpreted language/scripting language Matlab* Alice, Python, PERL,Basic, Bash, HTML, Java*, ... Compiled Language C/C++, C#, Obj-C, FORTRAN, Ada, LISP, Java*,Matlab*... 2 What's a C Program? A "Program": a written set of organized rules for a computer to follow, Written in C "language": a notation for these rules !written to be Human Readable, but Machine-translated (compiled) to machine code `portable' to almost any computer Structured (separate, nested, re-usable statements)... 3 `Structured': How C is Organized Literals and Variables (with their `data types') Operators Expressions Statements Functions Libraries Programs `Systems' (e.g. OS-X, TCP/IP, Web) 4 `The 8 Big Ideas of C' 1. Nesting, Problem Decomposition 2. Operators, Expressions and Statements 3. Functions, Modules and Interfaces(and ) 4. Arrays (and all the above ) 5. Strings (and all the above ) 6. Pointers (and all the above ) 7. Malloc/Free (and all the above ) 8. Structures (and all the above ) Several Ways to Do Most Things (ptr,array,struct, ADT...) 5 A Simple C Program /* test.c Show some key parts of all C programs. */ #include<stdio.h> #include<stdlib.h> int main() { printf("This is a test.\n"); return 0; } 6 A Simple C Program /* test.c Show some key parts of all C programs. */ A comment is any text #include<stdio.h> between /* and */ , like this. #include<stdlib.h> Use comments liberally to int main() { explain WHY you wrote what printf("This is a test.\n"); you did. WHY you made return 0; these choices; anything that } isn't obvious from the code. The compiler ignores all comments everywhere. 7 !! AAHHRRGGHH !! A !No! C Program Simple /* test.c This is a program that prints "This is a Test",newline */ A comment explains #include<stdio.h> WHY you wrote the code; #include<stdlib.h> your reasoning & motivation. int main() { Avoid tautology: printf("This is a test.\n"); Don't restate what's already return 0; obvious in the code. } /* this comment is a comment*/ 8 A Simple C Program /* test.c Show some key parts of all C programs. */ "#include" means #include "read in this file, put it here" #include<stdio.h> #include<stdlib.h> int main() { printf("This is a test.\n"); Thisreturn 0; is a Any file ending in } preprocessor directive. ".h" is called a "stdio" is a file (library) that provides "standard input/output" functions (such as printf) All preprocessor directives start with a # (more on this later) ".h" is called a header file 9 A Simple C Program /* test.c Show some key parts of all C programs. */ #include<stdio.h> #include<stdlib.h> int main() All C or C++ programs begin { in the `main()' function printf("This is a test.\n"); (sometimes hard to find) return 0; main() usually holds calls to } Every C or C++ program MUST have a `main' function. other functions. 10 A Simple C Program /* test.c All functions use Show some key parts of all open and close braces C programs. */ to mark the beginning and #include<stdio.h> #include<stdlib.h> int main() The block of statement(s) { printf("This is a test.\n"); between those curly braces are return 0; called the body of the function. } (Here it's just the end of the function. two statements long). 11 What is a `Function'? A function is a named block of statements that performs one sensible, well-defined operation. It hides the tedious details (so you can't ruin them by accident) A function has zero or more input arguments and zero or one output values. 12 A Simple C Program #include<stdio.h> #include<stdlib.h> A function == a block of statements with a given name. EX: main(), printf() int main() { printf("This is a test.\n"); return 0; } (Name + block of statements) == the definition of the function called main(). It's body holds two statements. 13 So what is a `Statement'? A statement is one logical step in your program. Ideally, it is one line in your program, but it can be longer if necessary. (hard to read. don't). Semicolon ; == "end of this statement". The main() function's body has two statements: (just count the semicolons; that's how the compiler does it...) 14 A Simple C Program #include<stdlib.h> #include<stdio.h> int main() { printf("This is a test.\n"); return 0; } ALL statements end with a semicolon ; `statement terminator' symbol for C/C++. printf() details: \n means `move on to the next line'. We will learn more of these format specifiers later. 15 A Simple C Program #include<stdlib.h> #include<stdio.h> int main() { printf("This is a test.\n"); return 0; } This statement says: "we are done, and everything completed successfully" (more about return statements later...) 16 A Simple C Program The output of this program is: >This is a test. > (printed in the `console' window) 17 A Simple C Program Notes: C is case sensitive. Printf() is NOT the same as printf(). All C commands are lowercase! Orderly indenting is very important; it makes your code readable. DevCPP, VisualStudio will help you (look up `smart indenting'). Always write comments in your programs. Comments are enclosed in /* and */ // DevCPP allows C++ style comments too! 18 Indenting Advice Crucial for Human-Readable code! C compiler ignores it (jargon: `white-space') `K&R-style': fewer lines, but sometimes confusing `BSD-style': simple--align curly-brackets vertically int main() { int i; for(i=0; i<10; i++) { printf("Old way:K&R"); printf("Confused?\n"); } return 0; } } 19 int main() { int i; for(i=0; i<10; i++) { printf("Newer way: BSD"); printf("is Simpler!\n"); } return 0; Comment Syntax: Pair-wise /* This /* small */ comment is wrong */ /* This small comment uses correct syntax */ /* And so does this one. */ 20 See? Comment Syntax: Pair-wise /* This /* small */ comment is wrong */ IGNORED /* This small comment uses correct syntax */ /* And so does this one. */ 21 See? Comment Syntax: Pair-wise /* This /* small */ comment is wrong */ comment ?code? /* Suggestion: line up comment delimiters vertically like this, but never put comments-within-comments ("nesting") because most compilers will get confused. */ 22 Numbers: Bits, Bytes and Memory Bit = BInary digiT = a 2-way choice = (1/0, yes/no) = the fundamental unit of information Byte = 8 bits grouped together: 28 = 256 choices Memory is a long list of stored bytes. Sequentially numbered; address. 23 Numbers: Bits, Bytes and Memory Address 0 1 2 3 4 5 stored bytes (8 bits) bits Memory 24 Variables Inside the computer: a variable is a reserved location in memory, and it holds data that can change. Same idea you know from algebra: 3x +5 = y x and y are the `variables' But Generalized: a variable is a `placeholder' for one piece of data. Value may be unknown until the program runs (`run-time'). ALWAYS Declare a variable first, (create it), then ALWAYS Initialize (set value) before you use it! 25 /******************************************* Hydra.c -- a program with variables. *******************************************/ #include <stdio.h> int main() { int heads; int eyes; heads = 3; eyes = heads * 2; (nice trick for emphasizing comments) /* assign a value */ /* compute a value */ printf("It has %d heads and %d eyes! \n", heads, eyes); return 0; } Output: > It has 3 heads and 6 eyes! 26 /******************************************* Hydra.c -- a program with variables. *******************************************/ #include <stdio.h> int main() { int heads; int eyes; heads = 3; eyes = heads * 2; `declare' each variable; write a statement to set its name and type /* assign a value */ /* compute a value */ printf("It has %d heads and %d eyes! \n", heads, eyes); return 0; } Output: > It has 3 heads and 6 eyes! 27 /******************************************* Hydra.c -- a program with variables. *******************************************/ #include <stdio.h> int main() { int heads; int eyes; heads = 3; eyes = heads * 2; After that, set values of variables /* assign a value */ /* compute a value */ printf("It has %d heads and %d eyes! \n", heads, eyes); return 0; } Output: > It has 3 heads and 6 eyes! 28 For Next Time: Read Sections 2.1-2.5 of textbook carefully (fundamentals!) Experiment! play around with the C code in any of the `starter code' projects... 29 END END 30 How to `declare' Variables Make declaration statement ; (always ends with ;) The format: data_type var, var, ... ; Example: int counter1, counter2; Where? Declare variables at the start of a function, right after the opening curly-brace { 31 ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online