C Language Main - W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the structure of A format specifier

%[-][width].[precision]conversion character

What are 2 types Basic Types C can be classified into?

1. Integer types 2. Floating-point types

What blocks threads until a particular condition is true

Condition Variables

What is the output: #include <stdio.h> int main( ) { char name[20]; printf("Enter name: "); scanf("%s", name); printf("Your name is %s.", name); return 0; }

Enter name: Dennis Ritchie Your name is Dennis. (Explanation: Even though Dennis Ritchie was entered in the above program, only "Dennis" was stored in the "name"string. It's because there was a space after Dennis. Also notice that we have used the code name instead of &name with scanf( ).)

What is recommended to put alot of in your C program to make it more readable?

Extra spacing

True or False: C does not have a boolean type.

True

What on Linux, programs that use the Pthreads API should be compiled using

cc -pthread.

What is specifically meant for storing character data. , but any integer type can be used?

char

What are integer Basic Types in C?

char unsigned char signed char int unsigned int short unsigned short long unsigned long (Explanation: Data types in c refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.)

What argument determines whether updates to the mapping are visible to other processes mapping the same region, and whether updates are carried through to the underlying file?

flags MAP_SHARED MAP_SHARED_VALIDATE MAP_PRIVATE

What function reads nmemb items of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr. (DESCRIPTION)

fread( )

What function does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred?

fread()

In what function in the POSIX locale, or in a locale where the radix character is notidefined, the radix character shall default to a <period> ('.').

fscanf( )

What function in all its forms shall allow detection of a language-dependent radix character in the input string. The radix character is defined in the current locale (category LC_NUMERIC).?

fscanf( )

What function shall read from the named input stream.?

fscanf( )

What function shall read from the string s.

sscanf ()

What header file is needed to use the the printf function?

stdio.h (Explanation: #include <stdio.h> is used a the start if a C function. A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler. You request to use a header file in your program by including it with the C preprocessing directive #include,z0)

What function is like strstr(), but ignores the case of both arguments.

strcasestr()

What Synopsis is this for: #include <string.h> char *strstr(const char *haystack, const char *needle); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <string.h> char *strcasestr(const char *haystack, const char *needle);

strstr()

What function finds the first occurrence of the substring needle in the string haystack.

strstr() (Explanation: The strstr() function finds the first occurrence of the substring needle in the string haystack. The terminating null bytes ('\0') are not compared)

What is the function call for a thread

void pthread_exit(void *NULL);

What C program prints: "Hello World"

# include <stdio.h> main () { >>>> printf ("hello, world \n"); } (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

MMAP(2) Synopsis

#include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); int munmap(void *addr, size_t length);

Fill in the Blank: If you have a variable var in your program, -----Blank---- will give you its address in the memory.

&var

What does &var here do here? scanf("%d" , &var);

&var will give you its address in the memory (Explanation: You must put & in front of the variable used in scanf).

What arithmetic operators does C support?

+ (addition), - (subtraction), * (multiplication), / (division) % (modulus division)

What are the 3 types in C?

1. Basic Types / Primitive Types (char, int, float/double, void) 2. User defined type - (array, pointer, structures, unions, enumeration types) 3. Derived types (Explanation: Data types in c refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.)

What 2 things do programs consist of?

1. Functions 2. Variables (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

What is used to read a line of text?

1. fgets ( ) 2. puts ( ) (Explanation: You can use the fgets( ) function to read a line of string. And, you can use puts() to display the string.)

What are 3 standard floating point types?

1. float 2. double 3. long double

What are the 2 variations of fread?

1. fread 2. fwrite

binary stream input/output

1. fread 2. fwrite

What basic data types does C language support?

1. int (integer, a whole number) 2. float (floating point, a number with a fractional part) 3. double (double-precision floating point value) 4. char (single character)

Fill in the Blank: After the Blank__________ call has returned, the file descriptor, Blank___________ can be closed immediately without invalidating the mapping.

1. mmap() 2. fd

Fill in the blanks to output "Hi, everyone!" to the screen: __________("Hi, everyone!")_________

1. printf 2. ;

What function each expects, as arguments, a control string format described below, and a set of pointer arguments indicating where the converted input should be stored?

1. scanf( ) 2. sscanf ( ) 3. fscanf ( )

What function reads bytes, interprets them according to a format, and stores the results in its arguments?

1. scanf( ) 2. sscanf( ) 3. fscanf( )

What function locates a substring?

1. strstr 2. strcasestr

What functions returns a pointer to the beginning of the located substring, or NULL if the substring is not found?

1. strstr 2. strcasestr (Explanation: strstr and strcasestr functions return a pointer to the beginning of the located substring, or NULL if the substring is not found. If the needle is the empty string, the return value is always haystack itself.)

What are the addresses assigned to pointers in this example? int* pc, c; c = 5; pc = &c;

5 is assigned to the c variable the address of c is assigned to the pc pointer. (C Pointers)

What announces the properties of variables and may consist of a type name and a list of variables, such as: int fahr, celsius; int lower, upper, step;

A Declaration (Explanation: In C, all variables must be declared before they are used, usually at the beginning of the function before any executable statements. A declaration announces the properties of variables; it consists of a type name and a list of variables) ex. int fahr, celsius; int lower, upper, step; (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

What characters are ignored by the compiler?

Any characters between /* and */ (Explanation: The two lines /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ are a comment, which in this case explains briefly what the program does. Any characters between /* and */ are ignored by the compiler; they may be used freely to make a program easier to understand. Comments may appear anywhere a blank or tab or newline can.) (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

What is a list of values that functions call on to exchange data with other functions?

Arguments (Explanation: arguments are a list of values that functions call on for exchanging data with other functions. The paranthesis after the function name surround the argument list) ex. printf("hello,world\n"); (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

Where do you place the return data type?

Before a function name that returns a value

Does fread( ) or fwrite( ) return the number of items read or written? This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero). (RETURN VALUE)

Both fread( ) and fwrite( )

What are statements or arguments of a function enclosed in?

Braces {} (Explanation: The statements/arguments of a function are enclosed in braces. A Function is called by naming it followed by a parathenisized list of arguments) ex. main () { >>>> printf ("hello, world \n"); } printf = function "hello,world\n" = argument (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

What Basic Type is a sequence of characters in double quotes? ex. "hello,world\n"

Character Strings or Constants (Explanation: Character Strings or Constants are a sequence of characters in double quotes) (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

In the calling function I do what?

Do something with the return value. Print it or assign it to something (Explanation: Calling a function is useless if you do nothing with the return value

What contains statements that specify the computing operations to be done?

Functions (Explanation: A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. A function definition has this form: return-type function-name(parameter declarations, if any) { declarations statements } (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

Sharing what mapping. Updates to the mapping are visible to other processes mapping the same region, and (in the case of file-backed mappings) are carried through to the underlying file. (To precisely control when updates are carried through to the underlying file requires the use of msync(2).)

MAP_SHARED

What flag provides the same behavior as MAP_SHARED except that MAP_SHARED mappings ignore unknown flags in flags. By contrast, when creating mapping using MAP_SHARED_VALIDATE, the kernel verifies all passed flags are known and fails the mapping with the error EOPNOTSUPP for unknown flags. This mapping type is also required to be able to use some mapping flags (e.g., MAP_SYNC).

MAP_SHARED_VALIDATE

SYNOPSIS #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); int munmap(void *addr, size_t length);

MMAP(2) (Variations: mmap, munmap - map or unmap)

What function files or devices into memory

MMAP(2) (Variations: mmap, munmap - map or unmap)

What special function begins executing at main?

Main (Explanation: Main is considered a function, this function is special because it begins executing at main. Main calls other functions to help do its job) ex. # include <stdio.h> (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

What allows only one threads a time to execute a specific section of code or to access specific data

Mutex Locks

( Example 2: fgets( ) and puts( ) ) #include <stdio.h> int main( ) { char name[30]; printf("Enter name: "); fgets(name, sizeof(name), stdin); // read string printf("Name: "); puts(name); // display string return 0; }

Output: Enter name: Tom Hanks Name: Tom Hanks (Explanation: Here, we have used fgets( ) function to read a string from the user. fgets(name), sizeof(name), stdlin); // read string The sizeof(name) results to 30. Hence, we can take a maximum of 30 characters as input which is the size of the name string. To print the string, we have used puts(name);. Note: The gets( ) function can also be to take input from the user. However, it is removed from the C standard. It's because gets( ) allows you to input any length of characters. Hence, there might be a buffer overflow.)

#include <stdio.h> int main( ) { int var = 5; printf("var: %d\n", var); // Notice the use of & before var printf("address of var: %p", &var); return 0; } (Here, the value entered by the user is stored in the address of var variable. Let's take a working example.)

Output: var: 5 address of var: 2686778

What syntax is this example: int* p;

Pointer Syntax (Explanation: Here is how we can declare pointers. Here, we have declared a pointer p of int type.) (C Pointers)

What are special variables that are used to store addresses rather than values?

Pointers (pointer variables) (C Pointers)

What gets the address of something

Putting a & in front of something

What Sequence in a string is C language notation for the new line character?

The Sequence \n (Explanation: The sequence \n is C notation for the newline character \n puts the output on the left margin of the next line. \n is also called an escape sequence) ex. printf("hello,world\n"); (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

What functions behave the same as atoi(), except that they convert the initial portion of the string to their return type of long or long long.

The atol() and atoll()

What does the following call do:

The following call uses fscanf() to read three floating-point numbers from standard input into the input array. (Reading Data into an Array

What does a controlled counted loop through a (hopefully) fixed number of iterations using a counter:

The for-loop (Explanation: For Loop Syntax: for(INIT; TEST; POST) { CODE; })

What indicates the end of the statement?

The semicolon ; #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }

What function is like strstr(), but ignores the case of both arguments?

The strcasestr()

Predict the Output: #include <stdio.h> int main() { printf("The tree has %d apples.\n", 22);

The tree has 22 apples.

What is the RETURN VALUE or converted value for ATOI(3) (atoi, atol, atoll)

They convert a string to an integer, but does not detect errors

What is strcasestr() attribute?

Thread safety

What is strstr() attribute?

Thread safety

What is used as the return data type or in the parameter list if you neither return nor pass values to a function.

Void

What is another name for an escape sequence?

\n (Explanation: The sequence \n is C notation for the newline character \n puts the output on the left margin of the next line. \n is also called an escape sequence) ex. printf("hello,world\n"); (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

What value appears after a return statement?

a return value

C is a: a. General-purpose programming language b. photo editing program c. Client-side scripting language

a. General-purpose programming language

(fscanf() example) The call: int i; float x; char name[50]; (void) scanf("%2d%f%*d %[0123456789]", &i, &x, name); with input: 56789 0123 56a72

assigns 56 to i, 789.0 to x, skips 0123, and places the string "56\0" in name. The next call to getchar() shall return the character 'a'.

(fscanf() example) The call: int i, n; float x; char name[50]; n = scanf("%d%f%s", &i, &x, name); with the input line: 25 54.32E-1 Hamster

assigns to n the value 3, to i the value 25, to x the value 5.432, and name contains the string "Hamster".

What convert a string to a double

atof

What function converts the initial portion of the string pointed to by nptr to double. The behavior is the same as strtod(nptr, NULL); except it does not detect errors.

atof()

What unction converts the initial portion of the string pointed to by nptr to int. The behavior is the same as strtol(nptr, NULL, 10); except that this function does not detect errors.

atoi()

What functions shall execute each directive of the format in turn? If a directive fails, as detailed below, the function shall return. Failures are described as input failures (due to the unavailability of input bytes) or matching failures (due to inappropriate input).

fscanf()

What function writes nmemb items of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr. (DESCRIPTION)

fwrite( )

With what function if addr is NULL, then the kernel chooses the (page-aligned) address at which to create the mapping; this is the most portable method of creating a new mapping. (DESCRIPTION)

mmap ()

What creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in addr. The length argument specifies the length of the mapping (which must be greater than 0).

mmap() (Explanation: The starting address for the new mapping is specified in addr. The length argument specifies the length of the mapping (which must be greater than 0).

What is a library function that prints output?

printf (Explanation: printf is a library function that prints output in the example below the output is the string of characters between the quotes) ex. printf("hello,world\n"); printf = function "hello,world\n" = argument (W., Kernighan Brian; Ritchie Dennis. C Programming Language.)

What argument describes the desired memory protection of the mapping (and must not conflict with the open mode of the file). It is either PROT_NONE or the bitwise OR of one or more of the following flags:

prot PROT_EXEC (Pages may be executed) PROT_READ (Pages may be read) PROT_WRITE (Pages may be written) PROT_NONE (Pages may not be accessed)

What subroutine blocks the calling thread until the specified thread terminates

pthread_join

What statement terminates the main( ) function and returns the value 0 to the calling process?

return 0 #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }

What function shall read from the standard input stream stdin?

scanf ( )

What statement waits for input and then makes assignments?

scanf( )

A call to what function scans input according to format specifiers that convert input as necessary?

scanf()

What function is used to assign input to variables?

scanf()

What is to be inserted if the function does not return or pass a value ?

void (Explanation: If your function doesn't return a value, or if your function isn't passed a value, you can insert the keyword void for either the return data type or the parameter list or both. Therefore, the first line of a function that neither gets any value nor returns any value might look like this: void doSomething(void) /* Neither is passed nor returns */)


Set pelajaran terkait

NSG 4520 EXAM 1: Metabolism & Homeostasis ATI Quiz

View Set

Peds Chapter 27: The Child with a Condition of the Blood, Blood-Forming Organs, or Lymphatic System

View Set

Personal Finance unit 2 vocab and fill in the blank

View Set

chapter 4 business principals exam

View Set

Straighterline Intro to Nutri Chap 7

View Set

Audio Week 3 (Patchbay, cables, connectors)

View Set

Week 3, Lecture 3 - Allery and Type I hypersensitivity: Clnical consequences

View Set