Suggestions logoSuggestions

Theory

Theory notes and core concepts.

1. C Language

  • C is a structured, procedural language.
  • Created by Dennis Ritchie in 1972.
  • Fast and portable; can work close to hardware.
  • Used for system software and general apps.

2. Structure of a C Program

Sections:

  • Documentation (comments)
  • Link section (#include header files)
  • Global declarations
  • main() function - starting point
  • User-defined functions

Syntax:

#include <headerfile.h>  // Link section
// Global declarations

int main() {            // main function
    // code
    return 0;
}

// User-defined function
return_type function_name(parameters) {
    // function body
}

Example:

#include <stdio.h>
int main() {
    printf("Hello World");
    return 0;
}

3. Keywords

  • Reserved words in C with predefined meaning.
  • Cannot be used as identifiers.
  • Examples: int, float, if, while, return

4. Identifiers

  • Names for variables, functions, or arrays.

Rules:

  • Start with a letter or underscore.
  • Do not use keywords.
  • Case-sensitive.

5. Constants

  • Fixed values that do not change.

Types and examples:

  • Integer: 10
  • Floating: 3.14
  • Character: 'A'
  • String: "Hello"

6. Data Types

  • Define what kind of data a variable can hold.

Types:

  • Basic: int, float, double, char
  • Derived: arrays, pointers
  • Void: no value

Syntax:

data_type variable_name = value;

Example:

int age = 20;
float pi = 3.14;
char grade = 'A';

7. Operators

  • Arithmetic: +, -, *, /, %
  • Relational: ==, !=, >, <, >=, <=
  • Logical: &&, ||, !
  • Assignment: =, +=, -=, *=, /=
  • Increment/Decrement: ++, --
  • Conditional: ? :

Syntax Example:

int a = 5, b = 3;
int sum = a + b;        // arithmetic
int max = (a > b) ? a : b; // conditional

8. Control Structures

Decision Making:

If:

if(condition) {
    // code
}

If-Else:

if(condition) {
    // code
} else {
    // code
}

Switch:

switch(expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // code
}

Loops:

For:

for(initialization; condition; increment/decrement) {
    // code
}

While:

while(condition) {
    // code
}

Do-While:

do {
    // code
} while(condition);

Jump Statements:

  • break - exit loop
  • continue - skip current iteration
  • goto - jump to label
  • return - exit function

9. Functions

  • Reusable block of code that performs a task.

Types:

  • Library functions: printf(), scanf()
  • User-defined functions

Syntax:

return_type function_name(parameters) {
    // code
    return value;  // if return_type is not void
}

Example:

#include <stdio.h>
void greet() {
    printf("Hello!\n");
}
int main() {
    greet();
    return 0;
}

Call Methods:

  • Call by value: copy passed; original unchanged
  • Call by reference: address passed; original may change

10. Arrays

  • Collection of same-type elements stored in order.

Syntax:

  • 1D Array: data_type array_name[size];
  • 2D Array: data_type array_name[rows][columns];

Examples:

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

11. Strings

  • Character array terminated by a null character \0.

Syntax:

char str[size] = "text";

Example:

char name[10] = "CSE";

Common Functions:

  • strlen(str) - returns length
  • strcpy(dest, src) - copy string
  • strcmp(str1, str2) - compare strings
  • strcat(dest, src) - concatenate strings

12. Pointers

  • Variable that stores the address of another variable.

Syntax:

data_type *pointer_name;
pointer_name = &variable;

Example:

int x = 10;
int *p = &x;
printf("%d", *p);  // prints 10

Uses:

  • Access arrays and strings
  • Pass by reference in functions

13. File Handling

  • Store and retrieve data permanently in files.

Functions:

  • fopen() - open file
  • fclose() - close file
  • fgetc(), fputc() - read/write characters
  • fprintf(), fscanf() - formatted I/O

Syntax:

FILE *fp;
fp = fopen("filename", "mode");
fclose(fp);

Modes:

  • "r" - read
  • "w" - write
  • "a" - append
  • "r+" - read and write

Example:

#include <stdio.h>
int main() {
    FILE *fp;
    fp = fopen("data.txt", "w");
    fprintf(fp, "Hello C\n");
    fclose(fp);
    return 0;
}

Key Differences (Plain Text, Bullets)

While loop and Do-while loop

  • While: checks condition first; may not run.
  • Do-while: runs once, then checks; runs at least once.
  • While: entry-controlled. Do-while: exit-controlled.
  • Use while when you may skip; use do-while when one run is needed.

For loop and While loop

  • For: count known. While: count unknown.
  • For: init, condition, update together. While: separate.
  • For: compact for counter loops. While: flexible for condition loops.
  • Example: For prints 1 to 100; while reads input until 0.

Array and String

  • Array: stores same-type elements. String: char array ending with '\0'.
  • Array can be numeric/char/etc.; string is always char.
  • Array elements stand alone; string characters form text.
  • Example: Array 3; String "Hello".

Array and Pointer

  • Array: fixed-size values. Pointer: stores an address.
  • Array size set at compile time; pointer flexible.
  • Array accessed directly; pointer dereferenced with *.
  • Example: Array int arr[5]; Pointer int *p = &arr[0].

Call by Value and Call by Reference

  • Value: pass a copy; original unchanged.
  • Reference: pass address; original may change.
  • Value is safer; reference is more efficient but can modify input.
  • Example: value swap(a,b); reference swap(&a,&b).

Local and Global Variable

  • Local: declared inside a function; accessible only there.
  • Global: declared outside; accessible everywhere.
  • Local exists during the function call; global for the whole program.
  • Example: Local int x inside main; Global int x outside all functions.

On this page