Notes on C Programming Concepts
Executable Statements
- Course: COP 3223C - Introduction to Programming with C
- Instructor: Yancy Vance Paredes, PhD.
Assignment Statements
- Assignment statements store a value or a result to a variable.
- The assignment operator is
=. - Important Note: Unlike in mathematics, the
=symbol is not symmetric!
Syntax
- Syntax structure:
variable_name = expression; - The expression can be either a literal value or the result of a computation.
- Example:
```c
int num;
num = 10;
- **Advance example:**
c
int num1, num2;
int result;
num1 = 10;
num2 = 20;
result = num1 + num2;
- Add additional computations to `result`:
c
result = num1 + num2 + 50;
result = result + 10;
result = result + 10;
result = result + result;
- **Key Reading Tip:**
- Always read code from top to bottom.
- In assignment statements, evaluate the right-hand side first before assigning it to the left-hand side.
### Your Turn!
c
include
int main(void) {
int sum = 0;
int num;
num = 5;
sum = sum + num;
sum = sum + num;
sum = sum + num;
return 0;
}
#### Question: What is the final value of `sum`?
- **Solution:**
- `sum` will equal 15 after executing the program.
### Input/Output (I/O) Operations
- **Input Operation:**
- Copies data from input devices (e.g., keyboard) into memory.
- **Output Operation:**
- Displays information stored in memory.
### I/O Functions in C
- Functions for I/O operations are defined in the `stdio.h` library.
- Common Functions:
- `printf()`: Outputs formatted data.
- `scanf()`: Inputs formatted data.
### The `printf()` Anatomy
- Example of using `printf`:
c
printf("Hello, World!\n");
- Breakdown:
- **Function Name:** `printf`
- **Format String:** A string enclosed in double quotes.
- **Argument Count:** The number of arguments passed.
#### Format Specifiers
- Placeholders:
- `%d`: integer
- `%lf`: double
- `%f`: float
- `%c`: character
- **Example:**
c
int num = 10;
printf("The value of num is: %d\n", num);
### Escape Sequences in C
- Common escape sequences and their meanings:
- `\a`: Alert
- `\b`: Backspace
- `\n`: Newline
- `\t`: Horizontal Tab
- `\\`: Backslash
- `\'`: Single Quote
- `\"`: Double Quote
- `\?`: Question Mark
- `%%`: Percent Symbol
### The `scanf()` Anatomy
- Example usage:
c
int num;
scanf("%d", &num);
- Breakdown:
- Uses `&` operator to specify the address where the input value is stored.
- **Example:**
c
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("The value of num is: %d\n", num);
### Common Mistakes with `scanf()`
- Wrong placeholder usage:
c
int num;
scanf("%f", &num);
- Forgetting to use the `&` operator:
c
scanf("%d", num);
### The `return` Statement
- Terminates the function and returns control to the calling function (or OS for `main()`).
- **Example:**
c
return 0;
```
- Can be used in functions where it is followed by a semicolon.
Questions?
- Any further inquiries about this content?