C Programs Tutorials | IT Developer
IT Developer

C Programming - C Basic Syntax



Share with a Friend

C Programming - C Basic Syntax

The basic syntax of C programming defines the rules for writing valid and executable programs. Understanding the syntax is essential to create, debug, and maintain C code effectively.

  1. Structure of a C Program

A typical C program consists of the following components:

  1. Preprocessor Directives: Begin with # and include libraries or macros.
  2. Main Function: Entry point of the program where execution starts.
  3. Statements and Expressions: Instructions for the compiler to execute.
  4. Functions: Modular blocks of code.

Example

#include <stdio.h> // Preprocessor directive

int main() {        // Main function<

    printf("Hello, World!\n");  // Statement

    return 0;       // Return statement

}

  1. Key Elements of C Syntax
  2. a) Case Sensitivity
  • C is case-sensitive: main, Main, and MAIN are different identifiers.
  1. b) Semicolon ;
  • Every statement must end with a semicolon.

Example:

int a = 10;  // Valid

printf("Hello");  // Valid

  1. c) Curly Braces {}
  • Denote the start and end of a block of code.

Example:

if (a > 0) {

    printf("Positive number\n");

}

  1. d) Comments
  • Single-line: Start with //
  • Multi-line: Enclosed between /* and */

Example:

// This is a single-line comment

/* This is a

   multi-line comment */

  1. Data Types and Variables
  • Variable Declaration: Every variable must have a type and be declared before use.

Example:

int age = 25;

float salary = 5000.50;

  1. Input and Output
  • Input: Use scanf() to take user input.
  • Output: Use printf() to display output.

Example:

int num;

printf("Enter a number: ");

scanf("%d", &num);  // Input

printf("You entered: %d\n", num);  // Output

  1. Keywords and Identifiers
  • Keywords: Reserved words (e.g., int, return).
  • Identifiers: User-defined names for variables, functions, etc.
  1. Control Flow Statements
  • Decision-making: if, else, switch
  • Loops: for, while, do-while

Example:

for (int i = 0; i < 5; i++) {

    printf("%d\n", i);

}

  1. Functions
  • Modular blocks of reusable code.

Example:

int add(int a, int b) {

    return a + b;

}

  1. Indentation and Whitespace
  • Proper indentation and spacing improve readability but do not affect program execution.

Example:

if (a > 0) {

    printf("Positive\n");

}

  1. Example: Simple C Program

#include <stdio.h>

int main() {

    int num1, num2, sum;

    // Input numbers

    printf("Enter two numbers: ");

    scanf("%d %d", &num1, &num2);

    // Calculate sum

    sum = num1 + num2;

    // Print result

    printf("Sum: %d\n", sum);

    return 0;

}

Key Points

Component Description Example

Preprocessor Directive

Includes libraries

#include <stdio.h>

Main Function

Entry point of the program

int main() { ... }

Variables

Store data

int age = 30;

Semicolon

Ends a statement

int a = 5;

Curly Braces

Define blocks

{ ... }

Input and Output

Take input and display output

scanf, printf

Comments

Document the code

//, /* ... */

By adhering to C's basic syntax, you can write functional and error-free programs.