100% found this document useful (1 vote)
1K views11 pages

C QUESTIONS How Do You Decide Which Integer Type To

Can't a structure in C contain a pointer to itself? What's the best way of implementing opaque (abstract) data types in C? How Can I pass constant values to functions which accept structure arguments?

Uploaded by

smilesuresh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views11 pages

C QUESTIONS How Do You Decide Which Integer Type To

Can't a structure in C contain a pointer to itself? What's the best way of implementing opaque (abstract) data types in C? How Can I pass constant values to functions which accept structure arguments?

Uploaded by

smilesuresh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

C QUESTIONS

How do you decide which integer type to use?

What should the 64-bit type on a machine that can support it?

What's the best way to declare and define global variables


and functions?

What does extern mean in a function declaration?

What's the auto keyword good for?

I can't seem to define a linked list successfully. I tried

typedef struct {
char *item;
NODEPTR next;
} *NODEPTR;

but the compiler gave me error messages. Can't a structure in C


contain a pointer to itself?

How do I declare an array of N pointers to functions returning


pointers to functions returning pointers to characters?

How can I declare a function that can return a pointer to a


function of the same type?

What's the right declaration for main()?


Is void main() correct?

What am I allowed to assume about the initial values


of variables which are not explicitly initialized?
If global variables start out as "zero", is that good
enough for null pointers and floating-point zeroes?

This code, straight out of a book, isn't compiling:

int f()
{
char a[] = "Hello, world!";
}

What's wrong with this initialization?

char *p = malloc(10);

What is the difference between these initializations?

char a[] = "string literal";


char *p = "string literal";

What's the difference between these two declarations?

struct x1 { ... };
typedef struct { ... } x2;
Why doesn't

struct x { ... };
x thestruct;

work?

Can a structure contain a pointer to itself?

What's the best way of implementing opaque (abstract) data types


in C?

I came across some code that declared a structure like this:

struct name {
int namelen;
char namestr[1];
};

and then did some tricky allocation to make the namestr array
act like it had several elements. Is this legal or portable?

Is there a way to compare structures automatically?

How can I pass constant values to functions which accept


structure arguments?

How can I read/write structures from/to data files?

Why does sizeof report a larger size than I expect for a


structure type, as if there were padding at the end?

How can I determine the byte offset of a field within a


structure?

How can I access structure fields by name at run time?

This program works correctly, but it dumps core after it


finishes. Why?

struct list {
char *item;
struct list *next;
}

/* Here is the main program. */

main(argc, argv)
{ ... }

Can I initialize unions?

What is the difference between an enumeration and a set of


preprocessor #defines?

Is there an easy way to print enumeration values symbolically?

Why doesn't this code:


a[i] = i++;

work?

I've experimented with the code

int i = 3;
i = i++;

on several compilers. Some gave i the value 3, and some gave 4.


Which compiler is correct?

Can I use explicit parentheses to force the order of evaluation


I want? Even if I don't, doesn't precedence dictate it?

How can I understand these complex expressions? What's a


"sequence point"?

If I'm not using the value of the expression, should I use i++
or ++i to increment a variable?

Why doesn't the code

int a = 1000, b = 1000;


long int c = a * b;

work?

I'm trying to declare a pointer and allocate some space for it,
but it's not working. What's wrong with this code?

char *p;
*p = malloc(10);

Does *p++ increment p, or what it points to?

I have a char * pointer that happens to point to some ints, and


I want to step it over them. Why doesn't

((int *)p)++;

work?

I have a function which accepts, and is supposed to initialize,


a pointer:

void f(int *ip)


{
static int dummy = 5;
ip = &dummy;
}

But when I call it like this:

int *ip;
f(ip);

the pointer in the caller remains unchanged.


Why?

Can I use a void ** pointer as a parameter so that a function


can accept a generic pointer by reference?

I have a function

extern int f(int *);

which accepts a pointer to an int. How can I pass a constant by


reference? A call like

f(&5);

doesn't seem to work.

Does C even have "pass by reference"?

What is infamous null pointer?

How do I get a null pointer in my programs?

Is the abbreviated pointer comparison "if(p)" to test for non-


null pointers valid? What if the internal representation for
null pointers is nonzero?

What is NULL and how is it #defined?

How should NULL be defined on a machine which uses a nonzero bit


pattern as the internal representation of a null pointer?

If NULL were defined as follows:

#define NULL ((char *)0)

wouldn't that make function calls which pass an uncast NULL


work?

If NULL and 0 are equivalent as null pointer constants, which should I use?

What does a run-time "null pointer assignment" error mean? How can I track it down?

Why are array and pointer declarations interchangeable as function formal parameters?

How can an array be an lvalue, if you can't assign to it?

Practically speaking, what is the difference between arrays and pointers?

How do I declare a pointer to an array?

How can I set an array's size at run time? How can I avoid fixed-sized arrays?

How can I declare local arrays of a size matching a passed-in array?

How can I dynamically allocate a multidimensional array?

How do I write functions which accept two-dimensional arrays when the width is not known at compile time?
How can I use statically- and dynamically-allocated multidimensional arrays interchangeably when passing them
to functions?
Why doesn't sizeof properly report the size of an array when the array is a parameter to a function?

Why doesn't this fragment work?

char *answer;
printf("Type something:\n");
gets(answer);
printf("You typed \"%s\"\n", answer);

I just tried the code

char *p;
strcpy(p, "abc");

and it worked. How? Why didn't it crash?

How much memory does a pointer variable allocate?

Why am I getting "warning: assignment of pointer from integer lacks a cast" for calls to malloc()?

Why does some code carefully cast the values returned by malloc to the pointer type being allocated?

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?

When I call malloc() to allocate memory for a pointer which is local to a function, do I have to explicitly free() it?

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?

Must I free allocated memory before the program exits?

How does free() know how many bytes to free?

Can I query the malloc package to find out how big an allocated block is?

Is it legal to pass a null pointer as the first argument to realloc()?

What's the difference between calloc() and malloc()? Is it safe to take advantage of calloc's zero-filling? Does
free() work on memory allocated with calloc(), or do you need a cfree()?

What is alloca() and why is its use discouraged?

Why doesn't

strcat(string, '!');

work?

How can I get the numeric (character set) value corresponding to a character, or vice versa?

What is the right type to use for Boolean values in C?

Is if(p), where p is a pointer, a valid conditional?

How can I write a generic macro to swap two values?


What's the best way to write a multi-statement macro?
Is it acceptable for one header file to #include another?

What's the difference between #include <> and #include "" ?

What are the complete rules for header file searching?

How can I construct preprocessor #if expressions which compare strings?

Does the sizeof operator work in preprocessor #if directives?

Can I use an #ifdef in a #define line, to define something two different ways?

Is there anything like an #ifdef for typedefs?

How can I use a preprocessor #if expression to tell if a machine is big-endian or little-endian?

How can I list all of the predefined identifiers?

How can I write a macro which takes a variable number of arguments?

What is the "ANSI C Standard?"

What's the difference between "const char *p" and "char * const p"?

Why can't I pass a char ** to a function which expects a const char **?

What's the correct declaration of main()?

Can I declare main() as void ?

What does the message "warning: macro replacement within a string literal" mean?

What are #pragmas and what are they good for?

What does "#pragma once" mean?

Is char a[3] = "abc"; legal? What does it mean?

Why can't I perform arithmetic on a void * pointer?

What's the difference between memcpy() and memmove()?

What should malloc(0) do? Return a null pointer or a pointer to 0 bytes?

What's wrong with this code?

char c;
while((c = getchar()) != EOF) ...

Why does the code

while(!feof(infp)) {
fgets(buf, MAXLINE, infp);
fputs(buf, outfp);
}

copy the last line twice?


How can I read one character at a time, without waiting for the RETURN key?
How can I print a '%' character in a printf format string?

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?

How can I implement a variable field width with printf?

How can I print numbers with commas separating the thousands? What about currency formatted numbers?

Why doesn't the call scanf("%d", i) work?

How can I specify a variable width in a scanf() format string?

Why doesn't this code:

double d;
scanf("%f", &d);

work?

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()?

What's the difference between fgetpos/fsetpos and ftell/fseek?

What are fgetpos() and fsetpos() good for?

How can I redirect stdin or stdout to a file from within a program?

How can I read a binary data file properly?

How can I convert numbers to strings (the opposite of atoi)? Is there an itoa() function?

Why does strncpy() not always place a '\0' terminator in the destination string?

Why do some versions of toupper() act strangely if given an upper-case letter?

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?

How can I sort a linked list?

How can I sort more data than will fit in memory?

How can I get the current date or time of day in a C program?

How can I add N days to a date? How can I find the difference between two dates?

How can I get random integers in a certain range?

How can I generate random numbers with a normal or Gaussian distribution?

What does it mean when the linker says that _end is undefined?

When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?

What's a good way to check for "close enough" floating-point equality?

How do I round numbers?


Why doesn't C have an exponentiation operator?

How do I test for IEEE NaN and other special values?

What's a good way to implement complex numbers in C?

How can %f be used for both float and double arguments in printf()? Aren't they different types?

How can I write a function that takes a variable number of arguments?

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?

How can I write a function analogous to scanf(), that calls scanf() to do most of the work?

How can I discover how many arguments a function was actually called with?

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)?

How can I call a function with an argument list built up at run time?

C QUESTIONS

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 are declared or defined?
What are the differences between malloc() and calloc()?
What are macros? what are its advantages and disadvantages?
Difference between pass by reference and pass by value?
What is static identifier?
Where are the auto variables stored?
Where does global, static, local, register variables, free memory and C Program
instructions get stored?
Difference between arrays and linked list?
What are enumerations?
Describe about storage allocation and scope of global, extern, static, local and register
variables?
What are register variables? What are the advantage of using register variables?
What is the use of typedef?
Can we specify variable field width in a scanf() format string? If possible how?
Out of fgets() and gets() which function is safe to use and why?
Difference between strdup and strcpy?
What is recursion?
Differentiate between a for loop and a while loop? What are it uses?
What are the different storage classes in C?
Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?
What is difference between Structure and Unions?
What the advantages of using Unions?
What are the advantages of using pointers in a program?
What is the difference between Strings and Arrays?
In a header file whether functions are declared or defined?
What is a far pointer? where we use it?
How will you declare an array of three function pointers where each function receives two
ints and returns a float?
what is a NULL Pointer? Whether it is same as an uninitialized pointer?
What is a NULL Macro? What is the difference between a NULL Pointer and a NULL
Macro?
What does the error 'Null Pointer Assignment' mean and what causes this error?
What is near, far and huge pointers? How many bytes are occupied by them?
How would you obtain segment and offset addresses from a far address of a memory
location?
Are the expressions arr and &arr same for an array of integers?
Does mentioning the array name gives the base address in all the contexts?
Explain one method to process an entire string as one unit?
What is the similarity between a Structure, Union and enumeration?
Can a Structure contain a Pointer to itself?
How can we check whether the contents of two structure variables are same or not?
How are Structure passing and returning implemented by the complier?
How can we read/write Structures from/to data files?
What is the difference between an enumeration and a set of pre-processor # defines?
what do the 'c' and 'v' in argc and argv stand for?
Are the variables argc and argv are local to main?
What is the maximum combined length of command line arguments including the space
between adjacent arguments?
If we want that any wildcard characters in the command line arguments should be
appropriately expanded, are we required to make any special provision? If yes, which?
Does there exist any way to make the command line arguments available to other
functions without passing them as arguments to the function?
What are bit fields? What is the use of bit fields in a Structure declaration?
To which numbering system can the binary number 1101100100111100 be easily
converted to?
Which bit wise operator is suitable for checking whether a particular bit is on or off?
Which bit wise operator is suitable for turning off a particular bit in a number?
Which bit wise operator is suitable for putting on a particular bit in a number?
Which bit wise operator is suitable for checking whether a particular bit is on or off?
which one is equivalent to multiplying by 2:Left shifting a number by 1 or Left shifting an
unsigned int or char by 1?
Write a program to compare two strings without using the strcmp() function.
Write a program to concatenate two strings.
Write a program to interchange 2 variables without using the third one.
Write programs for String Reversal & Palindrome check
Write a program to find the Factorial of a number
Write a program to generate the Fibinocci Series
Write a program which employs Recursion
Write a program which uses Command Line Arguments
Write a program which uses functions like strcmp(), strcpy()? etc
What are the advantages of using typedef in a program?
How would you dynamically allocate a one-dimensional and two-dimensional array of
integers?
How can you increase the size of a dynamically allocated array?
How can you increase the size of a statically allocated array?
When reallocating memory if any other pointers point into the same piece of memory do
you have to readjust these other pointers or do they get readjusted automatically?
Which function should be used to free the memory allocated by calloc()?
How much maximum can you allocate in a single call to malloc()?
Can you dynamically allocate arrays in expanded memory?
What is object file? How can you access object file?
Which header file should you include if you are to develop a function which can accept
variable number of arguments?
Can you write a function similar to printf()?
How can a called function determine the number of arguments that have been passed to
it?
Can there be at least some solution to determine the number of arguments passed to a
variable argument list function?
How do you declare the following:
• An array of three pointers to chars
• An array of three char pointers
• A pointer to array of three chars
• A pointer to function which receives an int pointer and returns a float pointer
• A pointer to a function which receives nothing and returns nothing
What do the functions atoi(), itoa() and gcvt() do?
Does there exist any other function which can be used to convert an integer or a float to a
string?
How would you use qsort() function to sort an array of structures?
How would you use qsort() function to sort the name stored in an array of pointers to
string?
How would you use bsearch() function to search a name stored in array of pointers to
string?
How would you use the functions sin(), pow(), sqrt()?
How would you use the functions memcpy(), memset(), memmove()?
How would you use the functions fseek(), freed(), fwrite() and ftell()?
How would you obtain the current time and difference between two times?
How would you use the functions randomize() and random()?
How would you implement a substr() function that extracts a sub string from a given
string?
What is the difference between the functions rand(), random(), srand() and randomize()?
What is the difference between the functions memmove() and memcpy()?
How do you print a string on the printer?
Can you use the function fprintf() to display the output on the screen?

C++ QUESTIONS

What is a class?
What is an object?
What is the difference between an object and a class?
What is the difference between class and structure?
What is public, protected, private?
What are virtual functions?
What is friend function?
What is a scope resolution operator?
What do you mean by inheritance?
What is abstraction?
What is polymorphism? Explain with an example.
What is encapsulation?
What do you mean by binding of data and functions?
What is function overloading and operator overloading?
What is virtual class and friend class?
What do you mean by inline function?
What do you mean by public, private, protected and friendly?
When is an object created and what is its lifetime?
What do you mean by multiple inheritance and multilevel inheritance? Differentiate
between them.
Difference between realloc() and free?
What is a template?
What are the main differences between procedure oriented languages and object oriented
languages?
What is R T T I ?
What are generic functions and generic classes?
What is namespace?
What is the difference between pass by reference and pass by value?
Why do we use virtual functions?
What do you mean by pure virtual functions?
What are virtual classes?
Does c++ support multilevel and multiple inheritance?
What are the advantages of inheritance?
When is a memory allocated to a class?
What is the difference between declaration and definition?
What is virtual constructors/destructors?
In c++ there is only virtual destructors, no constructors. Why?
What is late bound function call and early bound function call? Differentiate.
How is exception handling carried out in c++?
When will a constructor executed?
What is Dynamic Polymorphism?
Write a macro for swapping integers.

You might also like