Week 4 Lecture

KEY CONCEPTS

Q: What happens if you forget to initialize a variable? (Concept is called initialized variables — NTS: read more about this)

A: The result is unpredictable because:

  • variables are memory spaces on the stack

  • memory spaces are maps to physical addresses

  • these physical addresses may or may not have remaining numbers…

Q: Is there a signed float & signed double data type? Why or why not?

A:

Q3

A:

#include <stdio.h> is a preprocessor directive that tells the compiler to include the Standard Input/Output (stdio) library in your C program. This library provides essential functions for handling input and output operations, such as:

  • printf() – Used to display output on the screen.

  • scanf() – Used to take input from the user.

  • puts() and gets() – Used for string output and input (though gets() is unsafe).

  • fopen(), fclose() – Used for file operations.

Without #include <stdio.h>, if you try to use printf() or scanf(), the compiler won’t recognize them, resulting in errors.

Q4: Why is it crucial to match/use the right specifier with the right data type?

A:

#include <stdio.h>

int main() {
    printf("Hello, world!\n"); // Prints "Hello, world!" to the screen
    return 0;
}

If you remove #include <stdio.h>, printf() won’t work, and the program won’t compile.

Q: T or F: Does C language have function overloading? Why or why not?

because

Q: When should you use the sine f function?

Q: How come C does not have True or False?

Functions

  • Value-returning functions —> are as close to mathematical functions in real life that can be done in coding

    • Function prototypes are not optional — they should always be included

    • NTS: Determine diff b/w function prototype & function blue prints

  • Non-value returning functions —> they are different from value-returning functions because…

Pass by Value

***IMP: THE NOTES ON SLIDE TITLED “C—- STATEMENTS” ARE CODE YOU SHOULD NEVER DO:

Example: a + 2 = a; a + a = b

GENERAL NOTES

  • Each data type has a minimum and maximum value

    • NTS: Prof said you DON’T need to memorize these EXCEPT you must memorize them for char & unsigned char

  • C is a medium-level language

    • All its safety checks are removed to make the program run faster

    • You as a programmer have to be smart enough to determine appropriate choices for mathematical calculations (e.g. choice of data types, division by zero, etc.)

  • C is case-sensitive

  • The only special character identifiers can begin with are underscores

  • Microsoft (and us) uses the LLP64 Data Model —> therefore it still considers long int data type to be 4 bytes (which is same size as int), so in order to use the real long int type you must use unsigned long int

  • Specifiers in C are the same as those in Python in Java

Sample Codes

//PART 1 (Exercise1_1): Printf(), \n, \r, \t, \b
// \n & \t have keyboard shortcuts


int main() {
    printf("Hello World");
    printf("I am Alex"); 
    // OUTPUT: Hello WorldI am Alex

    //You must include a newline character "\n" to separate 2 lines: 
    printf("Hello World"\n); 
    printf("I am Wande");

    //or like this: 
    
    printf("Hello World!\n\n\nI am Alex."); //However this is not recommended as it makes your code hard to read 
    
    printf("Are you going to the party this weekend?"\r);
    printf("I think so.")
    
    printf("Where did you get that top from?\t26\n");
    
    printf("Hola.");
    
    return 0;
}

//PART 2: %d, data types

int main() {
    int x = 1; 
    int y = 2; 
    
    printf("x + y = %d\n", x+y); //%d is a decimal placeholder for the result of x + y
    
    printf(%d + %d = %d\n);
    //Why will the above calculation eventually lead to overflow?
    
    return 0; 
}

int main() {
    
    int x, y; 
    
    printf("Please enter x = ");
    
    scanf("%d , &x");
    
    printf("Please enter y = ");
    
    scanf("%d , &y");
    
    //be careful when using scanf because it doesn't detect errors
}