File types · Explainer

The C source file (.c)

A C file is a plain-text file of source code in the C programming language, which a compiler turns into a program you can run. This guide explains what's inside one, how to compile it on each system, the errors people hit most, and has free sample C files to download for testing.

At a glance

Full name
C source file
Extensions
.c, with headers in .h files
MIME type
None registered; text/x-c is common, and Linux desktops use text/x-csrc
Kind
Plain text (source code), compiled before it runs
Standard
ISO/IEC 9899, maintained by the ISO committee WG14; the current edition is ISO/IEC 9899:2024, known as C23
First released
Early 1970s, by Dennis Ritchie at Bell Labs; first standard ANSI X3.159-1989
Compiled with
GCC, Clang, Microsoft Visual C++ (MSVC) and many compilers for embedded chips
Opens with
Any text editor or IDE: VS Code, Visual Studio, CLion, Xcode, Vim
Comments
/* block */, and // line since C99
Magic number
None

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 .c file 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.
  • .c and .h files work as a pair. The .c file holds the code; a header file (.h) declares what one .c file 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
/* 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. 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. 2#include <stdio.h>

    An include

    The preprocessor pastes in the whole header file at this point. stdio.h declares printf; stdlib.h defines EXIT_SUCCESS. Angle brackets search the system's include folders; quotes, as in #include "stats.h", search next to this file first.

  3. 3#define STEP 20

    A macro

    A text substitution: every STEP below becomes 20 before the compiler proper sees the code. By convention macro names are in capitals.

  4. 4static 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. static keeps the name private to this file.

  5. 5int main(void)

    The entry point

    Every C program starts in main. (void) means it takes no arguments; the other standard form is int main(int argc, char *argv[]), which receives the command-line arguments.

  6. 6{ … }

    A block

    Braces group statements. Indentation means nothing to the compiler; the braces and semicolons do.

  7. 7for (int c = 0; c <= 100; c += STEP)

    A loop with a local variable

    c exists only inside the loop. Every variable has a fixed type (int, double, char…) that can't change.

  8. 8printf("%3d C = %5.1f F\n", …);

    Formatted output

    %3d prints an integer in 3 columns and %5.1f a number with one decimal. The format must match the arguments: C doesn't check at run time, though -Wall makes compilers warn.

  9. 9return EXIT_SUCCESS;

    The exit status

    What main returns goes back to the shell or script that ran the program. 0, or EXIT_SUCCESS, means it worked.

  10. 10static 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 to double.

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:

  1. 1Preprocessing: the #include and #define lines are carried out, producing one long stream of C code.
  2. 2Compiling: that code is checked and translated into assembly language for the target processor. Syntax and type errors stop the build here.
  3. 3Assembling: the assembly becomes an object file (.o, or .obj on Windows) of machine code.
  4. 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.
SystemGet a compilerCompile and run
LinuxGCC or Clang from the package manager, for example sudo apt install build-essential on Debian and Ubuntucc table.c -o table then ./table
macOSApple's Clang, with xcode-select --install (the Command Line Tools) or Xcodecc 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 compilerIn a Developer Command Prompt: cl /W4 /std:c17 table.c then table.exe
Windows (GCC)MinGW-w64, for example through MSYS2, or GCC inside WSLgcc 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.

Terminal
# 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 library

The 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:

FileWhat it is
.hHeader: declarations shared between .c files. Included, never compiled on its own.
MakefileInstructions for make: which files to compile, with which options, in which order.
CMakeLists.txt, meson.buildBuild descriptions for CMake and Meson, which generate the actual build for each system and IDE.
.o, .objObject files: compiled but not yet linked. Specific to one compiler and platform.
.a, .libStatic libraries: bundles of object files, copied into the program when it's linked.
.so, .dll, .dylibShared libraries (Linux, Windows, macOS): loaded when the program starts.
a.out, .exe, no extensionThe 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.c and stats.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: .c is lower case. GCC treats a capital .C as C++, so Main.C is 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.

NameStandardNotable for
C89 / C90ANSI X3.159-1989, then ISO/IEC 9899:1990The first standard: function prototypes, void, the standard library. Still the baseline for some embedded compilers
C99ISO/IEC 9899:1999// comments, declarations anywhere in a block, stdint.h types like int32_t, bool via stdbool.h, long long
C11ISO/IEC 9899:2011Threads and atomics, _Static_assert, _Generic, Unicode string literals (u8"…")
C17 (C18)ISO/IEC 9899:2018Corrections to C11, no new features. The safe choice for portable code
C23ISO/IEC 9899:2024, published October 2024bool, 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
C2yIn progressThe 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++.

ExtensionLanguageRole
.cCSource code: compiled into object files
`.h`C or C++Header: declarations shared between source files
`.cpp`, .cc, .cxxC++C++ source code
.iCPreprocessed C: includes and macros already expanded, as gcc -E outputs it
.s, .asmAssemblyHuman-readable machine instructions, as gcc -S writes them
.mObjective-CC with Smalltalk-style objects, used mainly on Apple platforms
`.rs`, `.go`Rust, GoNewer compiled languages often chosen for new systems code instead of C

Troubleshooting

C compile errors people hit most, and what they mean

MessageUsual causeFix
expected ';' before …A missing semicolon, usually at the end of the line above the one reportedLook one line up from where the compiler points
'x' undeclared / use of undeclared identifierA typo, or a variable used outside the block it was declared inCheck 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 warnedInclude the right header, or declare the function above the call
fatal error: x.h: No such file or directoryThe compiler can't find a headerCheck 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 inAdd 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 includeDeclare 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 PATHInstall 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 .c file 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-format re-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, configure script or CMake file can run any command on your computer as part of the build. Read them before running make on 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=2 reduce 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.

sample-program.c · 3.9 KBDownloadOpen
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.

  1. Comments 1 and 2: includes and a macro

    Three standard headers, then ARRAY_LEN, a macro that counts the elements of an array with sizeof.

  2. Comments 3 and 4: a struct and a table

    A typedef struct for a shop item and a static const array of five fictional items, private to the file.

  3. Comments 5 and 6: functions and pointers

    A function that takes a pointer to a constant item, and a comparison function for qsort that converts const void * pointers back to items.

  4. Comment 7: main

    int main(int argc, char *argv[]), the form that receives command-line arguments.

  5. Comment 8: reading an argument safely

    strtol with full error checks, so ./sample-program abc prints a usage message and exits with status 1 instead of treating the text as 0.

  6. Comment 9: heap memory

    malloc with a NULL check, an array of pointers sorted with qsort, and the matching free.

  7. Comments 10 and 11: output and the exit status

    printf field widths that line up a table, and return 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, malloc and free, safe argument parsing with strtol, 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)

    bool and nullptr as keywords, binary literals, digit separators, typeof, [[nodiscard]] and a message-less static_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.c compiles only in C23 mode, so it shows at once whether a toolchain or build file sets the standard you expect.
  • Encoding: compile sample-utf8.c with MSVC with and without /utf-8 and compare the output with the comments in the file.
  • Line endings: commit sample-hello.c to a Git repository and check whether your core.autocrlf or .gitattributes settings convert it.
  • Error reporting: build sample-compile-error.c in your CI or editor; GCC and Clang should each report the problems marked A, B and C.
  • Upload limits: sample-100kb.c is 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

Last reviewed 27 October 2026.