Pages

Wednesday, February 07, 2007

C Questions: Lesson 3




Back again, here is a new set of 13 questions.
Question No 1: You are given an English sentence, comprising of any number of words, ranging from one to N. You have to write a program to reverse this sentence.

Junk Question: Why does c allows an extra "," in array intialiser?Is there any
advantage of this?

ex: int arr[5]={1,2,3,4,5,};
^^Compiler does not give error for this!

Question No 2: How do you write a program which produces its own source code as its output?

Question No 3: What is the most efficient way to count the number of bits which are set in a value?

4. How can I convert integers to binary or hexadecimal?

5. How can I return multiple values from a function?

6. How can I allocate arrays or structures bigger than 64K?

7. Write a program to set 2nd bit in a 32 bit register with memory location 0×2000?

8. Write function in C that gets array of chars, and search for the longest sequence of repeatedly 1 bits. It returns the the first bit place in the sequence and the number of 1 bits in the sequence. - (a) loop of 2^0, 2^1, … , 2^7 is done with 1<<=j<=7. (b) Take care of remembering the first place of the bit sequence you are counting. 9.You have 16bit register that increment itself and loops about every second. When the register reach 0xffff it will issue an interupt and will run the function update_time(). There is a function unsigned long get_time() that returns the time. You need to implement the two functions. - (a) You need to count every interrupt in order to save the number of seconds. (b) The counter will be the 16bit MSB, and the actual register will be 16bit LSB. (c) If the register will be at ~0xfff0, you will return MSB that is not correct, because you will read the counter, then interrupt will accure and increment by one. Now you have counter that is not correct. (d) You need to check for the (c) problem, and if you catch the problem, you need to read once again the register and the counter before you return them. You depend on the fact the you have about another second until the register will loop.

10.
What is a callback function. Explain using C.

Q11. What is the difference between "gcc -v" and "gcc --version"?
Ans:
[root@monday kongkon]# gcc --version
gcc (GCC) 4.1.0 20060304 (Red Hat 4.1.0-3)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@monday kongkon]# gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.0 20060304 (Red Hat 4.1.0-3)
[root@monday kongkon]#

Q12.
main(){
unsigned i=-20;
printf("a=%d \n" "b=%c \n" "c=%u \n", i, i, i);
}

Ans:
[root@monday kongkon]# gcc 12.c
12.c: In function âmainâ:
12.c:3: warning: incompatible implicit declaration of built-in function âprintfâ
[root@monday kongkon]# ./a.out
a=-20
b=ì
c=4294967276
[root@monday kongkon]#

Q13.
main(){
unsigned char i=-20;
printf("a=%d \n" "b=%c \n" "c=%u \n", i, i, i);
}


Ans:
[root@monday kongkon]# gcc 13.c
13.c: In function âmainâ:
13.c:3: warning: incompatible implicit declaration of built-in function âprintfâ
[root@monday kongkon]# ./a.out
a=236
b=ì
c=236
[root@monday kongkon]#

In regards to Q12 and Q13, see there is a vast difference between this two question. This is the power of C.

No comments: