Logo F2FInterview

C Interview Questions

Q   |   QA

Yes

A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as ‘*’,'%’ operators.
It follows a special arithmetic called as pointer arithmetic.

A pointer is declared as:

int *ap;
int a = 5;

In the above two statements an integer a was declared and initialized to 5. A pointer to an integer with name ap was declared.

Next before ap is used

ap=&a;

This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.

Operations on a pointer:

Dereferencing operator ‘ * ‘:

This operator gives the value at the address pointed by the pointer . For example after the above C statements if we give

printf("%d",*ap);

Actual value of a that is 5 would be printed. That is because ap points to a.

Addition operator ‘ + ‘:

Pointer arithmetic is different from ordinary arithmetic.

ap=ap+1;

Above expression would not increment the value of ap by one, but would increment it by the number of bytes of the data type it is pointing to. Here ap is pointing to an integer variable hence ap is incremented by 2 or 4 bytes depending upon the compiler.

A pointer is a special variable in C language meant just to store address of any other variable or function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic operations such as ‘*’,'%’ operators. It follows a special arithmetic called as pointer arithmetic.

A pointer is declared as:

int *ap;
int a = 5;

In the above two statements an integer a was declared and initialized to 5. A pointer to an integer with name ap was declared.

Next before ap is used

ap=&a;

This operation would initialize the declared pointer to int. The pointer ap is now said to point to a.

A structure is a collection of pre-defined data types to create a user-defined data type. Let us say we need to create records of students. Each student has three fields:

int roll_number;
char name[30];
int total_marks;

This concept would be particularly useful in grouping data types. You could declare a structure student as:

struct student {
    int roll_number;
    char name[30];
    int total_marks;
} student1, student2;

The above snippet of code would declare a structure by name student and it initializes two objects student1, student2. Now these objects and their fields could be accessed by saying student1.

roll_number for accesing roll number field of student1 object, similarly student2.

name for accesing name field of student2 object.

A function is called ‘recursive’ if a statement within the body of a function calls the same function. It is also called ‘circular definition’. Recursion is thus a process of defining something in terms of itself.

Example

To calculate the factorial value using recursion.

#include <stdio.h>
int fact(int n);

int main() {
 int x, i;
 printf("Enter a value for x: \n");
 scanf("%d", &x);
 i = fact(x);
 printf("\nFactorial of %d is %d", x, i);
 return 0;
}

int fact(int n) {
 /* n=0 indicates a terminating condition */
 if (n <= 0) {
  return (1);
 } else {
  /* function calling itself */
  return (n * fact(n - 1));
  /*n*fact(n-1) is a recursive expression */
 }
}

Output:

Enter a value for x:

4

Factorial of 4 is 24

Explanation:

fact(n) = n * fact(n-1)

       If n=4

       fact(4) = 4 * fact(3)  there is a call to fact(3)

       fact(3) = 3 * fact(2)

       fact(2) = 2 * fact(1)

       fact(1) = 1 * fact(0)

       fact(0) = 1

 

       fact(1) = 1 * 1 = 1

       fact(2) = 2 * 1 = 2

       fact(3) = 3 * 2 = 6

       Thus fact(4) = 4 * 6 = 24

Terminating condition(n <= 0 here;) is a must for a recursive program. Otherwise the program enters into an infinite loop.

1101100100111100 can be easily converted to hexadecimal numbering system. Hexa-decimal integer constants consist of combination of digits from 0 to 9 and alphabets ‘A’ to ‘F’. The alphabets represent numbers 10 to 15 respectively. Hexa-decimal numbers are preceeded by ’0x’.

1101,1001,0011,1100
      1101 = D
      1001 = 9
      0011 = 3
      1100 = C
1101,1001,0011,1100 = 0xD93C

Thus the given binary number 1101100100111100 in hexadecimal form is 0xD93C

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: