C syntax
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, plus code that implements the function logic.
Entry point
For a hosted environment, a program starts at an entry point function named. 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 :int main;
int main;
int main;
int main;
The first two definitions are equivalent, meaning that the function does not use the two arguments. The second two are also equivalent, allowing the function to access the two arguments.
The return value, typed as, serves as a status indicator to the host environment. Defined in stdlib.h|, the standard library provides macros for standard status values: and. 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 a parameter less, empty function, like:
int main
Unlike other functions, the language requires that a program act as if it returns 0 even if it does not end with a statement.
In a free-standing environment, such as a system without an operating system, the standard allows for different startup handling. It need not require a 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 and an array of null-terminated strings with the program name as the first item.The following code prints the value of the command-line parameters.
- include
$./a.out abc def
argc = 3
argv =./a.out
argv = abc
argv = def
Reserved keywords
The following words are reserved not allowed as identifiers, of which there are 43.-
alignas -
alignof -
auto -
bool -
break -
case -
char -
const -
constexpr -
continue -
default -
do -
double -
else -
enum -
extern -
float -
for -
goto -
if -
inline -
int -
long -
register -
restrict -
return -
short -
signed -
sizeof -
static -
static_assert -
struct -
switch -
thread_local -
typedef -
typeof -
typeof_unqual -
union -
unsigned -
void -
volatile -
while
-
_Alignas -
_Alignof -
_Atomic -
_BitInt -
_Bool -
_Complex -
_Countof -
_Decimal32 -
_Decimal64 -
_Decimal128 -
_Generic -
_Noreturn -
_Static_assert -
_Thread_local
_Imaginary was removed in C2Y.The following words refer to literal values used by the language, of which there are 3.
-
nullptr -
true -
false
-
asm -
fortranPreprocessor directives
-
#if -
#elif -
#else -
#endif -
#ifdef -
#ifndef -
#elifdef -
#elifndef -
#define -
#undef -
#include -
#embed -
#line -
#error -
#warning -
#pragma -
#__has_include -
#__has_embed -
#__has_c_attribute
_Pragma operator provides an alternative syntax for the functionality provided by.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.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.
int i; // line comment
/* block
comment */
int ii = /* always zero */ 0;
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.
x = *p/*q;
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.
/*
First of comment block
/*
First line of what is intended to be an inner block
- /
- /
Identifiers
The syntax supports user-defined identifiers. An identifier must start with a letter or an underscore, subsequent characters can be letters, numbers, or underscores and it must not be a reserved word. Identifiers are case sensitive, making,, and distinct.Evaluation order
There can be multiple ways to evaluate an expression consistent with the mathematical notation. For example, may be evaluated in the order,,,, or in the order,,,.To reduce run-time issues with evaluation order yet afford some optimizations, the standard states that expressions may be evaluated in any order between sequence points which are defined as any of the following:
- Statement end
- A sequencing operator: a comma; commas that delimit function arguments are not sequence points
- A short-circuit operators: logical and and logical or
- A ternary operator : This operator evaluates its first sub-expression first, and then its second or third based on the value of the first
- Entry to and exit from a function call
|| b), if the first argument evaluates to nonzero, the result of the entire expression cannot be anything else than true, so is not evaluated. Similarly, in the expression, if the first argument evaluates to zero, the result of the entire expression cannot be anything else than false, so is not evaluated.The arguments to a function call may be evaluated in any order, as long as they are all evaluated by the time the function is entered. The following expression, for example, has undefined behavior:
printf;