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 Standrd 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 3.1.1 to test whether the program prints out argument 1 when supplied
  3. Add args, input, and output for user test 3.1.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.

You can follow the more detailed steps here or use the less detailed comments in the code

CTRL-Click Here for Follow Along Video

Requirements

  1. Set Breakpoints:
    • Click to the left of the line number in your source code where you want to pause the execution.
    • For your first debugging challenge, we will set a breakpoint on line 22, with the line
    • A red dot appears, indicating a breakpoint is set.
    • You can set multiple breakpoints.
  2. Start Debugging:
    • Click the Debugging Button in the side bar
    • Then press F5 or click on the green play button in the Debug Side Bar to start debugging.
    • The debugger will start and pause at the first breakpoint encountered.
  3. Inspecting Variables and State:
    • When the debugger pauses, you can hover over variables in your code to see their current values.
    • The Debug Side Bar will also show variables, call stack, and breakpoints.
    • When you stop for the first time, expand the variables heading in the Debug Side Bar
    • Under that section, you'll see x, new_value, and plain_value
  4. Execution Tool Bar:
    • Use the top control bar (appearing during debugging) to continue (F5), step over (F10), step into (F11), step out (Shift+F11), or stop (Shift+F5) the debugging process.
      • Continue (F5) restarts the execution until the next breakpoint is reached
      • Step Over (F10) executes the next line in the program but does not go into a function call
      • Step into (F11) works like Step Over but it steps into a function call.
      • Step Out (Shift-F11) will continue executing until the end of the current function is reached.
  5. Control Execution:
    • The program should be stopped at line the breakpoint, in the variables window, x will be 0
    • You will want to use either the continue (F5) or the Step Over (F10) to move the execution forward until you reach the breakpoint line and the value of x is 2
    • Write down the hex value of new_value (x is 2)
    • Step through again until reaching the print statement again, x should now be 3
    • Write down the hex value of new_value (x is 3)
  6. Change new_value's value
    • While still at the location from the last step (step 5) and stopped at line 18, where x==2 after looping three times
    • Change the value of new_value to 'b' which is 0x62 in hex by double clicking it and entering 'b'
  7. Complete the programs execution
    • Remove the breakpoint by clicking on the red dot
    • Click Continue (F5)
    • Write down the "Final value" printed out by the program
  8. Get Flag
    • Execute /challenge/getflag and enter in the 3 values you wrote down
    • You are done!

30-Day Scoreboard:

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

Rank Hacker Badges Score