240 likes | 419 Views
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)
E N D
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
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)
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.
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:
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.
Your first C Program: Hello.c /* Hello program */ #include <stdio.h> int main() { printf( "Hello!\n"); return 0; }
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)
Run the Program • To execute your program click the menu item ! Start Without Debugging
Hello.c: Line by Line /* Hello program */ #include <stdio.h> int main() { printf( "Hello!\n"); return 0; }
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
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
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.
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
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
Hello.c: Line by Line return 0; • Terminates the execution of main() • Indicates that main() returns the value 0
Review of Hello Program /* Hello program */ #include <stdio.h> int main() { printf( "Hello!\n"); return 0; }
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
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!
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; }