Open In App

AKTU 1st Year Sem 2 Solved Paper 2015-16 | COMP. SYSTEM & C PROGRAMMING | Sec C

Last Updated : 01 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Paper download link: Paper | Sem 2 | 2015-16

B.Tech. (SEM-II) THEORY EXAMINATION 2015-16 COMPUTER SYSTEM & PROGRAMMING IN C

Time: 3hrs Total Marks: 100 Note:-

  • There are three sections. Section A carries 20 marks, Section B carries 50 marks and Section C carries 30 marks.
  • Attempt all questions. Marks are indicated against each question.
  • Assume suitable data wherever necessary.

Section – C

Attempt two parts: (15*2 = 30)
3. What are pre-processor directives? Explain any three of them.

  • Conditional Compilation: Conditional Compilation directives help to compile a specific portion of the program or let us skip compilation of some specific part of the program based on some conditions. In our previous article, we have discussed about two such directives ‘ifdef‘ and ‘endif‘. In this post we will discuss #ifndef, #if, #else and #elif.
    1. #ifdef: This directive is the simplest conditional directive. This block is called a conditional group. The controlled text will get included in the preprocessor output if the macroname is defined. The controlled text inside a conditional will embrace preprocessing directives. They are executed only if the conditional succeeds. You can nest these in multiple layers, but they must be completely nested. In other words, ‘#endif’ always matches the nearest ‘#ifdef’ (or ‘#ifndef’, or ‘#if’). Also, you can’t begin a conditional group in one file and finish it in another. Syntax:
#ifdef MACRO
    controlled text
#endif /* macroname */
  1. #ifndef: We know that the in #ifdef directive if the macroname is defined, then the block of statements following the #ifdef directive will execute normally but if it is not defined, the compiler will simply skip this block of statements. The #ifndef directive is simply opposite to that of the #ifdef directive. In case of #ifndef, the block of statements between #ifndef and #endif will be executed only if the macro or the identifier with #ifndef is not defined. Syntax :
ifndef macro_name
    statement1;
    statement2;
    statement3;
    .
    .
    .
    statementN;
endif
  1. If the macro with name as ‘macroname‘ is not defined using the #define directive then only the block of statements will execute.
  2. #if, #else and #elif: These directives works together and control compilation of portions of the program using some conditions. If the condition with the #if directive evaluates to a non zero value, then the group of line immediately after the #if directive will be executed otherwise if the condition with the #elif directive evaluates to a non zero value, then the group of line immediately after the #elif directive will be executed else the lines after #else directive will be executed. Syntax:
#if macro_condition
   statements
#elif macro_condition
   statements
#else
   statements
#endif
  1. Example: 

CPP




#include <iostream>
 
#define gfg 7
 
#if gfg > 200
#undef gfg
#define gfg 200
#elif gfg < 50
#undef gfg
#define gfg 50
#else
#undef gfg
#define gfg 100
#endif
 
int main()
{
    std::cout << gfg; // gfg = 50
}


  1. Output:
50
  1. Notice how the entire structure of #if, #elif and #else chained directives ends with #endif.
  • Line control ( #line ): Whenever we compile a program, there are chances of occurrence of some error in the program. Whenever compiler identifies error in the program it provides us with the filename in which error is found along with the list of lines and with the exact line numbers where the error is. This makes easy for us to find and rectify error. However we can control what information should the compiler provide during errors in compilation using the #line directive. Syntax:
#line number "filename"
  • number – line number that will be assigned to the next code line. The line numbers of successive lines will be increased one by one from this point on. “filename” – optional parameter that allows to redefine the file name that will be shown.
  • Error directive ( #error ): This directive aborts the compilation process when it is found in the program during compilation and produces an error which is optional and can be specified as a parameter. Syntax:
#error optional_error
  • Here, optional_error is any error specified by the user which will be shown when this directive is found in the program. Example: 

CPP




#ifndef GeeksforGeeks
#error GeeksforGeeks not found !
#endif


  • Output:
error: #error GeeksforGeeks not found !

4. (i) Explain loading and linking of a program in detail. 
 

  • Loader is the program of the operating system which loads the executable from the disk into the primary memory(RAM) for execution. It allocates the memory space to the executable module in main memory and then transfers control to the beginning instruction of the program . Example: 

CPP




akash @aix(/ u / akash) #cat./ ak1.cpp
#include <stdio.h>
                               int main()
{
    printf("Testing of Loader !");
    return 0;
}


  • Compiling by xlC compiler:

akash @aix(/ u / akash) #xlC – o ak1.out./ ak1.cpp akash @aix(/ u / akash) #ls – lrt ak1 * -rw – rw – r– 1 akash dev 74 Nov 12 06 : 10 ak1.cpp – rwxrwxr – x 1 akash dev 8562 Nov 12 06 : 34 ak1.out akash @aix(/ u / akash) #

  • What really happens while running the executable: One could also use strace command for the same.

akash@aix(/u/akash)# truss ./ak1.out execve(“./ak1.out”, 0x2FF20A00, 0x200138A8) argc: 1 read_sysconfig(0xF06F8278, 0x00000010, 0xFFFFFFF9, 0x10000000, 0x200007BC, 0x000000C0, 0x06010000, 0xF076A0F0) = 0x00000000 sbrk(0x00000000) = 0x20000998 vmgetinfo(0x2FF20350, 7, 16) = 0 sbrk(0x00000000) = 0x20000998 sbrk(0x00000008) = 0x20000998 __libc_sbrk(0x00000000) = 0x200009A0 loadquery(2, 0x200009C8, 0x00001000) = 0 __loadx(0x0A040000, 0xF06F599C, 0x00000000, 0xF05BE208, 0x20001D20) = 0xF05BFD64 loadbind(0, 0xF0760BBC, 0xF06D0E54) = 0 kfcntl(0, F_GETFL, 0x00000000) = 67110914 kfcntl(1, F_GETFL, 0x00000000) = 67110914 kfcntl(2, F_GETFL, 0x00000000) = 67110914 kfcntl(2, F_GETFL, 0x00000000) = 67110914 kioctl(1, 22528, 0x00000000, 0x00000000) = 0 Testing of Loader !kwrite(1, ” T e s t i n g o f L”.., 19) = 19 kfcntl(1, F_GETFL, 0x00000070) = 67110914 kfcntl(2, F_GETFL, 0x2FF22FFC) = 67110914 _exit(0)

  • The first call which is displayed is ‘execve()‘ which actually is the loader . This loader creates the process which involves:
    • Reading the file and creating an address space for the process.
    • Page table entries for the instructions, data and program stack are created and the register set is initialized.
    • Then, Executes a jump instruction to the first instruction of the program which generally causes a page fault and the first page of your instructions is brought into memory.
  • Linking This is the final phase in which all the linking of function calls with their definitions are done. Linker knows where all these functions are implemented. Linker does some extra work also, it adds some extra code to our program which is required when the program starts and ends. For example, there is a code which is required for setting up the environment like passing command line arguments. This task can be easily verified by using $size filename.o and $size filename. Through these commands, we know that how output file increases from an object file to an executable file. This is because of the extra code that linker adds with our program. compil8 Note that GCC by default does dynamic linking, so printf() is dynamically linked in above program. Refer this, this and this for more details on static and dynamic linkings.

4. (ii) Write a program in C that will read a positive number from the keyboard and print it in reverse order.
Example:

Input: 24578 
Output: 87542

C




#include <stdio.h>
 
/* Iterative function to reverse digits of num*/
int reverseDigits(int num)
{
    int rev_num = 0;
    while (num > 0) {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
 
/* Driver program to test reverseDigits */
int main()
{
    int num;
 
    scanf("%d", &num);
    printf("Reverse of no. is %d",
           reverseDigits(num));
 
    return 0;
}


Output:

2654

5. Suppose a file contains student’s records with each record containing name and age of a student. Write a C program to read these records and display them in sorted order by name.
 

C




// C program to read Student records
// like id, name and age,
// and display them in sorted order by Name
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
// struct person with 3 fields
struct Student {
    char* name;
    int id;
    char age;
};
 
// setting up rules for comparison
// to sort the students based on names
int comparator(const void* p, const void* q)
{
    return strcmp(((struct Student*)p)->name,
                ((struct Student*)q)->name);
}
 
// Driver program
int main()
{
    int i = 0, n = 5;
 
    struct Student arr[n];
 
    // Get the students data
    arr[0].id = 1;
    arr[0].name = "bd";
    arr[0].age = 12;
 
    arr[1].id = 2;
    arr[1].name = "ba";
    arr[1].age = 10;
 
    arr[2].id = 3;
    arr[2].name = "bc";
    arr[2].age = 8;
 
    arr[3].id = 4;
    arr[3].name = "aaz";
    arr[3].age = 9;
 
    arr[4].id = 5;
    arr[4].name = "az";
    arr[4].age = 10;
 
    // Print the Unsorted Structure
    printf("Unsorted Student Records:\n");
    for (i = 0; i < n; i++) {
        printf("Id = %d, Name = %s, Age = %d \n",
            arr[i].id, arr[i].name, arr[i].age);
    }
    // Sort the structure
    // based on the specified comparator
    qsort(arr, n, sizeof(struct Student), comparator);
 
    // Print the Sorted Structure
    printf("\n\nStudent Records sorted by Name:\n");
    for (i = 0; i < n; i++) {
        printf("Id = %d, Name = %s, Age = %d \n",
            arr[i].id, arr[i].name, arr[i].age);
    }
 
    return 0;
}


Output:

Unsorted Student Records:
Id = 1, Name = bd, Age = 12 
Id = 2, Name = ba, Age = 10 
Id = 3, Name = bc, Age = 8 
Id = 4, Name = aaz, Age = 9 
Id = 5, Name = az, Age = 10 


Student Records sorted by Name:
Id = 4, Name = aaz, Age = 9 
Id = 5, Name = az, Age = 10 
Id = 2, Name = ba, Age = 10 
Id = 3, Name = bc, Age = 8 
Id = 1, Name = bd, Age = 12


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads