ENG200 Week 1 - Lectures Summary

1. What is the purpose of a compiler?

A compiler converts high-level code into low-level (machine) code that a computer can understand.

2. Identify the error in the following C program:

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

The missing semicolon (`;`) after printf("Hello World!") causes a compilation error.

3. What is the difference between local and global variables?

- Local variables are declared inside a function and can only be used within that function.

- Global variables are declared outside any function and can be used anywhere in the program.

4. What will be the output of the following code?

int a = 3;
int b = 6;
int c = a + b;
printf("%d", c);

The output will be 9 since c = a + b results in 3 + 6 = 9.

5. What is the correct syntax for scanning user input?

Use the scanf() function:

int a;
scanf("%d", &a);

6. What are the rules for defining an identifier in C?

- Must consist of letters, digits, or underscores (`_`).

- Cannot start with a digit.

- Cannot contain spaces or special characters (e.g., `-`).

- Cannot use reserved keywords (e.g., `int, return`).

- Case-sensitive: `Variable1` ≠ `variable1`.

7. What are comments in C, and what are their types?

Comments are used to describe code without affecting execution.

- Single-line comment: // This is a comment

- Multi-line (block) comment: /* This is a block comment */