C Questions & Answers - Embedded System Interview

Hot

Thứ Hai, 20 tháng 1, 2020

C Questions & Answers


Q: In printf() Function- What is the difference between "printf(...)" and "sprintf(...)"?
A: sprintf(...) writes data to the character array whereas printf(...) writes data to the standard output device.

Q: Compilation How to reduce a final size of executable?
A: Size of the final executable can be reduced using dynamic linking for libraries.

Q: Can you tell me how to check whether a linked list is circular?
A: 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) 
    {
        printf("circular");
    }
}

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet.

Q: Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba.
A:
#include <stdio.h>
#include <string.h>

void swap(char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(char *a, int l, int r)
{
   int i;
   if (l == r)
     printf("%s\n", a);
   else
   {
       for (i = l; i <= r; i++)
       {
          swap((a+l), (a+i));
          permute(a, l+1, r);
          swap((a+l), (a+i)); //backtrack
       }
   }
}

void main()
{
    char str[] = "ABC";
    int n = strlen(str);
    permute(str, 0, n-1);
} 

Q: What will be the output of the following code?
void main()
{
    int i = 0 , a[3] ;
    a[i] = i++;
    printf ("%d",a[i]) ;
}
A: The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.

Q: How do I know how many elements an array can hold?
A: The amount of memory an array can consume depends on the data type of an array. In DOS environment, the amount of memory an array can consume depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume more than 64 kb. Consider following program, which shows the maximum number of elements an array of type int, float and char can have in case of Small memory model.
void main()

{

    int i[32767] ;

    float f[16383] ;

    char s[65535] ;

}

Q: How do I write code that reads data at memory location specified by segment and offset?
A: Use peekb() function. This function returns byte(s) read from specific segment and offset locations in memory. The following program illustrates use of this function. In this program from VDU memory we have read characters and its attributes of the first row. The information stored in file is then further read and displayed using peek() function.

#include <stdio.h>
#include <dos.h>

void main()
{
    char far *scr = 0xB8000000 ;
    FILE *fp ;
    int offset ;
    char ch ;
    if((fp = fopen("scr.dat", "wb")) == NULL)
    {
        printf("Unable to open file\n") ;
        exit() ;
    }

    // reads and writes to file
    for( offset = 0 ; offset < 160 ; offset++)
        fprintf (fp, "%c", peekb(scr, offset)) ;

    fclose ( fp ) ;
    if ((fp = fopen("scr.dat", "rb")) == NULL)
    {
        printf("Unable to open file\n") ;
        exit() ;
    }

    // reads and writes to file
    for(offset = 0 ; offset < 160 ; offset++)
    {
        fscanf(fp, "%c", &ch);
        printf("%c", ch);
    }
    fclose(fp) ;
}

Không có nhận xét nào:

Đăng nhận xét

Thường mất vài phút để quảng cáo xuất hiện trên trang nhưng thỉnh thoảng, việc này có thể mất đến 1 giờ. Hãy xem hướng dẫn triển khai mã của chúng tôi để biết thêm chi tiết. Ðã xong