DOC PREVIEW
GSU CSC 3320 - chapterC

This preview shows page 1-2-3-23-24-25-26-47-48-49 out of 49 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 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 49 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 49 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 49 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 49 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 49 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 49 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 49 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 49 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 49 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C PrograA Modern K. N. CPC PrograA Modern W. W. Norton & COriginal Notes by Converted to presentpMichaelammingApproachKing, irammingApproach,Company, 1996.Raj Sunderramanation and updated by py WeeksNote AboutNote AboutThi l h lzThis class has only pardedicated to Cz You already know Javasyntaxsyntax z You should be able to rt Coveraget Coveragetfth trt of the semester a, which has similar read the book quicklySimilarities oSimilarities o/* C t */z/* Comments */z Variable declarationsa ab e dec a at o sz If / else statementsz For loopsWhile loopszWhile loopsz Function definitions (likz Main function starts proof C to Javaof C to Javake methods) ogramDifferences betwDifferences betwCd th bj tzC does not have object− There are “struct”ures− But data are not tied to Ci f ti lzC is a functional prograz C allows pointer manipppz Input / Output with C− Output with printf functio− Input with scanf functionpween C and Javaween C and Javattsmethodsilamming languageulationonnVariableC has the following simple data types:Primitive type Size Minimum Maboolean1bitJava has the following simple data typesboolean1-bit––char 16-bit Unicode 0 Unibyte 8-bit -128 +12short16bit215+21short16-bit-215+21int 32-bit -231+23long 64-bit -263+26float32bitIEEE754IEEfloat32-bitIEEE754IEEdouble 64-bit IEEE754 IEEvoid – – –e TypeIn C:hhchar ch;ch = ‘a’;ch=‘A’;ch A;ch = ‘0’;ch = ‘ ‘;ximum Wrapper typeBooleans:char ch;int I;i‘’Booleanicode 216-1 Character27 Byte[1]51Short1i = ‘a’ch = 65;ch = ch + 1;ch++;5–1Short131–1 Integer63–1 LongEE754Floatch++;if (‘a’<=ch && ch<=‘z’)EE754FloatEE754 DoubleVoid1)ch=ch-’a’+’A’Data TData Th itfltdblz char, int, float, doublez long int (long), short inog t(og),sotz signed char, signed inz unsigned char, unsign1234L is long integer−1234L is long integer, − 1234 is integer, − 12.34 is float, − 12.34L is long floatgTypesTypesnt (short), long doublet (s o t), o g doub entned intData TData T'''\t' '\''\0'tz'a', '\t', '\n', '\0', etc. are zstrings: character arrayst gs c a acte a ay− (see <string.h> for strin− "I am a string"− always null terminated.− 'x' is different from "x"TypesTypesht ttcharacter constantsys ysg functions)Type ConType Contz narrower types are converted into wider typ− f + i int i converted to flzcharacters <---> integezcharacters <---> integez <ctype.h> library contaconversion functions, e−tolower(c) isdigit(c) ettolower(c) isdigit(c) etz Boolean values: − true : >= 1 false: 0nversionsnversionspesint atoi(char s[]) {int i, n=0;for (i=0;s[i]>= '0'oat rsfor (i0; s[i] 0 && s[i] <= '9'; i++) n = 10*n + (s[i]-'0');trsins return n;} e.g.:tctc.Pointers aPointers azPointer variable containzPointer variable containvariable− unary operator & appliedaddress of variable− unary operator * appliedvariable pointer points toz char c; p = &c; dd f i i d− address of c is assignedand Arraysand Arraysns the address of anotherns the address of another d to variables gives the d to pointer accesses the odt i bld to variable pPointers aPointers aint x=1, y=2, z[10];int *p; /* p points to an integerp = &x; /* Set p to x's addressy = *p; /* Get value of p, store*p = 0; /* Set p's value to 0 *p = &z[0]; /* Set p to z[0]'s addand Arraysand Arraysr */s */e in y */*/dress */Pointer EPointer E$ cat ptr example.cp_p#include <stdio.h>int main() {int x=1;int x=1;int *p; /* p points to an integer */p = &x; /* Set p to x's address */printf(" x is %d\n", x);*p = 0; /* Set p's value to 0 */printf("x now is %d\n"x);printf( x now is %d\n, x);return 0;}$ gcc ptr_example.c -o ptr_example$ ./ptr_example xis1x is 1x now is 0ExampleExampleUsing pointers to ReferencPthdd fzPass the address of a vzAlter the valuete t e a uevoid swap (int *px, int *py) {int temp;temp = *px;*px = *py;*py = temp;py p}int a=10,b=20;,;swap(&a,&b);achieve Call-By-ce effectiblvariablePointers aPointers aint a[10],*pa, x;int a[10], pa, x;pa = &a[0];x=*pa;x pa;x = *(pa+1);x = *(pa+5);pa = a; /*same as pa = &a[0];pa = a; / same as pa = &a[0];and Arraysand ArraysMain difference bepoinEthhbthtzEven though both contazArray name is not a varay a e s ot a az pa = a; pa++ Oz a = pa; a++; NWhen an array name iszWhen an array name isto a function: − Actually the address of −Arrays are always passArrays are always passetween arrays and ntersiddain addresses:riable; soab e; soOKNOT OKs passed as a parameters passed as a parameter the first element is passeded as pointersed as pointersFinding the lenFinding the lenint strlen(char *s) { /* One way */int n; f(0*!'\0')for (n=0; *s!='\0'; s++) n++;return n;return n; } int strlen(char s[]) { /* Another possibilityint n;;for (n=0; s[n]!='\0';n++);return n;}ngth of a Stringngth of a Stringy */StriStriAfhtzArray of charactersz Example: string copya p e st g copyvoid strcpy(char *s, char *t) {int i=0;int i0; while ((s[i]= t[i]) != '\0') i++; } /* OR: */while ((*s = *t) != '\0') {s++;t++;s; t;}ngsngsScopeScope A t ti /L l V i bzAutomatic/Local Variab− Declared at the beginnig− Scope is the function boE t l/Gl b l V i blzExternal/Global Variabl− Declared outside functio− Scope is from the point until end of file (unless pu e do e(u esspRulesRulesblblesng of functionsgodyllesonswhere they are declared prefixed by extern)p e ed by e e )ScopeScope St ti V i bl tzStatic Variables: use stand variable declaratio− static prefix on external the rest of the source filfiles) −static prefix on functionsstatic prefix on functionsother filesstatic prefix on internal v−static prefix on internal vpermanent private storafunction exitfunction exitRulesRulestti fi f titatic prefix on functions ns to limit scopevariables will limit scope to le (not accessible in other (s will make them invisible tos will make them invisible to variables will createvariables will create age; retained even uponScopeScope Vibl bdlzVariables can be decla− scope is until end of thepRulesRulesdithiblktred within blocks tooe blockBitwise OBitwise OA lidt h it hzApplied to char, int, sho− And & − Or | Exclusive Or ^−Exclusive Or ^ − Left-shift <<− Right-shift >> −one's complement~one s complement OperationsOperationstlort, longExample:Example: /* count the 1 bits in a numbere.g. bitcount(0x45) (01000101 bina*/int bitcount (unsigned int x) {int b;for (b=0; x != 0; x = x >> 1) if (x & 01) /* octal 1 = 0000000b++;b++;return b;}}Bit CountBit Countary) returns 3001


View Full Document

GSU CSC 3320 - chapterC

Download chapterC
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 chapterC 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 chapterC 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?