Académique Documents
Professionnel Documents
Culture Documents
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
Topics
Getting Started pointers character arrays and string functions structs I/O dynamic memory (malloc and free)
Hello, World!
#include <stdio.h> int main() { printf(Hello World\n); return 0; } $ gcc -o hello hello.c $ ./hello Hello
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 */
i = *ip;
r e d \0
structs
A struct is used to group various data fields in a single type (classes without methods) typedef can be used to give a name to a particular struct declaration. Use . operator to access different fields.
typedef struct Employee Employee; struct Employee { char *name; double wage; int id; } Employee worker; worker.name = Fred; worker.id = 1; worker.wage = 15.25;
Arrays of Arrays
char *names[8]; names[0] = start;
names m e n d \0 i d d l e \0 s t a r t \0
#include <stdio.h> stdin, stdout, stderr standard input, standard output, and standard error file streams character I/O int getchar()
read a single character from stdin if none available return EOF (end-of-file) getchar() is a macro that uses getc
I/O
mycat.c
$ gcc mycat.c -o mycat $ ./mycat < mycat.c /* read character stream from stdin, copy to stdout */ #include <stdio.h> int main() { int c; while ( (c = getchar()) != EOF ) putchar( (char)c ); return 0; }
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; }
Example
/* a program illustrating character arrays and pointer arithmetic */ #include <stdio.h> #include <string.h> int main() { char name[20] = "Fred"; char *anothername; anothername = name; printf( "name = %s\n",name ); printf( "Another name for %s is %s\n, name, anothername ); printf( "The %d-th letter of %s = %c\n", strlen(name), name, name[3] ); printf( "The %d-th letter of %s = %c\n", strlen(name), name, *(name+3) ); return 0; }
Formatted Input
scanf is used to read a variable number of arguments with input format specified by a fomat string 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 input variables will be modified There must be sufficient arguments for the number of specified conversion types or unpredictable behavior may result There must be sufficient space in the arguments (e.g. character arrays) to hold the input or unpredictable behavior 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; }
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); }
Example (2/2)
/* -> operator: dereference fields of a struct given a ptr to the struct */ worker->name = malloc( (strlen("Fred")+1) * sizeof(char) ); /* same as (*worker).name */ strcpy( worker->name, "Fred" ); worker->id = 1; worker->wage = 15.25; printf( "%s earns $%4.2f per hour\n", worker->name, worker->wage); /* free space allocated for worker */ free( worker->name ); free( worker ); return 0; } /* main */
Lists in C
typedef struct Nameval Nameval; struct Nameval { char *name; int value; Nameval *next; /* in list */ }; /* newitem: create new item from name and value */ Nameval* newitem( char *name, int value ) { Nameval *newp; newp = (Nameval*) emalloc( sizeof( Nameval )); newp->name = name; newp->value = value; newp->next = NULL; return newp; }
Lists in C
/* addfront: add newp to front of listp */ Nameval* addfront( Nameval *listp, Nameval *newp ) { newp->next=listp; return newp; } nvlist = addfront( nvlist, newitem( smiley, 0x263A ));
char** Again
One must be careful when using multiple pointers to the same memory.
char name[10] = Fred; char *anothername; anothername = name;
If name changes so does anothername. If you want a copy, allocate additional memory and copy (strdup does this).
#include <stdio.h> #include <string.h> int main() { char name[20] = "Fred"; char *anothername;
Example
anothername = name; printf( "name = %s\n", name ); printf( "Another name for %s is %s\n", name, anothername ); name[3] = 'e'; printf( "name = %s\n", name); printf( "anothername has also changed to %s\n", anothername); /* the following causes problems since name and anothername point to the same space. */ free( anothername ); printf( "name = %s\n", name ); /* Bad! */ return 0; }
Example (1/2)
/* generate a sequence of n random numbers in the range 1..u */ #include <stdlib.h> #include <stdio.h> #define DEFLENGTH 10 #define DEFRANGE 1000 int main( int argc, char **argv ) { int n, u, i; n = DEFLENGTH; u = DEFRANGE;
Example (2/2)
if (argc != 3) { printf( "usage: %s n u\n", argv[0] ); exit( 1 ); } n = atoi( argv[1] ); u = atoi( argv[2] ); for( i=0; i<n; i++ ) { printf( "NR == %d\n", (rand() % u) + 1 ); } return 0; } /* main */