The basics
What is a C file?
A .c file holds source code written in C, one of the oldest programming languages still in wide use. Dennis Ritchie created it at Bell Labs in the early 1970s to write the Unix operating system, and it is still the language of operating system kernels such as Linux, of firmware in devices from routers to washing machines, and of widely used software such as SQLite and the reference Python interpreter.
The file is plain text: open it in any text editor and you can read it. A computer can't run it as it is, though. A compiler first translates it into machine code for one kind of processor and operating system, producing an executable program.
Three things that clear up common confusion:
- A
.cfile is not a program yet. Double-clicking it opens an editor, not the program. You compile it first, and the result (a.out,table,table.exe) is what runs. .cand.hfiles work as a pair. The.cfile holds the code; a header file (.h) declares what one.cfile offers to the others, so they can be compiled separately and linked together.- C is not C++. C++ started as an extension of C and can use most C code, but it is a separate, much larger language with its own files (
.cpp) and its own standard.
Anatomy
What's inside a C file
A C file is a list of declarations and function definitions. Lines starting with # are instructions to the preprocessor, which runs first. Here is a short but complete program that prints a temperature table, part by part.
/* table.c: print a Celsius to Fahrenheit table */
#include <stdio.h>
#include <stdlib.h>
#define STEP 20
static double to_fahrenheit(double celsius);
int main(void)
{
for (int c = 0; c <= 100; c += STEP) {
printf("%3d C = %5.1f F\n", c, to_fahrenheit(c));
}
return EXIT_SUCCESS;
}
static double to_fahrenheit(double celsius)
{
return celsius * 9.0 / 5.0 + 32.0;
}- 1
/* table.c: … */A comment
/* … */comments can span several lines and don't nest. Since C99,//starts a comment that runs to the end of the line. - 2
#include <stdio.h>An include
The preprocessor pastes in the whole header file at this point.
stdio.hdeclaresprintf;stdlib.hdefinesEXIT_SUCCESS. Angle brackets search the system's include folders; quotes, as in#include "stats.h", search next to this file first. - 3
#define STEP 20A macro
A text substitution: every
STEPbelow becomes20before the compiler proper sees the code. By convention macro names are in capitals. - 4
static double to_fahrenheit(double celsius);A prototype
Declares the function before it's used, so the compiler can check each call. The body comes later.
statickeeps the name private to this file. - 5
int main(void)The entry point
Every C program starts in
main.(void)means it takes no arguments; the other standard form isint main(int argc, char *argv[]), which receives the command-line arguments. - 6
{ … }A block
Braces group statements. Indentation means nothing to the compiler; the braces and semicolons do.
- 7
for (int c = 0; c <= 100; c += STEP)A loop with a local variable
cexists only inside the loop. Every variable has a fixed type (int,double,char…) that can't change. - 8
printf("%3d C = %5.1f F\n", …);Formatted output
%3dprints an integer in 3 columns and%5.1fa number with one decimal. The format must match the arguments: C doesn't check at run time, though-Wallmakes compilers warn. - 9
return EXIT_SUCCESS;The exit status
What
mainreturns goes back to the shell or script that ran the program. 0, orEXIT_SUCCESS, means it worked. - 10
static double to_fahrenheit(double celsius) { … }A function definition
The body that the prototype promised. Arguments are passed by value: the function gets a copy of
c, converted todouble.
Under the hood
How to compile and run a C file
Turning table.c into a program takes four steps, which one compiler command usually runs together:
- 1Preprocessing: the
#includeand#definelines are carried out, producing one long stream of C code. - 2Compiling: that code is checked and translated into assembly language for the target processor. Syntax and type errors stop the build here.
- 3Assembling: the assembly becomes an object file (
.o, or.objon Windows) of machine code. - 4Linking: the linker joins the object files with the libraries they use, such as the C standard library, into one executable. A function that was declared but never defined is reported here, not earlier.
| System | Get a compiler | Compile and run |
|---|---|---|
| Linux | GCC or Clang from the package manager, for example sudo apt install build-essential on Debian and Ubuntu | cc table.c -o table then ./table |
| macOS | Apple's Clang, with xcode-select --install (the Command Line Tools) or Xcode | cc table.c -o table then ./table |
| Windows (Microsoft) | Visual Studio or the Build Tools for Visual Studio, with the C++ workload, which includes the C compiler | In a Developer Command Prompt: cl /W4 /std:c17 table.c then table.exe |
| Windows (GCC) | MinGW-w64, for example through MSYS2, or GCC inside WSL | gcc table.c -o table.exe then table.exe |
cc is the traditional name for the system's C compiler; on Linux it is usually GCC and on macOS Clang. Without -o, GCC and Clang name the result a.out. Turn warnings on (-Wall -Wextra or /W4): many of C's worst bugs compile without a single error but do trigger a warning.
# one file: compile and link in one step
cc -Wall -Wextra -std=c17 table.c -o table
./table
# several files: compile each to an object file, then link them
cc -Wall -Wextra -std=c17 -c main.c # makes main.o
cc -Wall -Wextra -std=c17 -c stats.c # makes stats.o
cc main.o stats.o -lm -o report # links them, plus the maths libraryThe executable only runs where it was built for
A program compiled on Linux won't run on Windows, and one built for an x86 processor won't run on an ARM one without an emulator. The .c file is portable; the compiled output isn't. To run the same code elsewhere, compile it again there, or cross-compile for that target.
Conventions
The files that travel with a .c file
A real C project is rarely one file. Each .c file is compiled on its own (a translation unit), and in a program exactly one of them defines main. The others provide functions that main calls, declared in headers. Around them you'll usually find:
| File | What it is |
|---|---|
.h | Header: declarations shared between .c files. Included, never compiled on its own. |
Makefile | Instructions for make: which files to compile, with which options, in which order. |
CMakeLists.txt, meson.build | Build descriptions for CMake and Meson, which generate the actual build for each system and IDE. |
.o, .obj | Object files: compiled but not yet linked. Specific to one compiler and platform. |
.a, .lib | Static libraries: bundles of object files, copied into the program when it's linked. |
.so, .dll, .dylib | Shared libraries (Linux, Windows, macOS): loaded when the program starts. |
a.out, .exe, no extension | The executable itself. |
- Names: lower case with underscores or hyphens is usual (
string_utils.c). Keep a source file and its header under the same name:stats.candstats.h. - Encoding: save files as UTF-8. GCC and Clang read source as UTF-8 by default. Microsoft's compiler reads a file without a byte order mark in the system's code page unless you pass
/utf-8, which is why non-English text in string literals can come out garbled on Windows. - Line endings: compilers accept both Unix (LF) and Windows (CRLF) line endings. Git and editors are where they cause trouble: a file whose every line changed in a diff has usually just had its line endings converted.
- Case:
.cis lower case. GCC treats a capital.Cas C++, soMain.Cis compiled as C++ code.
Versions
C standards: C89 to C23
C is defined by an ISO standard, ISO/IEC 9899, revised every few years. A .c file doesn't say which version it is written in. You choose one when compiling (-std=c17 for GCC and Clang, /std:c17 for MSVC), and the code compiles if it only uses what that version allows. The macro __STDC_VERSION__ tells a program which one it was compiled with.
| Name | Standard | Notable for |
|---|---|---|
| C89 / C90 | ANSI X3.159-1989, then ISO/IEC 9899:1990 | The first standard: function prototypes, void, the standard library. Still the baseline for some embedded compilers |
| C99 | ISO/IEC 9899:1999 | // comments, declarations anywhere in a block, stdint.h types like int32_t, bool via stdbool.h, long long |
| C11 | ISO/IEC 9899:2011 | Threads and atomics, _Static_assert, _Generic, Unicode string literals (u8"…") |
| C17 (C18) | ISO/IEC 9899:2018 | Corrections to C11, no new features. The safe choice for portable code |
| C23 | ISO/IEC 9899:2024, published October 2024 | bool, true and false as keywords, nullptr, typeof, [[attributes]], binary literals 0b1010, digit separators 1'000'000, #embed for including binary files; old-style (K&R) function definitions removed |
| C2y | In progress | The working name of the next revision, being drafted by WG14 |
Compilers pick a default when you don't. GCC 15 and later default to C23 with GNU extensions (-std=gnu23), which broke some older code: a declaration like int f(); now means "takes no arguments", and bool can no longer be defined as a type of your own. Microsoft's compiler defaults to C89 with Microsoft extensions, and offers /std:c11, /std:c17 and /std:clatest.
Related formats
.c vs .cpp, .h and other neighbours
Compilers decide the language from the extension. GCC and Clang compile .c files as C and .cpp, .cc and .cxx files as C++; MSVC compiles .c as C unless told otherwise with /TP. The same code can behave differently: C lets you assign the result of malloc to any pointer without a cast, which C++ rejects, and words like class, new and template are ordinary names in C but keywords in C++.
| Extension | Language | Role |
|---|---|---|
.c | C | Source code: compiled into object files |
| `.h` | C or C++ | Header: declarations shared between source files |
`.cpp`, .cc, .cxx | C++ | C++ source code |
.i | C | Preprocessed C: includes and macros already expanded, as gcc -E outputs it |
.s, .asm | Assembly | Human-readable machine instructions, as gcc -S writes them |
.m | Objective-C | C with Smalltalk-style objects, used mainly on Apple platforms |
| `.rs`, `.go` | Rust, Go | Newer compiled languages often chosen for new systems code instead of C |
Troubleshooting
C compile errors people hit most, and what they mean
| Message | Usual cause | Fix |
|---|---|---|
| expected ';' before … | A missing semicolon, usually at the end of the line above the one reported | Look one line up from where the compiler points |
| 'x' undeclared / use of undeclared identifier | A typo, or a variable used outside the block it was declared in | Check the spelling and where the variable is declared |
| implicit declaration of function 'f' | A function called without a prototype, usually a missing #include. An error in GCC 14 and later and in Clang 16 and later; older GCC only warned | Include the right header, or declare the function above the call |
| fatal error: x.h: No such file or directory | The compiler can't find a header | Check the name, or add its folder with -I path; for a library, install its development package |
| undefined reference to 'f' (MSVC: LNK2019 unresolved external symbol) | A linker error: the function was declared but its code wasn't linked in | Add the missing .c file or library to the command, for example -lm for maths functions on many Linux systems |
| multiple definition of 'x' | A variable or function defined in a header that several .c files include | Declare it extern in the header and define it in one .c file |
| 'cc' / 'gcc' is not recognized (Windows) | No compiler installed, or not on the PATH | Install one, and use the Developer Command Prompt for MSVC |
Compiling isn't proof it works: undefined behaviour
C trusts the programmer. Reading past the end of an array, using memory after free, or overflowing a signed integer is undefined behaviour: the standard says nothing about what happens, so the program may crash, print nonsense or appear to work until it doesn't. Turn warnings on, and test with -fsanitize=address,undefined in GCC or Clang to catch it at run time.
How to
How to open and edit a C file
- Read it: any text editor works: Notepad, TextEdit (in plain-text mode), or a browser, which shows a
.cfile as text. - Edit it: a code editor gives you colours, completion and error markers. VS Code with Microsoft's C/C++ extension or the clangd extension is a popular free choice; Visual Studio, CLion, Xcode, Vim and Emacs are common too.
- Format it:
clang-formatre-indents a file to a consistent style, which keeps diffs readable. - Try it without installing anything: online compilers such as Compiler Explorer compile a snippet in the browser and show the result or the generated assembly.
- On a phone: text and code editor apps can open it; compiling needs a computer or an online compiler.
Security
Is it safe to open a C file?
Opening a .c file in an editor only shows text: nothing runs. The risk starts when you build and run code you don't trust:
- Build scripts run commands. A
Makefile,configurescript or CMake file can run any command on your computer as part of the build. Read them before runningmakeon a downloaded project. - The compiled program has your permissions. Like any program, it can read, change and delete your files. Running it in a virtual machine or container limits the damage.
- Memory bugs are security bugs. C doesn't check array bounds or pointer use, so mistakes like buffer overflows and use-after-free let attackers corrupt memory. They have been a long-standing source of serious vulnerabilities in C programs. Warnings, sanitizers, fuzzing and hardening options such as
-D_FORTIFY_SOURCE=2reduce the risk.
Free downloads
Sample C files to download
Free sample C files for testing compilers, editors, syntax highlighting, code viewers and upload filters. All but one compile with GCC and Clang using -Wall -Wextra -std=c17 without a single warning; sample-c23.c needs C23 mode, and sample-compile-error.c fails on purpose.
Loading…What's inside sample-program.c
The file is commented: open it in a text editor and each numbered comment marks one of these parts. Compile it with cc -Wall -Wextra -std=c17 sample-program.c -o sample-program and run ./sample-program to see a stock report.
Comments 1 and 2: includes and a macro
Three standard headers, then
ARRAY_LEN, a macro that counts the elements of an array withsizeof.Comments 3 and 4: a struct and a table
A
typedef structfor a shop item and astatic constarray of five fictional items, private to the file.Comments 5 and 6: functions and pointers
A function that takes a pointer to a constant item, and a comparison function for
qsortthat convertsconst void *pointers back to items.Comment 7: main
int main(int argc, char *argv[]), the form that receives command-line arguments.Comment 8: reading an argument safely
strtolwith full error checks, so./sample-program abcprints a usage message and exits with status 1 instead of treating the text as 0.Comment 9: heap memory
mallocwith aNULLcheck, an array of pointers sorted withqsort, and the matchingfree.Comments 10 and 11: output and the exit status
printffield widths that line up a table, andreturn EXIT_SUCCESS, which tells the shell the program worked.
All sample C files
Commented sample program
sample-program.c · 3.9 KB (4,038 bytes)
A complete program that prints a sorted stock report: includes, a macro, a struct, pointers,
qsort,mallocandfree, safe argument parsing withstrtol, and formatted output, with numbered comments explaining each part.- Checking compilers, linters and syntax highlighters
- Teaching the parts of a C program
Hello world with Windows (CRLF) line endings
sample-hello.c · 476 bytes
The classic first program, saved with CRLF line endings on purpose. It compiles everywhere, which makes it a quick test of editors, diffs and Git's line-ending settings.
- Line-ending handling in editors, Git and CI
- A minimal compile test
UTF-8 string literals
sample-utf8.c · 1.9 KB (1,930 bytes)
Greetings in seven scripts, from Latin to Japanese, plus symbols and emoji. For each one it prints the size in bytes (
strlen) and the number of characters, counted by hand from the UTF-8 bytes.- Testing source encoding in compilers, especially MSVC with and without `/utf-8`
- Showing why `strlen` counts bytes, not characters
C23 features
sample-c23.c · 1.6 KB (1,673 bytes)
boolandnullptras keywords, binary literals, digit separators,typeof,[[nodiscard]]and a message-lessstatic_assert. Compiles with-std=c23(GCC 14 and later, Clang 18 and later) and fails under-std=c17.- Checking which C standard a compiler or build uses
- Syntax highlighters and parsers that need to understand C23
Deliberate compile errors (does not compile)
sample-compile-error.c · 923 bytes
Clearly labelled, and broken on purpose: an undeclared identifier, a call to an undeclared function and a missing semicolon, each with its fix in a comment.
- Testing how editors, CI logs and build servers show errors
- Error parsers and problem matchers
100 KB C file
sample-100kb.c · 100 KB (102,400 bytes)
A table of the first 13,330 prime numbers and a program that checks every one, padded with a comment to exactly 102,400 bytes. Compiles cleanly and runs.
- Upload and size-limit tests
- Editor and syntax-highlighter performance
Ways to test with them
- Compiler and standard:
sample-c23.ccompiles only in C23 mode, so it shows at once whether a toolchain or build file sets the standard you expect. - Encoding: compile
sample-utf8.cwith MSVC with and without/utf-8and compare the output with the comments in the file. - Line endings: commit
sample-hello.cto a Git repository and check whether yourcore.autocrlfor.gitattributessettings convert it. - Error reporting: build
sample-compile-error.cin your CI or editor; GCC and Clang should each report the problems marked A, B and C. - Upload limits:
sample-100kb.cis exactly 102,400 bytes, for testing a limit at the boundary.
Free to use for testing, teaching and demos, commercially or not, with no attribution needed. A link back to this page is always appreciated.
Put it online
Have a C file? Give it a link.
Drop a .c file on 99helpers and it gets its own web address. Anyone who opens the link reads the code as plain text in their browser: it is never compiled or run, which makes it a simple way to share code for review, teaching or a bug report.
Questions
C files, frequently asked
What is a .c file?
A source code file written in the C programming language. It's plain text that a compiler such as GCC, Clang or Microsoft's MSVC turns into a program you can run.
How do I open a C file?
Open it in any text editor or code editor, such as VS Code, Notepad or Xcode. To run the code, you have to compile it first.
How do I run a .c file?
Compile it, then run the result. On Linux or macOS: cc file.c -o file and then ./file. On Windows with Visual Studio's tools: cl file.c in a Developer Command Prompt, then file.exe.
What's the difference between .c and .h files?
A .c file contains code that gets compiled. A .h header contains declarations that .c files include so they can use each other's functions; it is never compiled on its own.
What's the difference between .c and .cpp?
.c files are C, .cpp files are C++. Compilers choose the language from the extension. C++ accepts most C code but not all of it, and adds classes, templates, exceptions and a much larger standard library.
Which C standard should I use?
C17 (-std=c17) is the safe choice for code that has to build with many compilers. Use C23 if all your compilers support it; GCC 15 and later use it by default.
Why do I get "undefined reference to" errors?
That's the linker: a function was declared but its code wasn't found. Add the .c file that defines it to the compile command, or link the library it lives in, such as -lm for maths functions on many Linux systems.
Can I run C code online?
Yes. Online compilers such as Compiler Explorer compile and run short programs in the browser. To share the code itself as a link, upload the .c file and send the address.
Related formats
Other file types explained
Keep going
Related
Sources
- ISO/IEC 9899:2024, Programming languages: C (ISO)
- WG14: C standard projects and drafts
- GCC: Porting to GCC 15 (C23 by default)
- GCC: Porting to GCC 14 (implicit declarations are errors)
- GCC: Options controlling the kind of output (file suffixes)
- Microsoft Learn: /std (Specify language standard version)
Last reviewed 27 October 2026.