In this section, we will cover the fundamental syntax and structure of a C program. Understanding these basics is crucial as they form the foundation for writing any C program. We will go through the following key concepts:

  1. Structure of a C Program
  2. Comments
  3. Data Types and Variables
  4. Basic Input/Output
  5. Control Flow Statements

  1. Structure of a C Program

A typical C program consists of the following parts:

  • Preprocessor Directives: Instructions for the compiler to preprocess the information before actual compilation starts.
  • Functions: Blocks of code that perform specific tasks.
  • Variables: Storage locations with a name and a type.
  • Statements and Expressions: Instructions executed by the program.

Example:

#include <stdio.h>  // Preprocessor directive

// Main function
int main() {
    // Variable declaration
    int number = 10;

    // Print statement
    printf("Number: %d\n", number);

    // Return statement
    return 0;
}

Explanation:

  • #include <stdio.h>: This is a preprocessor directive that includes the standard input-output library.
  • int main() { ... }: This is the main function where the execution of the program begins.
  • int number = 10;: This declares an integer variable named number and initializes it with the value 10.
  • printf("Number: %d\n", number);: This prints the value of number to the console.
  • return 0;: This statement terminates the main function and returns 0 to the calling process.

  1. Comments

Comments are used to explain the code and are ignored by the compiler. They can be single-line or multi-line.

Example:

// This is a single-line comment

/*
This is a
multi-line comment
*/

  1. Data Types and Variables

C supports various data types, including:

  • int: Integer type
  • float: Floating-point type
  • char: Character type
  • double: Double-precision floating-point type

Example:

#include <stdio.h>

int main() {
    int age = 25;          // Integer variable
    float height = 5.9;    // Floating-point variable
    char grade = 'A';      // Character variable
    double pi = 3.14159;   // Double-precision variable

    printf("Age: %d\n", age);
    printf("Height: %.1f\n", height);
    printf("Grade: %c\n", grade);
    printf("Pi: %.5f\n", pi);

    return 0;
}

Explanation:

  • int age = 25;: Declares an integer variable age and initializes it with 25.
  • float height = 5.9;: Declares a floating-point variable height and initializes it with 5.9.
  • char grade = 'A';: Declares a character variable grade and initializes it with 'A'.
  • double pi = 3.14159;: Declares a double-precision variable pi and initializes it with 3.14159.

  1. Basic Input/Output

C provides functions like printf and scanf for output and input operations, respectively.

Example:

#include <stdio.h>

int main() {
    int number;

    // Input
    printf("Enter a number: ");
    scanf("%d", &number);

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

    return 0;
}

Explanation:

  • printf("Enter a number: ");: Prompts the user to enter a number.
  • scanf("%d", &number);: Reads an integer input from the user and stores it in the variable number.
  • printf("You entered: %d\n", number);: Prints the entered number.

  1. Control Flow Statements

Control flow statements include conditional statements and loops that control the execution flow of the program.

Example:

#include <stdio.h>

int main() {
    int number = 10;

    // Conditional statement
    if (number > 0) {
        printf("Number is positive\n");
    } else {
        printf("Number is non-positive\n");
    }

    // Loop statement
    for (int i = 0; i < 5; i++) {
        printf("i = %d\n", i);
    }

    return 0;
}

Explanation:

  • if (number > 0) { ... } else { ... }: Checks if number is positive and prints the appropriate message.
  • for (int i = 0; i < 5; i++) { ... }: Loops from 0 to 4 and prints the value of i.

Practical Exercise

Exercise:

Write a C program that takes two integers as input and prints their sum, difference, product, and quotient.

Solution:

#include <stdio.h>

int main() {
    int num1, num2;

    // Input
    printf("Enter first number: ");
    scanf("%d", &num1);
    printf("Enter second number: ");
    scanf("%d", &num2);

    // Calculations
    int sum = num1 + num2;
    int difference = num1 - num2;
    int product = num1 * num2;
    float quotient = (float)num1 / num2;

    // Output
    printf("Sum: %d\n", sum);
    printf("Difference: %d\n", difference);
    printf("Product: %d\n", product);
    printf("Quotient: %.2f\n", quotient);

    return 0;
}

Explanation:

  • The program takes two integers as input.
  • It calculates the sum, difference, product, and quotient of the two numbers.
  • It prints the results.

Conclusion

In this section, we covered the basic syntax and structure of a C program, including the structure of a C program, comments, data types and variables, basic input/output, and control flow statements. These fundamentals are essential for writing and understanding C programs. In the next module, we will delve deeper into data types and variables.

© Copyright 2024. All rights reserved