C Programs Tutorials | IT Developer
IT Developer

C Programming - Basics



Share with a Friend

C Programming - Basics

Introduction

C programming language is a foundational tool in computer science, combining simplicity with the ability to perform complex and hardware-level operations. Below is an outline of the key basics of C programming:

Structure of a C Program

Every C program follows a defined structure comprising the following components:

  1. Preprocessor Directives:
    Instructions to the compiler, starting with # (e.g., #include <stdio.h>).
  2. Main Function:
    The entry point of a C program, where execution begins.
  3. Statements and Expressions:
    Instructions written to perform specific tasks.
  4. Return Statement:
    Ends the main() function and returns control to the operating system.

Example:

#include <stdio.h> // Preprocessor directive int main() { printf("Hello, World! \n"); // Print statement return 0; // Exit the program }

Basic Syntax

  • Case Sensitivity:
    C is case-sensitive (main is different from Main).
  • Semicolon:
    Each statement ends with a semicolon (;).
  • Comments:
    Used for documentation.
    • Single-line: // Comment here
    • Multi-line: /* Comment here */
  • Curly Braces {}:
    Denote the beginning and end of blocks, such as functions.

Data Types

C provides a variety of data types to define variables:

  • Basic Data Types:
    • int: Integer numbers (e.g., 10, -5).
    • float: Floating-point numbers (e.g., 3.14).
    • double: Double-precision floating-point numbers.
    • char: Single character (e.g., 'A').
  • Derived Data Types:
    • Arrays, Pointers, Structures, Unions.
  • Void: Represents no value (e.g., void function).

Variables and Constants

  • Variables:
    Used to store data values.
int age = 25;  // Variable declaration and initialization
  • Constants:
    Immutable values defined using const or #define.

const float PI = 3.14;

#define MAX 100

Input and Output

  • Input: Use the scanf() function.

int num;

scanf("%d", &num);  // Reads an integer input

  • Output: Use the printf() function.

printf("Number is: %d", num);  // Prints the variable's value

printf("Number is: %d", num);  // Prints the variable's value

Operators

C supports various operators:

  • Arithmetic Operators: +, -, *, /, %.
  • Relational Operators: ==, !=, <, >, <=, >=.
  • Logical Operators: &&, ||, !.
  • Assignment Operators: =, +=, -=, etc.
  • Bitwise Operators: &, |, ^, ~, <<, >>.

Control Flow

C provides control structures for decision-making and looping:

  • Conditional Statements:
    • if, else if, else.
    • switch-case.
  • Loops:
    • for: For fixed iteration.
    • while: For condition-based iteration.
    • do-while: Executes at least once before checking the condition.

Example:

int i;

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

    printf("%d ", i);  // Outputs 0 1 2 3 4

}

Functions

Functions are used to encapsulate reusable code blocks.

  • Syntax:

returnType functionName(parameters) {

    // Code

    return value;

}

  • Example:

int add(int a, int b) {

    return a + b;

}

Arrays

Arrays store multiple values of the same type.

int numbers[5] = {1, 2, 3, 4, 5};

printf("%d", numbers[2]);  // Outputs 3

Pointers

Pointers store memory addresses of variables.

int x = 10;

int *p = &x;  // Pointer to x

printf("%d", *p);  // Dereferences p to output 10

Strings

Strings are arrays of characters terminated by a null character (\0).

char name[] = "John";

printf("Name: %s", name);  // Outputs "John"

Memory Management

C provides dynamic memory allocation functions:

  • malloc(), calloc() for allocation.
  • free() for deallocation.

File Handling

C allows reading and writing to files using file pointers.

  • File operations: fopen(), fclose(), fprintf(), fscanf().

FILE *fptr;

fptr = fopen("example.txt", "w");

fprintf(fptr, "Hello, File!");

fclose(fptr);