|
What is 1000Projects
'1000projects.com' is an educational content website dedicated to finding and realizing Final Year Projects, IEEE Projects, Engineering Projects, Science Fair Projects, Project Topics, Project Ideas, Major Projects, Mini Projects, Paper Presentations, Presentation Topics, IEEE Topics, .Net Projects, Java Projects, PHP Projects, VB Projects, SQL Projects, C & DS Projects, C++ Projects, Perl Projects, ASP Projects, Delphi Projects, HTML Projects, Cold Fusion Projects, Java Script Projects, Btech Projects, BE Projects, MCA Projects, Mtech Projects, MBA Projects, Project on Software, CBSE Projects, Testing Projects, Embedded Projects, Chemistry Projects, Electronics Projects, Electrical Projects, Science Projects, Mechanical Projects, Mba project Reports, Placement papers, Sample Resumes, Entrance Exams, Technical Faq's, Puzzles, etc
how it works?
Everything on this site is submitted by the students in this professional community. You Can submit your Projects, Project Topics & Ideas to info.1000projects{at}gmail.com after you submit your project/project Idea/Abstract/Seminar Topics, These are being verified and approved by our administrator. after approval of this project/project Idea/Abstract/Seminar Topics, It can be shown on 1000projects.com so that other users can read/discuss it.The entire content on this website is Only For Educational Purpose, Non Commercial use!
Please help us/Other Users by sending projects/project Ideas/Abstracts/Seminar Topics. Thanking You!!!!!
Category Articles
C Aptitude Questions and Answers 1
Added on Tue, Nov 24, 2009
s Predict the output or error(s) for the following: 1. void main() { int const * p=5;  ... Read More
C Aptitude Questions and Answers 2
Added on Tue, Nov 24, 2009
Predict the output or error(s) for the following: 1. struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=... Read More
Very Importent C Interview Questions
Added on Wed, Nov 11, 2009
1. what is the diffrence between gross profit& net proffit? capitaliq what is the journal?  ... Read More
C interview questions
Added on Wed, Nov 11, 2009
What does static variable mean? What is a pointer? What is a structure? What are the differences between structures and arrays? In header files whether functions... Read More
What do the ’c’ and ’ v ’ in argc and argv stand for? Explain their purpose?
Added on Mon, Nov 16, 2009
In C, we can supply arguments to ’main’ function. The arguments that we pass to main ( ) at command prompt are called command line arguments. These arguments are supplied at the time of invoking the program. The main ( ) function... Read More
Find the output of the C Program?
Added on Fri, Oct 23, 2009
2. main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); } Answer: mmmm aaaa nnnn Explanation: s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array... Read More
Programming in C _ A Tutorial
Added on Wed, Nov 11, 2009
Programming in C _ A Tutorial Brian W. Kernighan Bell Laboratories, Murray Hill, N. J. 1. Introduction. C is a computer language available on the GCOS and UNIX operating systems at Murray Hill and (in preliminary form) on OS/360 at Holmdel. C lets... Read More
What is difference between for loop and while loop in C language?
Added on Mon, Nov 16, 2009
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) { /... Read More
What are storage memory, default value, scope and life of Automatic and Register storage class?
Added on Mon, Nov 16, 2009
1. Automatic storage class: Storage : main memory. Default value : garbage value. Scope : local to the block in which the variable is defined. Lifetime : till control remains... Read More
C interview questions and answers - 13 programs with solutions
Added on Wed, Nov 11, 2009
C interview questions and answers What will print out? main() { char *p1=“name”; char *p2; ... Read More
Which bitwise operator is suitable for checking whether a particular bit is ON or OFF?
Added on Mon, Nov 16, 2009
Example: Suppose in byte that has a value 10101101 . We wish to check whether bit number 3 is ON (1) or OFF (0) . Since we want to check the bit number 3, the second operand for AND operation we choose is binary 00001000,... Read More
Are pointers really faster than arrays? How much do function calls slow things down? Is ++i faster than i = i + 1?
Added on Sun, Nov 15, 2009
Precise answers to these and many similar questions depend of course on the processor and compiler in use. If you simply must know, you’ll... Read More
What is the difference between an enumeration and a set of preprocessor #defines?
Added on Sat, Nov 14, 2009
At the present time, there is little difference. The C Standard says that enumerations may be freely intermixed with other integral types,... Read More
How can I set an array’s size at run time? How can I avoid fixed-sized arrays?
Added on Sat, Nov 14, 2009
The equivalence between arrays and pointers allows a pointer to malloc’ed memory to simulate an array quite effectively.... Read More
But what about the && and || operators? I see code like "while((c = getchar()) != EOF && c != ’n’)" ...
Added on Sat, Nov 14, 2009
There is a special "short-circuiting" exception for those operators. The right-hand side is not evaluated if the left- hand side determines the... Read More
Can’t a structure in C contain a pointer to itself?
Added on Wed, Nov 11, 2009
I can’t seem to define a linked list successfully. I tried typedef struct { ... Read More
This is strange. NULL is guaranteed to be 0, but the null pointer is not?
Added on Sat, Nov 14, 2009
When the term "null" or "NULL" is casually used, one of several things may be meant: 1. The conceptual null pointer, the... Read More
How do I... Use sockets? Do networking? Write client/server applications?
Added on Sun, Nov 15, 2009
All of these questions are outside of the scope of this list and have much more to do with the networking facilities which you have available than they do... Read More
I came across some "joke" code containing the "expression" 5["abcdef"] . How can this be legal C?
Added on Sat, Nov 14, 2009
Yes, Virginia, array subscripting is commutative in C. This curious fact follows from the pointer definition of array subscripting, namely that a[e]... Read More
What’s the best way to write a multi-statement macro?
Added on Sat, Nov 14, 2009
The usual goal is to write a macro that can be invoked as if it were a statement consisting of a single function call. This means that the "caller"... Read More
What are the different storage classes in C?
Added on Sun, Nov 15, 2009
C has three types of storage: automatic, static and allocated. Variable having block scope and without static specifier have automatic storage duration. Variables with block scope, and with static specifier have static scope. Global... Read More
What’s a good way to check for "close enough" floating-point equality?
Added on Sun, Nov 15, 2009
Since the absolute accuracy of floating point values varies, by definition, with their magnitude, the best way of comparing two floating point values is to... Read More
Where can I get a BNF or YACC grammar for C?
Added on Sun, Nov 15, 2009
The definitive grammar is of course the one in the ANSI standard;. Another grammar (along with one for C++) by Jim Roskind is in pub/c++grammar1.1... Read More
malloc() Function- What is the difference between "calloc(...)" and "malloc(...)"?
Added on Sun, Nov 15, 2009
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... Read More
Advantages of a macro over a function?
Added on Sun, Nov 15, 2009
Macro gets to see the Compilation environment, so it can expand __ __TIME__ __FILE__ #defines. It is expanded by the preprocessor. For example, you can’t do this without macros #define PRINT(EXPR) printf( #EXPR “=%d... Read More
What is Encapsulation?
Added on Sun, Nov 15, 2009
Encapsulation is binding of attributes and behaviors. Hiding the actual implementation and exposing the functionality of any object. Encapsulation is the first step towards OOPS, is the procedure of covering up of data and functions into a... Read More
1.What are the differences between malloc() and calloc()?
Added on Mon, Nov 16, 2009
Allocation of memory at the time of execution is called dynamic memory allocation. It is done using the standard library functions malloc() and calloc(). It is defined in "stdlib.h". malloc(): used to allocate required number of bytes in... Read More
What are the advantages of using unions?
Added on Mon, Nov 16, 2009
Union is a collection of data items of different data types. It can hold data of only one member at a time though it has members of different data types. If a union has two members of different data types, they are allocated the same memory.... Read More
1.How to swap two numbers using bitwise operators?
Added on Mon, Nov 16, 2009
Program: #include<stdio.h> main() { int i = 65; int k = 120; printf("
value of i=%d k=%d before swapping", i, k); i = i ^ k; k = i ^ k; i = i ^ k; printf("
value of i=%d k=%d after swapping", i, k); } Explanation: i = 65; binary equivalent of... Read More
But I heard that char a[] was identical to char *a.
Added on Sat, Nov 14, 2009
Not at all. (What you heard has to do with formal parameters to functions;) Arrays are not pointers. The array declaration char a[6]... Read More
How can I list all of the predefined identifiers?
Added on Sat, Nov 14, 2009
There’s no standard way, although it is a common need. gcc provides a -dM option which works with -E, and other compilers may provide something... Read More
What’s a good book for learning C?
Added on Sun, Nov 15, 2009
There are far too many books on C to list here; it’s impossible to rate them all. Many people believe that the best one was also the first:... Read More
How can I pass constant values to functions which accept structure arguments?
Added on Sat, Nov 14, 2009
As of this writing, C has no way of generating anonymous structure values. You will have to use a temporary structure variable or a little structure... Read More
How can I do graphics?
Added on Sun, Nov 15, 2009
Once upon a time, Unix had a fairly nice little set of device- independent plot functions described in plot(3) and plot(5). The GNU libplot package... Read More
Write a program to check whether a given number is a palindromic number.
Added on Mon, Nov 16, 2009
If a number, which when read in both forward and backward way is same, then such a number is called a palindrome number. Program: #include<stdio.h> main() { int n, n1, rev = 0, rem; printf("Enter any number: ... Read More
Find the output of the C Program1?
Added on Fri, Oct 23, 2009
1. void main() { int const * p=5; printf("%d",++(*p)); } Answer: Compiler error: Cannot modify a constant value. Explanation: p is a pointer to a "constant integer". But we tried to change the value of the "constant integer". Read More
This code, straight out of a book, isn’t compiling
Added on Sat, Nov 14, 2009
1.31: This code, straight out of a book, isn’t compiling: Read More
What’s the difference between these two declarations?
Added on Sat, Nov 14, 2009
Q : What’s the difference between these two declarations? struct x1 { ... }; ... Read More
Can I use explicit parentheses to force the order of evaluation I want? Even if I don’t, doesn’t precedence dictate it?
Added on Sat, Nov 14, 2009
Not in general. Operator precedence and explicit parentheses impose only a partial ordering on the evaluation of an expression. In the ... Read More
I have a char * pointer that happens to point to some ints, and
Added on Sat, Nov 14, 2009
Q: I have a char * pointer that happens to point to some ints, and Read More
If NULL were defined as follows: #define NULL ((char *)0)
Added on Sat, Nov 14, 2009
Q: If NULL were defined as follows: #define NULL ((char *)0) ... Read More
How much memory does a pointer variable allocate?
Added on Sat, Nov 14, 2009
That’s a pretty misleading question. When you declare a pointer variable, as in char *p;  ... Read More
Is there anything like an #ifdef for typedefs?
Added on Sat, Nov 14, 2009
Unfortunately, no. You may have to keep sets of preprocessor macros (e.g. MY_TYPE_DEFINED) recording whether certain typedefs ... Read More
How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they’re typed?
Added on Sun, Nov 15, 2009
Alas, there is no standard or portable way to do these things in C. Concepts such as screens and keyboards are not even mentioned in the Standard,... Read More
How can I allocate arrays or structures bigger than 64K?
Added on Sun, Nov 15, 2009
A reasonable computer ought to give you transparent access to all available memory. If you’re not so lucky, you’ll either have to rethink... Read More
How can I call FORTRAN (C++, BASIC, Pascal, Ada, LISP) functions from C?
Added on Sun, Nov 15, 2009
The answer is entirely dependent on the machine and the specific calling sequences of the various compilers in use, and may not be possible at all. ... Read More
Difference between const char* p and char const* p
Added on Sun, Nov 15, 2009
In const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location. in char const* p, the ptr ‘p’ is constant not... Read More
Which bit wise operator is suitable for turning OFF a particular bit in a number?
Added on Mon, Nov 16, 2009
Bitwise AND operator (&), one’s complement operator(~) Example: To unset the 4th bit of byte_data or to turn off a particular bit in a number. Explanation: consider, char byte_data= 0b00010111; byte_data= ... Read More
Write a program to swap two numbers without using a temporary variable. Swapping interchanges the values of two given variables.
Added on Mon, Nov 16, 2009
Swapping interchanges the values of two given variables. Logic: step1: x=x+y; step2: y=x-y; step3: x=x-y; Example: if x=7 and y=4 step1: x=7+4=11; step2: y=11-4=7; step3: x=11-7=4; Thus the values of the variables x and y are interchanged... Read More
Which one is equivalent to multiplying an unsigned int by 2, left shifting by 1 or right shifting by 1?
Added on Mon, Nov 16, 2009
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;... Read More
My compiler is leaving holes in structures, which is wasting
Added on Sat, Nov 14, 2009
Q: My compiler is leaving holes in structures, which is wasting Read More
If NULL and 0 are equivalent as null pointer constants, which should I use?
Added on Sat, Nov 14, 2009
Many programmers believe that NULL should be used in all pointer contexts, as a reminder that the value is to be thought of as a pointer. Others feel... Read More
I’m confused. I just can’t understand all this null pointer stuff.
Added on Sat, Nov 14, 2009
Here are two simple rules you can follow: 1. When you want a null pointer constant in source code,  ... Read More
I’m trying to update a file in place, by using fopen mode "rplus", reading a certain string, and writing back a modified string, but it’s not working.
Added on Sat, Nov 14, 2009
Be sure to call fseek before you write, both to seek back to the beginning of the string you’re trying to overwrite, and because an fseek or fflush... Read More
How can I get the current date or time of day in a C program?
Added on Sun, Nov 15, 2009
Just use the time(), ctime(), localtime() and/or strftime() functions. Here is a simple example: ... Read More
I had a frustrating problem which turned out to be caused by the line
Added on Sun, Nov 15, 2009
Q: I had a frustrating problem which turned out to be caused by the Read More
I have a varargs function which accepts a float parameter. Why isn"t
Added on Sun, Nov 15, 2009
Q: I have a varargs function which accepts a float parameter. Why Read More
I came across some code that puts a (void) cast before each call to printf(). Why?
Added on Sun, Nov 15, 2009
printf() does return a value, though few programs bother to check the return values from each call. Since some compilers (and lint) will warn about... Read More
What is static memory allocation and dynamic memory allocation?
Added on Sun, Nov 15, 2009
Static memory allocation: The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared... Read More
What is default Constructor?
Added on Mon, Nov 16, 2009
Constructor with no arguments or all the arguments has default values. In Above Question Test() is a default constructor Read More
Write a program to find the greatest among ten numbers.
Added on Mon, Nov 16, 2009
Program: #include<stdio.h> main() { int a[10]; int i; int greatest; printf("Enter ten values:"); //Store 10 numbers in an array for (i = 0; i < 10; i++) { scanf("%d", &a[i]); } //Assume that a[0] is greatest greatest = a[0]; ... Read More
What is the difference between these initializations?
Added on Sat, Nov 14, 2009
1.32: What is the difference between these initializations? Read More
I have a function which accepts, and is supposed to initialize,
Added on Sat, Nov 14, 2009
Q: I have a function which accepts, and is supposed to initialize, Read More
Why am I getting "warning: assignment of pointer from integer lacks a cast" for calls to malloc()?
Added on Sat, Nov 14, 2009
Have you #included <stdlib.h>, or otherwise arranged for malloc() to be declared properly? Read More
What does the message "warning: macro replacement within a string literal" mean?
Added on Sat, Nov 14, 2009
Some pre-ANSI compilers/preprocessors interpreted macro definitions like #define TRACE(var, fmt) printf(... Read More
What are # pragmas and what are they good for?
Added on Sat, Nov 14, 2009
The #pragma directive provides a single, well-defined "escape hatch" which can be used for all sorts of (nonportable) implementation-specific controls... Read More
How can I generate random numbers with a normal or Gaussian distribution?
Added on Sun, Nov 15, 2009
Here is one method, recommended by Knuth and due originally to Marsaglia: #include <stdlib.h> ... Read More
What’s a good way to implement complex numbers in C?
Added on Sun, Nov 15, 2009
It is straightforward to define a simple structure and some arithmetic functions to manipulate them. C9X will support ... Read More
I have a pre-ANSI compiler, without <stdarg.h>. What can I do?
Added on Sun, Nov 15, 2009
There’s an older header, <varargs.h>, which offers about the same functionality. Read More
When will the next International Obfuscated C Code Contest (IOCCC) be held? How can I get a copy of the current and previous winning entries?
Added on Sun, Nov 15, 2009
The contest is in a state of flux; see http://www.ioccc.org/index.html for current details. Contest winners are usually announced at a Usenix... Read More
Linked Lists -- Can you tell me how to check whether a linked list is circular?
Added on Sun, Nov 15, 2009
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 (... Read More
What does static variable mean?
Added on Sun, Nov 15, 2009
there are 3 main uses for the static. 1. If you declare within a function: It retains the value between function calls 2.If it is declared for a function name: By default function is extern..so it will be visible from other files if the... Read More
1.What is hashing?
Added on Sun, Nov 15, 2009
To hash means to grind up, and that’s essentially what hashing is all about. The heart of a hashing algorithm is a hash function that takes your nice, neat data and grinds it into some random-looking integer. The idea behind hashing is... Read More
Can include files be nested?
Added on Sun, Nov 15, 2009
Yes. Include files can be nested any number of times. As long as you use precautionary measures , you can avoid including the same file twice. In the past, nesting header files was seen as bad programming practice, because it complicates the... Read More
What is a null pointer?
Added on Sun, Nov 15, 2009
There are times when it’s necessary to have a pointer that doesn’t point to anything. The macro NULL, defined in , has a value that’s guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void*... Read More
What is a static function?
Added on Sun, Nov 15, 2009
A static function is a function whose scope is limited to the current source file. Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or... Read More
What are register variables? What are advantages of using register variables?
Added on Mon, Nov 16, 2009
Register variables are stored in the CPU registers. Its default value is a garbage value. Scope of a register variable is local to the block in which it is defined. Lifetime is till control remains within the block in which the register... Read More
Write a program to check whether a given number is a prime.
Added on Mon, Nov 16, 2009
A prime number is a natural number that has only one and itself as factors. Examples: 2, 3, 5, 7,… are prime numbers. Program: #include <stdio.h>main() { int n, i, c = 0; printf("Enter... Read More
Write a program to swap two numbers using a temporary variable.
Added on Mon, Nov 16, 2009
Swapping interchanges the values of two given variables. Logic: step1: temp=x; step2: x=y; step3: y=temp; Example if x=5 and y=8, consider a temporary variable temp. step1: temp=x=5; step2: x=y=8; step3: y=temp=5; Thus the values of the variables x... Read More
3. main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }
Added on Fri, Oct 23, 2009
3. main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); } Answer: I hate U 2 Explanation: For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the... Read More
What is NULL and how is it #defined?
Added on Sat, Nov 14, 2009
As a matter of style, many programmers prefer not to have unadorned 0’s scattered through their programs. Therefore, the preprocessor macro... Read More
use the preprocessor macro #define Nullptr(type) (type *)0
Added on Sat, Nov 14, 2009
Q: I use the preprocessor macro #define Nullptr(type) (type *)0 ... Read More
I had the definition char a[6] in one source file, and in another I declared extern char *a. Why didn’t it work?
Added on Sat, Nov 14, 2009
In one source file you defind an array of characters and in the other you declared a pointer to characters. The declaration extern char *a simply... Read More
Since array references decay into pointers, if arr is an array, what’s the difference between arr and &arr?
Added on Sat, Nov 14, 2009
The type. In Standard C, &arr yields a pointer, of type pointer-to-array- of-T, to the entire array. (In pre-ANSI C, the & in &... Read More
When I call malloc() to allocate memory for a pointer which is local to a function, do I have to explicitly free() it?
Added on Sat, Nov 14, 2009
Yes. Remember that a pointer is different from what it points to. Local variables are deallocated when the function returns, but in the case of... Read More
I have a program which mallocs and later frees a lot of memory, but I can see from the operating system that memory usage doesn’t actually go back down.
Added on Sat, Nov 14, 2009
Most implementations of malloc/free do not return freed memory to the operating system, but merely make it available for future malloc() calls within the... Read More
What’s the difference between#include <> and #include "" ?
Added on Sat, Nov 14, 2009
The <> syntax is typically used with Standard or system-supplied headers, while "" is typically used for a program’s own header ... Read More
How can I construct preprocessor #if expressions which compare strings?
Added on Sat, Nov 14, 2009
You can’t do it directly; preprocessor #if arithmetic uses only integers. An alternative is to #define several macros with symbolic names... Read More
Does the sizeof operator work in preprocessor #if directives?
Added on Sat, Nov 14, 2009
No. Preprocessing happens during an earlier phase of compilation, before type names have been parsed. Instead of sizeof, consider using the... Read More
How can I use a preprocessor #if expression to tell if a machine is big-endian or little-endian?
Added on Sat, Nov 14, 2009
You probably can’t. (Preprocessor arithmetic uses only long integers, and there is no concept of addressing.) Are you sure you need... Read More
Can you mix old-style and new-style function syntax?
Added on Sat, Nov 14, 2009
Doing so is legal, but requires a certain amount of care. Modern practice, however, is to use the prototyped form in both declarations and definitions. ... Read More
I’m getting strange syntax errors inside lines I’ve # ifdeffed out.
Added on Sat, Nov 14, 2009
Under ANSI C, the text inside a "turned off" #if, #ifdef, or #ifndef must still consist of "valid preprocessing tokens." This means that the characters... Read More
What does # pragma once mean? I found it in some header files.
Added on Sat, Nov 14, 2009
It is an extension implemented by some preprocessors to help make header files idempotent; it is equivalent to the #ifndef ... Read More
People keep saying that the behavior of i = i++ is undefined, but I just tried it on an ANSI-conforming compiler, and got the results I expected.
Added on Sat, Nov 14, 2009
A compiler may do anything it likes when faced with undefined behavior (and, within limits, with implementation-defined and unspecified behavior),... Read More
Why doesn’t the call scanf("%d", i) work?
Added on Sat, Nov 14, 2009
The arguments you pass to scanf() must always be pointers. To fix the fragment above, change it to scanf("%d", &i) . Read More
Why does everyone say not to use scanf()? What should I use instead?
Added on Sat, Nov 14, 2009
scanf() has a number of problems -- More generally, scanf() is designed for relatively structured, formatted input (its name is in fact derived... Read More
Why does everyone say not to use gets()?
Added on Sat, Nov 14, 2009
Unlike fgets(), gets() cannot be told the size of the buffer it’s to read into, so it cannot be prevented from overflowing  ... Read More
I’m trying to sort an array of strings with qsort(), using strcmp() as the comparison function, but it’s not working.
Added on Sun, Nov 15, 2009
By "array of strings" you probably mean "array of pointers to char." The arguments to qsort’s comparison function are pointers to the objects... Read More
What does it mean when the linker says that _end is undefined?
Added on Sun, Nov 15, 2009
That message is a quirk of the old Unix linkers. You get an error about _end being undefined only when other symbols are undefined, too -- fix the... Read More
I heard that you have to # include <stdio.h> before calling printf(). Why?
Added on Sun, Nov 15, 2009
So that a proper prototype for printf() will be in scope. A compiler may use a different calling sequence for functions which accept... Read More
Where are some collections of useful code fragments and examples?
Added on Sun, Nov 15, 2009
Bob Stout’s popular "SNIPPETS" collection is available from ftp.brokersys.com in directory pub/snippets or on the web at http://www.brokersys... Read More
Why can’t I open a file by its explicit path? The call
Added on Sun, Nov 15, 2009
Q: Why can’t I open a file by its explicit path? The call Read More
How can I read in an object file and jump to locations in it?
Added on Sun, Nov 15, 2009
You want a dynamic linker or loader. It may be possible to malloc some space and read in object files, but you have to know an awful lot about object... Read More
Is C a great language, or what? Where else could you write something like a+++++b ?
Added on Sun, Nov 15, 2009
Q: Is C a great language, or what? Where else could you write something like a+++++b ? A: Well, you can’t meaningfully write... Read More
Is C++ a superset of C? Can I use a C++ compiler to compile C code?
Added on Sun, Nov 15, 2009
C++ was derived from C, and is largely based on it, but there are some legal C constructs which are not legal C++. Conversely, ANSI C inherited several... Read More
How can I find the day of the week given the date?
Added on Sun, Nov 15, 2009
Use mktime() or localtime() beware of DST adjustments if tm_hour is 0), or Zeller’s congruence (see the sci.math FAQ list), or this... Read More
"union" Data Type What is the output of the following program? Why?
Added on Sun, Nov 15, 2009
#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);... Read More
What is the difference between strings and character arrays?
Added on Sun, Nov 15, 2009
A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicity specified by using the static keyword. Actually, a string is a character array with following properties: * the... Read More
What is modular programming?
Added on Sun, Nov 15, 2009
If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming. Read More
What is the difference between declaring a variable and defining a variable?
Added on Sun, Nov 15, 2009
Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined. ... Read More
What is an argument? Differentiate between formal arguments and actual arguments?
Added on Sun, Nov 15, 2009
An argument is an entity used to pass the data from calling function to the called function. Formal arguments are the arguments available in the function definition. They are preceded by their own data types. Actual arguments are available in the... Read More
What are advantages and disadvantages of external storage class?
Added on Sun, Nov 15, 2009
Advantages of external storage class 1)Persistent storage of a variable retains the latest value 2)The value is globally available Disadvantages of external storage class 1)The storage for an external variable exists even when the variable is... Read More
What is a pointer variable?
Added on Sun, Nov 15, 2009
A pointer variable is a variable that may contain the address of another variable or any valid address in the memory. Read More
What is friend function?
Added on Mon, Nov 16, 2009
As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition. Read More
What do you mean by pure virtual functions?
Added on Mon, Nov 16, 2009
A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero. class Shape { public: virtual... Read More
What is scope & storage allocation of static, local and register variables? Explain with an example.
Added on Mon, Nov 16, 2009
Register variables: belong to the register storage class and are stored in the CPU registers. The scope of the register variables is local to the block in which the variables are defined. The variables which are used for... Read More
Write a program to find the greatest of three numbers.
Added on Mon, Nov 16, 2009
Program: #include<stdio.h> main() { int a, b, c; printf("Enter a,b,c: ... Read More
What does extern mean in a function declaration?
Added on Wed, Nov 11, 2009
It can be used as a stylistic hint to indicate that the function’s definition is probably in another source file, but there is no formal difference between ... Read More
What’s the best way of implementing opaque (abstract) data types in c?
Added on Sat, Nov 14, 2009
One good way is for clients to use structure pointers (perhaps additionally hidden behind typedefs) which point to structure types which are not... Read More
So given a[i] = i++;
Added on Sat, Nov 14, 2009
If I’m not using the value of the expression, should I use i++ or ++i to increment a variable?
Added on Sat, Nov 14, 2009
Since the two forms differ only in the value yielded, they are entirely equivalent when only their side effect is needed. (However, the prefix form is... Read More
I see code like char *p = malloc(strlen(s) + 1);
Added on Sat, Nov 14, 2009
Q: I see code like char *p = malloc(strlen(s) + 1); ... Read More
Is it legal to pass a null pointer as the first argument to realloc()? Why would you want to?
Added on Sat, Nov 14, 2009
ANSI C sanctions this usage (and the related realloc(..., 0), which frees), although several earlier implementations do not support it, so it may not be... Read More
Is it acceptable for one header file to #include another?
Added on Sat, Nov 14, 2009
It’s a question of style, and thus receives considerable debate. Many people believe that "nested #include files" are to be avoided: the... Read More
I seem to be missing the system header file <sgtty.h>. Can someone send me a copy?
Added on Sat, Nov 14, 2009
Standard headers exist in part so that definitions appropriate to your compiler, operating system, and processor can be supplied. You cannot just... Read More
Can I use an #ifdef in a #define line, to define something two different ways?
Added on Sat, Nov 14, 2009
No. You can’t "run the preprocessor on itself," so to speak. What you can do is use one of two completely separate #define lines, depending... Read More
I inherited some code which contains far too many #ifdef’s for my taste.
Added on Sat, Nov 14, 2009
Q: I inherited some code which contains far too many #ifdef’s for my taste. How can I preprocess the code to leave only one ... Read More
I believe that declaring void main() can’t fail, since I’m calling exit() instead of returning, and anyway my operating system ignores a program’s exit/return status.
Added on Sat, Nov 14, 2009
It doesn’t matter whether main() returns or not, or whether anyone looks at the status; the problem is that when main() is misdeclared, its caller ... Read More
Is char a[3] = "abc"; legal? What does it mean?
Added on Sat, Nov 14, 2009
It is legal in ANSI C (and perhaps in a few pre-ANSI systems), though useful only in rare circumstances. It declares an array of size three,... Read More
Why does the ANSI Standard not guarantee more than six case- insensitive characters of external identifier significance?
Added on Sat, Nov 14, 2009
The problem is older linkers which are under the control of neither the ANSI/ISO Standard nor the C compiler developers on the systems which have them.... Read More
Why won’t the Frobozz Magic C Compiler, which claims to be ANSI compliant, accept this code? I know that the code is ANSI, because gcc accepts it.
Added on Sat, Nov 14, 2009
Many compilers support a few non-Standard extensions, gcc more so than most. Are you sure that the code being rejected doesn’t rely on such an... Read More
How can I print a ’%’ character in a printf format string? I tried %, but it didn’t work.
Added on Sat, Nov 14, 2009
Simply double the percent sign: %% . % can’t work, because the backslash is the *compiler’s* escape character, while here our... Read More
What are fgetpos() and fsetpos() good for? What are fgetpos() and fsetpos() good for?
Added on Sat, Nov 14, 2009
ftell() and fseek() use type long int to represent offsets (positions) in a file, and may therefore be limited to offsets of about 2 billion (2**31-1).... Read More
I need some code to do regular expression and wildcard matching.
Added on Sun, Nov 15, 2009
Make sure you recognize the difference between classic regular expressions (variants of which are used in such Unix utilities as ed and grep), and filename... Read More
Each time I run my program, I get the same sequence of numbers back from rand().
Added on Sun, Nov 15, 2009
You can call srand() to seed the pseudo-random number generator with a truly random initial value. Popular seed values are the time of day, or the... Read More
I’m trying to do some simple trig, and I am # including <math.h>, but I keep getting "undefined: sin" compilation errors.
Added on Sun, Nov 15, 2009
Make sure you’re actually linking with the math library. For instance, under Unix, you usually need to use the -lm option, at ... Read More
How can I write a function that takes a format string and a variable number of arguments, like printf(), and passes them to printf() to do most of the work?
Added on Sun, Nov 15, 2009
Use vprintf(), vfprintf(), or vsprintf(). Here is an error() function which prints an error message, preceded by the string "error: " and... Read More
Where can I get an ANSI-compatible lint?
Added on Sun, Nov 15, 2009
Products called PC-Lint and FlexeLint (in "shrouded source form," for compilation on ’most any system) are available from ... Read More
Here’s a good puzzle: how do you write a program which produces its own source code as output?
Added on Sun, Nov 15, 2009
It is actually quite difficult to write a self-reproducing program that is truly portable, due particularly to quoting and character set difficulties. ... Read More
Can a variable be both const and volatile?
Added on Sun, Nov 15, 2009
Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, in the example in FAQ 8, the timer structure was accessed through... Read More
When should a far pointer be used?
Added on Sun, Nov 15, 2009
Sometimes you can get away with using a small memory model in most of a given program. There might be just a few things that don’t fit in your small data and code segments. When that happens, you can use explicit far pointers and function... Read More
What is an lvalue?
Added on Sun, Nov 15, 2009
An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. Each assignment statement must have... Read More
What is Operator overloading ?
Added on Sun, Nov 15, 2009
When an operator is overloaded, it takes on an additional meaning relative to a certain class. But it can still retain all of its old meanings. Examples: 1) The operators >> and << may be used for I/O operations because in the header,... Read More
What is the difference between goto and longjmp() and setjmp()?
Added on Sun, Nov 15, 2009
A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. Generally, a jump in execution of any kind should be avoided because it is not... Read More
What is a class?
Added on Sun, Nov 15, 2009
Class is concrete representation of an entity. It represents a group of objects, which hold similar attributes and behavior. It provides Abstraction and Encapsulations. Classes are generally declared using the keyword class. Read More
What is an Object? What is Object Oriented Programming?
Added on Sun, Nov 15, 2009
Object represents/resembles a Physical/real entity. An object is simply something you can give a name. Object Oriented Programming is a Style of programming that represents a program as a system of objects and enables code-reuse. Read More
What is a nested class? Why can it be useful?
Added on Mon, Nov 16, 2009
A nested class is a class enclosed within the scope of another class. For example: // Example 1: Nested class // class Outer Class {class Nested Class {// …}; //... Read More
What is preprocessor?
Added on Mon, Nov 16, 2009
The preprocessor is used to modify your program according to the preprocessor directives in your source code. Preprocessor directives (such as #define) give the preprocessor specific instructions on how to modify your source code. The... Read More
What is the difference between overloading and overriding?
Added on Mon, Nov 16, 2009
Overloading - Two functions having same name and return Type, but with different type and/or number of arguments. Overriding - When a function of base class is re-defined in the derived class. Read More
C++ Memory Management operators
Added on Mon, Nov 16, 2009
The concept of arrays has a block of memory reserved. The disadvantage with the concept of arrays is that the programmer must know, while programming, the size of memory to be allocated in addition to the array size remaining constant... Read More
What is the difference between parameter and arguments?
Added on Mon, Nov 16, 2009
Parameter –While defining method, variable passed in the method are called parameters. Arguments-While using those methods value passed to those variables are called arguments. Read More
What does a static variable mean?
Added on Mon, Nov 16, 2009
A static variable is a special variable that is stored in the data segment unlike the default automatic variable that is stored in stack. A static variable can be initialised by using keyword static before variable name. For Example: ... Read More
What is an Enumeration?
Added on Mon, Nov 16, 2009
Enumeration is a data type. We can create our own data type and define values that the variable can take. This can help in making program more readable. enum definition is similar to that of a structure. Example: consider... Read More
What are the advantages of using pointers in a program?
Added on Mon, Nov 16, 2009
Pointers are special variables which store address of some other variables. Syntax: datatype *ptr; Here * indicates that ptr is a pointer variable which represents value stored at a particular address. ... Read More
What are header files? Are functions declared or defined in header files?
Added on Mon, Nov 16, 2009
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... Read More
What is the difference b/n array and pointer?
Added on Thu, Nov 19, 2009
Pointer –pointer is a variables that hold the address of another variable .It is used to manipulate data using the address, pointer use the * operator to access the data pointed by them. Array –array use... Read More
How do you decide which integer type to use?
Added on Wed, Nov 11, 2009
If you might need large values (above 32,767 or below -32,767), use long. Otherwise, if space is very important (i.e. if there are... Read More
What should the 64-bit type on a machine that can support it?
Added on Wed, Nov 11, 2009
The forthcoming revision to the C Standard (C9X) specifies type long long as effectively being at least 64 bits, and this type has been implemented by a number of... Read More
What’s the best way to declare and define global variables and functions?
Added on Wed, Nov 11, 2009
First, though there can be many "declarations" (and in many translation units) of a single "global" (strictly speaking, ... Read More
Why does sizeof report a larger size than I expect for a structure type, as if there were padding at the end?
Added on Sat, Nov 14, 2009
Structures may have this padding (as well as internal padding), if necessary, to ensure that alignment properties will be preserved when an array of... Read More
This program works correctly, but it dumps core after it finishes. Why?
Added on Sat, Nov 14, 2009
Q: This program works correctly, but it dumps core after it Read More
Can I initialize unions?
Added on Sat, Nov 14, 2009
The current C Standard allows an initializer for the first-named member of a union. C9X will introduce "designated initializers" which can be used to... Read More
Is there an easy way to print enumeration values symbolically?
Added on Sat, Nov 14, 2009
No. You can write a little function to map an enumeration constant to a string. (For debugging purposes, a good debugger should automatically... Read More
How can I understand these complex expressions? What’s a "sequence point"?
Added on Sat, Nov 14, 2009
A sequence point is a point in time (at the end of the evaluation of a full expression, or at the ||, &&, ?:, or comma operators, or just before a... Read More
I have a complicated expression which I have to assign to one o
Added on Sat, Nov 14, 2009
Q: I have a complicated expression which I have to assign to one of Read More
I’m trying to declare a pointer and allocate some space for it,
Added on Sat, Nov 14, 2009
Q: I’m trying to declare a pointer and allocate some space for it, Read More
Does *p++ increment p, or what it points to?
Added on Sat, Nov 14, 2009
Postfix ++ essentially has higher precedence than the prefix unary operators. Therefore, *p++ is equivalent to *(p++); it increments p, and... Read More
Can I use a void ** pointer as a parameter so that a function can accept a generic pointer by reference?
Added on Sat, Nov 14, 2009
Not portably. There is no generic pointer-to-pointer type in C. void * acts as a generic pointer only because conversions are applied... Read More
How should NULL be defined on a machine which uses a nonzero bit pattern as the internal representation of a null pointer?
Added on Sat, Nov 14, 2009
The same as on any other machine: as 0 (or some version of 0; Whenever a programmer requests a null pointer, either by writing ... Read More
But wouldn’t it be better to use NULL (rather than 0), in case the value of NULL changes, perhaps on a machine with nonzero internal null pointers?
Added on Sat, Nov 14, 2009
No. (Using NULL may be preferable, but not for this reason.) Although symbolic constants are often used in place of numbers because the numbers might... Read More
Given all the confusion surrounding null pointers, wouldn’t it be easier simply to require them to be represented internally by zeroes?
Added on Sat, Nov 14, 2009
If for no other reason, doing so would be ill-advised because it would unnecessarily constrain implementations which would otherwise naturally represent... Read More
What does a run-time "null pointer assignment" error mean? How can I track it down?
Added on Sat, Nov 14, 2009
This message, which typically occurs with MS-DOS compilers, means that you’ve written, via a null (perhaps because uninitialized) pointer, to an... Read More
Someone explained to me that arrays were really just constant pointers.
Added on Sat, Nov 14, 2009
This is a bit of an oversimplification. An array name is "constant" in that it cannot be assigned to, but an array is *not* a pointer, as the... Read More
How can I declare local arrays of a size matching a passed-in array?
Added on Sat, Nov 14, 2009
Until recently, you couldn’t. Array dimensions in C traditionally had to be compile-time constants. C9X will introduce variable-length... Read More
So what’s the right way to return a string or other aggregate?
Added on Sat, Nov 14, 2009
The returned pointer should be to a statically-allocated buffer, or to a buffer passed in by the caller, or to memory obtained with malloc(), but *not* to... Read More
Why does some code carefully cast the values returned by malloc to the pointer type being allocated?
Added on Sat, Nov 14, 2009
Before ANSI/ISO Standard C introduced the void * generic pointer type, these casts were typically required to silence warnings (and perhaps induce... Read More
I’m allocating a large array for some numeric work, using the line
Added on Sat, Nov 14, 2009
Q: I’m allocating a large array for some numeric work, using the Read More
I’ve got 8 meg of memory in my PC. Why can I only seem to malloc 640K or so?
Added on Sat, Nov 14, 2009
Under the segmented architecture of PC compatibles, it can be difficult to use more than 640K with any degree of transparency, ... Read More
You can’t use dynamically-allocated memory after you free it, can you?
Added on Sat, Nov 14, 2009
No. Some early documentation for malloc() stated that the contents of freed memory were "left undisturbed," but this ill- advised guarantee was never... Read More
Why isn’t a pointer null after calling free()? How unsafe is it to use (assign, compare) a pointer value after it’s been freed?
Added on Sat, Nov 14, 2009
When you call free(), the memory pointed to by the passed pointer is freed, but the value of the pointer in the caller probably remains unchanged, because... Read More
How does free() know how many bytes to free?
Added on Sat, Nov 14, 2009
The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. Read More
What is the right type to use for Boolean values in C? Why isn’t it a standard type? Should I use #defines or enums for the true and false values?
Added on Sat, Nov 14, 2009
C does not provide a standard Boolean type, in part because picking one involves a space/time tradeoff which can best be decided by the programmer.... Read More
What are the complete rules for header file searching?
Added on Sat, Nov 14, 2009
The exact behavior is implementation-defined (which means that it is supposed to be documented;. Typically, headers named with <> syntax are searched... Read More
How can I write a macro which takes a variable number of arguments?
Added on Sat, Nov 14, 2009
One popular trick is to define and invoke the macro with a single, parenthesized "argument" which in the macro expansion becomes the entire argument list,... Read More
What is the "ANSI C Standard?"
Added on Sat, Nov 14, 2009
In 1983, the American National Standards Institute (ANSI) commissioned a committee, X3J11, to standardize the C language. After a long, arduous process,... Read More
Why does the declaration extern int f(struct x *p);
Added on Sat, Nov 14, 2009
Q: Why does the declaration extern int f(struct x *p); give me an obscure warning... Read More
But what about main’s third argument, envp?
Added on Sat, Nov 14, 2009
It’s a non-standard (though common) extension. If you really need to access the environment in ways beyond what the standard getenv() function... Read More
Is exit(status) truly equivalent to returning the same status from main()?
Added on Sat, Nov 14, 2009
Yes and no. The Standard says that they are equivalent. However, a return from main() cannot be expected to work if data local to main() might be... Read More
Why can’t I perform arithmetic on a void * pointer?
Added on Sat, Nov 14, 2009
The compiler doesn’t know the size of the pointed-to objects. Before performing arithmetic, convert the pointer either to ... Read More
Why are some ANSI/ISO Standard library functions showing up as undefined, even though I’ve got an ANSI compiler?
Added on Sat, Nov 14, 2009
It’s possible to have a compiler available which accepts ANSI syntax, but not to have ANSI-compatible header files or run-time libraries installed.... Read More
Does anyone have a tool for converting old-style C programs to ANSI C, or vice versa, or for automatically generating prototypes?
Added on Sat, Nov 14, 2009
Two programs, protoize and unprotoize, convert back and forth between prototyped and "old style" function definitions and declarations. (These... Read More
My program’s prompts and intermediate output don’t always show up on the screen, especially when I pipe the output through another program.
Added on Sat, Nov 14, 2009
It’s best to use an explicit fflush(stdout) whenever output should definitely be visible (and especially if the text does not end with ... Read More
I’m reading a number with scanf %d and then a string with gets(), but the compiler seems to be skipping the call to gets()!
Added on Sat, Nov 14, 2009
scanf %d won’t consume a trailing newline. If the input number is immediately followed by a newline, that newline will immediately satisfy the... Read More
Why does errno contain ENOTTY after a call to printf()?
Added on Sat, Nov 14, 2009
Many implementations of the stdio package adjust their behavior slightly if stdout is a terminal. To make the determination, these implementations... Read More
How can I flush pending input so that a user’s typeahead isn’t read at the next prompt? Will fflush(stdin) work?
Added on Sat, Nov 14, 2009
fflush() is defined only for output streams. Since its definition of "flush" is to complete the writing of buffered characters (not to discard them),... Read More
Once I’ve used freopen(), how can I get the original stdout (or stdin) back?
Added on Sat, Nov 14, 2009
There isn’t a good way. If you need to switch back, the best solution is not to have used freopen() in the first place. Try using your... Read More
How can I convert numbers to strings (the opposite of atoi)? Is there an itoa() function?
Added on Sat, Nov 14, 2009
Just use sprintf(). (Don’t worry that sprintf() may be overkill, potentially wasting run time or code space; it works well in practice.) ... Read More
Why do some versions of toupper() act strangely if given an upper-case letter? Why does some code call islower() before toupper()?
Added on Sun, Nov 15, 2009
Older versions of toupper() and tolower() did not always work correctly on arguments which did not need converting (i.e. on digits or punctuation or... Read More
When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
Added on Sun, Nov 15, 2009
Most computers use base 2 for floating-point numbers as well as for integers. In base 2, one divided by ten is an infinitely- repeating fraction (0... Read More
Why doesn’t C have an exponentiation operator?
Added on Sun, Nov 15, 2009
Because few processors have an exponentiation instruction. C has a pow() function, declared in <math.h>, although explicit multiplication is usually... Read More
How do I test for IEEE NaN and other special values?
Added on Sun, Nov 15, 2009
Many systems with high-quality IEEE floating-point implementations provide facilities (e.g. predefined constants, and functions like isnan(), either as... Read More
I’m looking for some code to do:
Added on Sun, Nov 15, 2009
Q: I’m looking for some code to do: Read More
How can I write a function that takes a variable number of arguments?
Added on Sun, Nov 15, 2009
Use the facilities of the <stdarg.h> header. Here is a function which concatenates an arbitrary number of strings into malloc’ed... Read More
How can I write a function analogous to scanf(), that calls scanf() to do most of the work?
Added on Sun, Nov 15, 2009
C9X will support vscanf(), vfscanf(), and vsscanf(). (Until then, you may be on your own.) Read More
I’m getting baffling syntax errors which make no sense at all, and it seems like large chunks of my program aren’t being compiled.
Added on Sun, Nov 15, 2009
Check for unclosed comments or mismatched #if/#ifdef/#ifndef/ #else/#endif directives; remember to check header files, too. Read More
What’s the best style for code layout in C?
Added on Sun, Nov 15, 2009
K&R, while providing the example most often copied, also supply a good excuse for disregarding it: The... Read More
Why do some people write if(0 == x) instead of if(x == 0)?
Added on Sun, Nov 15, 2009
It’s a trick to guard against the common error of writing if(x = 0) If you’re in the... Read More
Where can I get the "Indian Hill Style Guide" and other coding standards?
Added on Sun, Nov 15, 2009
Various documents are available for anonymous ftp from: Site: File or... Read More
What’s a free or cheap C compiler I can use?
Added on Sun, Nov 15, 2009
A popular and high-quality free C compiler is the FSF’s GNU C compiler, or gcc. It is available by anonymous ftp from prep.ai.mit.edu in... Read More
How can I shut off the "warning: possible pointer alignment problem" message which lint gives me for each call to malloc()?
Added on Sun, Nov 15, 2009
The problem is that traditional versions of lint do not know, and cannot be told, that malloc() "returns a pointer to space suitably aligned for storage of... Read More
Don’t ANSI function prototypes render lint obsolete?
Added on Sun, Nov 15, 2009
No. First of all, prototypes work only if they are present and correct; an inadvertently incorrect prototype is worse than useless. Secondly,... Read More
Where and how can I get copies of all these freely distributable programs?s
Added on Sun, Nov 15, 2009
As the number of available programs, the number of publicly accessible archive sites, and the number of people trying to access them all grow, this... Read More
How can I do serial ("comm") port I/O?
Added on Sun, Nov 15, 2009
It’s system-dependent. Under Unix, you typically open, read, and write a device file in /dev, and use the facilities of the terminal driver to... Read More
How can I direct output to the printer?
Added on Sun, Nov 15, 2009
Under Unix, either use popen()to write to the lp or lpr program, or perhaps open a special file like /dev/lp. Under MS-DOS, write to the (nonstandard... Read More
How can I recover the file name given an open stream or file descriptor?
Added on Sun, Nov 15, 2009
This problem is, in general, insoluble. Under Unix, for instance, a scan of the entire disk (perhaps involving special permissions) would... Read More
How can I delete a file?
Added on Sun, Nov 15, 2009
Either use system() to invoke your operating system’s copy utility or open the source and destination files (using fopen() or some lower-level file... Read More
How can I find out how much memory is available?
Added on Sun, Nov 15, 2009
Your operating system may provide a routine which returns this information, but it’s quite system-dependent. Read More
How can a process change an environment variable in its caller?
Added on Sun, Nov 15, 2009
It may or may not be possible to do so at all. Different operating systems implement global name/value functionality similar to the Unix environment... Read More
How can I convert integers to binary or hexadecimal?
Added on Sun, Nov 15, 2009
Make sure you really know what you’re asking. Integers are stored internally in binary, although for most purposes it is not incorrect to think... Read More
Is there a way to switch on strings?
Added on Sun, Nov 15, 2009
Not directly. Sometimes, it’s appropriate to use a separate function to map strings to integer codes, and then switch on those. Otherwise... Read More
Why don’t C comments nest? How am I supposed to comment out code containing comments? Are comments legal inside quoted strings?
Added on Sun, Nov 15, 2009
C comments don’t nest mostly because PL/I’s comments, which C’s are borrowed from, don’t either. Therefore, it is usually ... Read More
Why doesn’t C have nested functions?
Added on Sun, Nov 15, 2009
It’s not trivial to implement nested functions such that they have the proper access to local variables in the containing function(s), so they were... Read More
Does anyone know of a program for converting Pascal or FORTRAN (or LISP, Ada, awk, "Old" C, ...) to C?
Added on Sun, Nov 15, 2009
Several freely distributable programs are available: p2c A Pascal to C converter written by Dave Gillespie, ... Read More
Will 2000 be a leap year? Is (year % 4 == 0) an accurate test for leap years?
Added on Sun, Nov 15, 2009
Yes and no, respectively. The full expression for the present Gregorian calendar is year % 4 == 0 &... Read More
What is "Duff’s Device"?
Added on Sun, Nov 15, 2009
It’s a devastatingly deviously unrolled byte-copying loop, devised by Tom Duff while he was at Lucasfilm. In its "classic" form, it looks like:... Read More
What was the entry keyword mentioned in K&R1?
Added on Sun, Nov 15, 2009
It was reserved to allow the possibility of having functions with multiple, differently-named entry points, a la FORTRAN. It was not, to anyone&rsquo... Read More
How do you pronounce "char"?
Added on Sun, Nov 15, 2009
You can pronounce the C keyword "char" in at least three ways: like the English words "char," "care," or "car" (or maybe even "character"); the choice is... Read More
Where can I get extra copies of this list? What about back issues?
Added on Sun, Nov 15, 2009
An up-to-date copy may be obtained from ftp.eskimo.com in directory u/s/scs/C-faq/. You can also just pull it off the net; it is normally posted to... Read More
printf() Function What is the output of printf("%d")?
Added on Sun, Nov 15, 2009
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... Read More
printf() Function- What is the difference between "printf(...)" and "sprintf(...)"?
Added on Sun, Nov 15, 2009
sprintf(...) writes data to the character array whereas printf(...) writes data to the standard output device. Read More
What is the difference between text and binary modes?
Added on Sun, Nov 15, 2009
Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline n character and... Read More
Difference between arrays and pointers?
Added on Sun, Nov 15, 2009
- Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them - Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer... Read More
What is indirection?
Added on Sun, Nov 15, 2009
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. Read More
When should a type cast not be used?
Added on Sun, Nov 15, 2009
A type cast should not be used to override a const or volatile declaration. Overriding these type modifiers can cause the program to fail to run correctly. A type cast should not be used to turn a pointer to one type of structure or data type into... Read More
When is a switch statement better than multiple if statements?
Added on Sun, Nov 15, 2009
A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type. Read More
What is a modulus operator? What are the restrictions of a modulus operator?
Added on Sun, Nov 15, 2009
A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integral operands and cannot be applied to float or double. Read More
What is a function and built-in function?
Added on Sun, Nov 15, 2009
A large program is subdivided into a number of smaller programs or subprograms. Each subprogram specifies one or more actions to be performed for a large program. such subprograms are functions. The function supports only static and extern storage... Read More
Why should I prototype a function?
Added on Sun, Nov 15, 2009
A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and... Read More
What is Polymorphism ?
Added on Sun, Nov 15, 2009
’Polymorphism’ is an object oriented term. Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate actions. In other words, polymorphism means taking more than one form.... Read More
What are memory management operators ?
Added on Mon, Nov 16, 2009
There are two types of memory management operators in C++: · new · delete Read More
What is copy constructor?
Added on Mon, Nov 16, 2009
Constructor which initializes it’s object member variables ( by shallow copying) with another object of the same class. If you don’t implement one in your class then compiler implements one for you. for example: .  ... Read More
What is Pass by Reference? Write a C program showing this concept.
Added on Mon, Nov 16, 2009
Pass by Reference: In this method, the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses, we would have an access to the... Read More
What is the use of typedef?
Added on Mon, Nov 16, 2009
typedef declaration helps to make source code of a C program more readable. Its purpose is to redefine the name of an existing variable type. It provides a short and meaningful way to call a data type. typedef is useful when... Read More
Where are the auto variables stored?
Added on Mon, Nov 16, 2009
Main memory and CPU registers are the two memory locations where auto variables are stored. Auto variables are defined under automatic storage class. They are stored in main memory. Memory is allocated to an automatic variable when the block... Read More
Write a program to check whether a given number is even or odd. Program:
Added on Mon, Nov 16, 2009
#include<stdio.h> main() { int a; printf("Enter a: ... Read More
Write a program in C to delete a specific line from a text file.
Added on Mon, Nov 16, 2009
In this program, user is asked for a filename he needs to change. User is also asked for the line number that is to be deleted. The filename is stored in ’filename’. The file is opened and all the data is transferred to another... Read More
Write a program in C to replace a specified line in a text file.
Added on Mon, Nov 16, 2009
Program: Program to replace a specified line in a text file. #include <stdio.h> int main(void) { FILE *fp1, *fp2; //’filename’is a 40 character string to store filename char filename[40]; char c; int del_line, temp = 1; /... Read More
Write a program in C to find the number of lines in a text file.
Added on Mon, Nov 16, 2009
Number of lines in a file can be determined by counting the number of new line characters present. Program: Program to count number of lines in a file. #include <stdio.h> int main(void) /* Ask for a filename and count number of lines in... Read More
Explain the variable assignment in the declaration
Added on Mon, Nov 16, 2009
int *(*p[10])(char *, char *); It is an array of function pointers that returns an integer pointer. Each function has two arguments which in turn are pointers to character type variable. p[0], p[1],....., p[9] are function pointers. Read more about... Read More
What is the value of sizeof(a) /sizeof(char *) in C code snippet below
Added on Mon, Nov 16, 2009
char *a[4]={"sridhar","raghava","shashi","srikanth"}; explain Explanation: Here a[4] is an array which holds the address of strings. Strings are character arrays themselves. Memory required to store an address is 4 bits. So memory... Read More
How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?
Added on Wed, Nov 11, 2009
The first part of this question can be answered in at least three ways: 1. char *(*(*a[N])())(); ... Read More
My compiler is complaining about an invalid redeclaration of a function, but I only define it once and call it once.
Added on Wed, Nov 11, 2009
Functions which are called without a declaration in scope (perhaps because the first call precedes the function’s ... Read More
I came across some code that declared a structure like this:
Added on Sat, Nov 14, 2009
Q : I came across some code that declared a structure like this: struct name {  ... Read More
I heard that structures could be assigned to variables and passed to and from functions, but K&R1 says not.
Added on Sat, Nov 14, 2009
What K&R1 said (though this was quite some time ago by now) was that the restrictions on structure operations would be lifted in a forthcoming version... Read More
How can I read/write structures from/to data files?
Added on Sat, Nov 14, 2009
It is relatively straightforward to write a structure out using fwrite(): fwrite(&somestruct, sizeof... Read More
How can I determine the byte offset of a field within a structure?
Added on Sat, Nov 14, 2009
ANSI C defines the offsetof() macro, which should be used if available; see <stddef.h>. If you don’t have it, one possible implementation... Read More
How can I access structure fields by name at run time?
Added on Sat, Nov 14, 2009
Build a table of names and offsets, using the offsetof() macro. The offset of field b in struct a is offsetb ... Read More
Why doesn’t this code:
Added on Sat, Nov 14, 2009
Under my compiler, the code
Added on Sat, Nov 14, 2009
Here’s a slick expression:
Added on Sat, Nov 14, 2009
What is this infamous null pointer, anyway?
Added on Sat, Nov 14, 2009
The language definition states that for each pointer type, there is a special value -- the "null pointer" -- which is distinguishable from all other... Read More
How do I get a null pointer in my programs?
Added on Sat, Nov 14, 2009
According to the language definition, a constant 0 in a pointer context is converted into a null pointer at compile time. That is, in an... Read More
is there so much confusion surrounding null pointers? Why do these questions come up so often?
Added on Sat, Nov 14, 2009
C programmers traditionally like to know more than they might need to about the underlying machine implementation. The fact that null pointers are... Read More
Seriously, have any actual machines really used nonzero null pointers, or different representations for pointers to different types?
Added on Sat, Nov 14, 2009
The Prime 50 series used segment 07777, offset 0 for the null pointer, at least for PL/I. Later models used segment 0, offset 0 for null pointers in... Read More
So what is meant by the "equivalence of pointers and arrays" in c?
Added on Sat, Nov 14, 2009
Much of the confusion surrounding arrays and pointers in C can be traced to a misunderstanding of this statement. Saying that arrays and pointers are... Read More
Then why are array and pointer declarations interchangeable as function formal parameters?
Added on Sat, Nov 14, 2009
It’s supposed to be a convenience. Since arrays decay immediately into pointers, an array is never actually passed to a function. ... Read More
How can an array be an lvalue, if you can’t assign to it?
Added on Sat, Nov 14, 2009
The ANSI C Standard defines a "modifiable lvalue," which an array is not. Read More
How do I declare a pointer to an array?
Added on Sat, Nov 14, 2009
Usually, you don’t want to. When people speak casually of a pointer to an array, they usually mean a pointer to its first element. ... Read More
How can I dynamically allocate a multidimensional array?
Added on Sat, Nov 14, 2009
The traditional solution is to allocate an array of pointers, and then initialize each pointer to a dynamically-allocated "row." Here is a two... Read More
My compiler complained when I passed a two-dimensional array to a function expecting a pointer to a pointer.
Added on Sat, Nov 14, 2009
The rule by which arrays decay into pointers is not applied recursively. An array of arrays (i.e. a two- dimensional array in C) decays into a... Read More
How do I write functions which accept two-dimensional arrays when the width is not known at compile time?
Added on Sat, Nov 14, 2009
It’s not always easy. One way is to pass in a pointer to the [0][0] element, along with the two dimensions, and simulate array subscripting "by hand": ... Read More
Why doesn’t sizeof properly report the size of an array when the array is a parameter to a function?
Added on Sat, Nov 14, 2009
The compiler pretends that the array parameter was declared as a pointer, and sizeof reports the size of the pointer. Read More
I just tried the code
Added on Sat, Nov 14, 2009
I have a function that is supposed to return a string, but when it returns to its caller, the returned string is garbage.
Added on Sat, Nov 14, 2009
Make sure that the pointed-to memory is properly allocated. For example, make sure you have *not* done something like ... Read More
I’ve heard that some operating systems don’t actually allocate malloc’ed memory until the program tries to use it. Is this legal?
Added on Sat, Nov 14, 2009
It’s hard to say. The Standard doesn’t say that systems can act this way, but it doesn’t explicitly say that they can’t, either. Read More
I’m allocating structures which contain pointers to other dynamically-allocated objects. When I free a structure, do I also have to free each subsidiary pointer?
Added on Sat, Nov 14, 2009
Yes. In general, you must arrange that each pointer returned from malloc() be individually passed to free(), exactly once (if it is freed at all).... Read More
What is alloca() and why is its use discouraged?
Added on Sat, Nov 14, 2009
alloca() allocates memory which is automatically freed when the function which called alloca() returns. That is, memory allocated with alloca is... Read More
How can I get the numeric (character set) value corresponding to a character, or vice versa?
Added on Sat, Nov 14, 2009
In C, characters are represented by small integers corresponding to their values (in the machine’s character set), so you don’t need a... Read More
Here are some cute preprocessor macros:
Added on Sat, Nov 14, 2009
Q: Here are some cute preprocessor macros: Read More
I’m getting strange syntax errors on the very first declaration in a file, but it looks fine.
Added on Sat, Nov 14, 2009
Perhaps there’s a missing semicolon at the end of the last declaration in the last header file you’re #including Read More
I have some old code that tries to construct identifiers with a macro like
Added on Sat, Nov 14, 2009
Q: I have some old code that tries to construct identifiers with a Read More
I’ve got this tricky preprocessing I want to do and I can’t figure out a way to do it.
Added on Sat, Nov 14, 2009
C’s preprocessor is not intended as a general-purpose tool. (Note also that it is not guaranteed to be available as a separate program.) Rather... Read More
How can I get a copy of the Standard?
Added on Sat, Nov 14, 2009
Copies are available in the United States from American National Standards Institute ... Read More
Where can I get information about updates to the Standard?
Added on Sat, Nov 14, 2009
You can find information (including C9X drafts) at the web sites http://www.lysator.liu.se/c/index.html, http://www.dkuug.dk/JTC1/SC22/WG14/, and http:/... Read More
What’s the difference between "const char *p" and "char * const p"?
Added on Sat, Nov 14, 2009
"const char *p" (which can also be written "char const *p") declares a pointer to a constant character (you can’t change the character); "char *... Read More
Why can’t I pass a char ** to a function which expects a const char **?
Added on Sat, Nov 14, 2009
You can use a pointer-to-T (for any type T) where a pointer-to- const-T is expected. However, the rule (an explicit exception) which permits slight... Read More
What’s the correct declaration of main()?
Added on Sat, Nov 14, 2009
Either int main(), int main(void), or int main(int argc, char *argv[]) (with alternate spellings of argc and *argv[] obviously allowed). Read More
The book I’ve been using, _C Programing for the Compleat Idiot_, always uses void main().
Added on Sat, Nov 14, 2009
Perhaps its author counts himself among the target audience. Many books unaccountably use void main() in examples, and assert that it’s correct. ... Read More
What’s the difference between memcpy() and memmove()?
Added on Sat, Nov 14, 2009
memmove() offers guaranteed behavior if the source and destination arguments overlap. memcpy() makes no such guarantee, and may therefore be more... Read More
My compiler is rejecting the simplest possible test programs, with all kinds of syntax errors.
Added on Sat, Nov 14, 2009
Perhaps it is a pre-ANSI compiler, unable to accept function prototypes and the like. Read More
People seem to make a point of distinguishing between implementation-defined, unspecified, and undefined behavior. What’s the difference?
Added on Sat, Nov 14, 2009
Briefly: implementation-defined means that an implementation must choose some behavior and document it. Unspecified means that an implementation... Read More
I’m appalled that the ANSI Standard leaves so many issues undefined. Isn’t a Standard’s whole job to standardize these things?
Added on Sat, Nov 14, 2009
It has always been a characteristic of C that certain constructs behaved in whatever way a particular compiler or a particular piece of hardware chose to... Read More
Someone told me it was wrong to use %lf with printf(). How can printf() use %f for type double, if scanf() requires %lf?
Added on Sat, Nov 14, 2009
It’s true that printf’s %f specifier works with both float and double arguments. Due to the "default argument promotions" (which apply in... Read More
How can I specify a variable width in a scanf() format string?
Added on Sat, Nov 14, 2009
You can’t; an asterisk in a scanf() format string means to suppress assignment. You may be able to use ANSI stringizing and string... Read More
How can I tell how much destination buffer space I’ll need for an arbitrary sprintf call? How can I avoid overflowing the destination buffer with sprintf()?
Added on Sat, Nov 14, 2009
When the format string being used with sprintf() is known and relatively simple, you can sometimes predict a buffer size in an ad-hoc way. If the... Read More
How can I arrange to have output go two places at once, e.g. to the screen and to a file?
Added on Sat, Nov 14, 2009
You can’t do this directly, but you could write your own printf variant which printed everything twice. Read More
How can I split up a string into whitespace-separated fields? How can I duplicate the process by which main() is handed argc and argv?
Added on Sun, Nov 15, 2009
The only Standard function available for this kind of "tokenizing" is strtok(), although it can be tricky to use and it may not do everything you want it... Read More
How can I sort a linked list
Added on Sun, Nov 15, 2009
Sometimes it’s easier to keep the list in order as you build it (or perhaps to use a tree instead). Algorithms like insertion sort and merge... Read More
How can I add N days to a date? How can I find the difference between two dates?
Added on Sun, Nov 15, 2009
The ANSI/ISO Standard C mktime() and difftime() functions provide some support for both problems. mktime() accepts non- normalized dates, so it is... Read More
I need a random number generator.
Added on Sun, Nov 15, 2009
The Standard C library has one: rand(). The implementation on your system may not be perfect, but writing a better one isn’t necessarily easy,... Read More
How can I get random integers in a certain range?
Added on Sun, Nov 15, 2009
The obvious way, rand() % N /* POOR */ (which tries to... Read More
I need a random true/false value, so I’m just taking rand() % 2, but it’s alternating 0, 1, 0, 1, 0...
Added on Sun, Nov 15, 2009
Poor pseudorandom number generators (such as the ones unfortunately supplied with some systems) are not very random in ... Read More
I keep getting errors due to library functions being undefined, but I’m #including all the right header files.
Added on Sun, Nov 15, 2009
In general, a header file contains only declarations. In some cases (especially if the functions are nonstandard) obtaining the actual *definitions*... Read More
I’m still getting errors due to library functions being undefined, even though I’m explicitly requesting the right libraries while linking.
Added on Sun, Nov 15, 2009
Many linkers make one pass over the list of object files and libraries you specify, and extract from libraries only those modules which satisfy references... Read More
My floating-point calculations are acting strangely and giving me different answers on different machines
Added on Sun, Nov 15, 2009
First, If the problem isn’t that simple, recall that digital computers usually use floating-point formats which provide a close but by ... Read More
The predefined constant M_PI seems to be missing from my machine’s copy of <math.h>.
Added on Sun, Nov 15, 2009
That constant (which is apparently supposed to be the value of pi, accurate to the machine’s precision), is not standard. If you need pi, you... Read More
I’m having trouble with a Turbo C program which crashes and says something like "floating point formats not linked."
Added on Sun, Nov 15, 2009
Some compilers for small machines, including Borland’s (and Ritchie’s original PDP-11 compiler), leave out certain floating point support... Read More
How can %f be used for both float and double arguments in printf()? Aren’t they different types?
Added on Sun, Nov 15, 2009
In the variable-length part of a variable-length argument list, the "default argument promotions" apply: types char and short int are promoted to int, and... Read More
How can I discover how many arguments a function was actually called with?
Added on Sun, Nov 15, 2009
This information is not available to a portable program. Some old systems provided a nonstandard nargs() function, but its use was always... Read More
My compiler isn’t letting me declare a function int f(...)
Added on Sun, Nov 15, 2009
Q: My compiler isn’t letting me declare a function Read More
I can’t get va_arg() to pull in an argument of type pointer-to- function.
Added on Sun, Nov 15, 2009
The type-rewriting games which the va_arg() macro typically plays are stymied by overly-complicated types such as pointer-to- function. If you use a... Read More
How can I write a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments)?
Added on Sun, Nov 15, 2009
In general, you cannot. Ideally, you should provide a version of that other function which accepts a va_list pointer (analogous to vfprintf();). ... Read More
How can I call a function with an argument list built up at run time?
Added on Sun, Nov 15, 2009
There is no guaranteed or portable way to do this. If you’re curious, ask this list’s editor, who has a few wacky ideas you could try... ... Read More
Why isn’t my procedure call working? The compiler seems to skip right over it.
Added on Sun, Nov 15, 2009
Does the code look like this? myprocedure; C has only functions, and function calls always require ... Read More
This program crashes before it even runs! (When single-stepping with a debugger, it dies before the first statement in main().)
Added on Sun, Nov 15, 2009
You probably have one or more very large (kilobyte or more) local arrays. Many systems have fixed-size stacks, and those which perform dynamic... Read More
What do "Segmentation violation" and "Bus error" mean?
Added on Sun, Nov 15, 2009
These generally mean that your program tried to access memory it shouldn’t have, invariably as a result of stack corruption or improper pointer use.... Read More
Here’s a neat trick for checking whether two strings are equal: if(!strcmp(s1, s2))
Added on Sun, Nov 15, 2009
Q: Here’s a neat trick for checking whether two strings are equal: Read More
What is "Hungarian Notation"? Is it worthwhile?
Added on Sun, Nov 15, 2009
Hungarian Notation is a naming convention, invented by Charles Simonyi, which encodes information about a variable’s type (and perhaps its... Read More
Some people say that goto’s are evil and that I should never use them. Isn’t that a bit extreme?
Added on Sun, Nov 15, 2009
Programming style, like writing style, is somewhat of an art and cannot be codified by inflexible rules, although discussions about style often seem to... Read More
How can I track down these pesky malloc problems?
Added on Sun, Nov 15, 2009
A number of debugging packages exist to help track down malloc problems; one popular one is Conor P. Cahill’s "dbmalloc", posted to comp.sources.misc... Read More
I just typed in this program, and it’s acting strangely. Can you see anything wrong with it?
Added on Sun, Nov 15, 2009
See if you can run lint first (perhaps with the -a, -c, -h, -p or other options). Many C compilers are really only half- compilers, electing not to... Read More
Are there any C tutorials or other resources on the net?
Added on Sun, Nov 15, 2009
There are several of them: Tom Torfs has a nice tutorial at http://members.xoom.com/tomtorfs/cintro.html . ... Read More
Where can I find the sources of the standard C libraries?
Added on Sun, Nov 15, 2009
One source (though not public domain) is _The Standard C Library_, by P.J. Plauger (see the Bibliography). Implementations of all or part of the C library... Read More
Is there an on-line C reference manual?
Added on Sun, Nov 15, 2009
Two possibilities are http://www.cs.man.ac.uk/standard_c/_index.html and http://www.dinkumware.com/htm_cl/index.html . Read More
I need code to parse and evaluate expressions.
Added on Sun, Nov 15, 2009
Two available packages are "defunc," posted to comp.sources.misc in December, 1993 (V41 i32,33), to alt.sources in January, 1994, and available from... Read More
I need code for performing multiple precision arithmetic.
Added on Sun, Nov 15, 2009
Some popular packages are the "quad" functions within the BSD Unix libc sources (ftp.uu.net, /systems/unix/bsd-sources/..../ /src/lib/libc/quad/*), the GNU... Read More
How can I display a percentage-done indication that updates itself in place, or show one of those "twirling baton" progress indicators?
Added on Sun, Nov 15, 2009
These simple things, at least, you can do fairly portably. Printing the character ’
’ will usually give you a carriage return without a line... Read More
How can I clear the screen? How can I print text in color? How can I move the cursor to a specific x, y position?
Added on Sun, Nov 15, 2009
Such things depend on the terminal type (or display) you’re using. You will have to use a library such as termcap, terminfo, or curses, or some... Read More
How do I read the mouse?
Added on Sun, Nov 15, 2009
Consult your system documentation, or ask on an appropriate system-specific newsgroup (but check its FAQ list first). Mouse handling is completely... Read More
How do I send escape sequences to control a terminal or other device?
Added on Sun, Nov 15, 2009
If you can figure out how to send characters to the device at all it’s easy enough to send escape sequences. In ASCII, the ESC code is 033 (27... Read More
How can I check whether a file exists? I want to warn the user if a requested input file is missing.
Added on Sun, Nov 15, 2009
It’s surprisingly difficult to make this determination reliably and portably. Any test you make can be invalidated if the file is created or... Read More
How can I find the modification date and time of a file?
Added on Sun, Nov 15, 2009
The Unix and POSIX function is stat(), which several other systems supply as well. Read More
How can a file be shortened in-place without completely clearing or rewriting it?
Added on Sun, Nov 15, 2009
BSD systems provide ftruncate(), several others supply chsize(), and a few may provide a (possibly undocumented) fcntl option F_FREESP. Under MS-DOS,... Read More
I’m getting an error, "Too many open files". How can I increase the allowable number of simultaneously open files?
Added on Sun, Nov 15, 2009
There are typically at least two resource limitations on the number of simultaneously open files: the number of low-level "file descriptors" or "file... Read More
How can I access memory (a memory-mapped device, or graphics memory) located at a certain address?
Added on Sun, Nov 15, 2009
Set a pointer, of the appropriate type, to the right number (using an explicit cast to assure the compiler that you really do intend this nonportable... Read More
How can I invoke another program (a standalone executable, or an operating system command) from within a C program?
Added on Sun, Nov 15, 2009
Use the library function system(), which does exactly that. Note that system’s return value is at best the command’s exit status (although even... Read More
How can my program discover the complete pathname to the executable from which it was invoked?
Added on Sun, Nov 15, 2009
argv[0] may contain all or part of the pathname, or it may contain nothing. You may be able to duplicate the command language interpreter’s... Read More
How can I automatically locate a program’s configuration files in the same directory as the executable
Added on Sun, Nov 15, 2009
It’s hard; Even if you can figure out a workable way to do it, you might want to consider making the program’s auxiliary (library)... Read More
How can I implement a delay, or time a user’s response, with sub- second resolution?
Added on Sun, Nov 15, 2009
Unfortunately, there is no portable way. V7 Unix, and derived systems, provided a fairly useful ftime() function with resolution up to a millisecond,... Read More
How can I trap or ignore keyboard interrupts like control-C?
Added on Sun, Nov 15, 2009
The basic step is to call signal(), either as #include <signal.h> ... Read More
How can I handle floating-point exceptions gracefully?
Added on Sun, Nov 15, 2009
On many systems, you can define a function matherr() which will be called when there are certain floating-point errors, such as errors in the math routines... Read More
How do I... Use BIOS calls? Write ISR’s? Create TSR’s?
Added on Sun, Nov 15, 2009
These are very particular to specific systems (PC compatibles running MS-DOS, most likely). You’ll get much better information in a specific... Read More
But I can’t use all these nonstandard, system-dependent functions, because my program has to be ANSI compatible!
Added on Sun, Nov 15, 2009
You’re out of luck. Either you misunderstood your requirement, or it’s an impossible one to meet. ANSI/ISO Standard C simply does... Read More
How can I return multiple values from a function?
Added on Sun, Nov 15, 2009
Either pass pointers to several locations which the function can fill in, or have the function return a structure containing the  ... Read More
How do I access command-line arguments?
Added on Sun, Nov 15, 2009
They are pointed to by the argv array with which main() is called. Read More
How can I write data files which can be read on other machines with different word size, byte order, or floating point formats?
Added on Sun, Nov 15, 2009
The most portable solution is to use text files (usually ASCII), written with fprintf() and read with fscanf() or the like. (Similar advice also applies to... Read More
If I have a char * variable pointing to the name of a function, how can I call that function?
Added on Sun, Nov 15, 2009
The most straightforward thing to do is to maintain a correspondence table of names and function pointers: int func(),... Read More
How can I implement sets or arrays of bits?
Added on Sun, Nov 15, 2009
Use arrays of char or int, with a few macros to access the desired bit at the proper index. Here are some simple macros to use with arrays of char: ... Read More
How can I determine whether a machine’s byte order is big-endian or little-endian?
Added on Sun, Nov 15, 2009
One way is to use a pointer: int x = 1; if(*(char *)&x == 1) ... Read More
Can I use base-2 constants (something like 0b101010)? Is there a printf() format for binary?
Added on Sun, Nov 15, 2009
No, on both counts. You can convert base-2 string representations to integers with strtol(). Read More
What’s the best way of making my program efficient?
Added on Sun, Nov 15, 2009
By picking good algorithms, implementing them carefully, and making sure that your program isn’t doing any extra work. For example, the most... Read More
Are the outer parentheses in return statements really optional?
Added on Sun, Nov 15, 2009
Yes. Long ago, in the early days of C, they were required, and just enough people learned C then, and wrote code which is still in ... Read More
What is assert() and when would I use it?
Added on Sun, Nov 15, 2009
It is a macro, defined in <assert.h>, for testing "assertions". An assertion essentially documents an assumption being made by the programmer, an... Read More
I need a sort of an "approximate" strcmp routine, for comparing two strings for close, but not necessarily exact, equality.
Added on Sun, Nov 15, 2009
Some nice information and algorithms having to do with approximate string matching, as well as a useful bibliography, can be found in Sun Wu and Udi Manber... Read More
Where does the name "C" come from, anyway?
Added on Sun, Nov 15, 2009
C was derived from Ken Thompson’s experimental language B, which was inspired by Martin Richards’s BCPL (Basic Combined Programming Language),... Read More
What do "lvalue" and "rvalue" mean?
Added on Sun, Nov 15, 2009
Simply speaking, an "lvalue" is an expression that could appear on the left-hand sign of an assignment; you can also think of it as denoting an object that... Read More
What is C language?
Added on Sun, Nov 15, 2009
The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is one of the most widely... Read More
Compilation How to reduce a final size of executable?
Added on Sun, Nov 15, 2009
Size of the final executable can be reduced using dynamic linking for libraries. Read More
What are the differences between malloc() and calloc()?
Added on Sun, Nov 15, 2009
There are 2 differences. First, is in the number of arguments. malloc() takes a single argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a single variable). Secondly,... Read More
How can you determine the size of an allocated portion of memory?
Added on Sun, Nov 15, 2009
You can’t, really. free() can , but there’s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there’s no guarantee the trick won’t change with the next release... Read More
What is a method?
Added on Sun, Nov 15, 2009
Method is a way of doing something, especially a systematic way; implies an orderly logical arrangement (usually in steps). Read More
How many levels deep can include files be nested?
Added on Sun, Nov 15, 2009
Even though there is no limit to the number of levels of nested include files you can have, your compiler might run out of stack space while trying to include an inordinately high number of files. This number varies according to your hardware... Read More
Differentiate between an internal static and external static variable?
Added on Sun, Nov 15, 2009
An internal static variable is declared inside a block with static storage class whereas an external static variable is declared outside all the blocks in a file.An internal static variable has persistent storage,block scope and no linkage.An... Read More
What is the difference between a string and an array?
Added on Sun, Nov 15, 2009
An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length. There are two kinds of programming languages: those in which a string is just an array of characters, and those in... Read More
What is a void pointer?
Added on Sun, Nov 15, 2009
A void pointer is a C convention for a raw address. The compiler has no idea what type of object a void Pointer really points to. If you write int *ip; ip points to an int. If you write void *p; p doesn’t point to a void! In... Read More
Differentiate between a linker and linkage?
Added on Sun, Nov 15, 2009
A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable. Read More
What is Abstraction?
Added on Sun, Nov 15, 2009
Hiding the complexity. It is a process of defining communication interface for the functionality and hiding rest of the things. Read More
What is Overloading?
Added on Sun, Nov 15, 2009
Adding a new method with the same name in same/derived class but with different number/types of parameters. It implements Polymorphism. Read More
What is Inheritance?
Added on Sun, Nov 15, 2009
It is a process in which properties of object of one class acquire the properties of object of another class. Read More
What is an Abstract class?
Added on Sun, Nov 15, 2009
An abstract class is a special kind of class that cannot be instantiated. It normally contains one or more abstract methods or abstract properties. It provides body to a class. Read More
What is the difference between a while statement and a do statement?
Added on Mon, Nov 16, 2009
A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next loop iteration should occur.. Read More
What is difference between visual c++ & ANSI c++?
Added on Mon, Nov 16, 2009
Don’t get confused with the words visual and ANSI. Follow this. Visual C++ is an IDE for developing GUI(graphical user interface) real time applications using++. Visual C++ deals with more complex applicationsANSI C++ is the... Read More
What is a structure?
Added on Mon, Nov 16, 2009
A structure is a collection of pre-defined data types to create a user-defined data type. Let us say we need to create records of students. Each student has three fields: int roll_number; char name[30]; int... Read More
What is recursion? Write a program using recursion (factorial)?
Added on Mon, Nov 16, 2009
Recursion: A function is called ’recursive’ if a statement within the body of a function calls the same function. It is also called ’circular definition’. Recursion is thus a process of defining something in terms of... Read More
What are the differences between structures and unions?
Added on Mon, Nov 16, 2009
Structures and Unions are used to store members of different data types. STRUCTURE UNION a)Declaration: struct { data type member1; data type member2; }; a)Declaration: union { data type member1; data type member2; }; ... Read More
What is scope & storage allocation of global and extern variables? Explain with an example
Added on Mon, Nov 16, 2009
Extern variables: belong to the External storage class and are stored in the main memory. extern is used when we have to refer a function or variable that is implemented in other file in the same project. The... Read More
What is Pass by Value? Write a C program showing this concept.
Added on Mon, Nov 16, 2009
Pass by Value: In this method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In pass by value, the changes made to formal arguments in... Read More
What are storage memory, default value, scope and life of Static and External storage class?
Added on Mon, Nov 16, 2009
1. Static storage class: Storage : main memory Default value : zero Scope : local to the block in which the variable is defined lifetime : till the value of the variable persists between... Read More
Out of fgets( ) and gets( ) which function is safer to use and why?
Added on Mon, Nov 16, 2009
Out of functions fgets( ) and gets( ), fgets( ) is safer to use. gets( ) receives a string from the keyboard and it is terminated only when the enter key is hit. There is no limit for the input string. The string can be too long and may lead... Read More
How can you increase the size of a dynamically allocated array?
Added on Mon, Nov 16, 2009
realloc(): This function is used to increase or decrease the size of any dynamic memory which is allocated using malloc() or calloc() functions. Syntax: void *realloc(void *ptr, size_t newsize); The first... Read More
Write a program in C to print "Hello World" without using semicolon anywhere in the code.
Added on Mon, Nov 16, 2009
Generally when we use printf("") statement, we have to use a semicolon at the end. If printf is used inside an if condition, semicolon can be avoided. Program: Program to print some thing with out using semicolon(;) #include<stdio.h> int main... Read More
Write a program in C to print a semicolon without using a semicolon anywhere in the code.
Added on Mon, Nov 16, 2009
Generally when use printf("") statement we have to use semicolon at the end. If we want to print a semicolon, we use the statement: printf(";"); In above statement, we are using two semicolons. The task of printing a semicolon without using semicolon... Read More
What are the differences between the C statements below:
Added on Mon, Nov 16, 2009
Q : (i)What are the differences between the C statements below: char *str = "Hello"; char arr[] = "Hello"; (ii)Whether following statements get complied or not? Explain each statement. arr++; *(arr + 1) = ’s’; printf("%s",arr); ... Read More
Write a program in C that returns 3 numbers from a function.
Added on Mon, Nov 16, 2009
Q : Write a program in C that returns 3 numbers from a function. A function in C can return only one value. If we want the function to return multiple values, we need to create a... Read More
What is the difference between the functions strdup and strcpy in C?
Added on Mon, Nov 16, 2009
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. ... Read More
Write down the equivalent pointer expression for referring the same element as a[i][j][k][l]. Explain.
Added on Mon, Nov 16, 2009
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]... Read More
What is the difference b/n public, private and protected?
Added on Thu, Nov 19, 2009
Public: The data members and methods having public as access outside the class. Protected: The data members and methods declared as protected will be accessible to the class methods and the derived class methods only. ... Read More
What is a void return type?
Added on Thu, Nov 19, 2009
A void return type indicates that a method does not return a value. Read More
What’s wrong with this initialization?
Added on Sat, Nov 14, 2009
1.31b: What’s wrong with this initialization? Read More
Why doesn’t
Added on Sat, Nov 14, 2009
Q : Why doesn’t struct x { ... }; x thestruct; ... Read More
Is there a way to compare structures automatically?
Added on Sat, Nov 14, 2009
No. There is no single, good way for a compiler to implement implicit structure comparison (i.e. to support the == operator for structures)... Read More
Why doesn’t the code
Added on Sat, Nov 14, 2009
Does C even have "pass by reference"?
Added on Sat, Nov 14, 2009
Not really. Strictly speaking, C always uses pass by value. You can simulate pass by reference yourself, by defining functions which accept pointers... Read More
I’ve seen different methods used for calling functions via pointers. What’s the story?
Added on Sat, Nov 14, 2009
Originally, a pointer to a function had to be "turned into" a "real" function, with the * operator (and an extra pair of parentheses, to keep the... Read More
Is the abbreviated pointer comparison "if(p)" to test for non- null pointers valid? What if the internal representation for null pointers is nonzero?
Added on Sat, Nov 14, 2009
When C requires the Boolean value of an expression, a false value is inferred when the expression compares equal to zero, and a true value otherwise. ... Read More
Practically speaking, what is the difference between arrays and pointers?
Added on Sat, Nov 14, 2009
Arrays automatically allocate space, but can’t be relocated or resized. Pointers must be explicitly assigned to point to allocated space ... Read More
My program is crashing, apparently somewhere down inside malloc, but I can’t see anything wrong with it. Is there a bug in malloc()?
Added on Sat, Nov 14, 2009
It is unfortunately very easy to corrupt malloc’s internal data structures, and the resulting problems can be stubborn. The most common source... Read More
Must I free allocated memory before the program exits?
Added on Sat, Nov 14, 2009
You shouldn’t have to. A real operating system definitively reclaims all memory and other resources when a program exits. Nevertheless, some... Read More
I think something’s wrong with my compiler: I just noticed that sizeof(’a’) is 2, not 1 (i.e. not sizeof(char)).
Added on Sat, Nov 14, 2009
Perhaps surprisingly, character constants in C are of type int, so sizeof(’a’) is sizeof(int) (though this is another area where C++ differs).... Read More
How can I write a generic macro to swap two values?
Added on Sat, Nov 14, 2009
There is no good answer to this question. If the values are integers, a well-known trick using exclusive-OR could perhaps be used, but it will not... Read More
My ANSI compiler complains about a mismatch when it sees
Added on Sat, Nov 14, 2009
Q: My ANSI compiler complains about a mismatch when it sees Read More
Can I declare main() as void, to shut off these annoying "main returns no value" messages?
Added on Sat, Nov 14, 2009
No. main() must be declared as returning an int, and as taking either zero or two arguments, of the appropriate types. If you’re calling exit()... Read More
What should malloc(0) do? Return a null pointer or a pointer to 0 bytes?
Added on Sat, Nov 14, 2009
The ANSI/ISO Standard says that it may do either; the behavior is implementation-defined Read More
What printf format should I use for a typedef like size_t when I don’t know whether it’s long or some other type?
Added on Sat, Nov 14, 2009
Use a cast to convert the value to a known, conservatively- sized type, then use the printf format matching that type. For example, to print the size of a... Read More
How can I print numbers with commas separating the thousands? What about currency formatted numbers?
Added on Sat, Nov 14, 2009
The functions in <locale.h> begin to provide some support for these operations, but there is no standard routine for doing either task. (The... Read More
How can I sort more data than will fit in memory?
Added on Sun, Nov 15, 2009
You want an "external sort," which you can read about in Knuth, Volume 3. The basic idea is to sort the data in chunks (as much as will fit in memory... Read More
Does C have any Year 2000 problems?
Added on Sun, Nov 15, 2009
No, although poorly-written C programs do. The tm_year field of struct tm holds the value of the year minus 1900; this field will therefore... Read More
I’m trying to take some square roots, but I’m getting crazy numbers.
Added on Sun, Nov 15, 2009
Make sure that you have #included <math.h>, and correctly declared other functions returning double. (Another library function to be careful... Read More
How do I round numbers?
Added on Sun, Nov 15, 2009
The simplest and most straightforward way is with code like (int)(x + 0.5) This technique won&rsquo... Read More
Does anyone have a C compiler test suite I can use?
Added on Sun, Nov 15, 2009
Plum Hall (formerly in Cardiff, NJ; now in Hawaii) sells one; other packages are Ronald Guilmette’s RoadTest(tm) Compiler Test Suites (ftp to netcom... Read More
How do I read the arrow keys? What about function keys?
Added on Sun, Nov 15, 2009
Terminfo, some versions of termcap, and some versions of curses have support for these non-ASCII keys. Typically, a special key sends a... Read More
How can I find out the size of a file, prior to reading it in?
Added on Sun, Nov 15, 2009
If the "size of a file" is the number of characters you’ll be able to read from it in C, it is difficult or impossible to determine this number... Read More
How can I insert or delete a line (or record) in the middle of a file?
Added on Sun, Nov 15, 2009
Short of rewriting the file, you probably can’t. The usual solution is simply to rewrite the file. (Instead of deleting records, you... Read More
How can I read a directory in a C program?
Added on Sun, Nov 15, 2009
See if you can use the opendir() and readdir() functions, which are part of the POSIX standard and are available on most Unix variants. ... Read More
How can I invoke another program or command and trap its output?
Added on Sun, Nov 15, 2009
Unix and some other systems provide a popen() function, which sets up a stdio stream on a pipe connected to the process running a command, so that the... Read More
What is the most efficient way to count the number of bits which are set in an integer?
Added on Sun, Nov 15, 2009
Many "bit-fiddling" problems like this one can be sped up and streamlined using lookup tables Read More
How can I swap two values without using a temporary?
Added on Sun, Nov 15, 2009
The standard hoary old assembly language programmer’s trick is: a ^= b; b... Read More
Is there a way to have non-constant case labels (i.e. ranges or arbitrary expressions)?
Added on Sun, Nov 15, 2009
No. The switch statement was originally designed to be quite simple for the compiler to translate, therefore case labels are limited to single,... Read More
What is hashing?
Added on Sun, Nov 15, 2009
Hashing is the process of mapping strings to integers, usually in a relatively small range. A "hash function" maps a string (or some other data... Read More
When does the compiler not implicitly generate the address of the first element of an array?
Added on Sun, Nov 15, 2009
Whenever an array name appears in an expression such as - array as an operand of the sizeof operator - array as an operand of & operator - array as a string literal initializer for a character array Then the compiler does not... Read More
How are pointer variables initialized?
Added on Sun, Nov 15, 2009
Pointer variable are initialized by one of the following two ways - Static memory allocation - Dynamic memory allocation Read More
Is using exit() the same as using return?
Added on Sun, Nov 15, 2009
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... Read More
What is a pointer value and address?
Added on Sun, Nov 15, 2009
A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location. Read More
What is inline function?
Added on Mon, Nov 16, 2009
Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function saved program... Read More
What is a scope resolution operator?
Added on Mon, Nov 16, 2009
A scope resolution operator (::), can be used to define the member functions of a class outside the class. Read More
What are the advantages of inheritance?
Added on Mon, Nov 16, 2009
It permits code reusability. Reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional Read More
What are virtual functions? Describe a circumstance in which virtual functions would be appropriate
Added on Mon, Nov 16, 2009
Virtual functions are functions with the same function prototype that are defined throughout a class hierarchy. At least the base class occurrence of the function is preceded by the keyword virtual. Virtual functions are used to enable generic... Read More
Distinguish between virtual functions and pure virtual functions
Added on Mon, Nov 16, 2009
A virtual function must have a definition in the class in which it is declared. A pure virtual function does not provide a definition. Classes derived directly from the abstract class must provide definitions for the inherited pure virtual... Read More
What is a pointer?
Added on Mon, Nov 16, 2009
A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as ’*’,’%... Read More
How to print below pattern?
Added on Mon, Nov 16, 2009
1 2 3 4 5 6 7 8 9 10 Program: #include <stdio.h> main () { int i, j, ctr = 1; for (i = 1; i < 5; i++) { for (j = 1; j <= i; j++) printf ("... Read More
How to swap two numbers using bitwise operators?
Added on Mon, Nov 16, 2009
Program: #include <stdio.h> main () { int i = 65; int k = 120; printf ("... Read More
To which numbering system, can the binary number 1101100100111100 be
Added on Mon, Nov 16, 2009
1101100100111100 can be easily converted to hexadecimal numbering system. Hexa-decimal integer constants consist of combination of digits from 0 to 9 and alphabets ’A’ to ’F’. The alphabets represent numbers 10 to 15... Read More
Write a program to check whether a given string is a palindrome.
Added on Mon, Nov 16, 2009
Palindrome is a string, which when read in both forward and backward way is same. Example: radar, madam, pop, lol, rubber, etc., Program: #include<stdio.h> #include<string.h> main() { char *string1; char *string2; printf("Enter a string: ... Read More
|