Professional Documents
Culture Documents
• C INTERVIEW QUESTIONS
• DOWNLOAD PDF
• CONTRIBUTORS
• ABOUT US
• CONTACT US
Interviewers and interviewees can use questions and answers in this blog as a reference guide
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-
knowledge of the operator used for left shift and right shift. See all C interview questions in this
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.
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.
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.****************/
Download code
view source
print?
01./****************
02.Index: restaurant.c
03.****************/
04.#include<stdio.h>
10.}
Download code
view source
print?
01./****************
02.Index: main.c
03.****************/
04.#include<stdio.h>
05.#include"restaurant.h"
06.
07.int main() {
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.
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.
view source
print?
01.#include<stdio.h>
02.#include<string.h>
03.
04.int main() {
05. char myname[10];
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>
view source
print?
01.#include<stdio.h>
02.#include<string.h>
03.#include<stdlib.h>
04.
05.int main() {
12. puts(name);
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
}
view source
print?
01.#include<stdio.h>
02.int main() {
03. int 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.
Syntax:
view source
print?
01.#include<stdio.h>
02.int main() {
09. flag = 1;
10. }
11. else {
12. i++;
13. }
14. }
16. return 0;
17.}
Download Code
Output:
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].
view source
print?
01.#include<stdio.h>
02.#include<string.h>
03.int main() {
13. return 0;
14.}
Download Code
Output:
Explanation:
This output may differ from computer to computer as the address locations are not same for
every computer.
Back to top.
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
view source
print?
1.#include<stdio.h>
2.int main(void)
3.{
4. int x=10,y=10;
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.
LinkWithin
3 comments:
a << b
This expression returns the value of " a " multiplied by 2 to the power of " b "
and
1
01
101
0111
#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;
}
Thanks,
Editor of Interview mantra
Post a Comment
Create a Link
EMAIL SUBSCRIPTION
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
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