Quiz 11/15/16 – C functions, arrays and strings

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
 #include  Using a directive to include a header file  Stdio.h = standard input output header file.
Kernighan/Ritchie: Kelley/Pohl:
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
Chapter 10.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Old Material escape sequences operator precedence printf() scanf() if() switch case &&,|| while, do-while, ++, -- functions New Material 1-D arrays string.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
CECS 121 Midterm Review Presented by REACH (If you have not signed the attendance sheet, please do so now!)
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Lecture 3: Getting Started & Input / Output (I/O)
Computer Organization and Design Pointers, Arrays and Strings in C
Lesson #8 Structures Linked Lists Command Line Arguments.
User-Written Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Strings CSCI 112: Programming in C.
Computer Science 210 Computer Organization
UNIT 5 C Pointers.
Lesson #6 Modular Programming and Functions.
C Characters and Strings
Lesson #6 Modular Programming and Functions.
C Programming Tutorial – Part I
Getting Started with C.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
A First Book of ANSI C Fourth Edition
C Short Overview Lembit Jürimägi.
Pointers.
Module 2 Arrays and strings – example programs.
Programmazione I a.a. 2017/2018.
Arrays in C.
C-Programming, continued
Computer Science 210 Computer Organization
Lesson #6 Modular Programming and Functions.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Functions I Creating a programming with small logical units of code.
Week 9 – Lesson 1 Arrays – Character Strings
Functions, Part 1 of 3 Topics Using Predefined Functions
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Classes and Objects.
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Exercise Arrays.
Strings in C Array of characters is called a string.
Programming Languages and Paradigms
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
C Characters and Strings
Functions I Creating a programming with small logical units of code.
CPS125.
CECS 130 Mid-term Test Review
Getting Started With Coding
Introduction to Problem Solving and Programming
Presentation transcript:

Quiz 11/15/16 – C functions, arrays and strings

C Functions - What are they? In general, functions are blocks of code that perform a number of pre- defined commands to accomplish something productive. You can either use the built-in library functions or you can create your own functions.

Prototypes Functions that a programmer writes will generally require a prototype. Just like a blueprint, the prototype gives basic structural information: it tells the compiler what the function will return, what the function will be called, as well as what arguments the function can be passed. When I say that the function returns a value, I mean that the function can be used in the same manner as a variable would be. For example: #include <stdlib.h> /* Include rand() */ int a = rand(); /* rand is a standard function that all compilers have */

Prototype Format The general format for a prototype is simple: return-type function_name ( arg_type arg1, ..., arg_type argN ); arg_type just means the type for each argument -- for instance, an int, a float, or a char There can be more than one argument passed to a function or none at all (where the parentheses are empty), and it does not have to return a value. Functions that do not return values have a return type of void.

Let's look at a function prototype: int mult ( int x, int y ); This prototype specifies that the function mult will accept two arguments, both integers, and that it will return an integer. Do not forget the trailing semi-colon. Without it, the compiler will probably think that you are trying to write the actual definition of the function.

Let's look at an example program: #include <stdio.h> int mult ( int x, int y ); int main() { int x; int y; printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your numbers is %d\n", mult( x, y ) ); getchar(); }

The mult() function int mult (int x, int y) { return x * y; } Notice in the main routine how printf actually takes the value of what appears to be the mult function. What is really happening is printf is accepting the value returned by mult, not mult itself. The result would be the same as if we had used printf( "The product of your numbers is %d\n", x * y );

INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found in most high-level programming languages, such as C, and offer a simple way of grouping like variables for easy access. Arrays in C share a few common attributes: • Variables in an array share the same name • Variables in an array share the same data type • Individual variables in an array are called elements • Elements in an array are accessed with an index number

Creating One-Dimensional Arrays Arrays in C are created in similar fashion to other variables. int IArray[10]; float fAverages[30]; //Float data type array with 30 elements double dResults[3]; //Double data type array with 3 elements short sSalaries[9]; //Short data type array with 9 elements char cName[19]; //Char array - 18 character elements and one null character

Initializing One-Dimensional Arrays In C, memory spaces are not cleared from their previous value when variables or arrays are created. Because of this, it is generally good programming practice to not only initialize your variables but to also initialize your arrays. There are two ways to initialize your arrays: within the array declaration and outside of the array declaration. In the first way, you simply assign one or more comma-separated values within braces to the array in the array’s declaration. int iArray[5] = {0, 1, 2, 3, 4};

Initializing One-Dimensional Arrays int iArray[5] = {0}; int x; int iArray[5]; for ( x = 0; x < 5; x++ ) iArray[x] = 0;

Searching One-Dimensional Arrays for ( x = 0; x < 5; x++ ) { if ( iArray[x] == iValue ) { iFound = x; break; } } //end for loop

A string is an array of characters. Strings must have a 0 or null character after the last character to show where the string ends. The null character is not included in the string.

There are 2 ways of using strings There are 2 ways of using strings. The first is with a character array and the second is with a string pointer.

A character array is declared in the same way as a normal array. char ca[10]; You must set the value of each individual element of the array to the character you want and you must make the last character a 0. Remember to use %s when printing the string. char ca[10]; ca[0] = 'H'; ca[1] = 'e'; ca[2] = 'l'; ca[3] = 'l'; ca[4] = 'o'; ca[5] = 0; printf("%s",ca);

Initializing Strings char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’}; /*String.c string variable*/ #include < stdio.h > main() { char month[15]; printf (“Enter the string”); fgets (month,8,stdin); printf (“The string entered is %s”, month); }

String pointers are declared as a pointer to a char. char *sp; When you assign a value to the string pointer it will automatically put the 0 in for you unlike character arrays. char *sp; sp = "Hello"; printf("%s",sp);

You can read a string into only a character array using scanf and not a string pointer. If you want to read into a string pointer then you must make it point to a character array. char ca[10],*sp; scanf("%s",ca); sp = ca; scanf("%s",sp);

String handling functions The strings.h header file has some useful functions for working with strings. Here are some of the functions you will use most often:

strcpy(destination,source) You can't just use string1 = string2 in C strcpy(destination,source) You can't just use string1 = string2 in C. You have to use the strcpy function to copy one string to another. strcpy copies the source string to the destination string. s1 = "abc"; s2 = "xyz"; strcpy(s1,s2); // s1 = "xyz"

strcat(destination,source) Joins the destination and source strings and puts the joined string into the destination string. s1 = "abc"; s2 = "xyz"; strcat(s1,s2); // s1 = "abcxyz"

strcmp(first,second) Compares the first and second strings strcmp(first,second) Compares the first and second strings. If the first string is greater than the second one then a number higher than 0 is returned. If the first string is less than the second then a number lower than 0 is returned. If the strings are equal then 0 is returned. s1 = "abc"; s2 = "abc"; i = strcmp(s1,s2); // i = 0

strlen(string) Returns the amount of characters in a string. s = "abcde"; i = strlen(s); // i = 5

Pointers - For Example #include <stdio.h> int main() { int x; /* A normal integer*/ int *p; /* A pointer to an integer ("*p" is an integer, so p must be a pointer to an integer) */ p = &x; /* Read it, "assign the address of x to p" */ scanf( "%d", &x ); /* Put a value in x, we could also use p here */ printf( "%d\n", *p ); /* Note the use of the * to get the value */ getchar(); }