Intro To C Part 1

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Medium: Define an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume the symbolic constant SIZE has been defined as 5.

unsigned int values[SIZE]={2,4,6,8,10};

Challanging: Assume We have defined three functions—function1, function2 and function3—that each take an integer argument and return nothing. Define an array of pointers to these three functions called "f".

void (*f[ 3 ])( int ) = { function1, function2, function3 };

Medium: Write the function header for a function called exchange that takes two pointers to floating-point numbers x and y as parameters and does not return a value.

void exchange (float * x, float * y)

Medium: if s1="12345" and s2=" 98765", then what is the output of the following printf function? printf("%s", strncat(s2,strncat(s2, s1,5),1));

3211233

Medium: How many tokens the following code will create? #include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.ucf.ed - website"; const char s[4] = ".-"; char *token; token = strtok(str, s); while( token != NULL) { printf( " %s\n", token ); token = strtok(NULL, s); } return(0); }

5

Medium: Assume int a; char b; char c[10]; char d[20]; float f; What will be the output of the following code if the user enters 12345 67890 as input? scanf("%c%s%2d%s%s", &b,c,a,d,c); printf("%s%c%d",c,b,a);

67890

Easy: Match the followings: double atof( const char *nPtr );

Converts the string nPtr to double.

Easy: Match the followings: int atoi( const char *nPtr );

Converts the string nPtr to int.

Easy: Match the followings: long atol( const char *nPtr );

Converts the string nPtr to int.

Easy: Match the followings: long strtol( const char *nPtr, char **endPtr, int base );

Converts the string nPtr to long.

size_t strcspn( const char *s1, const char *s2 );

Determines and returns the length of the initial segment of string s1 consisting of characters not contained in string s2.

Medium:The output of printf("%05d\n%+07d\n%07.1f", 1234,-1234,12.34); is:

False

Easy: Assume that floating-point variables number1 and number2 are defined and that number1 is initialized to 7.3. Assign the address of variable number1 to pointer variable fPtr.

flaot *fPtr; fptr=&number1;

Medium: What is the output of the following Code snippet. printf( "%s\n%s%s%s\n%s%s%s\n%s%s\n\n", "According to isspace:", "Newline", ? " is a " : " is not a ", "whitespace character", "Horizontal tab", ? " is a " : " is not a ", "whitespace character", ? "% is a " : "% is not a ", "whitespace character" );

According to isspace: Newline is a whitespace character Horizontal tab is a whitespace character % is not a whitespace character

size_t strspn( const char *s1, const char *s2 );

Determines and returns the length of the initial segment of string s1 consisting only of characters contained in string s2.

Easy: Function strcat appends its first string argument—including the terminating null character—to its second string argument.

False

Easy: Match the followings: char *fgets( char *s, int n, FILE *stream);

Inputs characters from the specified stream into the array s until a newline or end-of-file character is encountered, or until n - 1 bytes are read. In this chapter, we specify the stream as stdin—the standard input stream, which is typically used to read characters from the keyboard. A terminating null character is appended to the array. Returns the string that was read into s.

Easy: Match the followings: int getchar( void )

Inputs the next character from the standard input and returns it as an integer.

Medium: Describe the following function. void mystery( const char * const sPtr ) { if ( sPtr[ 0 ] == '\0' ) { return; } else { mystery (&sPtr[1]); putchar(sPtr[0]); } }

It reverses string sPtr.

Medium: What, if anything, prints when the following C statement is performed? Assume the following variable definitions: char s1[ 50 ] = "jack", s2[ 50 ] = " jill", s3[ 50 ], *sptr; printf( "%c%s", toupper( s1[ 0 ] ), &s1[ 2 ] );

Jack

char *strchr( const char *s, int c );

Locates the first occurrence of character c in string s. If c is found, a pointer to c in s is returned. Otherwise, a NULL pointer is returned.

char *strrchr( const char *s, int c );

Locates the last occurrence of c in string s. If c is found, a pointer to c in string s is returned. Otherwise, a NULL pointer is returned.

Easy: Match the followings: int putchar( int c );

Prints the character stored in c and returns it as an integer.

Easy: Match the followings: int puts( const char *s );

Prints the string s followed by a newline character. Returns a non-zero integer if successful, or EOF if an error occurs.

Easy: Match the followings: int isalpha( int c );

Returns a true value if c is a letter and 0 otherwise.

Easy: Match the followings: int isprint( int c );

Returns a true value if c is a printing character including a space ('') and returns 0 otherwise.

int isprint( int c );

Returns a true value if c is a printing character including a space ('') and returns 0 otherwise.

Easy: Match the followings: int isgraph( int c );

Returns a true value if c is a printing character other than a space ('') and returns 0 otherwise.

Easy: Match the followings: int ispunct( int c );

Returns a true value if c is a printing character other than a space, a digit, or a letter and returns 0 otherwise.

Easy: Match the followings: int ispunct( int c );

Returns a true value if c is a printing character other than a space, a digit, or a letter and returns 0 otherwise.

Challenging: Medium: if dest="12345" and src="67890", the value of dest after the following code will be 67145. memmove(memmove(dest, src, 2), memset(dest, '1', 3), 2);

True

Easy: fgets( sentence, 80, stdin ); Reads 80 characters of a line of text from the standard input stream .

True

Easy: A function receiving an address as an argument must define a pointer parameter to receive the address.

True

Easy: A limited set of arithmetic operations may be performed on pointers. A pointer may be incremented (++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=) and one pointer may be subtracted from another.

True

Easy: A parameter of type const char * typically represents a constant string.

True

Easy: A pointer can be assigned to another pointer if both have the same type. An exception to this is the pointer of type void * which can represent any pointer type. All pointer types can be assigned a void * pointer, and a void * pointer can be assigned a pointer of any type.

True

Easy: A pointer contains an address of another variable that contains a value. In this sense, a variable name directly references a value, and a pointer indirectly references a value.

True

Easy: A pointer to a function contains the address of the function in memory. A function name is really the starting address in memory of the code that performs the function's task. •

True

Easy: A void * pointer cannot be dereferenced.

True

Easy: An array name can be treated as a pointer and used in pointer arithmetic expressions that do not attempt to modify the address of the pointer.

True

Easy: Arrays may contain pointers. A common use of an array of pointers is to form an array of strings. Each entry in the array is a string, but in C a string is essentially a pointer to its first character. So each entry in an array of strings is actually a pointer to the first character of a string.

True

Easy: Floating-point values are printed with the following conversion specifiers: e or E for exponential notation, f for regular floating-point notation, and g or G for either e (or E) notation or f notation. When the g (or G) conversion specifier is indicated, the e (or E) conversion specifier is used if the value's exponent is less than -4 or greater than or equal to the precision with which the value is printed.

True

Easy: Function memchr searches for the first occurrence of a byte, represented as unsigned char, in the specified number of bytes of an object. If the byte is found, a pointer to the byte is returned; otherwise, a NULL pointer is returned.

True

Easy: Function strncpy is equivalent to strcpy, except that a call to strncpy specifies the number of characters to be copied from the string into the array. The terminating null character will be copied only if the number of characters to be copied is one more than the length of the string.

True

Easy: If the field width is larger than the object being printed, the object is right justified by default.

True

Easy: Integers are printed with the following conversion specifiers: d or i for optionally signed integers, o for unsigned integers in octal form, u for unsigned integers in decimal form and x or X for unsigned integers in hexadecimal form. The modifier h or l is prefixed to the preceding conversion specifiers to indicate a short or long integer, respectively.

True

Easy: NULL is a symbolic constant defined in the <stddef.h> header (and several other headers).

True

Easy: Pointers can be compared using equality and relational operators

True

Easy: Pointers can be defined to point to objects of any type.

True

Easy: Referencing a value through a pointer is called indirection.

True

Easy: The &, or address operator, is a unary operator that returns the address of its operand and The operand of the address operator must be a variable.

True

Easy: The compiler does not differentiate between a function that receives a pointer and a function that receives a single-subscripted array. Correct!

True

Easy: The const qualifier indicates that the value of a particular variable should not be modified. If an attempt is made to modify a value that is declared const, the compiler catches it and issues either a warning or an error, depending on the particular compiler.

True

Easy: The indirection operator * returns the value of the object to which its operand points.

True

Easy: The output of printf( "%+09d\n", 452 ); is +00000452 .

True

Easy: The output of printf( "%+d\n%+d\n", 786, -786); is +786 -786

True

Easy: Two pointers to elements of the same array may be subtracted from one another to determine the number of elements between them.

True

Easy: Unary operator sizeof determine the size in bytes of a variable or type at compilation time. When applied to the name of an array, sizeof returns the total number of bytes in the array. Operator sizeof can be applied to any variable name, type or value.

True

Easy: When the address of a variable is passed to a function, the indirection operator (*) may be used in the function to modify the value at that location in the caller's memory. Correct!

True

Easy: When the compiler encounters a function parameter for a single-subscripted array of the form int b[], the compiler converts the parameter to the pointer notation int *b.

True

Challenging: What is the output of the following code? #include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.ucf.ed - website"; const char s[4] = "."; char *token; token = strtok(str, s); while( token != NULL) { printf( " %s\n", token ); token = strtok(str, s); } return(0); }

an infinte loop

Easy: copies string s2 into array s1. The value of s1 is returned.

char *strcpy( char *s1, const char *s2 )

Medium: Declare s for the following function. sprintf( s, "integer:%6d\ndouble:%8.2f", x, y );

char s[ 80 ];

Medium: Define of string array of 4 elements "suit", which might be useful in representing a deck of cards.

const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

Medium: if dest="oldstring" and src="newstring", what will be the the values of dest and src after the following code: memmove(dest, memset(dest, 'a', 2), 1);

dest = aadstring, src = newstring

Medium: if dest="oldstring" and src="newstring", what will be the the values of dest and src after the following code: memmove(dest, memmove(dest, src, 1), 1);

dest = nldstring, src = newstring

Easy: Define the variable fPtr to be a pointer to an object of type float.

float* fPtr; float * fPtr; float *fPtr;

Medium: Assume an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Print the elements of array values using pointer/offset notation.

for(int i=0; i<=4; i++) printf("%d ", * (values+i));

Challanging: Write the function header for a function called evaluate that returns an integer and that takes as parameters integer x and a pointer to function poly. Function poly takes an integer parameter and returns an integer.

int evaluate( int x, int (*poly)( int ) )

Easy: Match the followings: int isalnum( int c );

returns a true value if c is a digit or a letter and 0 otherwise.

Easy: To print a space before a positive value not printed with the + flag we can use the following flag:

space


Kaugnay na mga set ng pag-aaral

Cranial Nerve V: Trigeminal Nerve Branches

View Set

Insurance Regulations - Life Insurance

View Set

2) Alzheimers: Amyloid plaque formation

View Set

Chapter 3: Biology and Behavior Psych 2400

View Set

Ch 41 Upper GI Problems Questions

View Set