C Programming: Features and Topics Differences between C and C++ - Prof. Kurt Schmidt, Papers of Computer Science

An introduction to the c programming language, focusing on its unique features that differ from c++. It covers topics such as pointers, character arrays and string functions, structs, i/o, dynamic memory management, and library support for strings. It also includes examples and code snippets.

Typology: Papers

Pre 2010

Uploaded on 08/19/2009

koofers-user-57p-1
koofers-user-57p-1 🇺🇸

10 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C What you Know*
Objective: To introduce some of the features of C.
This assumes that you are familiar with C++ or
java and concentrates on the features that are not
used in C++.
C does not have the OOP features of C++, nor
does it have as rich a library
no classes
I/O, string operations, and dynamic memory
management done through library calls
Not much support for higher level data structures (e.g.
STL)
Commonly lots of pointer manipulation
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download C Programming: Features and Topics Differences between C and C++ - Prof. Kurt Schmidt and more Papers Computer Science in PDF only on Docsity!

C What you Know*

•^ Objective: To introduce some of the features of C.This assumes that you are familiar with C++ orjava and concentrates on the features that are notused in C++. •^ C does not have the OOP features of C++, nordoes it have as rich a library^ – no classes^ – I/O, string operations, and dynamic memory

management done through library calls – Not much support for higher level data structures (e.g.STL) – Commonly lots of pointer manipulation

Topics

• Getting Started • pointers • character arrays and string functions • structs • I/O • dynamic memory (

malloc

and

free

Pointers

•^ To declare a pointer put

*^ in front of the variable

name.^ – int

i;^

/*^ declare

an^

int^

  • int

*ip;

declare

a^ pointer

to^

an^ int

  • char

*name;

declare

a^ pointer

to^

/*^

a^ character

•^ Use

&^ to get the address of a variable and

*^ to

dereference a pointer^ – ip

=^ &i;

assign

the

pointer

variable

/*^ ip

the

address

of^

i^ */

  • i^

=^ *ip;

assign

i^ the

value

of^

what

/*^

ip^ points

to.

Pointers and Arrays

•^ Arrays are related to pointers. An array variable isa pointer to the first location in the array.^ – declare static character array of size 10

  • char

name[10];

  • can initialize character arrays^ • char

name[10]

=^ “Fred”;

– char *anotherName; – anotherName = Name; • You can use pointer arithmetic to access arrayelements – access 4

th^ element of name

  • name[4]; • *(name

+^ 4);

Library Support for Strings

  • #include <

string.h

-^ strlen

( const char *s );

  • returns length of s (not including \0) • int^

strcmp

( const char *s1,^ const char *s2 );

  • return 0 if s1 == s2, <0 if s1 < s2, >0 if s1 > s2 • strcat

( char *dest, const char *src );

  • appends src to the end of dest – char D[20]; D[0] = ‘\0’; – strcat(D,”Jeremy”); strcat(D,” “); strcat(D,”Johnson”); • strncat

( char *dest,^ const char *src, int n )

-^ appends upto n characters of src to the end of dest

Library Support for Strings

  • char*

strcpy

( char *dest, const char *src );

  • copy the string pointed to by src (including

‘\0’

) to a

character array pointed to by dest. – dest must already point to a character array withsufficient space to hold src. – no error checking for insufficient space

  • char*

strncpy

( char *dest,

const char *src, int n );

  • copy at most n characters from the string pointed to bysrc to a character array pointed to by dest. – This should be used to prevent overflow errors.

Arrays of Arrays

• char

*names[8];

• names[0]

“start”;^ s

t

a

r

t

\

e^

n^

d^

\

m^

i^

d^

d^

l^

\0e

names

I/O

  • #include <stdio.h>• stdin

,^ stdout

,^ stderr

  • standard input, standard output, and standard error filestreams• character I/O – int

getchar

  • read a single character from stdin – if none available returnEOF (end-of-file) • getchar()

is a macro that uses

getc

  • int

getc

( FILE *FP )

  • read a single character from the file or stream identified by FP – int^

putchar

( char c )

  • write the character c to stdout – if successful return the thecharacter c otherwise EOF. – int^

putc

( char c, FILE *FP )

  • write the character c to the file or stream identified by FP

Formatted Output

-^ printf

prints a variable number of arguments using a specified format which is represented by a string^ – int

printf(

const

char

*FORMAT

[,^ ARG,

...]);

– FORMAT

is a string of characters (including special characters such as^ ‘\n’

for newline, and conversion formats for different types (with optional width and precision information). – man^

3 printf

for

more

info

-^ %d

int

-^ %c

character

-^ %s

string

-^ %f, %e, %g

floating point number

-^ %4.2f

(4 indicates width, 2 precision, i.e. xx.xx)

-^ %x

hexadecimal int

-^ %o

octal int

Example

#include <stdio.h> int main() {

typedef struct Employee Employee; struct Employee {

char

*name; double

wage; int^

id;

}; Employee worker; worker.name = "Fred"; worker.id = 1; worker.wage = 15.25; printf( "%s earns $%4.2f per hour\n",

worker.name, worker.wage ); return 0; }

Formatted Input

•^ scanf

is used to read a variable number of

arguments with input format specified by a fomatstring similar to

printf

  • int scanf(const char *FORMAT

[, ARG, ...]);

  • The arguments to

scanf

are the addresses to the

variables passed – this is required since the inputvariables will be modified – There must be sufficient arguments for the number ofspecified conversion types or unpredictable behaviormay result – There must be sufficient space in the arguments (e.g.character arrays) to hold the input or unpredictablebehavior may result

Example

int main() {^ char name[MAXNAMELEN];

/* fixed size array */

/* prompt the user to enter their name and^ read the string.

*/

/* "%s" tells scanf to read a string,^ which it stores in the array

name

.^

*/

/* A string is read until whitespace is^ encountered.

*/

/* The address of the name must be^ passed to scanf.

*/

printf("Enter first name\n"); scanf("%s",name); printf("Hello %s\n",name); return 0; }

free and realloc

•^ When the memory allocated by

malloc

is no

longer in use, it should be freed using

free

  • Not doing this causes a

memory leak

, which can cause

problems when lots of memory is required. – freeing memory when it is still being used is a commonbug; one that may be difficult to find.

•^ realloc

can be used to obtain additional

memory when the amount requested is insufficient(see

grow

from the text)

  • void

*realloc(

void

*APTR,

size_t

NBYTES

  • void

free(

void

*APTR

Example (1/2)

#include <stdlib.h> int main() {^ typedef struct Employee Employee;^ struct Employee {

char

*name;

double

wage;

int^

id;

}; Employee *worker;

/* allocate space for an Employee */ worker = (Employee *) malloc( sizeof( Employee ));

/* check for NULL - see emalloc in the text. */ if (worker == NULL) {

printf("malloc failed\n"); exit(1); }