2.1 First C Program

Preview:

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

Citation preview

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

• 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>

First Program

• First type in header files

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

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.

Int main(void)

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.

Main

• Inside the main function we start writing the instructions for the computer

• Computer executes code one after the other in order

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

Printf()

Printf()

• Double quote “hello”• Quotes needed because we just want to print

whatever is inside, no special operations or variables

;

• 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

Return 0;

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.

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

Compiler

• Source code -> Machine Code

• Needs to recompile after every change in program

• Input .c file -> output .exe file

Program Documentation and Formatting

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.

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

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

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 */

Reading

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

Recommended