1 CSSE 332 Preprocessor, Array and Strings. 2 External library files libname.a or libname.so Special functionality is provided in the form of external.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
C Intro.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Structure of a C program
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Day 01 Introduction to C. Saving Dyknow notes Save on the server. Can access from anywhere. Will have to install Dyknow on that machine. Must connect.
Why learn C (after Java)? Both high-level and low-level language Better control of low-level mechanisms Performance better than Java Java hides many details.
Computer Systems DV1 (1DT151) Operating Systems (1DT020) Cary Laxer, Ph.D. Visiting Lecturer.
C-strings Array with base type char One character per indexed variable
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
C LANGUAGE Characteristics of C · Small size
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
C is a high level language (HLL)
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Day 01 Introduction to C.
Lecture2.
Computer Science 210 Computer Organization
Day 02 Introduction to C.
Functions Separate Compilation
Day 01 Introduction to Linux and C
C programming language
Computer Science 210 Computer Organization
Lexical Elements, Operators, and the C Cystem
Lexical Elements, Operators, and the C Cystem
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
C++ Programming Lecture 20 Strings
Programming Languages and Paradigms
Introduction to C Programming
Presentation transcript:

1 CSSE 332 Preprocessor, Array and Strings

2 External library files libname.a or libname.so Special functionality is provided in the form of external libraries of ready-made functions Special functionality is provided in the form of external libraries of ready-made functions Ready-compiled code that the compiler merges, or links, with a C program during compilation 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 For example, libraries of mathematical functions, string handling functions, and input/output functions Look for the library files under /usr/lib and header files under /usr/include Look for the library files under /usr/lib and header files under /usr/include

3 To compile, use flag “l” and name i.e. –lname. 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 files

4 Using external library files To use the library files, you must always do two things: To use the library files, you must always do two things: –link the library with a -l option to gcc –include the library header files

5 Pre-processor directives A preprocessor is a program that examines C code before it is compiled and manipulates it in various ways. A preprocessor is a program that examines C code before it is compiled and manipulates it in various ways. Two main directives: Two main directives: –To include external files using #include –To define macros (names that are expanded by the preprocessor into pieces of text or C code) using #define Other directives include conditional compilation, e.g. #ifdef Other directives include conditional compilation, e.g. #ifdef

6 #define #define EXPRESSION #define EXPRESSION2 EXPRESSION The expression is NOT evaluated when it replaces the macro in the pre-processing stage. The expression is NOT evaluated when it replaces the macro in the pre-processing stage. Evaluation takes place only during the execution phase. Evaluation takes place only during the execution phase. EXPRESSION2 will be replaced everywhere by “ ” NOT by “20” EXPRESSION2 will be replaced everywhere by “ ” NOT by “20”

7 Example of pre-processor directives Example 2: Use wget Comment out the first line Type gcc -E example2.c #include #define STRING1 "A macro definition\n" #define STRING2 "must be all on one line!\n" #define EXPRESSION #define EXPRESSION2 EXPRESSION #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 %i2short %l4long %lf8double %f4float %c1char %d %i4int Short-hand#bytes (typical) Data-type String - %s address - %p(HEX) or %u (unsigned int) Simple Data Types

9 #include int main(){ int nstudents = 0; /* Initialization, required */ float age = ; 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.precision return 0; } >>> How many students does RHIT have ?: 2000 (enter) RHIT has 2000 students. The average age of the students is 21.5 >>> Example 3

10 Like 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;

11 Example 4 #include #define DANGERLEVEL 5 /* C Preprocessor – - substitution on appearance - like Java ‘final’ (in some respects) */ 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; }

12 One-Dimensional Arrays Example 5: #include int main() { int number[12]; /* 12 numbers*/ int index, sum = 0; /* Always initialize 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; }

13 More arrays - Strings char name[10]; //declaration name = {‘A’,’l’,’i’,’c’,’e’,’\0’}; //initialization /* ’\0’= end of string */ char name [] = “Alice”; //declaration and initialization char name [] = {‘A’,’l’,’i’,’c’,’e’,’\0’}; // ditto scanf(“%s”,name); //Initialization // ERROR: scanf(“%s”,&name); // ERROR: scanf(“%s”,&name); printf(“%s”, name); /* print until ‘\0’ */

14 Strings contd. #include or #include or strcpy(), strcmp(), strcat() strcpy(), strcmp(), strcat() C does not do array bounds checkingC does not do array bounds checking Characters stored beyond the end of the allocated space will overwrite other data (or code!)Characters stored beyond the end of the allocated space will overwrite other data (or code!) strncpy(), strncmp(), strncat() strncpy(), strncmp(), strncat() substr(), strlen(), strtok() substr(), strlen(), strtok() See the GNU C Library Manual See the GNU C Library Manual

Homework 2 Simple exercise to get you started on C. Simple exercise to get you started on C. Available on class’s Angel page Available on class’s Angel page