Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2018 Lecture 2: Basic C program structure IDE demo
2
ECE Application Programming: Lecture 2
Lecture outline Announcements/reminders Chapter 1 exercises due Monday, 9/10 Textbook exercises always due 3 days after related lecture—check ”Assignments” tab regularly!!! Program 1 due Wednesday, 9/12 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program Today’s lecture Basic C program structure Comments IDE demo 5/31/2019 ECE Application Programming: Lecture 2
3
ECE Application Programming: Lecture 2
Our first C program #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } 5/31/2019 ECE Application Programming: Lecture 2
4
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/31/2019 ECE Application Programming: Lecture 2
5
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/31/2019 ECE Application Programming: Lecture 2
6
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/31/2019 ECE Application Programming: Lecture 2
7
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/31/2019 ECE Application Programming: Lecture 2
8
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/31/2019 ECE Application Programming: Lecture 2
9
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/31/2019 ECE Application Programming: Lecture 2
10
Variations #1 of first program
#include <stdio.h> int main() { printf("Hello"); printf("there"); printf("World!"); return 0; } 5/31/2019 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; } 5/31/2019 ECE Application Programming: Lecture 2
12
Variations #3 of first program
#include <stdio.h> int main() { printf("Hello\nthere\nWorld!\n"); return 0; } 5/31/2019 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. 5/31/2019 ECE Application Programming: Lecture 2
14
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/31/2019 ECE Application Programming: Lecture 2
15
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/31/2019 ECE Application Programming: Lecture 2
16
ECE Application Programming: Lecture 2
Comment example /* ECE Application Programming Instructor: M. Geiger 5/31/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/31/2019 ECE Application Programming: Lecture 2
17
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): The name of the class The current semester The days on which lectures meet The instructor’s name For this assignment, spacing matters!!! 5/31/2019 ECE Application Programming: Lecture 2
18
ECE Application Programming: Lecture 2
IDE demo Basics of writing and compiling code Develop mode vs. submit mode Dealing with errors Test cases 5/31/2019 ECE Application Programming: Lecture 2
19
ECE Application Programming: Lecture 2
Final notes Next time Data types Variables Reminders Chapter 1 exercises due Monday, 9/10 Textbook exercises always due 3 days after related lecture—check ”Assignments” tab regularly!!! Program 1 due Wednesday, 9/12 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program 5/31/2019 ECE Application Programming: Lecture 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.