C programming for beginners

Fundamentals of a Program

Basics

  • Computers are very dumb machines
    • they only do what they are told to do
  • The basic operations of a computer will form what is know as the computer’s instruction set
  • To solve a problem using a computer, you must provide a solution to the problem by sending instructions to the instruction set
    • a computer program sends the instructions necessary to solve a specific problem
  • The approach or method that is used to solve the problem is known as an algorithm
    • So, if we were to create a program that tests if a number is odd or even
      • The statements that solve the problem becomes the program
      • The method that is used to test if the number is even or odd is the algorithm
  • To write a program, you need to write the instructions necessary to implement the algorithm
    • these instructions would be expressed in the statements of a particular computer language, such as Java, C++, Objective-C, or C

Terminology

  • CPU (central processing unit)
    • does most of the computing work
    • Instructions are executed here
  • RAM (random access memory)
    • Stores the data of a program while it is running
  • hard drive (permanent storage)
    • Stores files that contain program source code, even while the computer is turned off
  • Operating System
    • developed to help make it more convenient to use computers
    • a program that controls the entire operation of a computer
      • All input and output
      • manage the computer’s resources and handles the execution of programs
      • Windows, Unix, Android, etc.
  • fetch / Execute Cycle (life of a CPU)
    • fetches an instruction from memory (using registers) and executes it (loop)
    • A gigahertz CPU can do this about a billion times a second

Higher level programming languages

  • High-level programming languages make it easier to write programs
    • Opposite of assembly language
    • C is a higher level programming language that describe actions in a more abstract form
    • the instructions (statements) of a program look more like problem solving steps
    • do not have to worry about the precise steps a particular CPU would have to take to accomplish a particular task
      • total = x + vs.  mv as, 5, mv cx 4, etc……
  • Compilers
    • a program that translates the high-level language source code into the detailed set of machine language instructions the computer requires
    • the program does the high-level thinking and the compiler generates the tedious instructions to the CPU
  • Compilers will also check that your program has valid syntax for the programming language that you are compiling
    • finds errors and it reports them to you and doesn’t produce an executable until you fix them
  • high-level languages are easier to learn and much easier to program in than are machine languages

Writing a program

  • the act of writing a C program can be broken down into multiple steps

Steps in writing a program

  1. Define the program objectives
    1. Understand the requirements of the program
    1. Get a clear idea of what you want the program to accomplish
  2. Design
    1. Decide how the program will meet the above requirements
    1. What should the user interface be like?
    1. How should the program be organized?
  3. Write the code
    1. Start implementation, translate the design in the syntax of C
    1. you need to use a text editor to create what is called a source code file
  4. Compile
    1. translate the source code into machine code (executable code)
    1. consists of detailed instructions to the CPU expressed in a numeric code
  5. Run the program
    1. the executable file is a program you can run
  6. Test and Debug
    1. Just because a program is running, does not mean it works as intended
    1. Need to test, to see that your program does what it is supposed to do (may find bugs)
      1. Debugging is the process of finding and fixing program errors
      1. Making mistakes is a natural part of learning
  7. Maintain and modify the program
    1. programs are released and used by many people
    1. have to continue to fix new bugs or add new features
  8. Many new programmers ignore steps 1 and 2 and go directly to writing code
    1. A big mistake for larger programs, may be ok for very simple programs
    1. the larger and more complex the program is, the more planning it requires
    1. should develop the habit of planning before coding
  9. Also, while you are coding, you always want to work in small steps and constantly test (divide and conquer)

Leave a comment