Tricky C questions

  1. How do you write a program which produces its own source code as its output?
  2. How can I find the day of the week given the date?
  3. Why doesn’t C have nested functions?
  4. What is the most efficient way to count the number of bits which are set in a value?
  5. How can I convert integers to binary or hexadecimal?
  6. How can I call a function, given its name as a string?
  7. How do I access command-line arguments?
  8. How can I return multiple values from a function?
  9. How can I invoke another program from within a C program?
  10. How can I access memory located at a certain address?
  11. How can I allocate arrays or structures bigger than 64K?
  12. How can I find out how much memory is available?
  13. How can I read a directory in a C program?
  14. How can I increase the allowable number of simultaneously open files?
  15. What’s wrong with the call fopen(”c:\newdir\file.dat”, “r”)?
This entry was posted in C++. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

71 Comments on Tricky C questions

  1. St0le
    Posted 10/20/2007 at 11:02 am | Permalink

    Such prog. are called “Quines”. Following is a Basic Quine..

    //Quine By St0le!
    char *p=”#include %c%c char *p=%c%s%c; %c%c void main(){%c%c printf(p,13,10,34,p,34,13,10,13,10,13,10,13,10); %c%c }”;
    void main(){

  2. Devendra Singh
    Posted 12/10/2007 at 7:51 am | Permalink

    I have an query regarding C program…Its how we can execute a piece of code before execution of code written in main function? e.g if in main() we have added two numbers and outside main() we have subtracted two numbers ,so I want that the output of subtractibg two numbers should come first than adding two numbers…

    use #pragma

  3. Panchdev
    Posted 12/18/2007 at 3:42 am | Permalink

    Write a prgm that prints its own output.

    int main(){
    FILE *fp;
    char c;
    return 0;

  4. Posted 1/10/2008 at 12:41 am | Permalink

    Will this program is going to execute ?
    int main()
    return 0;
    if yes why ?
    if no why ?
    please reply fast

  5. brijesh
    Posted 1/21/2008 at 9:29 pm | Permalink

    without using third variable how to swap two variable?
    Advantage : with this method we can avoid overflow.

  6. st0le
    Posted 2/19/2008 at 12:20 am | Permalink

    better yet, use this


  7. Posted 2/24/2008 at 7:17 am | Permalink

    To print a semicolon without using semicolon else where in the program

    void main()

  8. Udaya Prakash
    Posted 3/23/2008 at 1:24 pm | Permalink

    What’s wrong with the call
    fopen(”c:\newdir\file.dat”, “r”)?

    In C strings, \ should be used as \\ escape sequence.
    Therefore the right way of doing this is 
    fopen(”c:\\newdir\\file.dat”, “r”)

  9. brahmanamda kar
    Posted 3/29/2008 at 7:13 am | Permalink

    we can convert decimal  to hexadecimal as follows: printf("%ox "23); or printf("%OX",23); there also method for finding dec to hex,oct.

  10. wingscng
    Posted 4/2/2008 at 7:35 am | Permalink

    To add two numbers without using "+"
    int add(int a, int b)
        int sum, carry;
        sum = a ^ b;
        carry = a & b;
        while (carry != 0)
            carry <<= 1;
            a = sum;
            b = carry;
            sum = a ^ b;
            carry = a & b;
        return sum;

  11. saswat
    Posted 4/19/2008 at 2:26 pm | Permalink

    How can I return multiple values from a function?

    Ans:-If you want to return multiple values from a function better to pass value by reference.So that whatever you made changes to variables it’ll be reflected.You don’t have to return anything.

  12. Posted 5/6/2008 at 5:03 am | Permalink

    void main()
    id *vptr = (void *) malloc(sizeof(void));
    what is the out put?

  13. goutam
    Posted 5/30/2008 at 1:40 am | Permalink

    we can print semocolon by the following program


    jst use the ascii value of it

  14. milon bhattacharya
    Posted 6/3/2008 at 10:09 am | Permalink

    ANSI C defines a macro __FILE__ which containes the filename which is been compiled.
    The simplest solution will be
    FILE *fp = fopen(__FILE__,”r”);For more on macros refer to K&R C programming language….

  15. Posted 8/7/2008 at 4:29 pm | Permalink

    How to print your program code as output??


    FILE *p;
    char c;


    try.c is my source file

  16. Pushpendra Kumar
    Posted 8/14/2008 at 6:29 am | Permalink

    I just want to say that your answer given with the question No. 50 is wrong………..

    The Right Answer is………
    -3 2 2 0

    Please check the answers before publishing it………….

  17. ganesh
    Posted 11/2/2008 at 2:08 am | Permalink

    how to swap two nos without using a third variable?

    int a=5;
    void fun(int);
    void main()
    int a=5,b=6;
    printf(”a=%d b=%d”, a, b );
    void fun(int b)

  18. Posted 11/11/2008 at 7:35 am | Permalink

    For question 67, answer is wrongly specified. Actual answer is

    int a = 5, b = 10;
    a = a + b; // so a = 15
    b = a - b; // So b = 5
    a = a - b; // now a = 10, values swapped

  19. Posted 12/25/2008 at 4:15 am | Permalink

    Q50 & Q66 is Wrong Answer.
    Correct answer is -1,1,1,0

    request to publisher,before published pls ensure that ur answer is correct..
    dont send wrong answer.

  20. ganesh
    Posted 1/9/2009 at 7:10 am | Permalink

    Ans: 50,66 and 69 are incorrect…

    Its Compiler dependent and Machine dependent to implement it..

    Output vary from one to another..

    Check with turboc/gcc/dev-c and so on..
    Correct ans: Undefined Behaviour…

    If it is incorrect,leave me message

  21. Sandeep
    Posted 1/10/2009 at 4:50 pm | Permalink


    char * fileName = “C:\\work\\c\\test.c”;
    char myString[100];

    int main(){
    FILE * pFile = fopen(fileName, “r”);
    while(!feof(pFile)) {
    fgets(&myString[0], 100, pFile);
    return 0;

Post a Comment

Your email is never published nor shared. Required fields are marked *