90 min

Your first C program

A Visual Studio 2022 lab: an Empty Project and main.c, the Hello, C! program line by line, save, build and run, repairing the first error, a breakpoint and a team mission.

Today's mission

By the end of this lab, your program prints a short device status report, exactly like this:

Sensor Console
Status: READY
Team: <your team name>

Success means three things: the source file is saved, the build succeeds, and the output is exact. On the way you will create a project, write, save, build and run a program, and repair your first error.

Before class: check that the C tools are installed

We write C in Visual Studio 2022. It needs the Microsoft C/C++ compiler, which comes with one "workload":

  1. Open Visual Studio Installer and click Modify on Visual Studio 2022.
  2. Select Desktop development with C++. This installs the Microsoft C/C++ compiler and build tools.
  3. Click Modify to apply, then launch Visual Studio.

If "Empty Project" is missing later, come back to this check.

The IDE

Visual Studio is an IDE (סביבת פיתוח משולבת), an Integrated Development Environment: one program that puts the essential tools in one place.

  • a code editor;
  • a project and file organizer;
  • the compiler and build controls;
  • a program runner;
  • a debugger and error tools.

The IDE is the workshop. C is the language.

Today you need five areas of the Visual Studio window:

#AreaWhat it is for
1Menus and toolbarevery command: File, Build, Debug
2Solution Explorerthe project and its files
3Editorwhere you type the code
4Output / Error Listbuild messages and errors
5Status barwhat Visual Studio is doing now
A simplified Visual Studio window with its five areas numbered.Open full screen

The working loop

Every program in this course goes through the same loop:

CREATE → EDIT → SAVE → BUILD → RUN → CHECK

If CHECK fails, read the evidence (the error message or the wrong output), repair the cause, and go round again from EDIT. Programming is an engineering process that repeats. This loop matters more than any menu.

Create an Empty Project

  1. In the start window, choose Create a new project (or File > New > Project).
  2. Search for Empty Project.
  3. Select Empty Project. Do not choose Console App: its starter file contains C++ code, not C.
  4. Click Next. Do not choose "Empty Solution".
  5. Project name: 01_HelloC. Use English letters and digits only.
  6. Location: C:\C_Course\Lesson01. Not the Downloads folder. Click Create.

Visual Studio has no separate C template: C uses the C++ tools here. The file name ending .c (next step) is what makes the compiler treat the code as C.

Give every project a predictable home. You must know where the .sln file is stored, so you can open the project again next week.

Solution, project, file

A solution (01_HelloC.sln) contains a project (01_HelloC.vcxproj), and the project contains the source files. Your C instructions live in the source file main.c. Open your work later with the .sln file, not with main.c alone.

Add main.c

  1. In Solution Explorer, right-click Source Files. Click inside the project, not on the solution.
  2. Choose Add > New Item... and select C++ File (.cpp).
  3. Change the name to main.c. The .c ending tells the compiler that this file is C.
  4. Click Add. main.c opens in the editor.

Checkpoint: Solution Explorer shows Source Files > main.c.

Type the first program

Type this program exactly, character by character. Do not paste it: typing is how you notice the semicolons, the quotes and the braces.

#include <stdio.h>
 
int main(void)
{
    printf("Hello, C!\n");
    return 0;
}

Predict before you run it: what will appear on the screen, and how many lines?

Hello, C!

One line. printf prints only the text between the quotes, and \n is not printed: it moves to a new line.

Predict first, then click your answer.Open full screen

Read it like an engineer

PartWhat it does
#include <stdio.h>brings in the standard input/output tools; printf is one of them
int main(void)the entry point: every C program starts running at main
{ }a block: everything main does is between the braces
printfformatted output (פלט): prints text to the console
\nnewline: the next output starts on a new line
return 0;ends main and tells the operating system: success

Each instruction that ends with ; is a statement (הוראה). The computer runs the statements in order, from the top of main down.

House style: how we write C

Every program in this course follows six rules:

#RuleExample
1Always int main(void)int main(void)
2The opening brace on its own line{ under int main(void)
3Indent with 4 spaces return 0;
4Every statement ends with ;printf("Hi\n");
5Names in lowercase_with_underscoressensor_count
6A comment before every function/* Prints the status. */

Rules 5 and 6 matter from the next units on (names come with variables, functions come in unit 7), but start the habit now.

Save, build, run

Save

Press Ctrl+S to save the file you are editing, or Ctrl+Shift+S to save all files. An asterisk after the file name on the tab (main.c *) means there are unsaved changes. Saving stores the source code. It does not build or run it.

Build

Press Ctrl+Shift+B, or choose Build > Build Solution. Building has two parts:

main.c → compiler → object code (main.obj) → linker + libraries → program (01_HelloC.exe)

The compiler checks your code and translates it into object code. The linker (מקשר) connects it with the libraries (the code of printf lives in a library) and writes the program. Look in the Output window for:

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Run

Run only after the build succeeds.

  • Ctrl+F5: start without debugging. This is our normal way to run; the console window stays open so you can read it.
  • F5: start with the debugger.
Hello, C!
 
C:\C_Course\Lesson01\01_HelloC\x64\Debug\01_HelloC.exe (process 4120) exited with code 0 (0x0).
Press any key to close this window . . .

Verify three things: the expected text appears; the spelling, punctuation and number of lines match; and the program exits with code 0 (the value from return 0;). The process number changes every run.

The first error

Delete the semicolon at the end of the printf line and build again:

#include <stdio.h>
 
int main(void)
{
    printf("Hello, C!\n")
    return 0;
}

Predict first: what will the compiler complain about? The build fails, and the Error List shows:

C2143   syntax error: missing ';' before 'return'   main.c   line 6

The error points to line 6, but the missing ; belongs at the end of line 5. The compiler only notices the problem when it reaches the next statement, return. That is why we always look at the line the error points to and the line before it.

How to repair any build error:

  1. Read the first error. Later errors are often caused by the first one.
  2. Double-click it: the editor jumps to the line.
  3. Look at the code around it.
  4. Repair and save.
  5. Build again.
The missing semicolon, the error Visual Studio shows, and where the fix goes.Open full screen

Optional: a breakpoint

A breakpoint (נקודת עצירה) tells the debugger to stop before a line runs.

  1. Click the left margin beside the printf line. A red circle marks the breakpoint.
  2. Press F5. The program stops before printf runs: a yellow arrow points at the line, and the console is still empty.
  3. Press F10 to run one line. Now Hello, C! appears in the console. Press F5 to continue to the end.

This is the first time you see the computer run a program one line at a time. We will use the debugger much more with loops.

Team mission: a device status report

Change your program so the console shows exactly:

Sensor Console
Status: READY
Team: <your team name>

Acceptance tests: main.c is saved; the build succeeds; exactly three output lines; the text and the capital letters match; the program exits with code 0. Stretch: add a fourth line that names one sensor.

The answer is on the practice page (question 15). Try it yourself first.

Before you leave

Before closing Visual Studio:

  • Save All (Ctrl+Shift+S).
  • Check that main.c is inside the project in Solution Explorer.
  • Check that the last build succeeded.
  • Know the project folder.

To open the project again: File > Open > Project/Solution, select 01_HelloC.sln (not main.c alone), then build and run once to prove it still works.

Exit check: explain CREATE > SAVE > BUILD > RUN > CHECK to a partner.

Key words

EnglishHebrewMeaning
IDEסביבת פיתוח משולבתone program with an editor, compiler, runner and debugger
projectפרויקטa program's files and settings
source fileקובץ מקורmain.c, the code you write
buildבנייהcompile and link the code into a program
linkerמקשרjoins the object code with the libraries
runהרצהstart the program (Ctrl+F5)
outputפלטwhat the program prints
statementהוראהone instruction; it ends with ;
errorשגיאהa problem that stops the build; read the first one
breakpointנקודת עצירהthe debugger stops before this line

Practice

More questions, with answers, are on the unit 1 practice page.