You are on page 1of 20

• HOME

• C INTERVIEW QUESTIONS
• DOWNLOAD PDF
• CONTRIBUTORS
• ABOUT US
• CONTACT US

C Interview Questions- header file, string functions, iterative loops, array


pointers, bitwise operators
Posted By sridhar On January 19, 2009 at 10:50 PM | Filed Under: c, interview, Interview Questions, ratan |
Comments (3)
Here is another set of challenging questions that could be asked in C language interviews.

Interviewers and interviewees can use questions and answers in this blog as a reference guide

before conducting/attending an interview. Question.46 is about basics of header files in C. It tests

whether the candidate under test is aware of the best practice of declaring rather than defining

functions in a header file. Question.47 tests the candidate's ability to discern between strdup and

strcpy functions included in string.h header file. Question.48 is about hte subtle differences between

for loop and while loop in C. Question.49 tests an advanced concept of pointer notation of multi-

dimensional arrays. Question.50 is a question on bitwise operators in C. It tests the candidate's

knowledge of the operator used for left shift and right shift. See all C interview questions in this

blog in one page.

< Previous Questions(41-45) | All | Next Questions(51-56) >

C Language Interview Questions (45 to 50)


46. What are header files? Are functions declared or defined in header files ?
47. What is the difference between the functions strdup and strcpy in C?
48. What is difference between for loop and while loop in C language?
49. Write down the equivalent pointer expression for referring the same element as
a[i][j][k][l].
50. Which one is equivalent to multiplying an unsigned int by 2, left shifting by 1 or
right shifting by 1?

< Previous Questions(41-45) | All | Next Questions(51-56) >

46. What are header files? Are functions declared or defined in header files ?
Functions and macros are declared in header files. Header files would be included in source
files by the compiler at the time of compilation.

Header files are included in source code using #include directive.#include<some.h>


includes all the declarations present in the header file 'some.h'.

A header file may contain declarations of sub-routines, functions, macros and also variables
which we may want to use in our program. Header files help in reduction of repetitive code.

Syntax of include directive:


#include<stdio.h> //includes the header file stdio.h, standard input output header into the
source code

Functions can be declared as well as defined in header files. But it is recommended only to
declare functions and not to define in the header files. When we include a header file in our
program we actually are including all the functions, macros and variables declared in it.

In case of pre-defined C standard library header files ex(stdio.h), the functions calls are
replaced by equivalent binary code present in the pre-compiled libraries. Code for C
standard functions are linked and then the program is executed. Header files with custom
names can also be created. Read more about header files
Program: Custom header files example

view source

print?

1./****************

2.Index: restaurant.h

3.****************/

4.int billAll(int food_cost, int tax, int tip);

Download code

view source

print?

01./****************

02.Index: restaurant.c

03.****************/

04.#include<stdio.h>

05.int billAll(int food_cost, int tax, int tip) {

06. int result;

07. result = food_cost + tax + tip;


08. printf("Total bill is %d\n",result);

09. return result;

10.}

Download code

view source

print?

01./****************

02.Index: main.c

03.****************/

04.#include<stdio.h>

05.#include"restaurant.h"

06.

07.int main() {

08. int food_cost, tax, tip;

09. food_cost = 50;

10. tax = 10;

11. tip = 5;

12. billAll(food_cost,tax,tip);
13. return 0;

14.}

Download code

Back to top.

47. What is the difference between the functions strdup and strcpy in C?
strcpy function: copies a source string to a destination defined by user. In strcpy function
both source and destination strings are passed as arguments. User should make sure that
destination has enough space to accommodate the string to be copied.

'strcpy' sounds like short form of "string copy".

Syntax:
strcpy(char *destination, const char *source);
Source string is the string to be copied and destination string is string into which source
string is copied. If successful, strcpy subroutine returns the address of the copied string.
Otherwise, a null pointer is returned.

Program: Example program.

view source

print?

01.#include<stdio.h>

02.#include<string.h>

03.

04.int main() {
05. char myname[10];

06. //copy contents to myname

07. strcpy(myname, "nodalo");

08. //print the string

09. puts(myname);

10. return 0;

11.}

Download Code

Output:

nodalo

Explanation:
If the string to be copied has more than 10 letters, strcpy cannot copy this string into the
string 'myname'. This is because string 'myname' is declared to be of size 10 characters
only.
In the above program, string "nodalo" is copied in myname and is printed on output screen.

strdup function: duplicates a string to a location that will be decided by the function itself.
Function will copy the contents of string to certain memory location and returns the address
to that location. 'strdup' sounds like short form of "string duplicate"

Syntax:
strdup (const char *s);
strdup returns a pointer to a character or base address of an array. Function returns address
of the memory location where the string has been copied. In case free space could not be
created then it returns a null pointer. Both strcpy and strdup functions are present in header
file <string.h>

Program: Program to illustrate strdup().

view source

print?

01.#include<stdio.h>

02.#include<string.h>

03.#include<stdlib.h>

04.

05.int main() {

06. char myname[] = "nodalo";

07. //name is pointer variable which can store the address of


memory location of string

08. char* name;

09. //contents of myname are copied in a memory address and are


assigned to name

10. name = strdup(myname);

11. //prints the contents of 'name'

12. puts(name);

13. //prints the contents of 'myname'


14. puts(myname);

15. //memory allocated to 'name'is now freed

16. free(name);

17. return 0;

18.}

Download Code

Output:

nodalo
nodalo

Explanation:
string myname consists of "nodalo" stored in it. Contents of myname are copied in a
memory address and memory is assigned to name. At the end of the program, memory can
be freed using free(name);

Back to top.

48. What is difference between for loop and while loop in C language?
for loop: When it is desired to do initialization, condition check and increment/decrement
in a single statement of an iterative loop, it is recommended to use 'for' loop.

Syntax:

for(initialisation;condition;increment/decrement)
{
//block of statements
increment or decrement
}

Program: Program to illustrate for loop

view source

print?

01.#include<stdio.h>

02.int main() {

03. int i;

04. for (i = 1; i <= 5; i++) {

05. //print the number

06. printf("\n %d", i);

07. }

08. return 0;

09.}

Download Code

Output:

1
2
3
4
5

Explanation:
The loop repeats for 5 times and prints value of 'i' each time. 'i' increases by 1 for every
cycle of loop.

while loop: When it is not necessary to do initialization, condition check and


increment/decrement in a single statement of an iterative loop, while loop could be used. In
while loop statement, only condition statement is present.

Syntax:

view source

print?

01.#include<stdio.h>

02.int main() {

03. int i = 0, flag = 0;

04. int a[10] = { 0, 1, 4, 6, 89, 54, 78, 25, 635, 500 };

05. //This loop is repeated until the condition is false.

06. while (flag == 0) {

07. if (a[i] == 54) {

08. //as element is found, flag = 1,the loop terminates

09. flag = 1;
10. }

11. else {

12. i++;

13. }

14. }

15. printf("Element found at %d th location", i);

16. return 0;

17.}

Download Code

Output:

Element found at 5th location

Explanation:
Here flag is initialised to zero. 'while' loop repeats until the value of flag is zero, increments
i by 1. 'if' condition checks whether number 54 is found. If found, value of flag is set to 1
and 'while' loop terminates.

Back to top.

49. Write down the equivalent pointer expression for referring the same element as
a[i][j][k][l]. Explain.
Consider a multidimensional array a[w][x][y][z].
In this array, a[i] gives address of a[i][0][0][0] and a[i]+j gives the address of a[i][j][0][0]
Similarly, a[i][j] gives address of a[i][j][0][0] and a[i][j]+k gives the address of a[i][j][k][0]
a[i][j][k] gives address of a[i][j][k][0] and a[i][j][k]+l gives address of a[i][j][k][l]
Hence a[i][j][k][l] can be accessed using pointers as *(a[i][j][k]+l)
where * stands for value at address and a[i][j][k]+l gives the address location of a[i][j][k][l].

Program: Example program to illustrate pointer denotation of multi-dimensional arrays.

view source

print?

01.#include<stdio.h>

02.#include<string.h>

03.int main() {

04. int a[3][3][3][3];

05. //it gives address of a[0][0][0][0] .

06. printf(" \n address of array a is %u", a);

07. printf("\n address of a[2][0][0][0] is %u ,given by a[2], %u


given by a+2",

08. a[2], a + 2);

09. printf("\n address of a[2][2][0][0] is %u ,given by a[2][2],


%u given by a[2]+2",

10. a[2][2], a[2] + 2);

11. printf("\n address of a[2][2][1][0] is %u ,given by a[2][2]


[1] , %u given by a[2][2]+1",
12. a[2][2][1], a[2][2] + 1);

13. return 0;

14.}

Download Code

Output:

address of array a is 65340


address of a[2][0][0][0] is 65448, given by a[2] , 65448 given by a+2
address of a[2][2][0][0] is 65484, given by a[2][2] ,65484 given by a[2]+2
address of a[2][2][1][0] is 65490, given by a[2][2][1] , 65490 given by a[2][2]+1

Explanation:
This output may differ from computer to computer as the address locations are not same for
every computer.

Back to top.

50. Which one is equivalent to multiplying an unsigned int by 2,left shifting by 1 or


right shifting by 1?
Left shifting of an unsigned integer is equivalent to multiplying an unsigned int by 2.

Eg1: 14<<1;
consider a number 14-----00001110 (8+4+2)is its binary equivalent
left shift it by 1--------------00011100(16+8+4) which is 28.

Eg2: 1<<1;
consider the number as 1---00000001(0+0+1).
left shift that by 1------------00000010(0+2+0) which is 2.
left shift by 1 bit of a number=2*number
left shift by 1 bit of 2*number=2*2*number
left shift by n bits of number=(2^n)*number

Program: Program to illustrate left shift and right shift operations.

view source

print?

1.#include<stdio.h>

2.int main(void)

3.{

4. int x=10,y=10;

5. printf("left shift of 10 is %d \n",x<<1);

6. printf("right shift of 10 is %d \n",y>>1);

7. return 0;

8.}

Download code

Output:

left shift of 10 is 20
right shift of 10 is 5
Explanation:
Left shift (by 1 position) multiplies a number by two. Right shift divides a number by 2.
More about bitwise operators

Back to top.

Previous 1 2 3 4 5 6 7 8 9 10 11 12 13 Next

If you have come to this site for the first time, please consider to Subscribe to Interview Mantra by
Email. We would send you the latest posts as emails.

Or Subscribe to the RSS Feed of posts in this site. Learn more about RSS feed.

If you wish to contribute questions, mail us at contribute@nodalo.com Email your feedback at


feedback@nodalo.com

More C interview questions:


 String functions, while loop vs for loop, pointer to arrays
 Function pointer, structure pointer, sizeof operator
You might also like:

Twitter Live Quiz on C language -3

Worst is yet to come to India in November 2009?

Does an MBA help you start your own business?

LinkWithin

| Permalink | Email This | ShareThis| Comments (3)

3 comments:

•Commented by Srikanth on 1/24/2009 12:33:00 PM


Please refer to your Q50: Dealing with Left and Right Shift operators. I guess the
explanation is partially correct only in case where the shift number is 1.
But the actually , its done as this

a << b
This expression returns the value of " a " multiplied by 2 to the power of " b "

and

You have not spoken about the right shift operator:


a >> b
This expression returns the value of " a " divided by 2 to the power of " b ".

•Commented by Anonymous on 8/05/2009 09:16:00 AM


can anyone say me how to write a c programme to print numbers in following fashion

1
01
101
0111

•Commented by sridhar on 8/05/2009 02:44:00 PM


/****************************************
* Copy Right- 2009, Interview Mantra
* Some Rights Reserved
****************************************/

#include <stdio.h>
int main(){
int i,j;
for(i=0;i<4;i++){
for(j=0;j<=i;j++){
if(((i+j)%2)==0){
printf("0");
}
else{
printf("1");
}
printf("\t");
}
puts("\n");
}
return 0;
}

I hope this solution solves your problem.

Thanks,
Editor of Interview mantra
Post a Comment

It is so quiet here. Pour in your views.

Links to this post

Create a Link

Newer Post Older Post Home

EMAIL SUBSCRIPTION

Enter your email address:

Free Signup

CATEGORIES

BLOG ARCHIVE

• ▼ 2009 (45)
o ► September (10)
 Effective Job Interviewing From Both Sides of the ...
 Roundup of C Interview Programs
 Interview Questions on Structures in C
 Interview Questions on Functions in C
 Interview Questions on Operators and Constants in ...
 Interview Questions on Variables and Storage class...
 Interview Questions on Pointers in C
 Answers to Twitter Quiz -2 on C language
 What is your greatest weakness?
 Does an MBA help you start your own business?
o ► August (14)
 Fifteen things you can do when your job is lost
 Interview Mantra to be permanently moved to interv...
 Answers to Twitter Quiz -1 on C language
 C, SQL, UNIX interview for experienced developer @...
 Interview for experienced PL/SQL programmer @ Mast...
 Think your employees suck? Think again, it may be ...
 Interview Mantra gets a new Address: interviewmant...
 Top 6 things to do, a day before a Job Interview
 Java SQL UNIX Interview for 2 yr experienced @ AMD...
 Swine Flu effect on Interviews
 A recent grad sues her College for not fetching a ...
 Worst is yet to come to India in November 2009?
 Dos and Donts in an interview for entry level job
 Comments - First Interview Experience of a Fresh G...
o ► July (6)
 How to Create an Executable Jar file
 First Interview Experience of a Fresh Graduate
 How to install apache ant
 Answers to Twitter Live quiz-3 on C
 Google Chrome Blog: Google Chrome OS - FAQ
 Twitter Live Quiz on C language -3
o ► April (6)
 Define vs declare a variable, formal vs actual arg...
 Do college grades affect one's job chances?
 Contact Us
 C Interview Programs to print number patterns
 Traffic Statistics for months February, March 2009...
 Sneak peek of Interview Mantra's proposed makeover...
o ► March (1)
 Interview Mantra to sport a new look
o ► February (2)
 About Us
 C Interview Programs in Function Pointers, string ...
o ▼ January (6)
 Traffic Statistics of Interview Mantra for month J...
 Download PDF
 Meet our contributors
 C Interview Questions- header file, string functio...
 C Interview Questions - string literal, function p...
 Sample of Email subscription

• ► 2008 (15)
o ► December (4)
 Traffic Statistics of Interview Mantra for month N...
 C interview puzzles - semicolon programs, delete o...
 List of all C interview questions in this blog
 C interview programs - swap numbers, palindrome or...
o ► November (4)
 C interview programs: even or odd, prime or not, g...
o ► October (2)
o ► September (1)
o ► August (1)
o ► July (2)
o ► May (1)

• ► 2007 (2)
o ► November (1)
o ► August (1)

RESOURCES

• Home
• C Interview Questions
• Interview Corner
• Download Pdf
• Contributors
• About Us
• Contact Us

RECOMMENDED READING

1. C Puzzles
2. C FAQ
3. C Standard Library
4. Punk Rock HR- a career advice website
5. The Working Geek

DISCLAIMER

All the content and opinions in this blog are author's opinion and not his employer organisation's. The views given here
donot assure any fitness for a purpose. Author is not responsible for any kind of damage/loss incurred by following the
advice given in this blog.
Contact us at feedback@nodalo.com

COPY RIGHT INFO

© 2009 Interview Mantra, Some Rights Reserved.

Interview Mantra by Sridhar Jammalamadaka is licensed under a Creative Commons Attribution-Share Alike 3.0
United States License.
© Interview Mantra 2009 | Some Rights Reserved

You might also like