Top C Programming Interview Questions
Top C Programming Interview Questions
In this example Name of the function is Sum, the return type is the integer data type and
it accepts two integer parameters.
Q #13) What is the explanation for the cyclic nature of data types in C?
Answer: Some of the data types in C have special characteristic nature when a
developer assigns value beyond the range of the data type. There will be no compiler
error and the value changes according to a cyclic order. This is called cyclic nature.
Char, int, long int data types have this property. Further float, double and long double
data types do not have this property.
Q #14) Describe the header file and its usage in C programming?
Answer: The file containing the definitions and prototypes of the functions being used in
the program are called a header file. It is also known as a library file.
Example: The header file contains commands like printf and scanf is from the stdio.h
library file.
Q #15) There is a practice in coding to keep some code blocks in comment
symbols than delete it when debugging. How this affects when debugging?
Answer: This concept is called commenting out and this is the way to isolate some part
of the code which scans possible reason for the error. Also, this concept helps to save
time because if the code is not the reason for the issue it can simply be removed from
comment.
Q #16) What are the general description for loop statements and available loop
types in C?
Answer: A statement that allows the execution of statements or groups of statements in
a repeated way is defined as a loop.
The following diagram explains a general form of a loop.
There are 4 types of loop statements in C.
While loop
For Loop
Do…While Loop
Nested Loop
Q #17) What is a nested loop?
Answer: A loop that runs within another loop is referred to as a nested loop. The first
loop is called the Outer Loop and the inside loop is called the Inner Loop. The inner loop
executes the number of times defined in an outer loop.
Q #18) What is the general form of function in C?
Answer: The function definition in C contains four main sections.
return_type function_name( parameter list )
{
body of the function
}
Return Type: Data type of the return value of the function.
Function Name: The name of the function and it is important to have a
meaningful name that describes the activity of the function.
Parameters: The input values for the function that are used to perform the
required action.
Function Body: Collection of statements that performs the required action.
Q #19) What is a pointer on a pointer in C programming language?
Answer: A pointer variable that contains the address of another pointer variable is called
pointer on a pointer. This concept de-refers twice to point to the data held by a pointer
variable.
Answer:
#include <stdio.h>
int main () {
int a;
int b;
/* for loop execution */
for( a = 1; a < 6; a++ )
{
/* for loop execution */
for ( b = 1; b <= a; b++ )
{
printf("%d",b);
}
printf("\n");
}
return 0;
}
Q #26) Explain the use of function toupper() with an example code?
Answer: Toupper() function is used to convert the value to uppercase when it used with
characters.
Code:
#include <stdio.h>
#include <ctype.h>
int main()
{
char c;
c = 'a';
printf("%c -> %c", c, toupper(c));
c = 'A';
printf("\n%c -> %c", c, toupper(c));
c = '9';
printf("\n%c -> %c", c, toupper(c));
return 0;
}
Result:
Q #27) What is the code in a while loop that returns the output of the given code?
#include <stdio.h>
int main () {
int a;
Answer:
#include <stdio.h>
int main () {
int a;
while (a<=100)
{
printf ("%d\n", a * a);
a++;
}
return 0;
}
Q #28) Select the incorrect operator form in the following list(== , <> , >= , <=) and
what is the reason for the answer?
Answer: Incorrect operator is ‘<>’. This format is correct when writing conditional
statements, but it is not the correct operation to indicate not equal in C programming. It
gives a compilation error as follows.
Code:
#include <stdio.h>
int main () {
if ( 5 <> 10 )
printf( "test for <>" );
return 0;
}
Error:
Q #29) Is it possible to use curly brackets ({}) to enclose a single line code in C
program?
Answer: Yes, it works without any error. Some programmers like to use this to organize
the code. But the main purpose of curly brackets is to group several lines of codes.
Q #30) Describe the modifier in C?
Answer: Modifier is a prefix to the basic data type which is used to indicate the
modification for storage space allocation to a variable.
Example– In a 32-bit processor, storage space for the int data type is 4.When we use it
with modifier the storage space change as follows:
Long int: Storage space is 8 bit
Short int: Storage space is 2 bit
Q #31) What are the modifiers available in C programming language?
Answer: There are 5 modifiers available in the C programming language as
follows:
Short
Long
Signed
Unsigned
long long
Q #32) What is the process to generate random numbers in C programming
language?
Answer: The command rand() is available to use for this purpose. The function returns
an integer number beginning from zero(0). The following sample code demonstrates the
use of rand().
Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int a;
int b;
Output:
int main(){
printf("String 01 ");
printf("String 02 ");
printf("String 03 \n");
printf("String 01 \n");
printf("String 02 \n");
return 0;
}
Output:
Q #34) Is that possible to store 32768 in an int data type variable?
Answer: Int data type is only capable of storing values between – 32768 to 32767. To
store 32768 a modifier needs to used with the int data type. Long Int can use and also if
there are no negative values, unsigned int is also possible to use.
Q #35) Is there any possibility to create a customized header file with C
programming language?
Answer: Yes, it is possible and easy to create a new header file. Create a file with
function prototypes that are used inside the program. Include the file in the ‘#include’
section from its name.
Q #36) Describe dynamic data structure in C programming language?
Answer: Dynamic data structure is more efficient to memory. The memory access
occurs as needed by the program.
Q #37) Is that possible to add pointers to each other?
Answer: There is no possibility to add pointers together. Since pointer contains address
details there is no way to retrieve the value from this operation.
Q #38) What is indirection?
Answer: If you have defined a pointer to a variable or any memory object, there is no
direct reference to the value of the variable. This is called the indirect reference. But
when we declare a variable, it has a direct reference to the value.
Q #39) What are the ways to a null pointer that can be used in the C programming
language?
Answer: Null pointers are possible to use in three ways.
As an error value.
As a sentinel value.
To terminate indirection in the recursive data structure.
Q #40) What is the explanation for modular programming?
Answer: The process of dividing the main program into executable subsection is called
module programming. This concept promotes reusability.
Conclusion
The questioner is based on the C programming language concepts including memory
management with pointers, the knowledge of its syntax and some example programs
that use the Basic C program structure. Theatrical and practical knowledge of the
candidate is examined with the questions.
Top C Programming Interview Questions you Need to
Master in 2021
Beginners C Programming Interview Questions
Ans: The Datatypes in C Language are broadly classified into 4 categories. They are
as follows:
Basic Datatypes
Derived Datatypes
Enumerated Datatypes
Void Datatypes
Q3. What do you mean by the Scope of the variable? What is the
scope of the variables in C?
Ans: Scope of the variable can be defined as the part of the code area where the
variables declared in the program can be accessed directly. In C, all identifiers are
lexically (or statically) scoped.
Ans: The variables and functions that are declared using the keyword Static are
considered as Static Variable and Static Functions. The variables declared using
Static keyword will have their scope restricted to the function in which they are
declared.
Ans: calloc() and malloc() are memory dynamic memory allocating functions. The
only difference between them is that calloc() will load all the assigned memory
locations with value 0 but malloc() will not.
Q6. What are the valid places where the programmer can apply Break
Control Statement?
Ans: Break Control statement is valid to be used inside a loop and Switch control
statements.
Ans: To store a negative integer, we need to follow the following steps. Calculate the
two’s complement of the same positive integer.
Ans: The Parameters which are sent from main function to the subdivided function
are called as Actual Parameters and the parameters which are declared a the
Subdivided function end are called as Formal Parameters.
Ans: The program will be compiled but will not be executed. To execute any C
program, main() is required.
Ans: When a data member of one structure is referred by the data member of
another function, then the structure is called a Nested Structure.
In case you are facing any challenges with these C Programming Interview
Questions, please write your problems in the comment section below.
Ans: C introduced many core concepts and data structures like arrays, lists,
functions, strings, etc. Many languages designed after C are designed on the basis
of C Language. Hence, it is considered as the mother of all languages.
Ans:
Ans: printf() is used to print the values on the screen. To print certain values, and
on the other hand, scanf() is used to scan the values. We need an appropriate
datatype format specifier for both printing and scanning purposes. For example,
%d: It is a datatype format specifier used to print and scan an integer value.
%s: It is a datatype format specifier used to print and scan a string.
%c: It is a datatype format specifier used to display and scan a character value.
%f: It is a datatype format specifier used to display and scan a float value.
Ans. The array is a simple data structure that stores multiple elements of the same
datatype in a reserved and sequential manner. There are three types of arrays,
namely,
Q18. What is the main difference between the Compiler and the
Interpreter?
Ans: Compiler is used in C Language and it translates the complete code into the
Machine Code in one shot. On the other hand, Interpreter is used in Java
Programming Langauge and other high-end programming languages. It is designed
to compile code in line by line fashion.
Ans: No, Integer datatype will support the range between -32768 and 32767. Any
value exceeding that will not be stored. We can either use float or long int.
Intermediate C Programming Interview Questions
2 {
3 Function_Body;
4 }
Ans: Dynamic Memory Allocation is the process of allocating memory to the program
and its variables in runtime. Dynamic Memory Allocation process involves three
functions for allocating memory and one function to free the used memory.
Syntax:
Syntax:
Syntax:
1 free(ptr);
Ans: We cannot use & on constants and on a variable which is declared using
the register storage class.
1 struct employee
2 {
char name[10];
3
int age;
4
}e1;
5
int main()
6
{
7 printf("Enter the name");
8 scanf("%s",e1.name);
9 printf("n");
10
printf("Enter the age");
11
scanf("%d",&e1.age);
12
printf("n");
13
printf("Name and age of the employee: %s,%d",e1.name,e1.age);
14
return 0;
15 }
16
Ans:
Actual arguments cannot be changed and remain Operations are performed on actu
Safety
safe hence not safe
Memory Separate memory locations are created for actual Actual and Formal arguments sh
Location and formal arguments memory space.
1 #include<stdio.h>
2 void change(int,int);
int main()
3
{
4
int a=25,b=50;
5
change(a,b);
6
printf("The value assigned to a is: %d",a);
7 printf("n");
8 printf("The value assigned to of b is: %d",b);
9 return 0;
10 }
{
12
x=100;
13
y=200;
14
15 }
16
//Output
1
#include<stdio.h>
2
void change(int*,int*);
3
int main()
4
{
5
int a=25,b=50;
6 change(&a,&b);
7 printf("The value assigned to a is: %d",a);
8 printf("n");
10 return 0;
}
11
void change(int *x,int *y)
12
{
13
*x=100;
14
*y=200;
15 }
16
//Output
In case you are facing any challenges with these C Programming Interview
Questions, please write your problems in the comment section below.
getch(): reads characters from the keyboard but it does not use any buffers. Hence,
data is not displayed on the screen.
getche(): reads characters from the keyboard and it uses a buffer. Hence, data is
displayed on the screen.
//Example
1
#include<stdio.h>
2
#include<conio.h>
3
int main()
4 {
5 char ch;
6 printf("Please enter a character ");
7 ch=getch();
//Output
//Example
1 #include<stdio.h>
2
#include<ctype.h>
3 int main()
4 {
5 char c;
6 c=a;
c=B;
8
printf("%c after conversions %c", c, toupper(c));
9
//Output:
a after conversions A
B after conversions B
1
#include<stdio.h>
2
#include<stdlib.h>
3
int main()
4 {
5 int a,b;
6 for(a=1;a<=10;a++)
7 {
8 b=rand();
printf("%dn",b);
9
}
10
return 0;
11
}
12
//Output
1987384758
2057844389
3475398489
2247357398
1435983905
Q29. Can I create a customized Head File in C language?
Ans: It is possible to create a new header file. Create a file with function prototypes
that need to be used in the program. Include the file in the ‘#include’ section in its
name.
1 #include<stdio.h>
2 #include<stdlib.h>
int main()
3
{
4
int* ptr;
5
int n, i, sum = 0;
6
n = 5;
7 printf("Enter the number of elements: %dn", n);
8 ptr = (int*)malloc(n * sizeof(int));
9 if (ptr == NULL)
10 {
exit(0);
12
}
13
else
14
{
15
printf("Memory successfully allocated using malloc.n");
16 for (i = 0; i<= n; ++i)
17 {
18 ptr[i] = i + 1;
19 }
25 }
26 }
return 0;
27
}
28
29
//Output
In case you are facing any challenges with these C Programming Interview
Questions, please write your problems in the comment section below.
Ans: A local static variable is a variable whose life doesn’t end with a function call
where it is declared. It extends for the lifetime of the complete program. All calls to
the function share the same copy of local static variables.
1 #include<stdio.h>
2 void fun()
{
3
static int x;
4
printf("%d ", x);
5
x = x + 1;
6
}
7 int main()
8 {
9 fun();
10 fun();
11 return 0;
}
12
13
//Output
0 1
Q32. What is the difference between declaring a header file with < >
and ” “?
Ans: If the Header File is declared using < > then the compiler searches for the
header file within the Built-in Path. If the Header File is declared using ” ” then the
compiler will search for the Header File in the current working directory and if not
found then it searches for the file in other locations.
Ans: We use Register Storage Specifier if a certain variable is used very frequently.
This helps the compiler to locate the variable as the variable will be declared in one
of the CPU registers.
Ans: x++; is the most efficient statement as it just a single instruction to the compiler
while the other is not.
Q35. Can I declare the same variable name to the variables which
have different scopes?
Ans: Yes, Same variable name can be declared to the variables with different
variable scopes as the following example.
1 int var;
2 void function()
{
3
int variable;
4
}
5
int main()
6
{
7
int variable;
8
}
9
Q36. Which variable can be used to access Union data members if the
Union variable is declared as a pointer variable?
Ans: Arrow Operator( -> ) can be used to access the data members of a Union if the
Union Variable is declared as a pointer variable.
Ans: Basic File Handling Techniques in C, provide the basic functionalities that
user can perform against files in the system.
Function Operation
In case you are facing any challenges with these C Programming Interview
Questions, please write your problems in the comment section below.
auto
register
static
extern
Ans: Typecasting is a process of converting one data type into another is known as
typecasting. If we want to store the floating type value to an int type, then we will
convert the data type into another data type explicitly.
Syntax:
1 (type_name) expression;
Ans:
1 #include<stdio.h>
2 void main()
3 {
4 if(printf("hello world")){}
}
5
//Output:
hello world
Q41. Write a program to swap two numbers without using the third
variable.
Ans:
1
#include<stdio.h>
2
#include<conio.h>
3
main()
4 {
5 int a=10, b=20;
6 clrscr();
8 a=a+b;
b=a-b;
9
a=a-b;
10
printf("nAfter swapping a=%d b=%d",a,b);
11
getch();
12
}
13
//Output
Before swapping a=10 b=20
After swapping a=20 b=10
Q42. How can you print a string with the symbol % in it?
Ans: There is no escape sequence provided for the symbol % in C. So, to print %
we should use ‘%%’ as shown below.
Ans: To print the above pattern, the following code can be used.
1 #include<stdio.h>
2 int main()
{
3
for(i=1;i<=5;1++)
4
5
{
6 for(j=1;j<=5;j++)
7 {
8 print("%d",j);
9 }
10 printf("n");
}
11
return 0;
12
}
13
This is a preprocessor directive that can be used to turn on or off certain features.
It is of two types #pragma startup, #pragma exit and pragma warn.
#pragma startup allows us to specify functions called upon program startup.
#pragma exit allows us to specify functions called upon program exit.
#pragma warn tells the computer to suppress any warning or not.
Ans: The following program will help you to remove duplicates from an array.
1 #include <stdio.h>
2 int main()
{
3
int n, a[100], b[100], calc = 0, i, j,count;
4
printf("Enter no. of elements in array.n");
5
scanf("%d", &n);
6
printf("Enter %d integersn", n);
7 for (i = 0; i < n; i++)
8 scanf("%d", &a[i]);
9 for (i = 0; i<n; i++)
10 {
13 {
14 if(a[i] == b[j])
15 break;
16 }
17 if (j== calc)
{
18
b[count] = a[i];
19
calc++;
20
}
21
}
22 printf("Array obtained after removing duplicate elementsn");
23 for (i = 0; i<calc; i++)
24 {
25 printf("%dn", b[i]);
26 }
return 0;
27
}
28
29
//Output
Ans: Bubble sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements and swaps them if they are in the wrong order. The
pass through the list is repeated until the list is sorted.
The following code executes Bubble Sort.
2 int main()
3 {
12 {
13 swap=array[j];
array[j]=array[j+1];
14
array[j+1]=swap;
15
}
16
}
17
}
18
printf("Sorted Array:n");
19 for(i = 0; i < n; i++)
20 printf("%dn", array[i]);
21 return 0;
22 }
23
3 int main()
{
4
int i, limit, total = 0, x, counter = 0, time_quantum;
5
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], tem
6
float average_wait_time, average_turnaround_time;
7
printf("nEnter Total Number of Processes:t");
8 scanf("%d", &limit);
9 x = limit;
10 for(i = 0; i<limit; i++)
11 {
printf("Arrival Time:t");
13
scanf("%d", &arrival_time[i]);
14
printf("Burst Time:t");
15
scanf("%d", &burst_time[i]);
16
temp[i] = burst_time[i];
17 }
18
22 for(total = 0, i = 0; x != 0;)
{
23
if(temp[i] <= time_quantum && temp[i] > 0)
24
{
25
total = total + temp[i];
26
temp[i] = 0;
27 counter = 1;
28 }
29 else if(temp[i]>0)
30 {
33 }
46 {
47 i++;
}
48
else
49
{
50
i = 0;
51
}
52
}
53
return 0;
58
}
59
60
61
62
63
64
//Output
In case you are facing any challenges with these C Programming Interview
Questions, please write your problems in the comment section below.
Q48. Which structure is used to link the program and the operating
system?
Ans: The differences between macros and functions can be explained as follows:
Macro call replaces the templates with the expansion in a literal way.
The Macro call makes the program run faster but also increases the program size.
Macro is simple and avoids errors related to the function calls.
In a function, call control is transferred to the function along with arguments.
It makes the functions small and compact.
Passing arguments and getting back the returned value takes time and makes the
program run at a slower rate.
Q51. Suppose a global variable and local variable have the same
name. Is it is possible to access a global variable from a block where
local variables are defined?
Ans: No. It is not possible in C. It is always the most local variable that gets
preference.
Now that you have understood the basics of Programming in C, check out
the training provided by Edureka on many technologies like Java, Spring, and
many more, a trusted online learning company with a network of more than 250,000
satisfied learners spread across the globe
Got a question for us? Mention it in the comments section of this “C Programming
Interview Questions” blog and we will get back to you as soon as possible.
When using Call by Value, you are sending the value of a variable as parameter
to a function, whereas Call by Reference sends the address of the variable. Also,
under Call by Value, the value in the parameter is not affected by whatever
operation that takes place, while in the case of Call by Reference, values can be
affected by the process within the function.
Answer:
a=1;
while (a<=100) {
a++;
}
5) What is a stack?
A stack is one form of a data structure. Data is stored in stacks using the FILO
(First In Last Out) approach. At any particular instance, only the top of the stack
is accessible, which means that in order to retrieve data that is stored inside the
stack, those on the upper part should be extracted first. Storing data in a stack is
also referred to as a PUSH, while data retrieval is referred to as a POP.
When writing programs that will store and retrieve data in a file, it is possible to
designate that file into different forms. A sequential access file is such that data
are saved in sequential order: one data is placed into the file after another. To
access a particular data within the sequential access file, data has to be read
one data at a time, until the right one is reached.
This refers to the process wherein a variable is assigned an initial value before it
is used in the program. Without initialization, a variable would have an
unknown value, which can lead to unpredictable outputs when used in
computations or other operations.
Spaghetti programming refers to codes that tend to get tangled and overlapped
throughout the program. This unstructured approach to coding is usually
attributed to lack of experience on the part of the programmer. Spaghetti
programing makes a program complex and analyzing the codes difficult, and so
must be avoided as much as possible.
Source codes are codes that were written by the programmer. It is made up of
the commands and other English-like keywords that are supposed to instruct
the computer what to do. However, computers would not be able to understand
source codes. Therefore, source codes are compiled using a compiler. The
resulting outputs are object codes, which are in a format that can be
understood by the computer processor. In C programming, source codes are
saved with the file extension .C, while object codes are saved with the file
extension .OBJ
10) In C programming, how do you insert quote characters (' and ") into the
output screen?
This is a common problem for beginners because quotes are normally part of a
printf statement. To insert the quote character as part of the output, use the
format specifiers \' (for single quote), and \" (for double quote).
The modulus operator outputs the remainder of a division. It makes use of the
percentage (%) symbol. For example: 10 % 3 = 1, meaning when you divide 10 by
3, the remainder is 1.
A nested loop is a loop that runs within another loop. Put it in another sense,
you have an inner loop that is inside an outer loop. In this scenario, the inner
loop is performed a number of times as specified by the outer loop. For each
turn on the outer loop, the inner loop is first performed.
15) Which of the following operators is incorrect and why? ( >=, <=, <>, ==)
<> is incorrect. While this operator is correctly interpreted as "not equal to" in
writing conditional statements, it is not the proper operator to be used in C
programming. Instead, the operator != must be used to indicate "not equal to"
condition.
Compilers and interpreters often deal with how program codes are executed.
Interpreters execute program codes one line at a time, while compilers take the
program as a whole and convert it into object code, before executing it. The key
difference here is that in the case of interpreters, a program may encounter
syntax errors in the middle of execution, and will stop from there. On the other
hand, compilers check the syntax of the entire program and will only proceed to
execution when no syntax errors are found.
17) How do you declare a variable that will hold string values?
The char keyword can only hold 1 character value at a time. By creating an array
of characters, you can store string values in it. Example: "char MyName[50]; "
declares a string variable named MyName that can hold a maximum of 50
characters.
18) Can the curly brackets { } be used to enclose a single line of code?
While curly brackets are mainly used to group several lines of codes, it will still
work without error if you used it for a single line. Some programmers prefer this
method as a way of organizing codes to make it look clearer, especially in
conditional statements.
19) What are header files and what are its uses in C programming?
Header files are also known as library files. They contain two essential things:
the definitions and prototypes of functions being used in a program. Simply put,
commands that you use in C programming are actually functions that are
defined from within each header files. Each header file contains a set of
functions. For example: stdio.h is a header file that contains definition and
prototypes of commands like printf and scanf.
20) What is syntax error?
21) What are variables and it what way is it different from constants?
Variables and constants may at first look similar in a sense that both are
identifiers made up of one character or more characters (letters, numbers and a
few allowable symbols). Both will also hold a particular value. Values held by a
variable can be altered throughout the program, and can be used in most
operations and computations. Constants are given values at one time only,
placed at the beginning of a program. This value is not altered in the program.
For example, you can assigned a constant named PI and give it a value 3.1415 .
You can then use it as PI in the program, instead of having to write 3.1415 each
time you need it.
Arrays contain a number of elements, depending on the size you gave it during
variable declaration. Each element is assigned a number from 0 to number of
elements-1. To assign or retrieve the value of a particular element, refer to the
element number. For example: if you have a declaration that says
"intscores[5];", then you have 5 accessible elements, namely: scores[0],
scores[1], scores[2], scores[3] and scores[4].
23) Can I use "int" data type to store the value 32768? Why?
No. "int" data type is capable of storing values from -32768 to 32767. To store
32768, you can use "long int" instead. You can also use "unsigned int", assuming
you don't intend to store negative values.
24) Can two or more operators such as \n and \t be combined in a single line
of program code?
Yes, it's perfectly valid to combine operators, especially if the need arises. For
example: you can have a code like " printf ("Hello\n\n\'World\'") " to output the
text "Hello" on the first line and "World" enclosed in single quotes to appear on
the next two lines.
25) Why is it that not all header files are declared in every C program?
The choice of declaring a header file at the top of each C program would depend
on what commands/functions you will be using in that program. Since each
header file contains different function definitions and prototype, you would be
using only those header files that would contain the functions you will need.
Declaring all header files in every program would only increase the overall file
size and load of the program, and is not considered a good programming style.
When declaring functions, you will decide whether that function would be
returning a value or not. If that function will not return a value, such as when the
purpose of a function is to display some outputs on the screen, then "void" is to
be placed at the leftmost part of the function header. When a return value is
expected after the function execution, the data type of the return value is placed
instead of "void".
When storing multiple related data, it is a good idea to use arrays. This is
because arrays are named using only 1 word followed by an element number.
For example: to store the 10 test results of 1 student, one can use 10 different
variable names (grade1, grade2, grade3... grade10). With arrays, only 1 name is
used, the rest are accessible through the index name (grade[0], grade[1],
grade[2]... grade[9]).
30) Write a loop statement that will show the following output:
12
123
1234
12345
Answer:
printf("%d",b);
printf("\n");
An ampersand & symbol must be placed before the variable name whatnumber.
Placing & means whatever integer value is entered by the user is stored at the
"address" of the variable name. This is a common mistake for programmers,
often leading to logical errors.
Random numbers are generated in C using the rand() command. For example:
anyNum = rand() will generate any integer number beginning from 0, assuming
that anyNum is a variable of type integer.
33) What could possibly be the problem if a valid function name such as
tolower() is being reported by the C compiler as undefined?
The most probable reason behind this error is that the header file for that
function was not indicated at the top of the program. Header files contain the
definition and prototype for functions and commands used in a C program. In
the case of "tolower()", the code "#include <ctype.h>" must be present at the
beginning of the program.
The && is also referred to as AND operator. When using this operator, all
conditions specified must be TRUE before the next action can be performed. If
you have 10 conditions and all but 1 fails to evaluate as TRUE, the entire
condition statement is already evaluated as FALSE
if (num % 2 == 0)
printf("EVEN");
else
printf("ODD");
38) What does the format %10.2 mean when included in a printf statement?
This format is used for two things: to set the number of spaces allotted for the
output number and to set the number of decimal places. The number before the
decimal point is for the allotted space, in this case it would allot 10 spaces for
the output number. If the number of space occupied by the output number is
less than 10, addition space characters will be inserted before the actual output
number. The number after the decimal point sets the number of decimal places,
in this case, it's 2 decimal spaces.
39) What are logical errors and how does it differ from syntax errors?
Program that contains logical errors tend to pass the compilation process, but
the resulting output may not be the expected one. This happens when a wrong
formula was inserted into the code, or a wrong sequence of commands was
performed. Syntax errors, on the other hand, deal with incorrect commands
that are misspelled or not recognized by the compiler.
No. "if" command can only be used to compare numerical values and single
character values. For comparing string values, there is another function called
strcmp that deals specifically with strings.
44) What will be the outcome of the following conditional statement if the
value of variable s is 10?
The outcome will be TRUE. Since the value of s is 10, s >= 10 evaluates to TRUE
because s is not greater than 10 but is still equal to 10. s< 25 is also TRUE since
10 is less then 25. Just the same, s!=12, which means s is not equal to 12,
evaluates to TRUE. The && is the AND operator, and follows the rule that if all
individual conditions are TRUE, the entire statement is TRUE.
You cannot use the = sign to assign values to a string variable. Instead, use the
strcpy function. The correct statement would be: strcpy(myName, "Robin");
47) How do you determine the length of a string value that was stored in a
variable?
To get the length of a string value, use the function strlen(). For example, if you
have a variable named FullName, you can get the length of the stored string
value by using this statement: I = strlen(FullName); the variable I will now have
the character length of the string value.
Yes, you don't have to write a separate assignment statement after the variable
declaration, unless you plan to change it later on. For example: char planet[15]
= "Earth"; does two things: it declares a string variable named planet, then
initializes it with the value "Earth".
This is because C language is rich in features that make it behave like a high
level language while at the same time can interact with hardware using low
level methods. The use of a well structured approach to programming, coupled
with English-like words used in functions, makes it act as a high level language.
On the other hand, C can directly access memory structures similar to assembly
language routines.
50) What are the different file extensions involved when programming in C?
Source codes in C are saved with .C file extension. Header files or library files
have the .H file extension. Every time a program source code is successfully
compiled, it creates an .OBJ object file, and an executable .EXE file.
Reserved words are words that are part of the standard C language library. This
means that reserved words have special meaning and therefore cannot be used
for purposes other than what it is originally intended for. Examples of reserved
words are int, void, and return.
Binary trees are actually an extension of the concept of linked lists. A binary tree
has two pointers, a left one and a right one. Each side can further branch to form
additional nodes, which each node having two pointers as well.
55) Not all reserved words are written in lowercase. TRUE or FALSE?
56) What is the difference between the expression "++a" and "a++"?
In the first expression, the increment would happen first on variable a, and the
resulting value will be the one to be used. This is also known as a prefix
increment. In the second expression, the current value of variable a would the
one to be used in an operation, before the value of a itself is incremented. This is
also known as postfix increment.
57) What would happen to X in this expression: X += 15; (assuming the value
of X is 5)
58) In C language, the variables NAME, name, and Name are all the same.
TRUE or FALSE?
An endless loop can mean two things. One is that it was designed to loop
continuously until the condition within the loop is met, after which a break
function would cause the program to step out of the loop. Another idea of an
endless loop is when an incorrect loop condition was written, causing the loop
to run erroneously forever. Endless loops are oftentimes referred to as infinite
loops.
60) What is a program flowchart and how does it help in writing a program?
Assuming that INT is a variable of type float, this statement is valid. One may
think that INT is a reserved word and must not be used for other purposes.
However, recall that reserved words are express in lowercase, so the C compiler
will not interpret this as a reserved word.
When you create and use functions that need to perform an action on some
given values, you need to pass these given values to that function. The values
that are being passed into the called function are referred to as actual
arguments.
These are errors that occur while the program is being executed. One common
instance wherein run-time errors can happen is when you are trying to divide a
number by zero. When run-time errors occur, program execution will pause,
showing which program line caused the error.
70) Write a simple code fragment that will check if a number is positive or
negative.
If (num>=0)
printf("number is positive");
else
The switch statement is best used when dealing with selections based on a
single variable or expression. However, switch statements can only evaluate
integer and character data types.
72) What are global variables and how do you declare them?
Global variables are variables that can be accessed and manipulated anywhere
in the program. To make a variable global, place the variable declaration on the
upper portion of the program, just after the preprocessor directives section.
73) What are enumerated types?
It is used to convert any letter to its upper case mode. Toupper() function
prototype is declared in <ctype.h>. Note that this function will only convert a
single character, and not an entire string.
Yes, that is allowed in C programming. You just need to include the entire
function prototype into the parameter field of the other function where it is to
be used.
The strcat function. It takes two parameters, the source string and the string
value to be appended to the source string.
Both functions will accept a character input value from the user. When using
getch(), the key that was pressed will not appear on the screen, and is
automatically captured and assigned to a variable. When using getche(), the key
that was pressed by the user will appear on the screen, while at the same time
being assigned to a variable.
81) What does the characters "r" and "w" mean when writing programs
that will make use of files?
"r" means "read" and will open a file as input wherein data is to be retrieved.
"w" means "write", and will open a file for output. Previous data that was stored
on that file will be erased.
82) What is the difference between text files and binary files?
Text files contain data that can easily be understood by humans. It includes
letters, numbers and other characters. On the other hand, binary files contain 1s
and 0s that only computers can interpret.
Dynamic data structure provides a means for storing data more efficiently into
memory. Using dynamic memory allocation, your program will access memory
spaces as needed. This is in contrast to static data structure, wherein the
programmer has to indicate a fix number of memory space to be used in the
program.
The basic data types are int, char, and float. Int is used to declare variables that
will be storing integer values. Float is used to store real numbers. Char can store
individual character values.
If the amount of data stored in a file is fairly large, the use of random access will
allow you to search through it quicker. If it had been a sequential access file, you
would have to go through one record at a time until you reach the target data. A
random access file lets you jump directly to the target address where data is
located.
If a break statement was not placed at the end of a particular case portion? It
will move on to the next case portion, possibly causing incorrect output.
One thing to note is that you cannot pass the entire array to a function. Instead,
you pass to it a pointer that will point to the array first element in memory. To
do this, you indicate the name of the array without the brackets.
Pointers point to specific areas in the memory. Pointers contain the address of a
variable, which in turn may contain a value or even an address to another
memory.
The gets() function allows a full line data entry from the user. When the user
presses the enter key to end the input, the entire line of characters is stored to a
string variable. Note that the enter key is not included in the variable, but
instead a null terminator \0 is placed after the last character.
93) The % symbol has a special use in a printf statement. How would you
place this character as part of the output on the screen?
You can do this by using %% in the printf statement. For example, you can write
printf("10%%") to have the output appear as 10% on the screen.
94) How do you search data in a data file using random access method?
Use the fseek() function to perform random access input/ouput on a file. After
the file was opened by the fopen() function, the fseek would require three
parameters to work: a file pointer to the file, the number of bytes to search, and
the point of origin in the file.
95) Are comments included during the compilation stage and placed in the
EXE file as well?
96) Is there a built-in function in C that can be used for sorting data?
Yes, use the qsort() function. It is also possible to create user defined functions
for sorting, such as those based on the balloon sort and bubble sort algorithm.
Storing data on the heap is slower than it would take when using the stack.
However, the main advantage of using the heap is its flexibility. That's because
memory in this structure can be allocated and remove in any particular order.
Slowness in the heap can be compensated if an algorithm was well designed
and implemented.
You can write you own functions to do string to number conversions, or instead
use C's built in functions. You can use atof to convert to a floating point value,
atoi to convert to an integer value, and atol to convert to a long integer value.
99) Create a simple code fragment that will swap the values of two
variables num1 and num2.
int temp;
temp = num1;
num1 = num2;
num2 = temp;
100) What is the use of a semicolon (;) at the end of every program
statement?
It has to do with the parsing process and compilation of the code. A semicolon
acts as a delimiter, so that the compiler knows where each statement ends, and
can proceed to divide the statement into smaller elements for syntax checking.