2.1 first c program
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/1.jpg)
2.1 First C Program
![Page 2: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/2.jpg)
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!!!!
![Page 3: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/3.jpg)
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>
![Page 4: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/4.jpg)
First Program
• First type in header files
![Page 5: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/5.jpg)
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
![Page 6: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/6.jpg)
Int main(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.
![Page 7: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/7.jpg)
Int main(void)
![Page 8: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/8.jpg)
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.
![Page 9: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/9.jpg)
Main
• Inside the main function we start writing the instructions for the computer
• Computer executes code one after the other in order
![Page 10: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/10.jpg)
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
![Page 11: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/11.jpg)
Printf()
![Page 12: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/12.jpg)
Printf()
• Double quote “hello”• Quotes needed because we just want to print
whatever is inside, no special operations or variables
![Page 13: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/13.jpg)
;
• 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
![Page 14: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/14.jpg)
Return 0;
![Page 15: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/15.jpg)
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.
![Page 16: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/16.jpg)
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
![Page 17: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/17.jpg)
Compiler
• Source code -> Machine Code
• Needs to recompile after every change in program
• Input .c file -> output .exe file
![Page 18: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/18.jpg)
Program Documentation and Formatting
![Page 19: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/19.jpg)
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.
![Page 20: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/20.jpg)
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
![Page 21: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/21.jpg)
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
![Page 22: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/22.jpg)
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 */
![Page 23: 2.1 First C Program](https://reader036.vdocuments.us/reader036/viewer/2022062423/56814e35550346895dbb9d8a/html5/thumbnails/23.jpg)
Reading
• Pg 26-28 for further description of initial C code