Saturday, May 25, 2013

fgets

fgets(buffer, requiredsizeofbuffer, stdin)

stdin is the file stream


Friday, May 24, 2013

fflush

Causes all remaining data in the buffer to be written - If the stream is a null pointer, it will be flushed. The stream remains open.

fread

Reads a block of data from stream. A stream is simply an array. fread reads an array.

The position indicator of the stream is advanced by the number of bytes read.

It reads through bytes but does not 'read the number of bytes successfully returned from a program' as many of the tricky questions will ask.


stderr

The standard error stream. Often, stderr and stdout are associated with the same device but may be split.

gets & fgets

gets is a not a safe method to get data as it allows unbounded String size - leading to buffer overflows.

Should use fgets as a buffer size must be provided as an argument.

fclose

All remaining contents to written or read are finalised, all buffers are flushed.

If the stream closes successfully, 0 is returned or else EOF is returned.

stdio

Standard input and output
Operates with streams - input devices, etc
Streams are handled as pointers to FILE objects
3 standard streams - stdin, stdout, stderr - automatically created and available to all programs using the library.

Friday, April 5, 2013

structs


A struct in C is a complex data type, like an array, except each element can be of a different type.

Each element, known as a member, has its own name (not a subscript) and its own type. A member can itself be a structured type. A structcorresponds to the "record" structure in other languages.

structs Example





The above snippet of code is a structure template ( as the structure declaration is not followed by variable names); the name "studentRecord" is a structure tag; the variables within are called members or components of the structure.

Whilst an array is an aggregate of values of the same type, a struct is an aggregate of inhomogeneous values ( that is, of distinct type).

We can then declare variables student1, student2 of this type by e.g.:

Alternatively, variables can be declared with the struct definition e.g. :

but we shall see a better way soon.

Once variables have been declared, we reference individual members using the 'dot' (.) operator:



Memory-Handling Functions



Also, in <string.h> are the mem... functions which are useful for manipulating objects as character arrays e.g.


Some Special C String Functions from





Always Check the Length of Your arrays



The Generic Pointer Type : void *


Any pointer maybe converted to type void * without loss of any information. Typical use of void * appears in function parameter type definitions ( so the function can receive pointers of arbitrary type: e.g 
void *memcpy(void *s, const char *t);)


(Think of void pointers as java Object)

fgets

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

http://www.cplusplus.com/reference/cstdio/fgets/

Thursday, April 4, 2013

The preprocessor


Lines that begin with # communicate with the preprocessor.

#include <stdio.h>

tells the preprocessor to include a copy of the header file stdio.h at this point in the code. (This is a header file provided by the C system).

The angle brackets < > indicate that the file is to be found in the usual place (system dependant property).

Symbolic Constants are defined:

#define MAX 100


Backslash n & escape sequences


\n

Represents a single character (even though it looks to be 2 characters) called 'New line' - a non-printing character which advances the cursor on the screen to the beginning of the next line.

\r

Carriage return

From http://www.dummies.com/how-to/content/c-language-escape-sequences.html


Problem Solving Notes

Problem Solving != Debugging

Problem Solving != ad-hoc compile & tweak

A designers 3 most important tools:

  • Knowledge
  • Experience
  • Good judgement

Past problems can be useful starting points for new problems.

Programming in the small:
1 or small number of people contributing to the project.

Programming in the large:
many people contributing modules to the same project.

Decomposition & abstraction:
Take a large problem and break it down into smaller sub problems.

Abstraction involves considering sub-problems in isolation to reduce complexity.

Wednesday, April 3, 2013

Pointer Arithmetic

Pointer arithmetic

Can only use addition and subtraction.

Can use to step through an array - use the increment operator to generate the address of the next element.

2 * sizeof(<type-pointed-to>)

of any array of any type, the following is true:
x[i] is equivalent to *(x+i)

Array notes


Array names are considered to be pointers - also constant pointers.

An array name cannot be changed/modified.

x = &y is illegal.

Array name considered to be the address of the 0th element (base address) of the array.

The expression x is equivalent to x[0].

char





More on the char Type

  • Usually 8 bits, one byte.
  • Represented as a small integer (short)
  • If it is an integer, and it is signed (7 bits plus a sign bit) which means we have 27 -1 = 127 as the largest integer stored 01111111, or
    11111111 = -128 = -27 when it is signed
  • Characters are represented via an ASCII encoding which is a positive integer corresponding to each character. This implies that if characters are stored assigned, we can accommodate 127 characters.

Unsigned Characters

  • If the internal representation of characters is unsigned, that is, stored as an unsigned short integer, then we have available to us 28 -1 = 255 characters.
  • C does not specify how they are stored. Often they are stored as an unsigned short integer.

Type qualifiers



sizeof operator


C does not specify maximum/minimum values or the storage space allocation for any of its types. They are machine dependent. The operator sizeof() is defined which evaluates to the number of bytes allocated for a variable or type. Type char is always 1 byte (which is assumed to be 8 bits) and type wchar_t is provided for Unicode. 

Also, C does not specify the internal representation for data values e.g. negative numbers, real numbers etc The limits and internal representations aremachine dependent . 
Note that the libraries <limits.h> and <float.h> have the details of these.

The type of a sizeof expression is implementation defined, and can be a problem. Since sizeof returns an unsigned integer type, it is safest to cast the return value to ( unsigned long ) and then print it using %lu conversion.


Skippng Characters on the Input Line