CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • First C Program
  • Statement
  • The main() function
  • Variable Declaration
  • Compilation Error vs. Run-Time Error
  • CS1010 I/O
  • Standard Input and Standard Output
  • Arithmetic Operations
  • Operators
  • The % Operator
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lecture

Lec 03 - Basic C Programming

PreviousLec 02 - Functions and TypesNextLec 04 - Conditionals

Last updated 6 months ago

Slides:

First C Program

Statement

A statement is a unit in a programming language that expresses either a command to be executed or declares a new variable or function.

The main() function

In C, main() is the entry point to the program. It is where the operating system will begin to execute the program.

main returns an integer of type int to the operating system, to signal to the operating system whether the program exits successfully or not. In this case, we always return 0 (success) assuming that nothing goes wrong for simplicity.

In modern C, the main function always returns 0 when it exits. So, we will skip this statement return 0; from the main function from now on.

Variable Declaration

To declare a vairable, we use a declaration statement looks like as follows:

int hypotenuse_square;

A variable is only visible within the innermost block that encloses the declaration, as specified by the { and }.

Compilation Error vs. Run-Time Error

Errors that occur during compiling is called "compilation error". In constrast, errors that occured during execution of a program is called a "run-time error".

However, in real life in C, you may encounter the following three errors/warnings:

Compilation Warning

A compilation warning is a message provided by the compiler that indicates potential issues in the code, but does not prevent the code from compiling or running.

  • Example: Unused variables or deprecated functions. While the program can still run, warnings suggest areas that might cause problems or be improved in the future.

  • Impact: The code still compiles and runs, but you should address warnings for better code quality.

Compilation Error

A compilation error is a mistake in the code that prevents the program from being compiled into an executable.

  • Example: Syntax errors (like missing semicolons, undeclared variables, or incorrect function calls). The compiler cannot generate machine code due to these errors.

  • Impact: The program doesn't compile at all. You need to fix the error before the code can be run.

Run-Time Error

A run-time error occurs while the program is running and leads to abnormal behavior or crashes.

  • Example: A segmentation fault occurs when the program tries to access an illegal memory location, such as dereferencing a null pointer or accessing out-of-bounds array indices.

  • Impact: The program crashes or behaves unexpectedly while running, and the error needs to be fixed to ensure stability.

CS1010 I/O

Standard Input and Standard Output

In Unix-flavored operating systems, the input is read from an abstract channel called the standard input or stdin for short, and an output is sent to an abstract channel called the standard output or stdout for short.

The fact that these channels are abstract is a powerful concept -- when we write our code, we do not have to worry about where the inputs come from and where the outputs go to. It will depend on how the user runs our program. Thus, it allows the users of our program the flexibility to control where the data comes from or goes.

For instance, the standard input, by default, reads from the keyboard. But the user can choose to read from a file, using the redirection < operator from the command line or the output of another process, using the pipe | operator from the command line. Similarly, the standard output, by default, writes to the terminal. But the user can choose to write to a file using the redirection > operator on the command line or to the input of another process, using the pipe | operator, again, on the command line when invoking the program. You will see how cool these are later. But for C programming, it suffices to know for now that we only need to read from stdin and write to stdout in our code, and we let the users decide where they come from / go to.

Arithmetic Operations

Operators

In C, The module remainder operator % works only on integer types.

The % Operator

It is commonly misunderstood that the % operator is equivalent to the modulo operation in number theory, where the operator always gives a positive remainder.

This equivalence holds when the two operands are positive. E.g., 9 % 4 gives 1 as the answer. But, when one of the operands is negative, the % operator may return a negative number.

The % operator in C is defined as follows: x % n is equivalent to x - ((x / n) * n) (where / is the integer division operator).

For instance, -9 % 4 is evaluated as -9 - ((-9 / 4) * 4) which is -9 - (-8), and gives -1.

On the other hand, 9 % -4 is evaluated as 9 - ((9 / -4) * -4), which gives 9 - 8, or 1.

To avoid confusion between the % operator in C and the modulo operation in number theory, we will call the % as the remainder operator.

All variables must be declared with its corresponding type before used in C. Indicating the type during declaration lets the computing device running the program knows how much memory should be reserved for the value of this variable, and subsequently, how to interpret the binary sequence stored associated with this variable. (See for deeper understanding)

CS1010 AY24/25 S1 Lecture 3
Lecture Slides
Types