+ All Categories
Home > Documents > 16.216 ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015...

16.216 ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015...

Date post: 03-Jan-2016
Category:
Upload: julia-ray
View: 213 times
Download: 0 times
Share this document with a friend
25
16.216 ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 2: Basic C program structure Data in C: Data types, constants, and variables
Transcript

16.216ECE Application

ProgrammingInstructors:

Dr. Michael Geiger & Nasibeh NasiriFall 2015

Lecture 2:Basic C program structure

Data in C: Data types, constants, and variables

Lecture outline Announcements/reminders

Sign up for the course discussion group No lecture Monday (Labor Day) Program 1 due Wednesday, 9/9

40 points: complete simple C program 10 points: introduce yourself to your instructor

If we’ve met previously, you still have to meet with me this semester to earn these points.

Today’s lecture Basic C program structure Comments Data types Constants Visual Studio demo

04/20/23ECE Application Programming:

Lecture 2 2

3

Our first C program#include <stdio.h>int main(){

printf("Hello World!\n");return 0;

}

04/20/23ECE Application Programming:

Lecture 1

4

Our first C program

#include <stdio.h>int main(){

printf("Hello World!\n");return 0;

}

# 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

04/20/23ECE Application Programming:

Lecture 1

5

Our first C program

#include <stdio.h>int main(){

printf("Hello World!\n");return 0;

}

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 ()

04/20/23ECE Application Programming:

Lecture 1

6

Our first C program

#include <stdio.h>int main(){

printf("Hello World!\n");return 0;

}

{ } 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.

04/20/23ECE Application Programming:

Lecture 1

7

Our first C program

#include <stdio.h>int main(){

printf("Hello World!\n");return 0;

}

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.

Every statement must end with a ";". Preprocessing directives do not end with a ";" (but must end with a return).

04/20/23ECE Application Programming:

Lecture 1

8

Our first C program

#include <stdio.h>int main(){

printf("Hello World!\n");return 0;

}

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

04/20/23ECE Application Programming:

Lecture 1

9

Our first C program

#include <stdio.h>int main(){

printf("Hello World!\n");return 0;

}

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.

04/20/23ECE Application Programming:

Lecture 1

10

Variations #1 of first program

#include <stdio.h>int main(){ printf("Hello"); printf("there"); printf("World!"); return 0;}

04/20/23ECE Application Programming:

Lecture 1

11

Variations #2 of first program

#include <stdio.h>int main(){ printf("Hello\n"); printf("there\n"); printf("World!\n"); return 0;}

04/20/23ECE Application Programming:

Lecture 1

12

Variations #3 of first program

#include <stdio.h>int main(){ printf("Hello\nthere\nWorld!\n"); return 0;}

04/20/23ECE Application Programming:

Lecture 1

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.

04/20/23ECE Application Programming:

Lecture 1

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

04/20/23ECE Application Programming:

Lecture 2 14

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

04/20/23ECE Application Programming:

Lecture 2 15

Comment example/* 16.216 ECE Application Programming Instructor: M. Geiger 04/20/23 hello.c: Intro program to demonstrate basic C program structure and output */

#include <stdio.h>

// Main program: prints basic string and exitsint main(){

printf("Hello World!\n"); // Commentreturn 0;

}

04/20/23ECE Application Programming:

Lecture 2 16

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

04/20/23ECE Application Programming:

Lecture 2 17

Four Types of Basic Data Integer int Floating point (single precision) float Double Precisiondouble

Character char

1804/20/23ECE Application Programming:

Lecture 2

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)

1904/20/23ECE 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)

04/20/23ECE Application Programming:

Lecture 2

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

2104/20/23ECE Application Programming:

Lecture 2

float/double Constants Range of float (32 bits)

± 1.175494351 E – 38± 3.402823466 E + 38

Range of double (64 bits)2.2250738585072014 E – 3081.7976931348623158 E + 308

2204/20/23ECE 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 e-mail to [email protected] File name matters!

04/20/23ECE Application Programming:

Lecture 2 23

Visual Studio demo Basics of setting up project

Steps covered in detail in Program 1 spec. Xcode users: steps are very similar

Choose “Start New Project” when Xcode opens Project type:

Under list of “OS X” choices on the left, choose “Application” Choose “Command Line Tool” from the options that appear

Name your project (project name doesn’t matter) and choose a directory. Also, ensure that the type of project is set to “C” using appropriate drop-down menu

Project includes simple C file named “main.c” You can edit this file to include your own code Rename this file so the name matches the program spec

04/20/23ECE Application Programming:

Lecture 2 24

Final notes Next time (Wednesday, 9/9)

Variables Operators Basic input/output with scanf()/printf()

Reminders: Sign up for the course discussion group No lecture Monday (Labor Day) Program 1 due Wednesday, 9/9

40 points: complete simple C program 10 points: introduce yourself to your instructor

If we’ve met previously, you still have to meet with me this semester to earn these points.

04/20/23ECE Application Programming:

Lecture 2 25


Recommended