2.1 First C Program

23
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 Presentation

Transcript of 2.1 First C Program

Page 1: 2.1 First C Program

2.1 First C Program

Page 2: 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!!!!

Page 3: 2.1 First C Program

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

First Program

• First type in header files

Page 5: 2.1 First C Program

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

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

Int main(void)

Page 8: 2.1 First C Program

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

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

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

Printf()

Page 12: 2.1 First C Program

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

;

• 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

Return 0;

Page 15: 2.1 First C Program

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

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

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

Program Documentation and Formatting

Page 19: 2.1 First C Program

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

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

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

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

Reading

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