1 / 26

16.216 ECE Application Programming

16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2012 Lecture 2: Basic C program structure Introducing data representations in C. Lecture outline. Announcements/reminders Sign up for the course discussion group on Piazza Program 1 due Monday, 9/10

Download Presentation

16.216 ECE Application Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 2: Basic C program structure Introducing data representations in C

  2. Lecture outline • Announcements/reminders • Sign up for the course discussion group on Piazza • Program 1 due Monday, 9/10 • Basic C program structure • Preprocessor directives • Main program • Simple output • Readability: formatting, comments • Representing data in C • Number systems • Variables and constants ECE Application Programming: Lecture 2

  3. Our first C program #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;} ECE Application Programming: Lecture 2

  4. Our first C program # indicates pre-processor directive include is the directive stdio.h is the name of the file to "insert" into our program. The <> means it is part of the C development system #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;} ECE Application Programming: Lecture 2

  5. Our first C program main is the name of the primary (or main) procedure. All ANSI C programs must have a main routine named main The () indicates that main is the name of a procedure. All procedure references must be followed with () #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;} ECE Application Programming: Lecture 2

  6. Our first C program { } enclose a "block". A block is zero or more C statements. Note that code inside a block is typically indented for readability—knowing what code is inside the current block is quite useful. #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;} ECE Application Programming: Lecture 2

  7. Our first C program printf() is a "built-in" function (which is actually defined in stdio.h). "Hello World!" is the string to print. More formally, this is called the control string or control specifier. #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;} Every statement must end with a ";". Preprocessing directives do not end with a ";" (but must end with a return). ECE Application Programming: Lecture 2

  8. Our first C program The \n is an escape character used by the printf function; inserting this character in the control string causes a “newline” to be printed—it’s as if you hit the “Enter” key #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;} ECE Application Programming: Lecture 2

  9. Our first C program The int tells the compiler our main() program will return an integer to the operating system; the return tells what integer value to return. This keyword could be void, indicating that the program returns nothing to the OS. #include <stdio.h>int main(){ printf("Hello World!\n"); return 0;} ECE Application Programming: Lecture 2

  10. Variations #1 of first program #include <stdio.h>int main(){ printf("Hello"); printf("there"); printf("World!"); return 0;} ECE Application Programming: Lecture 2

  11. Variations #2 of first program #include <stdio.h>int main(){ printf("Hello\n"); printf("there\n"); printf("World!\n"); return 0;} ECE Application Programming: Lecture 2

  12. Variations #3 of first program #include <stdio.h>int main(){ printf("Hello\nthere\nWorld!\n"); return 0;} ECE Application Programming: Lecture 2

  13. Variations #4 of first program #include <stdio.h>int main(){printf ("Hello\nthere\nWorld!\n");return 0;} Note while this is syntactically correct, it leaves much to be desired in terms of readability. ECE Application Programming: Lecture 2

  14. Code readability • Readability wouldn’t matter if: • Entire code project written by one person • All code was in same file • Same person is the only one to use the code • Code was used only for a short period of time • More typically: • Projects are split—multiple programmers and files • Code usually reused • Multiple users • Used/adapted (hopefully) over long period of time • You may reuse code ... but forget what you originally wrote! • Bottom line: code needs to be readable ECE Application Programming: Lecture 2

  15. Comments • C allows you to add comments to your code • Single line comments: start with // • Multi-line comments: start with /* end with */ • Typical uses • Multi-line comment at start of program with • Author’s name (& other info if appropriate) • Date started/modified • File name • Description of overall file functionality • For individual code sections • Single/multi-line comment for major section of code performing single function • Single line comment for single line of code if that line alone is important ECE Application Programming: Lecture 2

  16. Comment example /* 16.216 ECE Application Programming Instructor: M. Geiger 11/8/2014 hello.c: Intro program to demonstrate basic C program structure and output */ #include <stdio.h> // Standard I/O library /* Main program: prints basic string and exits */ int main() {printf("Hello World!\n"); return 0; } ECE Application Programming: Lecture 2

  17. Representing data in C • Two major questions (for now) • What kind of data are we trying to represent? • Data types • Can the program change the data? • Constants vs. variables ECE Application Programming: Lecture 2

  18. Four Types of Basic Data • Integer int • Floating point (single precision) float • Double Precision double • Character char ECE Application Programming: Lecture 2

  19. Integer Constants • Any positive or negative number without a decimal point (or other illegal symbol). • Legal values: 5 -10 +251000 253 -26351 +98 • Illegal values:2,523 (comma) 6.5 (decimal point)$59 (dollar sign) 5. (decimal point) ECE Application Programming: Lecture 2

  20. Range of Integers (Machine Dependent) unsigned signed • char 0  255 -128  +127 (8 bits) • short int 0  65535 -32768  + 32767short (16 bits) • int 0 to 4294967295 -2147483648  2147483647 longlong int (32 bits) ECE Application Programming: Lecture 2

  21. float/double Constants • Any signed or unsigned number with a decimal point • Legal values: 5. .6 +2.70.0 -6.5 +8. 43.4 • Legal (exponential notation):1.624e3 7.32e-2 6.02e23 1.0e2-4.23e2 +4.0e2 1.23e-4 +11.2e+7 • Illegal: $54.23 6,349.70 1.0E5 ECE Application Programming: Lecture 2

  22. float/double Constants • Range of float (32 bits) ± 1.175494351 E – 38 ± 3.402823466 E + 38 • Range of double (64 bits)± 2.2250738585072014 E – 308± 1.7976931348623158 E + 308 ECE Application Programming: Lecture 2

  23. Character Constants • Stored in ASCII or UNICODE • Signified by single quotes (’ ’) • Valid character constants ’A’ ’B’ ’d’ ’z’ ’1’ ’2’’!’ ’+’ ’>’ ’?’ ’ ’ ’#’ • Invalid character constants’GEIGER’ ’\’ ’CR’ ’LF’ ’’’ ’’’’ ’”’ ”Q” ECE Application Programming: Lecture 2

  24. Character Escape Sequences ECE Application Programming: Lecture 2

  25. Using #define with constants • Often makes sense to give constant value a symbolic name for readability • Don’t want constant wasting memory space • Use #define to define a macro • Macro: named code fragment; when name is used, the preprocessor replaces name with code • Syntax: #define <name> <code> • Common use: defining constants • By convention, start constant values with capital letters • e.g. #define NumberOne 1 • At compile time, all uses of “NumberOne” in your program are replaced with “1” and then compiled ECE Application Programming: Lecture 2

  26. Next time • Continue discussing data in C • Remember: Program 1 due next Monday, 9/10 ECE Application Programming: Lecture 2

More Related