LecLabs 2.1 C Intro and Vars


Intro to Programming Languages.

[ ← Module List ]

This module introduces the basics of Linux, including its history, architecture, and common commands. It is designed for beginners who are new to Linux and want to understand its fundamental concepts.


Challenges

Lecture video on Paradigms and C

Lecture video on First C Program and Vars

Lecture video on Standard Output

Lecture video on Standard In

Objective

In this lab, you will create a program that receives multiple types of input from arguments and standard input.

CTRL-Click Here for Follow Along Video

Steps to Complete

  1. The program should implement the requirements, which are marked with //CODE:
  2. Add args, input, and output for user test 21.21.1 to test whether the program prints out argument 1 when supplied
  3. Add args, input, and output for user test 21.21.2 to test whether the program prints the correct message when 't' is provided via standard input.
  4. Make sure to use gcc main.c -g -o main.bin to compile your program
  5. Test your program from the command line and using /challenge/tester
  6. /challenge/tester will give you the flag

Lecture video on Flow Control & Command Line Arguments

Write a C program that gets a number from standard input and then prints out "I can do it!\n" that many times with each line being the count.

For example, if the input is 3 then the program will print out

I can do it!
I can do it!
I can do it!     

Lecture video on Dec & Def Functions


int main(int argc, char *argv[]) {
    // Function body
    return 0;
}

Lecture video on Globals & Locals

Lecture video on Function Params and Ret Vals

Modify the provided main.c, to call the proper function (read the comments)

Objective

In this lab, you will practice writing and using functions in C by creating a simple simulation of a galactic pizza delivery service. You will create functions to handle different aspects of the delivery process, focusing on function usage and parameter passing.

CTRL-Click Here for Follow Along Video

Requirements

  1. Define Pizza Order Variables
    • Create global variable that holds the delivery speed of 50
    • Create a local variables in main for customer1's Id, customer1's distance, make id = 1, and distance = 50
    • Create local variables in main for customer2's Id, and customer2's distance, make id = 2 and distance = 25.
  2. Functions
    • Create both a prototype (forward declaration) and a definition for each function below and place the main function inbetween them.
    • Functions to Implement:
      • void display_order(int customer_number, int distance): This function will take a pizza order's attributes as arguments and display them.
        • Print "Customer number: \n"
        • Print "Distance to customer is light years.\n"
      • double calculate_delivery_time(int distance, int speed): This function will calculate the delivery time based on distance and speed, and return the delivery time.
        • Calculate the delivery time
        • Cast the result of the calculation to a double by placing (double) before the calcuation.
        • Return the result of the calculation
      • void deliver_pizza(int customer_number, int distance, int speed): This function will simulate the pizza delivery.
        • Calculate the delivery time using the calculate_delivery_time function
        • Print "Customer 's pizza will arrive in hours!\n."
        • Use the appropriate formatter for delivery time and make sure only 2 decimal points are shown, it should look like 0.50 or 1.00.
  3. main
    • Define main
    • Add the local variables
    • For each customer: Call display order and Call deliver pizza
  4. Tests
    • Write one test following the requirements in the user test descriptions.
    • Use /challenge/tester to pass the tests and get the flag

Lecture video on Global and Local Memory

Lecture video on Function Frames

Lecture video on Debugging C Programs

Using VSCode's debugger you can quickly find bugs!

Objective

We will work with the VSCode debugger to extract a few values from the program and change a variable in the middle of execution.

Review the debugger concepts below, then follow the numbered instructions in main.c to complete the lab.

CTRL-Click Here for Follow Along Video

Debugger Concepts

  • Breakpoints pause a running program before a selected line executes. Click beside a source-code line number to add or remove one.
  • Continue (F5) resumes execution until the program finishes or reaches another breakpoint.
  • Step Over (F10) executes the current line without entering a function call.
  • Step Into (F11) enters a function call so you can debug it one line at a time.
  • Step Out (Shift+F11) finishes the current function and pauses after returning to its caller.
  • The Variables section and editor hover show values that are in scope at the current execution point. Numeric values are normally displayed in decimal.
  • While execution is paused, you can double-click an editable value in the Variables section to change it.

Follow the numbered comments in main.c to complete the lab. The flag checker accepts decimal values or hexadecimal values beginning with 0x.


30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score