Skip to content

Programming Essentials in C - Lecture 2: Introduction to C

Fundamentals of Programming Languages

A programming language is a formal language used to communicate instructions to a computer, enabling developers to write programs for specific tasks.

Key Uses

  1. Create Software Applications
    • Example: Mobile apps using Java (Android) or Swift (iOS).
    • Real-World: WhatsApp (Java for Android).
  2. Control Hardware
    • Example: C/C++ for microcontrollers in embedded systems.
    • Real-World: Airbag systems in cars.
  3. Perform Computational Tasks
    • Example: Python for data analysis or machine learning.
    • Real-World: Netflix recommendation algorithms.

Types of Programming Languages

Low-Level Languages

Interact directly with hardware. Examples include:

  • Machine Code

    • Binary (0s and 1s) executed by the CPU.
    • Example: 10110000 01100001 (move data to a register).
    • Fast but hard to write/debug.
  • Assembly Language

    • Human-readable mnemonics for machine code, converted by an assembler.
assembly
MOV AL, 61h  ; Load 61 (hex) into register AL
ADD AL, 1    ; Increment AL by 1

High-Level Languages

Use English-like keywords, easier for humans to understand. Divided into:

  • Structured Programming

    • Breaks programs into functions/modules. The data is separated from its behavior.
    • Examples: C, Basic, Fortran, Cobol, Algol, Pascal.
  • Object-Oriented Programming (OOP)

    • Based on objects (data + behavior) and classes.
    • Examples: Java, Python, C#, C++, JavaScript, PHP, Ruby, Dart, Perl, Swift, Scala.

Structured vs. Object-Oriented Programming

AspectStructured ProgrammingObject-Oriented Programming
ParadigmDivides code into functions/modulesUses objects combining data and behavior
FocusFunction-based subprogramsObject-based encapsulation
ModifiabilityHard to modifyEasier to modify
CommunicationMain function calls other functionsObjects pass messages
Access SpecifiersNonePublic, private, protected
Data SecurityNot securedSecured via encapsulation
Code ReuseDifficultEasy (inheritance, polymorphism)

Compiler vs. Interpreter

Compiler

Translates entire high-level code into machine code at once.

How The Compiler Works

  1. Lexical Analysis
    Breaks the source code into tokens, such as keywords, operators, and identifiers.

  2. Syntax Analysis (Parsing)
    Checks the code's structure against the language's grammar to ensure correctness.

  3. Semantic Analysis
    Verifies the logic and meaning, such as type checking and scope resolution.

  4. Optimization
    Improves the efficiency of the code, reducing resource usage and execution time.

  5. Code Generation
    Converts the optimized intermediate representation into machine code or bytecode.

  6. Linking
    Combines different compiled modules and libraries into a single executable.

  • Examples: C, C++, Java (to bytecode).

Interpreter

Translates and executes code line-by-line.

How The Interpreter Works

  1. Line-by-Line Execution
    Reads the source code one line at a time.

  2. Immediate Execution
    Converts each line into machine code and executes it immediately.

  3. No Intermediate Object Code
    Does not generate an intermediate object file; execution happens directly.

  • Examples: Python, JavaScript.

Comparison

FeatureCompilerInterpreter
TranslationEntire program at onceLine-by-line
SpeedFaster (post-compilation)Slower (line-by-line interpretation)
ErrorsShown after full compilationStops at first error
Intermediate CodeYes (object code)No

Introduction to C Programming

What is C?

  • A high-level, general-purpose language developed by Dennis Ritchie in 1972 at Bell Labs.

Why Learn C?

  • Foundation for all major languages: C++, Java, Python.
  • Fast and efficient due to low-level memory control.
  • Portable across platforms.
  • Used in operating systems, embedded systems, and hardware drivers.

C vs. C++

  • C++ extends C with OOP (classes/objects).
  • C focuses on structured programming.
  • C++ supports classes and objects, while C does not.

Getting Started

NOTE

Web-based IDE's can work as well, but functionality is limited.

Your First Program in C

Hello World Example

c
#include <stdio.h>

int main() {
  /* My First Program in C */
  printf("Hello, World!\n");

  return 0;
}
  • Output: Hello, World!
  • Breakdown:
    • #include <stdio.h>: Adds input/output functions from stdio.h header file library.
    • int main(): Entry point of the program.
    • printf(): Displays text.
    • return 0: Ends program successfully.

Notes

  • Semicolons (;) end statements.
  • Most lines of C code end with a semicolon.
  • Whitespace is ignored by the compiler but improves readability.

NOTE

The body of int main() could also be written as:

c
int main() { printf("Hello World!"); return 0; }

Syntax Errors

Syntax errors occur when code violates C's grammatical rules.

  • Example: Missing semicolon.
c
printf("No semicolon here")  // Error: Missing semicolon
  • Fix: Add the semicolon: printf("No semicolon here");.

Escape Sequences in C

Escape sequences help format output in C:

  • \n - New line
  • \t - Tab space (typically 8 spaces)
  • \\ - Backslash
  • \" - Double quote (inside single quotes)

Example:

c
printf("Hello, \nWorld!\tI'm Learning \"C\" Language\n");

Output:

text
Hello,
World!	I'm Learning "C" Language

Comments in C

Comments improve code readability and can prevent execution for testing.

Single-line comment:

c
// This is a comment
printf("This will run!\n");

Multi-line comment:

c
/* This is
a really
long comment
*/
printf("This will run!\n");

Data Types in C

Categories

CategoryTypes
Basicint, char, float, double
Derivedarray, pointer, structure, union
Enumerationenum
Voidvoid

Basic Data Types

TypeRange (Approx.)Description
char-128 to 127 (signed), 0 to 255 (unsigned)Single character (1 byte)
int-32,768 to 32,767 (2 bytes) or -2,147,483,648 to 2,147,483,647 (4 bytes)Integer (4 bytes typically)
float1.2E-38 to 3.4E+38Single-precision float (4 bytes)
double2.2E-308 to 1.8E+308Double-precision float (8 bytes)
voidN/AUsed for functions that don't return a value

Examples

  • Char: Stores a single character (as an ASCII value).
c
char c = 'A';  // Store a character
printf("Char: %c, ASCII: %d\n", c, c);
  • Output: Char: A, ASCII: 65

  • Int: Stores whole numbers (typically 4 bytes in modern systems).

c
int num = 100;  // Store an integer
printf("Integer: %d\n", num);
  • Output: Integer: 100

  • Float: Stores decimal numbers with precision up to 6-7 decimal places.

c
float pi = 3.14159;  // Store a float
printf("Float: %.2f\n", pi);  // display 2 decimal places
  • Output: Float: 3.14

  • Double: Stores decimal numbers with precision up to 15-16 decimal places.

c
double bigNum = 155000.789123;  // Store a double
printf("Double: %.6lf\n", bigNum);  // display 6 decimal places
  • Output: Double: 155000.789123

Derived Data Types

Data TypeDescription
arrayCollection of elements of the same type
pointerStores memory addresses of another variable
structureCollection of variables of different types
unionSimilar to a structure but shares memory among members
enumDefines a set of named integer constants
  • Array:
c
int nums[3] = { 1, 2, 3 };  // Array of 3 integers
printf("Array: %d, %d, %d\n", nums[0], nums[1], nums[2]);
  • Output: Array: 1, 2, 3

  • Pointer:

c
int x = 10;
int* ptr = &x;  // Pointer to x's address

printf("Value: %d\n", *ptr); // Dereferencing
printf("Address: %p\n", ptr); // The address may vary
  • output:
text
Value: 42
Address: 0x7ffc5d361954
  • Struct:
c
#include <stdio.h>

struct Person {
  char name[20];
  int age;
};

int main() {
  struct Person p1 = {"Alice", 22};
  printf("Name: %s, Age: %d\n", p1.name, p1.age);
  return 0;
}
  • Output: Name: Alice, Age: 22

  • Union:

c
#include <stdio.h>

union Data {
  int i;
  float f;
};

int main() {
  union Data d;
  d.i = 10;
  printf("Union Integer: %d\n", d.i);
  return 0;
}
  • Output: Union Integer: 10

  • Enum:

c
#include <stdio.h>

enum Color { RED, GREEN, BLUE };

int main() {
  enum Color myColor = GREEN;
  printf("Enum Color Value: %d\n", myColor);
  return 0;
}
  • Output: Enum Color Value: 1

Type Qualifiers (Modifiers) in C

Type modifiers adjust the size and behavior of integer and floating-point types.

ModifierApplies ToExampleDescription
shortintshort int x;Uses less memory than int.
longint, doublelong int y;, long double z;Increases range (int) or precision (double).
signedchar, intsigned char a; (default)Supports both positive and negative values.
unsignedchar, intunsigned int b;Stores only non-negative values (0 and up).

Practical Example

Calculate total cost using different data types:

c
#include <stdio.h>
int main() {
  int items = 50; // Number of items
  float costPerItem = 9.99;
  float totalCost = items * costPerItem;
  char currency = '$'; // Currency symbol

  printf("Items: %d\n", items);
  printf("Cost per item: %.2f%c\n", costPerItem, currency);
  printf("Total cost: %.2f%c\n", totalCost, currency);
  return 0;
}
  • Output:
text
Items: 50
Cost per item: 9.99$
Total cost: 499.50$