DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 3 of 64
←PreviousPrevNextNext→

C Syntax

Basic Structure of a C Program

A C program follows a consistent structure. The compiler expects specific elements in order: preprocessing directives, functions, and statements.

At minimum, a program must include the `main()` function, which is the entry point. Inside `main()`, we place instructions that define the program's behavior.

Example
#include <stdio.h>

int main(void) {
    printf("Hello, C!\n");
    return 0;
}
Output
Hello, C!
ℹ️ Note: The `#include <stdio.h>` directive allows the use of the `printf` function for output. Using `int main(void)` is the idiomatic signature for a program with no command-line arguments.

C Program Components

**Preprocessor Directives**: Lines beginning with `#`, such as `#include`, are processed before compilation.

**Functions**: Named blocks of code that perform tasks. Every program needs `main()`, while additional functions are optional.

**Statements and Expressions**: Instructions inside functions, typically ending with a semicolon.

**Comments**: Non-executable notes in code. Use `//` for single-line and `/* ... */` for multi-line comments.

Example
#include <stdio.h>

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

int main(void) {
    int result = add(3, 4);
    printf("%d\n", result);
    return 0;
}
Output
7

Identifiers, Keywords, and Formatting

Identifiers are names given to variables, functions, and other elements. They must begin with a letter or underscore, may contain digits and underscores thereafter, and cannot be keywords. C identifiers are case-sensitive.

Keywords are reserved words in C, such as `int`, `return`, `while`, and `if`. These cannot be used as identifiers.

Consistent formatting—using indentation, spacing, and braces—improves readability but does not change program execution.

CategoryExamples
Valid identifiersx, total_sum, _count
Invalid identifiers2ndValue, float (keyword)
Keywordsint, char, if, else, return

Escape Sequences in Strings

C supports escape sequences within strings, starting with a backslash (`\`).

These sequences allow inclusion of special characters such as newlines, tabs, or quotation marks.

Example
#include <stdio.h>

int main(void) {
    printf("Hello World\tC\n");
    return 0;
}
Output
Hello World	C
Escape SequenceMeaning
\\nNew line
\\tTab space
\\\\Backslash
\\"Double quote
Test your knowledge: C Syntax
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C BasicsTopic 3 of 64
←PreviousPrevNextNext→