DOC PREVIEW
Rose-Hulman CSSE 332 - Preprocessor, Array and Strings

This preview shows page 1-2-3-4-5 out of 16 pages.

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

Unformatted text preview:

CSSE 332Preprocessor, Array and Strings2Checkout your SVN repository Your SVN repository is located athttp://svn.cs.rose-hulman.edu/repos/csse332-200920-usernamewhere username is your Rose-Hulman username Change directory to the csse332 directory you created during session 1cd csse332 Checkout the Examples foldersvn co http://svn.cs.rose-hulman.edu/repos/csse332-200920-username/Examples  Change directory to Examples and examine the listing of the files in that directorycd Examplesls –l3External library fileslibname.a or libname.so  Special functionality is provided in the form of external librariesof ready-made functions  Ready-compiled code that the compiler merges, or links, with a C program during compilation  For example, libraries of mathematical functions, string handling functions, and input/output functions  Look for the library files under /usr/liband header files under /usr/include4 To compile, use flag “l” and name i.e. –lname.Eg. gcc –o test test.c –lm where “m” in “lm” comes from libm.so i.e. The math library.Compiling with library files5Using external library files To use the library files, you must always do two things: – link the library with a -l option to gcc– include the library header files6Pre-processor directives A preprocessor is a program that examines C code before it is compiled and manipulates it in various ways.  Two main functions:– To include external files using #include– To define macros (names that are expanded by the preprocessor into pieces of text or C code) using #define7Example of pre-processor directivesExample 2:#include <stdio.h> #define STRING1 "A macro definition\n" #define STRING2 "must be all on one line!\n" #define EXPRESSION1 1 + 2 + 3 + 4 #define EXPRESSION2 EXPRESSION1 + 10 #define ABS(x) ((x) < 0) ? -(x) : (x) #define MAX(a,b) (a < b) ? (b) : (a) #define BIGGEST(a,b,c) (MAX(a,b) < c) ? (c) : (MAX(a,b)) int main (){ printf (STRING1); printf (STRING2); printf ("%d\n", EXPRESSION1); printf ("%d\n", EXPRESSION2); printf ("%d\n", ABS(-5));printf ("Biggest of 1, 2, and 3 is %d\n", BIGGEST(1,2,3)); return 0;}8#define The expression is NOT evaluated when it replaces the macro in the pre-processing stage. Evaluation takes place only during the execution phase.9%i2short%l4long%lf8double%f4float%c1char%d %i4intConversion codes#bytes (typical)Data-typeString - %s address - %p(HEX) or %u (unsigned int)Simple Data Types10#include <stdio.h>int main(){int nstudents = 0; /* Initialization, required */float age = 21.527;printf(“How many students does RHIT have ?”);scanf (“%d”, &nstudents); /* Read input */printf(“RHIT has %d students.\n”, nstudents); printf(“The average age of the students is %3.1f\n”,age);//3.1 => width.precisionreturn 0;}>>> How many students does RHIT have ?:2000 (enter)RHIT has 2000 students.The average age of the students is 21.5>>>Example 311Like Java• Operators same as Java: • Arithmetic• int i = i+1; i++; i--; i *= 2;• +, -, *, /, %,• Relational and Logical• <, >, <=, >=, ==, !=• &&, ||, &, |, !• Syntax same as in Java:• if ( ) { } else { }• while ( ) { }• do { } while ( );• for(i=1; i <= 100; i++) { }• switch ( ) {case 1: … }• continue; break;12Example 4#include <stdio.h>#define DANGERLEVEL 5 /* C Preprocessor –- substitution on appearance - like Java „final‟ */int main() {float level=1; /* if-then-else as in Java */if (level <= DANGERLEVEL){ /*replaced by 5*/printf(“Low on gas!\n”);}else {printf(“On my way !\n”);}return 0;}13One-Dimensional ArraysExample 5:#include <stdio.h>int main() {int number[12]; /* 12 numbers*/int index, sum = 0;/* Alwaysinitialize array before use */for (index = 0; index < 12; index++) {number[index] = index;}/* now, number[index]=index; will cause error:why ?*/for (index = 0; index < 12; index = index + 1) {sum += number[index]; /* sum array elements */}return 0;}14More arrays - Stringschar name[10]; //declarationname = {„A‟,‟l‟,‟i‟,‟c‟,‟e‟,‟\0‟}; //initialization /* ‟\0‟= end of string */char name [] = “Alice”; //declaration and initializationchar name [] = {„A‟,‟l‟,‟i‟,‟c‟,‟e‟,‟\0‟}; // dittoscanf(“%s”,name); //Initialization// ERROR: scanf(“%s”,&name);printf(“%s”, name); /* print until „\0‟ */15Strings contd. Functions to operate on strings• strcpy, strncpy, strcmp, strncmp, strcat, strncat, substr, strlen,strtok• #include <strings.h> or <string.h> at program start CAUTION: C allows strings of any length to be stored. Characters beyond the end of the array will overwrite data in memory following the array.Homework 2 Simple exercise to get you started on C. Available on class‟s Angel page– follow link from the schedule


View Full Document

Rose-Hulman CSSE 332 - Preprocessor, Array and Strings

Download Preprocessor, Array and Strings
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 Preprocessor, Array and Strings 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 Preprocessor, Array and Strings 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?