C syntax is the form that text must have in order to be C programming language code. The language syntax rules are designed to allow for code that is terse, has a close relationship with the resulting object code, and yet provides relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development. C syntax makes use of the maximal munch principle. As a free-form language, C code can be formatted different ways without affecting its syntactic nature. C syntax influenced the syntax of succeeding languages, including C++, Java, and C#.
High level structure C code consists of preprocessor directives, and core-language types, variables and functions, organized as one or more source files. Building the code typically involves preprocessing and then compiling each source file into an object file. Then, the object files are linked to create an executable image. Variables and functions can be declared separately from their definition. A declaration identifies the name of a user-defined element and some if not all of the information about how the element can be used at run-time. A definition is a complete description of an element that includes the declaration aspect as well as additional information that completes the element. For example, a function declaration indicates the name and optionally the type and number of arguments that it accepts. A function definition includes the same information (argument information is not optional), plus code that implements the function logic.
Entry point
For a hosted environment, a program starts at an entry point function named main. The function is passed two arguments although an implementation of the function can ignore them. The function must be declared per one of the following prototypes (parameter names shown are the conventional names, but can be anything):
Note the following nuances:
int main(); and int main(void); are equivalent since C23, indicating taking exactly zero arguments; historically, void f(); indicated that f took an unspecified number of arguments, while void f(void); indicated that f took exactly zero arguments. char* argv[] and char** argv are equivalent, as arrays decay to pointers in function parameters (see #Array passing). Sometimes, an envp parameter is supported, which is supported by most environments and is an array of pointers to the execution environment variables. However, this is not guaranteed by the C standard.
The return value, typed as int, serves as a status indicator to the host environment. Defined in <stdlib.h>, the standard library provides macros for standard status values: EXIT_SUCCESS and EXIT_FAILURE. Regardless, a program can indicate status using any values. For example, the kill command returns the numerical value of the signal plus 128. A minimal program consists of an empty main function, like:
Unlike other functions, the language requires that a program act as if it returns 0 even if it does not end with a return statement. In a free-standing (non-hosted) environment, such as a system without an operating system, the standard allows for different startup handling. It need not require a main function.
Command-line arguments Arguments included in the command line to start a program are passed to a program as two values – the number of arguments (customarily named argc) and an array of null-terminated strings (customarily named argv) with the program name as the first item. The following code prints the value of the command-line parameters.
Reserved keywords The following words are reserved – not allowed as identifiers, of which there are 43.
The following keywords are often substituted for a macro or an appropriate keyword from the above list, of which there are 14. Some of the following keywords are deprecated since C23.
The keyword _Imaginary was removed in C29. The following words refer to literal values used by the language, of which there are 3.
Implementations may reserve other keywords, although implementations typically provide non-standard keywords that begin with one or two underscores. The following keywords are classified as extensions and conditionally-supported, of which there are 2.
asm (denotes inline Assembly) fortran (denotes external Fortran linkage)
Preprocessor directives The following are directives to the preprocessor, of which there are 19.
The _Pragma operator provides an alternative syntax for the functionality provided by #pragma.
Comments A comment – informative text to a programmer that is ignored by a language translator – can be included in code either as the line or block comment syntax. A line comment starts with // and ends at the end of the same line. A block comment starts with /* and ends with */ – spanning any number of lines (or just one). In some situations, comment markers are ignored. The text of a string literal is exempt from being considered a comment start. And, comments cannot be nested. For example, /* in a line comment is not as treated as the start of a block comment. And, // in a block comment is not treated as the start of a line comment. The line comment syntax, sometimes called C++ style originated in BCPL and became valid syntax in C99. It is not available in the original K&R version nor in ANSI C. The following code demonstrates comments. Line 1 contains a line comment and lines 3-4 contain a block comment. Line 4 demonstrates that a block comment can be embedded in a line with code both before and after it.
The following demonstrates a potential problem with the comment syntax. What is intended to be the divide operator / and then the dereference operator *, is evaluated as the start of a block comment.
The following text is not valid C syntax since comments do not nest. It seems that lines 3-5 are a comment nested inside the comment block spanning lines 1-7. But, actually line 5 ends the comment started on line 1. This leaves line 6 to be interpreted as code which is clearly not valid C syntax.
Identifiers The syntax supports user-defined identifiers. An identifier must start with a letter (A-Z, a-z) or an underscore (_), subsequent characters can be letters, numbers (0-9), or underscores and it must not be a reserved word. Identifiers are case sensitive, making foo, FOO, and Foo distinct.
… excerpt ends here. Continue reading the full article.


