Go Back   2023 2024 MBA > MBA > Main Forum

  #1  
Old 8th October 2014, 09:43 AM
Unregistered
Guest
 
Default Important Questions related to C with Answers

Here I am looking for some of the Important Questions related to C with Answers, can you please provide me the same??
Reply With Quote Quick reply to this message
  #2  
Old 8th October 2014, 02:04 PM
Super Moderator
 
Join Date: May 2012
Default Re: Important Questions related to C with Answers

As you are looking for some of the Important Questions related to C with Answers, so here I am sharing the same with you

What are the advantages of the functions?
- Debugging is easier
- It is easier to understand the logic involved in the program
- Testing is easier
- Recursive call is possible
- Irrelevant details in the user point of view are hidden in functions
- Functions are helpful in generalizing the program

Is NULL always defined as 0?
NULL is defined as either 0 or (void*)0. These values are almost identical; either a literal zero or a void pointer is converted automatically to any kind of pointer, as necessary, whenever a pointer is needed (although the compiler can’t always tell when a pointer is needed).

What is the difference between NULL and NUL?
NULL is a macro defined in for the null pointer.
NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. There’s no standard macro NUL in C, but some people like to define it.
The digit 0 corresponds to a value of 80, decimal. Don’t confuse the digit 0 with the value of ‘’ (NUL)! NULL can be defined as ((void*)0), NUL as ‘’.

Can the sizeof operator be used to tell the size of an array passed to a function?
No. There’s no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element.

Is using exit() the same as using return?
No. The exit() function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit() function are similar.

Can math operations be performed on a void pointer?
No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you don’t know what it’s pointing to, so you don’t know the size of what it’s pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.

Can the size of an array be declared at runtime?
No. In an array declaration, the size must be known at compile time. You can’t specify a size that’s known only at runtime. For example, if i is a variable, you can’t write code like this:
char array[i]; /* not valid C */
Some languages provide this latitude. C doesn’t. If it did, the stack would be more complicated, function calls would be more expensive, and programs would run a lot slower. If you know that you have an array but you won’t know until runtime how big it will be, declare a pointer to it and use malloc() or calloc() to allocate the array from the heap.

Can you add pointers together? Why would you?
No, you can’t add pointers together. If you live at 1332 Lakeview Drive, and your neighbor lives at 1364 Lakeview, what’s 1332+1364? It’s a number, but it doesn’t mean anything. If you try to perform this type of calculation with pointers in a C program, your compiler will complain.
The only time the addition of pointers might come up is if you try to add a pointer and the difference of two pointers.

Are pointers integers?
No, pointers are not integers. A pointer is an address. It is merely a positive number and not an integer.

How do you redirect a standard stream?
Most operating systems, including DOS, provide a means to redirect program input and output to and from different devices. This means that rather than your program output (stdout) going to the screen; it can be redirected to a file or printer port. Similarly, your program’s input (stdin) can come from a file rather than the keyboard. In DOS, this task is accomplished using the redirection characters, < and >. For example, if you wanted a program named PRINTIT.EXE to receive its input (stdin) from a file named STRINGS.TXT, you would enter the following command at the DOS prompt:
C:> PRINTIT <STRINGS.TXT
Notice that the name of the executable file always comes first. The less-than sign (<) tells DOS to take the strings contained in STRINGS.TXT and use them as input for the PRINTIT program.
The following example would redirect the program’s output to the prn device, usually the printer attached on LPT1:
C :> REDIR > PRN
Alternatively, you might want to redirect the program’s output to a file, as the following example shows:
C :> REDIR > REDIR.OUT
In this example, all output that would have normally appeared on-screen will be written to the file
REDIR.OUT.
Redirection of standard streams does not always have to occur at the operating system. You can redirect a standard stream from within your program by using the standard C library function named freopen(). For example, if you wanted to redirect the stdout standard stream within your program to a file named OUTPUT.TXT, you would implement the freopen() function as shown here:
... freopen(output.txt, w, stdout);
...
Now, every output statement (printf(), puts(), putch(), and so on) in your program will appear in the file OUTPUT.TXT.

What is a method?
Method is a way of doing something, especially a systematic way; implies an orderly logical arrangement (usually in steps).

What is the easiest searching method to use?
Just as qsort() was the easiest sorting method, because it is part of the standard library, bsearch() is the easiest searching method to use. If the given array is in the sorted order bsearch() is the best method.
Following is the prototype for bsearch():
void *bsearch(const void *key, const void *buf, size_t num, size_t size, int (*comp)(const void *, const void*));
Another simple searching method is a linear search. A linear search is not as fast as bsearch() for searching among a large number of items, but it is adequate for many purposes. A linear search might be the only method available, if the data isn’t sorted or can’t be accessed randomly. A linear search starts at the beginning and sequentially compares the key to each element in the data set.

Is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?
It’s easier for a C compiler to generate good code for pointers than for subscripts.

What is indirection?
If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value.

How are portions of a program disabled in demo versions?
If you are distributing a demo version of your program, the preprocessor can be used to enable or disable portions of your program. The following portion of code shows how this task is accomplished, using the preprocessor directives #if and #endif:
int save_document(char* doc_name)
{
#if DEMO_VERSION
printf(Sorry! You can’t save documents using the DEMO version of this programming);
return(0);
#endif
...
}

printf() Function
What is the output of printf("%d")?

1. When we write printf("%d",x); this means compiler will print the value of x. But as here, there is nothing after %d so compiler will show in output window garbage value.

2. When we use %d the compiler internally uses it to access the argument in the stack (argument stack). Ideally compiler determines the offset of the data variable depending on the format specification string. Now when we write printf("%d",a) then compiler first accesses the top most element in the argument stack of the printf which is %d and depending on the format string it calculated to offset to the actual data variable in the memory which is to be printed. Now when only %d will be present in the printf then compiler will calculate the correct offset (which will be the offset to access the integer variable) but as the actual data object is to be printed is not present at that memory location so it will print what ever will be the contents of that memory location.

3. Some compilers check the format string and will generate an error without the proper number and type of arguments for things like printf(...) and scanf(...).
malloc()

What is the difference between "calloc(...)" and "malloc(...)"?

1. calloc(...) allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size).

malloc(...) takes in only a single argument which is the memory required in bytes. malloc(...) allocated bytes of memory and not blocks of memory like calloc(...).

2. malloc(...) allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available.

calloc(...) allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space. calloc(...) calls malloc(...) in order to use the C++ _set_new_mode function to set the new handler mode.

printf() Function- What is the difference between "printf(...)" and "sprintf(...)"?

sprintf(...) writes data to the character array whereas printf(...) writes data to the standard output device.

Compilation How to reduce a final size of executable?

Size of the final executable can be reduced using dynamic linking for libraries.

Linked Lists -- Can you tell me how to check whether a linked list is circular?

Create two pointers, and set both to the start of the list. Update each as follows:
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular");
}
}

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.

"union" Data Type What is the output of the following program? Why?

#include
main() {
typedef union {
int a;
char b[10];
float c;
}
Union;

Union x,y = {100};
x.a = 50;
strcpy(x.b,"hello");
x.c = 21.50;
printf("Union x : %d %s %f n",x.a,x.b,x.c);
printf("Union y : %d %s %f n",y.a,y.b,y.c);
}

String Processing --- Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba.

void PrintPermu (char *sBegin, char* sRest) {
int iLoop;
char cTmp;
char cFLetter[1];
char *sNewBegin;
char *sCur;
int iLen;
static int iCount;

iLen = strlen(sRest);
if (iLen == 2) {
iCount++;
printf("%d: %s%s\n",iCount,sBegin,sRest);
iCount++;
printf("%d: %s%c%c\n",iCount,sBegin,sRest[1],sRest[0]);
return;
} else if (iLen == 1) {
iCount++;
printf("%d: %s%s\n", iCount, sBegin, sRest);
return;
} else {
// swap the first character of sRest with each of
// the remaining chars recursively call debug print
sCur = (char*)malloc(iLen);
sNewBegin = (char*)malloc(iLen);
for (iLoop = 0; iLoop < iLen; iLoop ++) {
strcpy(sCur, sRest);
strcpy(sNewBegin, sBegin);
cTmp = sCur[iLoop];
sCur[iLoop] = sCur[0];
sCur[0] = cTmp;
sprintf(cFLetter, "%c", sCur[0]);
strcat(sNewBegin, cFLetter);
debugprint(sNewBegin, sCur+1);
}
}
}

void main() {
char s[255];
char sIn[255];
printf("\nEnter a string:");
scanf("%s%*c",sIn);
memset(s,0,255);
PrintPermu(s, sIn);
}
Reply With Quote Quick reply to this message
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hindi Important Questions for SSC Unregistered Main Forum 2 10th April 2018 03:08 PM
Important Questions For MBA 1st Sem JNTU payal Online MBA Discussions 2 27th March 2018 01:38 PM
Important MBA Interview Questions kavita Online MBA Discussions 2 23rd March 2018 08:23 AM
MBA HR Related Questions kavita Online MBA Discussions 2 17th March 2018 04:47 PM
OU MBA 4 Sem Important Questions kavita Online MBA Discussions 2 8th March 2018 12:41 PM
Important Questions for RBI Grade B Exam Unregistered Main Forum 1 16th November 2016 11:04 AM
Important Questions For Engineering Physics RGPV Unregistered Main Forum 1 10th October 2016 12:55 PM
Important Questions For JEE Main Unregistered Main Forum 1 28th September 2016 08:47 AM
CA CPT important questions Unregistered Main Forum 0 20th July 2015 09:09 AM
Important Questions for IBPS Clerk Interview Unregistered Main Forum 0 18th July 2015 11:42 AM
Anna University Ge6151 Important Questions Unregistered Main Forum 0 17th July 2015 10:57 AM
Important Questions for UGC NET Commerce Unregistered Main Forum 0 16th July 2015 05:16 PM
OS Important Questions Anna University Unregistered Main Forum 0 14th July 2015 01:51 PM
CDS Exam Important Questions Unregistered Main Forum 0 10th July 2015 08:54 AM
Important Questions related to Java Basic with Answers Unregistered Main Forum 2 8th October 2014 02:14 PM
Important Questions related to C++ with Answers Unregistered Main Forum 1 8th October 2014 02:06 PM
Andhra Pradesh SSC important questions of all subjects? jagade9 Main Forum 7 31st March 2014 11:33 AM
MBA HR Questions and Answers Neha Majithian Main Forum 2 28th February 2014 04:02 PM
IIM Questions And Answers Karan Arjun Main Forum 1 29th March 2013 05:10 PM
MHT-CET Questions Answers ankitjain_ Main Forum 1 26th September 2012 10:35 AM


Quick Reply
Your Username: Click here to log in

Message:
Options




All times are GMT +5.5. The time now is 11:56 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0 PL2

1 2