EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Programming With C.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 2: Basic C program structure Data in C: Data types,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
ECE Application Programming
CSCE 206 Structured Programming in C
User-Written Functions
ECE Application Programming
CSC201: Computer Programming
ECE Application Programming
ECE Application Programming
EECE.2160 ECE Application Programming
ECE Application Programming
ECE Application Programming
Chapter 2 - Introduction to C Programming
ECE Application Programming
EECE.2160 ECE Application Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Programming Fundamentals Lecture #3 Overview of Computer Programming
EECE.2160 ECE Application Programming
Introduction to C Topics Compilation Using the gcc Compiler
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 2 - Introduction to C Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Introduction to C Topics Compilation Using the gcc Compiler
EECE.2160 ECE Application Programming
Introduction to C Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 2 part #1 C++ Program Structure
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2018 Lecture 2: Basic C program structure Visual Studio Demo

ECE Application Programming: Lecture 2 Lecture outline Announcements/reminders Sign up for the course discussion group Program 1 due Monday, 1/29 10 points: e-mail Dr. Geiger for shared Dropbox folder Please specify e-mail address associated with Dropbox account You will receive invitation to join shared folder—must accept invitation 10 points: introduce yourself to your instructor 30 points: complete simple C program Today’s lecture Basic C program structure Comments Visual Studio demo 5/28/2019 ECE Application Programming: Lecture 2

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

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/28/2019 ECE Application Programming: Lecture 2

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/28/2019 ECE Application Programming: Lecture 2

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/28/2019 ECE Application Programming: Lecture 2

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/28/2019 ECE Application Programming: Lecture 2

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/28/2019 ECE Application Programming: Lecture 2

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/28/2019 ECE Application Programming: Lecture 2

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

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

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

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/28/2019 ECE Application Programming: Lecture 2

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/28/2019 ECE Application Programming: Lecture 2

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/28/2019 ECE Application Programming: Lecture 2

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

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 to your Dropbox folder File name matters! 5/28/2019 ECE Application Programming: Lecture 2

ECE Application Programming: Lecture 2 Visual Studio demo Basics of setting up project Steps covered in detail in Program 1 spec. Xcode users: steps are very similar (also covered in Program 1 spec) 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 5/28/2019 ECE Application Programming: Lecture 2

ECE Application Programming: Lecture 2 Final notes Next time Data types Variables Reminders: Sign up for the course discussion group Program 1 due Monday, 1/22 10 points: e-mail Dr. Geiger for shared Dropbox folder Please specify e-mail address associated with Dropbox account You will receive invitation to join shared folder—must accept invitation 10 points: introduce yourself to your instructor 30 points: complete simple C program 5/28/2019 ECE Application Programming: Lecture 2