Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Chapter 3

1. What are the trigraph characters? How are they useful?

Answer: Trigraphs are sequences of three characters that start with two consecutive question
marks (??) and are used in programming languages like C and C++. They represent a single
character. For example, the trigraph ??/ is replaced by the character \\.

The main purpose of trigraphs is to allow C programs to be written using only the ISO
(International Standards Organization) Invariant Code Set. This is particularly useful when the
character set being used does not support all the characters of the C syntax4. For instance, some
keyboards may not have keys to cover the entire character set of the language, input of special
characters may be difficult, or text editors may reserve some characters for special use.

Here are some examples of trigraphs in C and their replacements:

 ??= is replaced by #
 ??/ is replaced by \\
 ??' is replaced by ^
 ??(is replaced by [
 ??) is replaced by]
 ??! is replaced by |
 ??< is replaced by {
 ??> is replaced by}
 ??- is replaced by ~

It’s worth noting that trigraphs are not commonly encountered outside compiler test suites5, and
some compilers support an option to turn recognition of trigraphs off, or disable trigraphs by
default and require an option to turn them on5. For example, in gcc, you can enable trigraphs by
compiling with -trigraphs.

2. Describe the four basic data types. How could we extend the range of values they
represent?

Answer: The four basic data types in programming are:

1. Integer: This data type represents whole numbers, both positive and negative. It typically
has a range of values from -2,147,483,648 to 2,147,483,647.
2. Floating Point (Real): This data type represents fractional numbers. It can store numbers
with decimal points, such as -87.5, 0.0, or 3.14159.
3. String: This data type represents a sequence of characters. It can store text, such as
"Hello world!".
4. Boolean: This data type represents logical true or false1. It is used to make decisions in
programming.
To extend the range of these data types, you can use larger data types or modifiers. For example:

 For integers, you can use a long int or long long int in C/C++, which uses more bytes of
memory and thus can represent a larger range of values4.
 For floating-point numbers, you can use a double or long double in C/C++, which
provides more precision and a larger range of values.

Please note that the exact range and behavior of these data types can vary between different
programming languages and systems.

3. What is an unsigned integer constant? What is the significance of declaring a constant


unsigned?

Answer: An unsigned integer constant is an integer constant that can only represent positive
values or zero1. It is identified by appending the letter ‘u’ or ‘U’ to the end of the constant2. The
range of an unsigned integer constant is from 0 to 65536.

The significance of declaring a constant as unsigned is that it almost doubles the size of the
largest possible value that can be represented1. This happens because when a constant is declared
as unsigned, the highest bit, which is typically used to store the sign of the constant in a signed
integer, is now free and not used to store the sign of the constant1. Instead, it is used to store data,
effectively increasing the range of positive values that can be represented.

For example, in a system with a 16-bit integer, a signed integer has the range -32768 to 32767.
But an unsigned integer in the same system has the range 0 to 655353. This can be particularly
useful when you know that a variable should never hold a negative value.

However, it’s important to note that declaring a variable as unsigned doesn’t prevent errors from
assigning negative values to it. The negative value gets implicitly converted to an unsigned form
according to the rules of unsigned arithmetic. Some compilers might issue warnings in such
cases, while others will do it quietly.

4. When dealing with very small or very large numbers, what steps would you take to
improve the accuracy of the calculation?

Answer: When dealing with very small or very large numbers, there are several steps you can
take to improve the accuracy of the calculation:

1. Use Appropriate Data Types: Ensure that the data types of your variables are suitable
for the range of values you are dealing with1. For example, in some programming
languages, using a double instead of a float can provide more precision for very small or
very large numbers1.
2. Rescale Your Problem: If you’re dealing with very large or very small numbers,
consider rescaling your problem2. This can help avoid issues with numerical instability2.
3. Use Scientific Notation: Scientific notation can help ensure accuracy and reduce the
possibility of error when using very small or very large numbers3. It makes it easier to
interpret larger numbers, especially for individuals who don’t have much experience
working with such large or small numbers3.
4. Change the Order of Computations: If your very small numbers are intermediary
results, changing the order of your computations might help2.
5. Check Your Assumptions: It’s always a good idea to go back to the assumptions of your
model. Is it really meaningful in the real-world problem you are trying to solve to divide
by such a small number? Maybe there is a special condition in practice when
measurements are very close to zero2.
6. Add More Data: Having more data is always a good idea and it helps improve accuracy4.
7. Treat Missing and Outlier Values: This can help improve the accuracy of your
calculations4.
8. Feature Selection: Select the features that are important4.
9. Use Multiple Algorithms: Using multiple algorithms and checking which one has the
best accuracy can also help4.

5. Which of the following are invalid constants and why?

0.0001 5*1.5 99999


+100 75.45E-2 *15.75*

-1.79e+4 0.00001234
-45.6

Answer: Here are the invalid constants from your list and the reasons why:

 5*1.5: This is not a constant but an expression. Constants are fixed values, while this is a
multiplication operation between two constants.
 +100: While 100 is a valid constant, the + sign is not typically included when defining
positive constants. However, some programming languages may allow it.
 *15.75*: The asterisks (*) are not valid when defining constants. They are typically used
for multiplication in programming languages.

The rest of the constants in your list are valid. They include floating-point constants (0.0001,
75.45E-2, -45.6, -1.79e+4, 0.00001234) and an integer constant (99999).

6. Which of the following are invalid variable names and why?

Minimum First.name n1+n2 &Name


Doubles 3rd_row n$ Row1
Float Sum Total Row Total Colum-total

Answer: Here are the invalid variable names from your list and the reasons why:

 First.name: Variable names cannot contain a period (.) as it is typically used for accessing
properties or methods on an object.
 n1+n2: The plus sign (+) is not allowed in variable names as it is an arithmetic operator.
 &Name: The ampersand (&) is not allowed in variable names as it is used for referencing
and dereferencing pointers.
 3rd_row: Variable names cannot start with a number.
 n$: The dollar sign ($) is not typically allowed in variable names.
 Sum Total: Variable names cannot contain spaces.
 Row Total: Variable names cannot contain spaces.

The rest of the variable names in your list are valid, assuming the programming language being
used allows for them. These include Minimum, Doubles, Row1, Float, and Colum-total. Please
note that the rules for variable names can vary between different programming languages.

Chapter 5
definition of printf and work of printf?

Answer: The printf function is a part of the C standard library <stdio.h> and is used to print
formatted output to the standard output stdout, which is generally the console screen1. The C
language standard declares printf as follows: int printf (const char *format, ...);2. It returns an
integer and takes a first parameter of a pointer to a constant character and an arbitrary number of
subsequent parameters of arbitrary type2.

The printf function works by taking a formatting string and a couple of optional variables as
input and outputs strings to the console while converting input variables to strings3. It’s
important to note that printf itself doesn’t display anything on the screen. It just causes formatted
output to be sent to the standard output stream4. If that happens to be connected to a terminal
driver, then that driver displays the output (on the screen). If it’s connected to some other kind of
device, then it is handled appropriately for that device4.

How can we use the computer assign to the variable’s year, code, and count?
1. How can we use the getchar() function to read multicharacter strings?
Answer: The getchar() function in C reads a single character from the standard input (usually the
keyboard) and returns it as an integer. This function can be used in a loop to read multiple
characters until a newline character or end-of-file (EOF) is encountered12.

Here’s a simple example of how you can use getchar() to read a string:

#include <stdio.h>

int main () {
char c;
char str [100];
int i = 0;

printf ("Enter a string: ");

while ((c = getchar()) != '\n' && c != EOF) {


str[i++] = c;
}
str[i] = '\0'; // Null-terminate the string

printf ("You entered: %s\n", str);

return 0;
}

In this code, getchar() is used inside a while loop to read characters one by one until a newline
character ('\n') or end-of-file (EOF) is encountered. Each character is then added to the str array.
After the loop, a null character ('\0') is appended to the end of the array to make it a proper
string12.

Please note that this code does not handle buffer overflow. If the user enters more than 99
characters, it will result in undefined behavior. In a real-world application, you should always
make sure that you do not write past the end of the array1.

2. How can we use the putchar () function to output multicharacter strings?

Answer: The putchar() function in C is used to write a single character to the standard output
(usually the console screen)1. To output multicharacter strings, you can use putchar() in a loop to
print each character of the string one by one23.

Here’s a simple example of how you can use putchar () to output a string:

#include <stdio.h>

int main () {
char str [] = "Hello, world!";
int i = 0;

while (str[i]! = '\0') {


putchar(str[i]);
i++;
}

return 0;
}

In this code, putchar() is used inside a while loop to print characters one by one until a null
character ('\0') is encountered, which signifies the end of the string23. Each character of the string
str is passed to putchar () to be written to the standard output1.

3. What is the purpose of scanf () function?

Answer: The scanf() function in C is used to read formatted data from the standard input stream,
which is usually the keyboard12. It stands for “Scan Formatted String” and writes the result into
the given arguments1. It accepts character, string, and numeric data from the user using standard
input1.

The syntax of scanf () in C is similar to the syntax of printf (). It returns three types of values1:

 0: The number of values converted and assigned successfully.


 0: No value was assigned.
 <0: Read error encountered or end-of-file (EOF) reached before any assignment was
made1.

The scanf () function is essential for most programs as it provides an easy way to read input in a
variety of formats2. However, it’s important to use scanf () carefully and to always validate user
input to prevent security vulnerabilities and unexpected program behavior2.

4. Describe the purpose of commonly used conversion characters in a scanf () function.

Answer: The scanf() function in C uses conversion characters in its format string to specify the
type of data that it expects to read from the standard input1. Here are some commonly used
conversion characters and their purposes2:

 %d: Used to read an integer2.


 %ld: Used to read a long integer2.
 %lld: Used to read a long long integer2.
 %f: Used to read a real number2.
 %c: Used to read a character2.
 %s: Used to read a string2.

In addition to these, there are also some special conversion characters used with scanf()3:

 []: Matches a nonempty sequence of characters from the specified set3.


 [^]: Matches a nonempty sequence of characters not from the specified set3.

These conversion characters tell scanf () how to interpret the input data and how to store it in the
variables provided1. It’s important to note that when using scanf (), you typically need to provide
the address of the variable where the read data should be stored. This is done using the &
operator1.

5. What is the purpose of printf () function?

Answer: In C language, the printf() function is used to print formatted output to the standard
output stdout, which is generally the console screen1. The printf function is a part of the C
standard library <stdio.h> and it can allow formatting the output in numerous ways1.

The syntax of printf is printf ("formatted string", arguments list);1. Here, formatted string is a
string that specifies the data to be printed. It may also contain a format specifier to print the value
of any variable such as a character and an integer1arguments_list are the variable names
corresponding to the format specifier1.

printf () returns the number of characters printed after successful execution. If an error occurs, a
negative value is returned1.

For example, in the statement printf ("Hello Geek!"); what we write inside the double quotes is
printed in the output1.

Formatting in C printf can be done using format specifiers that start with the percentage symbol
%. Some commonly used specifiers are:

 %d: for printing integers


 %f: for printing floating-point numbers
 %c: for printing characters
 %s: for printing strings
 %p: for printing memory addresses
 %x: for printing hexadecimal values1.

For instance, printf ("%c", char variable); would print the character value of char_variable 1.

6. Describe the purpose of commonly used conversion characters in a printf () function.


Answer: The printf() function in C uses conversion characters in its format string to specify the
type of data that it expects to print to the standard output1. Here are some commonly used
conversion characters and their purposes1:

 %d: Used to print a signed decimal integer1.


 %u: Used to print an unsigned decimal integer1.
 %x: Used to print an unsigned hexadecimal integer1.
 %o: Used to print an unsigned octal integer1.
 %s: Used to print a string1.
 %c: Used to print a single character1.
 %f: Used to print a fixed decimal floating point1.
 %e: Used to print a scientific notation floating point1.
 %g: Used to print a floating-point number in either fixed-decimal or scientific notation,
whichever is shorter1.

These conversion characters tell printf () how to interpret the data and how to print it1. It’s
important to note that when using printf(), you typically need to provide the variables that
contain the data to be printed1.

7. How does a control string in a printf () function differ from the control string in scanf
()?

Answer: The control strings in printf() and scanf() functions serve different purposes123:

 The control string in printf() is used to format the output1. It specifies how the subsequent
arguments are converted for output1. For example, %d in the control string of printf() tells
the function to print an integer1.
 The control string in scanf (), on the other hand, is used to read and parse input2. It
specifies how to divide the input and convert it into arguments2. For example, %d in the
control string of scanf () tells the function to read an integer from the input2.

There are also differences in how certain specifiers are interpreted in printf() and scanf()1. For
instance, scanf() requires %lf to scan a double value whereas printf() uses %f to output a double
value1. The %i specifier in printf() outputs in decimal, but when used in scanf() it can accept
input in decimal, octal, or hexadecimal notation1.

Moreover, the * character in a specifier indicates a width/precision in printf(), and to not save the
scanned item in scanf()1. A whitespace in scanf() consumes 0 or more any white spaces, while in
printf(), it is always printed as is1. A width in scanf() is the maximum number of characters to
scan, while with printf(), it is the minimum number of characters to print1.

In short:

The control string in printf () formats the output, while in scanf() it’s used to read and parse
input. Certain specifiers are interpreted differently in both functions. For example, scanf()
requires %lf to scan a double value, but printf() uses %f to output a double value. The * character
in a specifier indicates a width/precision in printf(), and to not save the scanned item in scanf().
A whitespace in scanf() consumes 0 or more any white spaces, while in printf(), it is always
printed as is. A width in scanf() is the maximum number of characters to scan, while with
printf(), it is the minimum number of characters to print.

You might also like