The 'C' programming language was originally developed for and implemented on the UNIX operating system, on a DEC PDP-11 by Dennis Ritchie. One of the best features of C is that it is not tied to any particular hardware or system. This makes it easy for a user to write programs that will run without any changes on practically all machines. C is often called a middle-level computer language as it combines the elements of high-level languages with the functionalism of assembly language.
C allows the manipulation of bits, bytes and addresses- the basic elements with which the computer functions. Another good point about C is its portability which makes it possible to adapt software written for one type of computer to another. C was created, influenced, and field tested by working programmers. The end result is that C gives the programmer what the programmer wants. C offers the speed of assembly language and the extensibility of FORTH, but few of the restrictions of Pascal and Modula-2.
C Language Interviews are getting tough these days as the technology grows faster. To get through the C Language interview one needs to update him/herself in a regular manner. Having said that, just before the interview, it is very important to have a quick glance of the reputed C Language questions and answers to make yourself comfortable during the interview process. This is where DoAnswers.com helps you in renewing yourself on C Language and various other technologies interview preparation.
1. How can I access memory located at a certain address
Ans : Use a pointer to that address. 12 How can I read a directory in a C program? Like the following code illustrates: #include ?stdio.h? #include ?dirent.h? #include ?errno.h? int main() { DIR *pdir; struct dirent *pfile; if (!(pdir = opendir( ?MyDirectoryPath? ) ) ) { perror(?Can?t open this directory.?); return 1; } while( (pfile = readdir(pdir)) ) { if ( 0 == strcmp(pfile->d_name,?.?) ) continue; else if( 0 == strcmp(pfile->d_name,?..?) ) continue; else printf(?File : %s\n?, pfile->d_name ); } return 0; }
2. How can I call a function, given its name as a string?
Simple way (students) void foo( void ); void zoo( void ); void caller( const char * fname ) { if( strcmp(fname,?foo?) == 0 ) foo(); else if ( strcmp(fname,?zoo?) == 0 ) zoo(); else default; } Better Way: Use an XML schema that describes keywords associated with functions implemented as dynamic libraries. Use expat library to parse the elements and call functions. Correct Way: Use a lexer and compiler builder (lex,yacc) to create a set of tokens and handler functions. Good but time expensive way. Compile the function as a dll and call it during run time using
3. How can I convert integers to binary or hexadecimal?
In order to print them use appropriate format in printf. In order to assign them use specifiers like int x = FFx; x = 101110b; Integers are kept in the same format inside program?s heap/stack.
4. How can I increase the allowable number of simultaneously open files?
This is system depended. In some you cannot.
5. How can I invoke another program from within a C program?
Use in UNIX use the exec, execl, execv, execle, execve, execlp, execvp functions.
6. How can I return multiple values from a function?
return structure from function which can hold multiple variable
7. How do I access command-line arguments?
Ans : Declare your main() like: int main(int argc, char * argv[]) { } argc is the number of arguments stated in the command line. Use argv[0] to get the program (process) name Use argv[1],?, to argv[argc-1] to get the command lines
8. How do I know how many elements an array can hold?
The amount of memory an array can consume depends on the data type of an array. In DOS environment, the amount of memory an array can consume depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume more than 64 kb. Consider following program, which shows the maximum number of elements an array of type int, float and char can have in case of Small memory model. main( ) { int i[32767] ; float f[16383] ; char s[65535] ; }
9. How do you write a program which produces its own source code as its output?
#include #include using namespace std; int main() { char buf[100]; string str; int size=100; ifstream in(?filename.cpp?,ios::in); while(!in.eof()) { while(in.getline(buf,size)) { str=buf; cout
10. Program to counte the number of bits set to 1
int bits_set( int word ) { int tmp; tmp = (word >> 1) & 033333333333; tmp = word - tmp - ((tmp >> 1) & 033333333333); return (((tmp + (tmp >> 3)) & 030707070707) % 077); }