Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Quiz 11/15/16 – C functions, arrays and strings"— Presentation transcript:

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

2 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.

3 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 */

4 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.

5 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.

6 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(); }

7 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 );

8 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

9 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

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

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

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

13 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.

14 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.

15 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);

16 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); }

17 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);

18 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);

19 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:

20 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"

21 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"

22 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

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

24 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(); }


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

Similar presentations


Ads by Google