Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

10
Unit 10 Miscellaneous Advanced Topics Introduction to C Programming

Transcript of Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Page 1: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Unit 10

MiscellaneousAdvanced Topics

Introduction toC Programming

Page 2: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Unit 9 Review

Unit 10: Review of Past Material

Page 3: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Unit 9 Review - StringsStrings are manipulated with the string library

The string library is made available by including <string.h>

The strcpy() function copies from one string to anotherConcatenation (joining) of two strings uses the strcat()

functionThe strlen() function returns the string lengthVersions of the string functions that contain the letter 'n'

strncat()strncpy()strncmp()

The sscanf() and sprintf() functions read and write strings

Page 4: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Unit 9 Review - FilesYou must first use a FILE * (file pointer) variableUse fopen() to access a file, and fclose() when

doneUsing the "r" mode allows a file to be opened for

readingUsing the "w" mode empties/creates a file for

writingUse fscanf() and fprintf() to read and write text

filesUse fread() and fwrite() to read and write binary

files

Page 5: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Recursive Functions

Unit 10: Miscellaneous Advanced Topics

Page 6: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Recursive FunctionsA problem which is defined in terms of a

simpler version is suitable for a recursive solution

For example, 2^n = 2 * 2^(n-1)A recursive function calls itself for a simpler

version of the problemThe base problem (n=0 or n=1) must be a

known answerFor example, 2^1 = 2

Page 7: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Structures

Unit 10: Miscellaneous Advanced Topics

Page 8: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

StructuresA structure is an implementation of a database

recordA record is a collection of related information stored

in fieldsExample: First-name, Last-name, Telephone-numberC language syntax

Keyword "struct" followed by braceDeclare each field just like a variable declarationClosing brace

Fields can be arrays or other structuresData is placed consecutively in memory, making a

block

Page 9: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

Unions

Unit 10: Miscellaneous Advanced Topics

Page 10: Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.

UnionsSyntax is like a struct, except for keyword unionThe fields are mutually exclusive, only one

applies at any timeThe memory for each field is overlaid in the

same physical memory, all starting at the same location (beginning of union)

Typically, a union appears as part of a larger struct record

There is typically a sentinel variable which is used in the program to determine which is the applicable union member to use