• Home
  • 1.30 Basic C Syntax Reference

1.30 Basic C Syntax Reference

View Categories

1.30 Basic C Syntax Reference

1 min read

C syntax defines how declarations, expressions, statements, functions, and blocks are written. The following example combines the basic syntax used throughout a C program.

#include <stdio.h>

int add(int a, int b)
{
    return a + b;
}

int main(void)
{
    int number = 10;

    if (number > 0)
    {
        printf("Positive\n");
    }

    printf("Sum = %d\n", add(number, 20));

    return 0;
}

Example Output #

Positive
Sum = 30

Explanation #

The main syntax elements in the example are:

Syntax Example Purpose
Header inclusion #include <stdio.h> Includes a header
Function definition int add(int a, int b) Defines a function
Parameter declaration int a Declares a function parameter
Block { ... } Groups declarations and statements
Variable declaration int number; Declares an object
Initialization int number = 10; Declares and initializes an object
Function call add(number, 20) Calls a function
Conditional statement if (number > 0) Conditionally executes a block
Expression number > 0 Produces a value
Statement terminator ; Terminates declarations and expression statements
Return statement return 0; Returns a value from a function
Comment /* comment */ Adds source-code documentation

Common Syntax Rules #

Statements normally end with a semicolon:

int number = 10;
number = 20;
printf("%d\n", number);

Blocks use braces:

if (number > 0)
{
    printf("Positive\n");
}

Identifiers are case-sensitive:

int value;
int Value;

value and Value are different identifiers.

Strings use double quotes:

printf("Hello\n");

Character constants use single quotes:

char letter = 'A';

Keywords cannot be used as identifiers:

int return;    /* Invalid */

These rules form the basic syntax used by the later C language features.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *