ArticleslgStudy

science

C syntax

C syntax is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand C syntax rather than just read about it. In short: 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 syntax — main illustration
C syntax — illustration

Key takeaways

  • C syntax belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect C syntax to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of C syntax from memory before moving on to harder problems.

Reference excerpt

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.

Illustrations

C syntax: A snippet of C code from the Linux kernel
A snippet of C code from the Linux kernel
C syntax: C code for a program that prints "Hello, World!"
C code for a program that prints "Hello, World!"

Worked examples

Example 1 — a first encounter with C syntax

Start with the simplest possible case. Write down what C syntax claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to C syntax before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about C syntax ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of C syntax

In research
C syntax appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses C syntax in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
C syntax is common in secondary-school and first-year university syllabi. It links to neighbouring topics C (programming language), Programming language syntax, Source code, so understanding it makes those chapters shorter.
In everyday life
Look for C syntax outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “C syntax” →

How to study C syntax in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what C syntax means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain C syntax out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is C syntax in simple terms?

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.

Why does C syntax matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study C syntax?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on C syntax.

Tags

  • C (programming language)
  • Programming language syntax
  • Source code

Keep exploring