Lab 1: Installation, Environment Setup and Starting with C

Getting Started with C Programming - Lab Session conducted on August 6, 2025

Lab Objectives

Development Environment Setup

Required Tools and Software

  1. C Compiler (GCC): Install GCC compiler for compiling C programs. On Windows, use MinGW or install through Dev-C++, Code::Blocks, or Visual Studio.
  2. IDE/Text Editor: Choose from Visual Studio Code, Dev-C++, Code::Blocks, CLion, or simple text editors like Notepad++.
  3. Git for Version Control: Download and install Git from git-scm.com for version control and collaboration.
  4. GitHub Account: Create a GitHub account for code repository management and collaboration.

Complete Setup Guides

Download comprehensive guides for environment setup and GitHub integration:

Environment Setup Guide GitHub Integration Guide

Programming Exercises

Program 1: Hello World

Objective: Write a C program to print "Hello World"

hello_world.c
#include <stdio.h>

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

Expected Output:

Hello World

Key Concepts:

  • #include <stdio.h> - Preprocessor directive for standard I/O
  • int main() - Main function where program execution begins
  • printf() - Function to print formatted output
  • return 0; - Indicates successful program termination

Program 2: Multi-line Address

Objective: Print an address in multiple lines using newline characters

address.c
#include <stdio.h>

int main() {
    printf("John Doe\n");
    printf("123 Main Street\n");
    printf("Apartment 4B\n");
    printf("New York, NY 10001\n");
    printf("United States\n");
    return 0;
}

Expected Output:

John Doe
123 Main Street
Apartment 4B
New York, NY 10001
United States

Key Concepts:

  • \n - Newline escape sequence
  • Multiple printf() statements
  • String formatting and output control

Program 3: User Input (Name and Age)

Objective: Prompt user to enter their name and age, then display the information

user_info.c
#include <stdio.h>

int main() {
    char name[50];
    int age;
    
    printf("Enter your name: ");
    scanf("%s", name);
    
    printf("Enter your age: ");
    scanf("%d", &age);
    
    printf("\nHello %s!\n", name);
    printf("You are %d years old.\n", age);
    
    return 0;
}

Sample Input/Output:

Enter your name: Alice
Enter your age: 20

Hello Alice!
You are 20 years old.

Key Concepts:

  • char name[50]; - Character array declaration
  • int age; - Integer variable declaration
  • scanf() - Function to read formatted input
  • %s and %d - Format specifiers
  • &age - Address-of operator for scanf

Program 4: Addition of Two Numbers

Objective: Add two numbers entered by the user and display the result

addition.c
#include <stdio.h>

int main() {
    float num1, num2, sum;
    
    printf("Enter first number: ");
    scanf("%f", &num1);
    
    printf("Enter second number: ");
    scanf("%f", &num2);
    
    sum = num1 + num2;
    
    printf("\nSum of %.2f and %.2f is %.2f\n", 
           num1, num2, sum);
    
    return 0;
}

Sample Input/Output:

Enter first number: 15.5
Enter second number: 10.3

Sum of 15.50 and 10.30 is 25.80

Key Concepts:

  • float - Floating-point data type
  • %f - Format specifier for float
  • %.2f - Format specifier with precision
  • Arithmetic operations in C
  • Variable assignment and calculation

Compilation and Execution

Using GCC Compiler (Command Line)

Terminal Commands
# Compile a C program
gcc program_name.c -o program_name

# Run the compiled program (Windows)
program_name.exe

# Run the compiled program (Linux/Mac)
./program_name

# Example: Compile and run hello_world.c
gcc hello_world.c -o hello_world
./hello_world

Using IDE (Integrated Development Environment)

  1. Create New Project/File: Open your IDE and create a new C file with .c extension
  2. Write Code: Type your C program code in the editor
  3. Build/Compile: Use the Build or Compile option in your IDE (usually F9 or Ctrl+F9)
  4. Run Program: Execute the program using Run option (usually F10 or Ctrl+F10)

Common Errors and Troubleshooting

Compilation Errors

  • Missing semicolon: Each statement must end with a semicolon (;)
  • Missing header files: Include necessary header files like #include <stdio.h>
  • Undefined variables: Declare variables before using them
  • Mismatched brackets: Ensure all opening brackets have corresponding closing brackets

Runtime Errors

  • Buffer overflow: Ensure array sizes are sufficient for input
  • Wrong format specifiers: Use correct format specifiers (%d for int, %f for float, %s for string)
  • Missing & operator: Use & operator with scanf for variables (except arrays)

Lab Assignment

Tasks to Complete:

  1. Set up your C programming environment using the provided setup guide
  2. Create a GitHub account and configure Git (follow GitHub integration guide)
  3. Write and execute all four programs from this lab
  4. Modify Program 3 to also ask for the user's email and display it
  5. Modify Program 4 to perform subtraction, multiplication, and division as well
  6. Create a GitHub repository and push your lab programs
  7. Document any issues faced during setup and how you resolved them

Submission Requirements:

  • All four basic programs working correctly
  • Modified versions of Programs 3 and 4
  • GitHub repository link with all code
  • Screenshots of program outputs
  • Brief report on environment setup experience

Learning Outcomes

Upon completion of this lab, students will be able to: