Hello
Members/Administrator

I urgently needed help for the following questions as
i have very little time in my hand for preparing the interview.

->Why doesn't C have nested functions?
->What is the most efficient way to count the number of bits which are set in a value?
->How can I call a function, given its name as a string?
->How can I return multiple values from a function?
->How can I invoke another program from within a C program?
->How can I access memory located at a certain address?
->How can I allocate arrays or structures bigger than 64K?
->How can I find out how much memory is available?
->How can I read a directory in a C program?
->How can I increase the allowable number of simultaneously open files?
->What's wrong with the call "fopen("c:\newdir\file.dat", "r")"?


its
"dwija"

Dani AI

Generated

Short, interview‑ready clarifications and corrections to Dwija’s list — synthesizes points raised by and others and adds compact examples and caveats.

Standard C and nested functions

  • ISO C has no syntax for defining a function inside another because nested definitions require capturing an outer function’s stack frame (closures) and complicate calling conventions and lifetime rules. Some compilers (GCC) offer nested functions as a nonstandard extension; mention non‑portability in an interview and prefer file‑scope static helpers, function pointers, or move to a language with closures when closures are required.

Calling by name at runtime

  • Turning an identifier into a string with the preprocessor does not call the function. Two practical approaches: build a dispatch table that maps names to function pointers, or perform dynamic symbol lookup for shared libraries (dlsym/GetProcAddress). Example dispatch table:
typedef void (*fn_t)(void);
struct { const char *name; fn_t fn; } tbl[] = {
  { "hello", hello },
  { NULL, NULL }
};
/* lookup: strcmp(tbl[i].name, query) and call tbl[i].fn(); */

Counting bits efficiently

  • Best: use compiler intrinsics that map to CPU POPCNT (e.g. __builtin_popcount on GCC/Clang). Portable fallback: Kernighan’s algorithm — repeatedly clear the lowset bit until zero:
unsigned int popcount(unsigned int v){
  unsigned int c = 0;
  while (v) { v &= v - 1; c++; }
  return c;
}

Quick fire corrections & caveats

  • Multiple returns: use out‑parameters (pointers) or return a struct.
  • Invoke programs: prefer fork+exec (Unix) or CreateProcess (Windows); system() is simple but has security/shell issues.
  • Read directory: POSIX opendir/readdir or Windows FindFirstFile family.
  • 64K limit: a 64K cap is a legacy 16‑bit issue; on modern 32/64‑bit systems use heap allocation (malloc) or OS APIs.
  • Accessing an arbitrary address: cast and dereference only when the address is valid for the process (use volatile for MMIO; mmap or kernel code for physical memory).
  • fopen("c:\newdir\file.dat", "r") fails because backslashes are escapes; use "c:\\newdir\\file.dat" or "c:/newdir/file.dat".

Where pointed in the thread is directionally useful but watch portability and safety when using OS‑specific APIs.

Recommended Answers

All 10 Replies

1) b/c C is a minimal, spartan like language
2) shift each bit out and test it:

unsigned int num = 0xdeadbeef;
 for(int x = 0; x < (sizeof(int) * 8); x++)
 	if(num & (1 << x) ) printf("bit %d is on\n", x);

3)using the `stringification' operator in a preprocessor macro:
#define(func2call) #func2call()
4)by passing some arguments by reference, or by returning a structure packed full of stuff
5)by using execve() or CreateProcess()
6)by dereferencing a p ointer to that address
7)the only limit on dynamically allocated memory is the size of ur RAM i believe
8)see above
9)by using the readdir() function
9)by suing the setrlimit() function in linux
10)looks fine to me

I disagree C does have nested functions...recursion is pretty nested to me.

that's not what 'nested' functions means. nested functions means being able to define a function inside another. and some compilers allow and others dont, so it's not something u can rely on.

ummm... are we sure this is an interview? This sounds more like a homework assignment to me... Who's doing the interviewing?

that's not what 'nested' functions means. nested functions means being able to define a function inside another. and some compilers allow and others dont, so it's not something u can rely on.

oh, i see. Sounds like java, main is inside another class method, is that a correct analogy?

maybe the interview is a homework assignment :lol:

exactly.

Infamous, you have better things to do than waste your time doing someone else's homework.

what am i, psychic? i didnt know if it was homework or not, i was just trying to clear up some of the old posts; worry about yourself. that post was almost 3 months old anyhow, dont think it helped him much if it was homework.

well it helped me at least, now I know what a nested function is :D

well dicounting the other questions
lets drop the how and the way you solve the can i part is

yes!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.