Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE Application Programming

Similar presentations


Presentation on theme: "ECE Application Programming"— Presentation transcript:

1 16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2011 Lecture 2: Basic C program structure Number system basics

2 ECE Application Programming: Lecture 2
Lecture outline Announcements/reminders Course home page: Need for discussion group (to be set up) Office hours change: No T 11-1 Adding Th 11-1 Will be on campus M/T—just don’t have formal office hours Assignment 1 available today, due Friday Assignment 2 coming Friday, due 9/16 IDE updates (see next slide) Today Basic C program structure Preprocessor directives Main program Simple output Representing data in C: number systems 5/17/2018 ECE Application Programming: Lecture 2

3 ECE Application Programming: Lecture 2
IDE updates Any IDE that allows you to write C programs is acceptable, but you only need one Mac users: Xcode is free Eclipse Can be installed on any platform A couple of potentially useful links (install/use for Linux/Windows) library/os-ecc/ 5/17/2018 ECE Application Programming: Lecture 2

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

5 ECE Application Programming: Lecture 2
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; } 5/17/2018 ECE Application Programming: Lecture 2

6 ECE Application Programming: Lecture 2
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; } 5/17/2018 ECE Application Programming: Lecture 2

7 ECE Application Programming: Lecture 2
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; } 5/17/2018 ECE Application Programming: Lecture 2

8 ECE Application Programming: Lecture 2
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). 5/17/2018 ECE Application Programming: Lecture 2

9 ECE Application Programming: Lecture 2
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; } 5/17/2018 ECE Application Programming: Lecture 2

10 ECE Application Programming: Lecture 2
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; } 5/17/2018 ECE Application Programming: Lecture 2

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

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

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

14 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. 5/17/2018 ECE Application Programming: Lecture 2

15 ECE Application Programming: Lecture 2
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 5/17/2018 ECE Application Programming: Lecture 2

16 ECE Application Programming: Lecture 2
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 5/17/2018 ECE Application Programming: Lecture 2

17 ECE Application Programming: Lecture 2
Comment example /* * ECE Application Programming * Instructor: M. Geiger * 5/17/2018 * hello.cpp: 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; } 5/17/2018 ECE Application Programming: Lecture 2

18 Number representation: bases
Humans operate in decimal (base 10) Why don’t computers? Computers operate in binary (base 2) Each digit is a bit (binary digit) Can also use octal (base 8) or hexadecimal (base 16) Hex digits: 0-15, with A = 10, B = 11, C = 12, D = 13, E = 14, F = 15 Base conversion Binary  hex: start with LSB and make 4-bit groups e.g = 1B716 = 0x1B7 Note that an extra 0 is implied for the first group: 0010001 Binary  decimal: multiply bit 0 by 20, bit 1 by 21, etc. e.g = (0 x 23) + (1 x 22) + (1 x 21) + (1 x 20) = = = 710 Decimal  binary / hex: repeated integer division, where remainder gives you each digit, starting with LSB 5/17/2018 CIS 273: Lecture 9

19 Decimal  binary / hex example
3510 = ?2 35 / 2 = 17 R1 (LSB) 17 / 2 = 8 R1 8 / 2 = 4 R0 4 / 2 = 2 R0 2 / 2 = 1 R0 1 / 2 = 0 R1 (MSB)  3510 = 3510 = ?16 35 / 16 = 2 R3 (LS digit) 2 / 16 = 0 R2 (MS digit)  3510 = 2316 Double-check: does this answer match the binary value we found?  2316 5/17/2018 CIS 273: Lecture 9

20 Base Conversions Convert 2BAD|16 to Base 10 2 B A D |16 = ? |10 | | | | | | | `--- D x 160 = 13 x 1 = 13 | | `----- A x 161 = 10 x 16 = 160 | ` B x 162 = 11 x 256 = 2816 ` x 163 = 2 x 4096 = Therefore, 2BAD|16 = 11181|10

21 Base Conversions Convert 20DD|16 to Base 10 ??

22 Base Conversions Convert 20DD|16 to Base D D |16 = ? |10 | | | | | | | `--- D x 160 = 13 x 1 = 13 | | `----- D x 161 = 13 x 16 = 208 | ` x 162 = 0 x 256 = 0 ` x 163 = 2 x 4096 = Therefore, 20DD|16 = 8413|10

23 Base Conversions Convert FACE|16 to Base 10 ??

24 Base Conversions Convert FACE|16 to Base 10 F A C E |16 = ? |10 | | | | | | | `--- E x 160 = 14 x 1 = 14 | | `----- C x 161 = 12 x 16 = 192 | ` A x 162 = 10 x 256 = 2560 ` F x 163 = 15 x 4096 = Therefore, FACE|16 = 64206|10

25 Work from right to left Divide into 4 bit groups
Base Conversions Dec Bin Oct Hex 0000 00 1 0001 01 2 0010 02 3 0011 03 4 0100 04 5 0101 05 6 0110 06 7 0111 07 8 1000 10 9 1001 11 1010 12 A 1011 13 B 1100 14 C 1101 15 D 1110 16 E 1111 17 F |2=?|16 ## 2 B D |2=2B8D|16 NOTE: # is a place holder for zero Work from right to left Divide into 4 bit groups

26 Base Conversions FACE|16=?|2 F A C E 1111 1010 1100 1110
Dec Bin Oct Hex 0000 00 1 0001 01 2 0010 02 3 0011 03 4 0100 04 5 0101 05 6 0110 06 7 0111 07 8 1000 10 9 1001 11 1010 12 A 1011 13 B 1100 14 C 1101 15 D 1110 16 E 1111 17 F FACE|16=?|2 F A C E \ FACE|16= |2

27 Example 1: Base conversions
Perform the following base conversions 1110 = ?2 = ?16 3710 = ?2 = ?16 1116 = ?10 0x2F = ?2 = ?10 5/17/2018 ECE Application Programming: Lecture 8

28 ECE Application Programming: Lecture 8
Example 1 solution Perform the following base conversions 1110 = = B16 3710 = = 2516 1116 = 1710 0x2F = = 4710 5/17/2018 ECE Application Programming: Lecture 8

29 ECE Application Programming: Lecture 2
Visual Studio demo Brief demonstration to show Starting new project Entering code Executing basic code Dealing with simple compiler/linker errors 5/17/2018 ECE Application Programming: Lecture 2

30 ECE Application Programming: Lecture 2
Assignment #1 Basic assignment to ensure you can write, run, and submit programs Write a short program that prints (each item on its own line): Your name Your major Your class (i.e. freshman, sophomore, etc.) The name and semester of this course Submit only your source (prog1_simple.c) file via to 5/17/2018 ECE Application Programming: Lecture 2

31 ECE Application Programming: Lecture 2
Next time Using data in C Basic I/O 5/17/2018 ECE Application Programming: Lecture 2


Download ppt "ECE Application Programming"

Similar presentations


Ads by Google