CS349 SE382 A1 C Programming Tutorial
20 pages
English

CS349 SE382 A1 C Programming Tutorial

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
20 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

CS349/SE382 A1 C Programming TutorialCS349/SE382 A1 C Programming TutorialErin LesterJanuary 2005CS349/SE382 A1 C Programming TutorialOutlineDi erences from C++CommentsVariable DeclarationsObjectsDynamic MemoryBoolean Typestructs, enums and unionsOther DierencesThe Event LoopCS349/SE382 A1 C Programming TutorialDierences from C++CommentsComments:I C has no one-line comments, only block comments/* this comment is valid in C *//* so isthis one */// but this one is noti = 2; // and neither is this oneCS349/SE382 A1 C Programming TutorialDierences from C++Variable DeclarationsVariable Declarations:I Variables must be declared at the beginning of code blocks,before any other types of statements#include void main(){char c;int i;i = 10 + 2;char *mystring = "Hello World"; /* not allowed */c = ’\n’;printf("%s%c", mystring, c);}CS349/SE382 A1 C Programming TutorialDierences from C++ObjectsObjects:I C has no objectsI use ADTs — remember cs246 — to be modularCS349/SE382 A1 C Programming TutorialDierences from C++ObjectsCreating a mock class/ADTStep 1. Create a header le for your mock classinclude:I empty structure declarationI function headers w/ structure as parameterCS349/SE382 A1 C Programming TutorialDierences from C++ObjectsCreating a mock class/ADTStep 2. Implement the code for the mock classin a ‘.c’ le:I include your ‘.h’ leI declare the structureI implement the ADT’s functionsCS349/SE382 A1 C ...

Informations

Publié par
Nombre de lectures 39
Langue English

Extrait

CS349/SE382
A1 C Programming Tutorial
CS349/SE382
A1
C
Erin
Programming
Lester
January
2005
Tutorial
CS349/SE382 A1 C Programming Tutorial Outline
Differences from C++ Comments Variable Declarations Objects Dynamic Memory Boolean Type structs, enums and unions Other Differences
The Event Loop
Comments:
I C has no one-line comments, only block comments
/* this comment is valid in C */ /* so is this one */ // but this one is not i = 2; // and neither is this one
PCor281AimgnrgmaC/SE3S349om+CntmefresC+omeiDcnerotuTlairs
inmmutgTiaorilDnerefsecCmoraV++CS34/9ES83A2C1rPgoartaoilcraeleDirbans
Variable Declarations:
#include <stdio.h> void main() { char c; int i; i = 10 + 2; char *mystring = "Hello World"; /* not allowed */ c = ’\n’; printf("%s%c", mystring, c); }
I Variables must be declared at the beginning of code blocks, before any other types of statements
CS349/SE382 A1 C Programming Tutorial Differences from C++ Objects
Objects:
I
I
C has no objects
use ADTs — remember
cs246
to
be
modular
CS349/SE382 A1 C Programming Tutorial Differences from C++ Objects
Creating a mock class/ADT
Step 1. Create a header file for include:
I empty structure declaration
I function headers w/ structure
your mock class
as parameter
CS349/SE382 A1 C Programming Tutorial Differences from C++ Objects
Creating a mock class/ADT
Step 2. Implement the code for the in a ‘.c’ file:
I include your ‘.h’ file
I declare the structure
I implement the ADT’ functions s
mock
class
CPA1grro/S4982E3irotiDlaimmauTgnC3Scnseeer+CD+rfmoicMeynammory
I the pointer can then be cast to the desired type and the memory assigned to a variable
I it returns a (void*) pointer to the memory allocated
I malloc() takes the number of bytes of memory as its parameter
I memory is allocated using ‘malloc()’
Dynamic Memory Allocation
Dynamic Memory:
DynamicMemoryireneecfsorCm++mmragTinorutlDia/94383ESC1A2gorP
);
sizeof(
malloc(
)
myStruct
CS
{
...
myStruct *m (myStruct
= *)
typedef struct ... } myStruct;
2A1CProg349/SE38SCrosf++mCericeenrotuDlaimmarTgniymeroimMcyDan
Freeing Dynamic Memory
free( m );
I memory is freed using ‘free()’ I free() takes the address of a non-null variable as its parameter I there is no garbage collection, so don’t forget to clean-up what you create
CS349/SE382 A1 C Programming Tutorial Differences from C++ Boolean Type
Boolean Type:
I
I
I
zero evaluates to false
non-zero evaluates to true
use typedef and defines to
mimic
the
boolean
type
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents