FREQUENTLY ASKED QUESTIONS IN C!
- Why do we call C a middle-level language?
The C programming language is considered by some to be a “middle-level” language because it provides low-level capability at the expense of some user-friendliness.
Initially, some cynics remarked sarcastically, “C combines the flexibility and power of high-level language with the user-friendliness of assembly language.” However, they were wrong, as C soon became the “lingua franca” of the computer professionals.
- Why do we write “# include <stdio.h>” at the top of a C source file?
It lets the compiler know some important information about some of the library functions, e.g., printf() and scanf(). The prototypes for the standard I/O (input and output) functions are stored in this file. This information is useful for the error checking by the compiler.
- Why should we use comments in a C program?
Comments are non executable statements that are explicitly added in a program to make it easier for a human reader to understand the code better. The compiler simply ignores them and so they do not contribute anything to the C program as far as the compiler is concerned.
We can use a comment for describing what a particular variable is for, describing what a function is for and how it works, documenting the name, version number, purpose, and programmer of an entire program, explaining any tricky or hard-to-understand part about a program etc.
- What are the largest and smallest values that can be reliably stored in a variable of type int?
32,767 and -32,768
- What is the difference between the constants 7, ‘7’, and “7”?
The constant 7 is the integer 7. The constant ‘7’ is a character constant consisting of the character ‘7’. The constant “7” is a string constant consisting of one character, the character ‘7’.
- What is the difference between the constants 123 and “123”?
The constant 123 is the integer 123. The constant “123” is a string constant containing the characters ‘1’ ‘2’ and ‘3’.
- What is the function of the semicolon in a C statement?
It is the statement terminator.
- Name three uses of the semicolon in C.
Terminating declarations, terminating statements, and separating the three control expressions in a for loop.
- What is the numeric value of the expression 3<4?
1 (or “true”), because three is in fact less than 4.
- What is wrong with the #define N 10; ?
The semicolon at the end of the line will become part of N’s definition, which is hardly ever what you want.
- What are the two different kinds of division that the / operator can do? Under what circumstances does it perform each?
The two kinds of division are integer division, which discards any remainder, and floating-point division, which yields a floating-point result (with a fractional part, if necessary).
Floating-point division is performed if either operand (or both) is floating-point (that is, if either number being operated is floating-point); integer division is performed if both operands are integers.
- What are the definitions of the “Boolean” values – true and false in C?
False is always represented by a zero value. Any non zero value is considered to be true. The built-in operators <, <=, >, >=, ==, !=, &&, ||, and ! always generate 0 for false and 1 for true.
- What would the expression i = i++ do?
Since it tries to modify i twice, it is undefined. If we just wanted to increment i once, we should have written just i++ or written i = i + 1 (but not both).
- What does printf(“%-10d”, n) print if n is an integer?
Normally, an integer is printed right justified. %-10d causes the text to be left-justified and printed in 10 spaces irrespective of its value.
- Where are header (“#include”) files searched for?
The exact behavior is implementation defined. Typically, headers named with <> syntax area searched for in one or more standard places. Header files named with “” syntax are first searched for in the current directory, then (if not found) in the same standard places.
- How can a ‘%’ character be printed in a printf format string? (\% doesn’t work)
Double the percent sign: %%. \% can’t work, because the backslash \ is the compiler’s escape character, while here our problem is that the % is printf’s escape character.
- When a number is read with scanf %d and then a string is read with gets(), the compiler seems to skip the call to gets()! Why?
Scanf %d would not consume a trailing newline. If the output number is immediately followed by a newline, that newline will immediately satisfy the gets().
As a general rule, we shouldn’t try to interlace calls to scanf with calls to gets() (or any other input routines); scanf’s peculiar treatment of newlines almost always leads to trouble. Either use scanf to read everything or nothing.
- What are the important syntactic characteristics of a C Program?
The important characteristics of a C program are:
- Comments are enclosed by “/*” and “*/”. Comments can go almost anywhere, since the compiler ignores them.
- Most lines of statements in the program end with a “;” but some do not. Either forgetting a “;” or adding one where it isn’t needed is a common source of C programming buts. Lines of codes are grouped by using curly braces (“{“ and “}”).
- C is case-sensitive. All C keywords are in lower-case. We can declare program variables and function names in whatever case we want, but by convention they should be in lower case. Constants are upper case by convention.
- How many elements does the array int a[5] contain? Which is the first element? The last?
The array has 5 elements. The first is a[0]; the last is a[4].
- What would the expression c = getchar() != EOF do?
It would read one character and compare it to the constant EOF. If the character read was equal to EOF, it would set c to 1, otherwise (i.e., for any other character) it would set c to 0.
What it would not do is read a character, assign it to c, and then test it against EOF, which is what we usually want to do, and which we need to write
(c = getchar()) != EOF
- Define string in C.
An array of characters, terminated with the null character ‘\0’.
- What is the difference between these initializations?
char a[] = “string literal”;
char *p = “string literal”;
A string literal can be used in two slightly different ways. As an array initializer (as in the declaration of char a[]), it specifies the initial value of the characters in that array. Anywhere else, it turns into an unnamed, static array of characters, which may be stored in read-only memory, which is why we can’t safely modify it.
In an expression context, the array is converted at once to a pointer, as usual, so the second declaration initializes p to point to the unnamed array’s first element. Our program may crash if we try to assign a new value to p[i].
- What happens if we overrun the elements of an array and crossover the end?
In such a case, we have the danger of over-writing any variables that we may have stored beyond the end of the array. The C compiler will simply generate code to access as many elements as we specify- it is not concerned with whether we reach the end of the array or not.
- What are console I/O functions? What is special about the console I/O functions?
DOS-based compilers also have an alternative library of console I/O functions. These functions require the declaration:
#include<conio.h>
The three most useful PC console I/O functions are:
getch(): To get a character from the keyboard (no need to press Enter).
getche(): Gets a character from the keyboard and echo it.
kbhit(): Checks to see if a key has been pressed.
These are DOS-only commands, though some compilers on other platforms may have similar functions.
- What is the utility of the kbhit() function?
The kbhit() function only tells use if a key has been pressed or not. It returns a nonzero value if a key has been pressed, and zero if it hasn’t. This allows the program to poll the keyboard for input rather than hanging and waiting for something to happen.
- If we say int i=5; int *ip = &I; then what is ip? What is its value?
ip is a variable which can point to an int (that is, its value will be a pointer to an int; or informally, we say that ip is a pointer to an int). Its value is a pointer that points to the variable i. If we wanted ip itself to store that address of or point to i we should have written ip = &i;
- If ip is a pointer to an int, what does ip++ mean? What does *ip++ = 0 do?
ip++ means about the same as it does for any other variable: increment it by 1, that is, as if we had written ip = ip + 1. In the case of pointer, this means to make it point to the object (the int) one past the one it used to. *ip++ = 0 sets the int variable pointed to by ip to 0, and then increments ip to point to the next int.
- If p is a pointer, what does p[i] mean?
It means the same as *(p+i), or, in other words, the contents of the object i locations past the one pointed to by p.
- Since array references decay into pointers, if arr is an array, what’s the difference between arr and &arr?
The type. In standard C, &arr is a pointer, to the entire array. Under all C compilers, a simple reference (without an explicit &) to an array yields a pointer, of type pointer-to-T, to the array’s first element.
- What are the four important parts of a function? Which three do we need to know to call a function?
The name of the function, the number and type of arguments to be passed, the return type, and the body are the four important parts of a function. The caller needs to know the first three.
- What is the difference between a parameter and an argument?
We must note that the word “parameter” is sometimes used in place of “argument”. There is actually a fine distinction between these terms: the calling routine specifies “arguments” to the called function, while the called function receives the “parameters” from the calling routine.
- How is a parameter handled in a C function?
When we list a parameter in the function header, it becomes a local variable to the function. It is initialized to the value provided to it as an argument by the calling routine. Parameters are matched with arguments in the order in which they are sent.
- Is it necessary that every function should have at least one return statement that returns a particular value?
A function does not necessarily have to return a value. Without a value, “return” simply causes the function to return control to the calling routine. This of course implies that the data type of the function be declared as “void”.
- Is it possible to define a function that accepts a variable number of parameters?
Yes, it is possible to define functions with a variable number of parameters. In fact, printf() is such a function that we are familiar with.
- What are the four parts of a structure definition?
The keyword struct, the structure tag (optional), the brace-enclosed list of member declarations (optional), and a list of variables of this structure type to be declared (optional) are the four parts of a structure definition.
- Why doesn’t the following structure declaration work?
struct x{... … …};
x thestruct;
Typedef names are not automatically generated for structure tags. We have to generate them explicitly.
- How are structure passing and returning implemented?
When structures are passed as arguments to functions, the entire structure is typically pushed on the stack, using as many words as are required. Programmers often choose to use pointers to structures instead, precisely to avoid this overhead. Some compilers merely pass a pointer to the structure, though they may have to make a local copy to preserve pass-by-value semantics.
Structures are often returned from functions in a location pointed to by an extra, compiler-supplied “hidden” argument to the function.
Question Bank for the subject
"Fundamentals of Computing and Computer Programming"
is available here!
Question Bank for the subject
"Fundamentals of Computing and Computer Programming"
is available here!