FUNDAMENTALS C FINAL EXAM SUMMARY

UTS AUTUMN 2018 EXAM

(Fundamentals of C Programming 2018)

REVISION NOTES ARCHIVE

复习资料备份

1. PROGRAMMING FUNDAMENTALS

Variables

  • Name
  • type
  • Definition
  • Initialisation

printf()

  • printf(“%d”,123);
    SYNTAX MEANING
    d,I int as a signed decimal number
    f Double
    lf Long double
    .xxf To xx decimial
    s String
    C char

scanf().

  • scanf(“%d”,%i);

The general structure of a C program

/*Documentation Section: program to find the area of circle*/
#include "stdio.h"   /*link section*/
#include "conio.h"  /*link section*/
#define PI 3.14     /*definition section*/
float area;          /*global declaration  section*/
void main()
{
float r;                /*declaration part*/
printf("Enter the radius of the circle\n");   /*executable part starts here*/
scanf("%f",&r);
area=PI*r*r;
printf("Area of the circle=%f",area);
getch();
}
Ref:

Pre-processors

  • #include
  • #define
  • #ifndef

Types of errors – syntax, runtime and logic.

  • Compilation errors – using incorrect syntax. The compiler will pick these errors.
  • Logical errors – incorrect logic and operations. Only you can pick these errors through comprehensive testing.
  • Runtime errors – errors that happen in special corner cases. Only you can pick these errors through comprehensive testing.

Steps to follow when running a high-level language program.

  • Word processor (editor) – used to type in a program and corrections
  • Compiler – attempts to translate a program into machine code
  • Linker – resolves cross-references among object files
  • Loader – copies the executable file into memory and initiate the execution of instructions

Software development stages – requirement, analyze, design, implement, test, maintain.

  1. Problem requirement – understand what the program is expected to do
  2. Problem analysis – identify the inputs, outputs, and required resources
  3. Solution design – design the algorithm
  4. Implement – write the code
  5. Test – think about possible test cases and thoroughly verify the program
  6. Maintain and update

 

DATA TYPES, OPERATORS AND SIMPLE FUNCTIONS

Variables

  • Scope

Functions

  • Prototype/definition
  • Basic structure
    • return type
    • function name
    • Input parameters
    • Definition(函数实现)
  • Invoke
    • Call a function
  • pre-defined
    • Someone else has written the function prototype and definition, and you use it
  • user-defined
    • You write the function prototype and definition, and you use it.

Type casting

  • int a = 1, b = 2; float c; c = a/b; = 1/2 [Int]

Order of operations

  • Top to bottom, left to right. unary (+, -), binary (+, -, *, /, %, ()), assignment (=).

03 CONTROL STRUCTURES (IF AND LOOP)

General structure

  • if-else, for, while, do while
  • Switch
  • Break
  • Initialisation
  • check condition (if?)
  • Variation
  • Counter-controlled
    • 知道要遍历多少次用
  • Sentinel-controlled
    • 不知道要遍历多少次用

04 ARRAYS AND STRINGS

Array basic

  • Define an array
  • Access to an element

Function

  • Passing arrays to functions
  • Scope
  • Size
  • const
    • error: read-only variable is not assignable

functions from string.h

  • strcpy(), strcmp(), strcat().

Array Passing (Pointer)

  • Consider int a[10]; Value/address – a (Address), a[0](Value), &a[0] (Address). True/false  a==&a[0] ? Can I write – int* p = a ? [YES]
  • Dynamic sized arrays. How to define and use? (动态数组) malloc? calloc?

String

  • null termination. \0
  • Strings in C are – char stringa [], or char* stringp.

05 STRUCTURES AND FILE PROCESSING

STRUCTURES

  • define a structure type
  • access an element
  • Passing strctures to functions
  • Scope
  • Initialize
  • Can a function return a struct? – YES

FILE PROCESSING

  • Text/binary files [!]
Different Advantages Disadvantages
Text file contains human readable characters

line is marked by LF (Linux) or CR LF (WIN)

Human can read Loss
Binary file not limited to human readable characters

Copy every single byte from RAM

Accurate Only machine understandable
  • File Processing function
    • Basic function 
      • fopen(fp,”mode”)
      • fclose(fp)
      • FILE *fp
    • Text File
      • fprintf(fp,”%c”,p.letter)
      • fscanf(fp,”%c %d %lf”,&p.letter,&p.age,&p.avg);
    • Binary File
      • fwrite(&p,sizeof(p),N of elements,fp);
      • fread (&p,sizeof(p),number of element,fp)
        • n number of byte a time
      • fgetc() fputc()
        • 1 byte a time.
    • read till the end of a file [EOF]

06 07 POINTERS AND DYNAMIC MEMORY MANAGEMENT

POINTERS

  • Meaning
    • int x; x; &x;
    • int* xp; xp; *xp;
    • void fun(double* yp); double d; fun(&d);
  • Scope of variables
    • block scope
  • Function
    • output more than one result using pointers.
  • Arrays
    • Pointers and arrays
    • 混淆地方 [!]
    • int * intPtr = (int*) malloc(sizeof(int));
          *intPtr = 1;
          printf("int is %d",*intPtr);
          int * intArrPtr = (int*) malloc(sizeof(int)*2);
          intArrPtr[0] = 1;
          intArrPtr[1] = 2;
          printf("idx 0 is %d idx 1 is %d",intArrPtr[0],intArrPtr[1]);

       

DYNAMIC MEMORY MANAGEMENT

Memory Management

  • stdlib.h functions
    • malloc, free
  • Common mistakes
    • uninitialised pointers (未初始化的指针)
      • point to any (unknown) memory location
    • dangling pointers (指针指向错误的对象)

do not point to a valid object of the appropriate type

  • double deallocation (内存被free了多次)
    • leads to undefined behavior
    • double-freeing a block of memory will corrupt the state of the memory manager
    • cause existing blocks of memory to get corrupted or for future allocations to fail in bizarre ways
  • memory leaks (内存溢出)
    • when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released.
  • Linked lists
    • (Assumed Knowledge)

09 10 11 OTHER TOPICS

Programming in the large

  • custom libraries
    • Own developed library (Assumed Knowledge)
  • compiling multiple c source codes
    • Multiple header file issue
      • Pre-processing directive
  • Makefiles
    • Make other person to build easier.

Cross development for embedded systems

  • Develop the program on pc and flash it to the embed system to debug.

Command line parsing

  • Allows to pass parameters to a program through the command line at the time of execution.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください