1 / 23

2.1 First C Program

2.1 First C Program. First Program. Open visual studio, click new file Save as “ programName.c ” Program must start with letter and have no spaces Must end with .c (visual studio will autoset .c if you select file type There can be no spaces in the name!!!!. Header files.

gilles
Télécharger la présentation

2.1 First C Program

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. 2.1 First C Program

  2. First Program • Open visual studio, click new file • Save as “programName.c” • Program must start with letter and have no spaces • Must end with .c (visual studio will autoset .c if you select file type • There can be no spaces in the name!!!!

  3. Header files • At the top of any program, you must include “header files” • These files are subprograms that tell the computer how to use built in features of the language. • Ex: <stdio.h>, <math.h>, <string.h>

  4. First Program • First type in header files

  5. Main Function • Ever Program in C has something called a main function • Basically tells the computer where to start executing code. • Goes after the header files

  6. Intmain(void) • int main(void) • Int we’ll talk about later • Main tells computer this is where to start • Void tells computer not to expect any inputs at very beginning.

  7. Int main(void)

  8. Curly Brace • Whenever we want to enclose chunks of code, we always put the curly brace around them. • In this case, we want to section off the main chuck of code.

  9. Main • Inside the main function we start writing the instructions for the computer • Computer executes code one after the other in order

  10. Printf() • Printf() is a built in feature of C that was activated when we included stdio.h into the code • Its function is to print things to the screen

  11. Printf()

  12. Printf() • Double quote “hello” • Quotes needed because we just want to print whatever is inside, no special operations or variables

  13. ; • Ever separate execeuted line in C needs to be ended with semi colon • If you forget semi colon the program DOESN’T WORK • Imagine a program with 500 lines of code and you forget one semi color…..= frustrating

  14. Return 0;

  15. return 0; • Most chucks of code, from now on will be called Functions usually return some type of value. • In our simple program, were not creating a special value so we will return 0 which in computer language is a sign of successful execution.

  16. First Program • Click Save • Open VS Command Prompt • Navigate using “cd” to folder that holds your .c file • Type “cl firstProgram.c” +Entr • Type “firstProgram”+Entr • Viola

  17. Compiler • Source code -> Machine Code • Needs to recompile after every change in program • Input .c file -> output .exe file

  18. Program Documentation and Formatting

  19. Formatting • All functions are indented from their start • Documentation is usually parallel to each other • Separate header files, different functions, variables, and other types of code.

  20. Documenting • Required for full marks and in real world • Helps reader/programmer understand code • Two ways to document code • /* ……… */ is one way that omits everything inside slashes from compiler • // omits everything on that line from compiler

  21. Things to Remember • Always include ; when needed • Always document • Always save as .c file • Keep track of opening and closing brackets • Formatting/Documenting makes things much easier to debug

  22. Assignment • Create another program (brand new not just last program copy and pasted) that says “I am now a programmer” but this time try to : • return 1 • Forget ; • Forget to close bracket • Forget header file • Spell printf wrong • Forget to include closing */

  23. Reading • Pg 26-28 for further description of initial C code

More Related