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":
- Open Visual Studio Installer and click Modify on Visual Studio 2022.
- Select Desktop development with C++. This installs the Microsoft C/C++ compiler and build tools.
- 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:
| # | Area | What it is for |
|---|---|---|
| 1 | Menus and toolbar | every command: File, Build, Debug |
| 2 | Solution Explorer | the project and its files |
| 3 | Editor | where you type the code |
| 4 | Output / Error List | build messages and errors |
| 5 | Status bar | what Visual Studio is doing now |
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
- In the start window, choose Create a new project (or File > New > Project).
- Search for
Empty Project. - Select Empty Project. Do not choose Console App: its starter file contains C++ code, not C.
- Click Next. Do not choose "Empty Solution".
- Project name:
01_HelloC. Use English letters and digits only. - 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
- In Solution Explorer, right-click Source Files. Click inside the project, not on the solution.
- Choose Add > New Item... and select C++ File (.cpp).
- Change the name to
main.c. The.cending tells the compiler that this file is C. - Click Add.
main.copens 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.
Read it like an engineer
| Part | What 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 |
printf | formatted output (פלט): prints text to the console |
\n | newline: 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:
| # | Rule | Example |
|---|---|---|
| 1 | Always int main(void) | int main(void) |
| 2 | The opening brace on its own line | { under int main(void) |
| 3 | Indent with 4 spaces | return 0; |
| 4 | Every statement ends with ; | printf("Hi\n"); |
| 5 | Names in lowercase_with_underscores | sensor_count |
| 6 | A 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 6The 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:
- Read the first error. Later errors are often caused by the first one.
- Double-click it: the editor jumps to the line.
- Look at the code around it.
- Repair and save.
- Build again.
Optional: a breakpoint
A breakpoint (נקודת עצירה) tells the debugger to stop before a line runs.
- Click the left margin beside the
printfline. A red circle marks the breakpoint. - Press F5. The program stops before
printfruns: a yellow arrow points at the line, and the console is still empty. - 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.cis 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
| English | Hebrew | Meaning |
|---|---|---|
| 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.