DOC PREVIEW
DREXEL CS 265 - C What you Know

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C What you Know*TopicsHello, World!PointersPointers and Arrayscharacter arrays (strings)Library Support for StringsSlide 8structsArrays of ArraysI/Omycat.cFormatted OutputExampleSlide 15Formatted InputSlide 17Dynamic Memory Allocationfree and reallocExample (1/2)Example (2/2)Lists in CSlide 23char** AgainSlide 25Command Line ArgumentsSlide 27Slide 28C 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 manipulationTopics•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$ ./helloHelloPointers•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 is a 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 array elements–access 4th element of name•name[4];•*(name + 4);character arrays (strings)•In C, there is no built-in support for strings. Instead strings are represented as null-terminated character arrays.•char name[7];•name = “Fred” /* compiler adds ‘\0’ */•name[0] = ‘F’; name[1] = ‘r’; •name[2] = ‘e’; name[3] = ‘d’;•name[4] = ‘\0’Fred\0nameLibrary 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 destLibrary 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 with sufficient 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 by src to a character array pointed to by dest.–This should be used to prevent overflow errors.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”;s t a r t \0e n d \0m i d d l \0e……namesI/O•#include <stdio.h>•stdin, stdout, stderr–standard input, standard output, and standard error file streams•character I/O–int get char()•read a single character from stdin – if none available return EOF (end-of-file)•getchar() is a macro that uses getc–int get c( FILE *FP )•read a single character from the file or stream identified by FP–int put char( char c )•write the character c to stdout – if successful return the the character c otherwise EOF.–int put c( char c, FILE *FP )•write the character c to the file or stream identified by FPmycat.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 intExample#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 resultExampleint main(){char name[MAXNAMELEN]; /* fixed size array


View Full Document

DREXEL CS 265 - C What you Know

Download C What you Know
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C What you Know and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C What you Know 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?