1 / 24

EE114: Programming Concepts for Engineers

EE114: Programming Concepts for Engineers. Lecture 1. Introduction. Why C? C is a general purpose programming language Efficient and popular Other programming languages: C++, Java, Matlab, etc Software Microsoft Visual Studio C++ 6.0 (included in the book)

orsen
Download Presentation

EE114: Programming Concepts for Engineers

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. EE114: Programming Concepts for Engineers Lecture 1

  2. Introduction • Why C? • C is a general purpose programming language • Efficient and popular • Other programming languages: C++, Java, Matlab, etc • Software • Microsoft Visual Studio C++ 6.0 (included in the book) • Microsoft Visual Studio .net (available at computer labs: SE322, CS21, CS21A, Math & Science Center etc) • Textbook • C How to Program, 4th Edition, Prentice-Hall, by Deitels • We only cover Chapter 1 to Chapter 13 • Excellent book for you to self-study C++ and Java

  3. Visual Studio C++ .net • Simple programming concepts • Create • Type in the source code of the program (.c, .h files) • Compile • Convert the source code into machine code (generate .exe file) • Run • Tell the computer to run your program (run the .exe file)

  4. Open VS C++ .net

  5. Create a Project • Click the menu item File | New | Project.... • A dialog box is displayed. • In the left pane expand the selection named Visual C++ Projects • and then select Win32. • In the right pane, select Win32 Console Project. • In the Name: text box type hello. • In the Location: text box, click on the button to browse to the folder you created in the previous step.

  6. click OK to continue

  7. Click Application Settings on the left side of the window and then place a check in the box marked Empty project. The screen should look like this:

  8. click OK to continue

  9. Create a source file • Click the menu item Project | Add New Item... • Select C++ File (.cpp) from the list on the right and • type the name Hello.c in the text box labeled Name: • Click the Open button to enter the edit mode.

  10. Your first C Program: Hello.c /* Hello program */ #include <stdio.h> int main() { printf( "Hello!\n"); return 0; }

  11. Compile the Program • Build ---> Build Hello.c Compiling... Hello.c Hello.obj - 0 error(s), 0 warning(s) • Build ---> Build hello.exe Linking... hello.exe - 0 error(s), 0 warning(s)

  12. Run the Program • To execute your program click the menu item ! Start Without Debugging

  13. Hello.c: Line by Line /* Hello program */ #include <stdio.h> int main() { printf( "Hello!\n"); return 0; }

  14. Hello.c: Line by Line /* Hello program */ • /* … */ marks a comment block • Anything in comments is ignored by the compiler • Extremely useful for including explanatory information • You can add comments anywhere in a program

  15. Hello.c: Line by Line #include <stdio.h> • #include • a C preprocessor command • lets you include the code from another file (such as a library) • stdio.h • the standard input-output library • < > means the file stdio.h is in C library directory • “ ” means the file is in the current directory • #include <stdio.h> • allows you to use code from the standard I/O library

  16. Hello.c: Line by Line int main() { ... } • It is a function, only one main function in each program • Curly brackets { } mark the start and the end of the function • Statements of a function are enclosed in { } • Program execution begins at main() • Empty () means main function has no argument. You may also use int main (void) • int means the main function returns an integer. Don't be too concerned about what this means.

  17. Hello.c: Line By Line { printf(“Hello!\n”); return 0; } • Statements of the main function • All statements are terminated by a semicolon • Statements are executed in sequence, from top to bottom

  18. Hello.c: Line by Line printf( "Hello!\n"); • A function defined in stdio.h • Prints out the message “Hello!” to the screen • “Hello!\n” is a string consisting of a series of characters • \n is a new line character • begin a new line of text after the message

  19. Hello.c: Line by Line return 0; • Terminates the execution of main() • Indicates that main() returns the value 0

  20. Review of Hello Program /* Hello program */ #include <stdio.h> int main() { printf( "Hello!\n"); return 0; }

  21. Important Basic Rules for C • Statements end with a semicolon • Strings are enclosed inside double-quotes • Functions start and end with curly brackets • White space doesn’t matter

  22. Hello.c Revisited /* Hello program */ #include <stdio.h> int main () { printf( "Hello!\n"); return 0;} • Edit hello.c • Save file • Recompile: • Click Build Rebuild All • Run Works exactly the same as before but unreadable. Proper indentation is required in this course!

  23. Example 1 • Run the following 3 codes and see the differences of codes and results #include <stdio.h> int main() { printf("Welcome to"); printf("EE114!\n"); return 0; } #include <stdio.h> int main() { printf("Welcome to\n"); printf("EE114!\n"); return 0; } #include <stdio.h> int main() { printf("Welcome to\nEE114!\n"); return 0; }

More Related