C -Set 1

**Registers are faster than memory to access, so the variables which are most frequently used in a C program can be put in registers using register keyword. The keyword register hints to compiler that a given variable can be put in a register. It’s compiler’s choice to put it in a register or not. Generally, compilers themselves do optimizations and put the variables in register.
1) If you use & operator with a register variable then compiler may give an error or warning (depending upon the compiler you are using), because when we say a variable is a register, it may be stored in a register instead of memory and accessing address of a register is invalid. Try below program.
int main()
{
  register int i = 10;
  int *a = &i;
  printf("%d", *a);
  getchar();
  return 0;
}
Register is a storage class, and C doesn’t allow multiple storage class specifiers for a variable. So, register can not be used with static . Try below program.
int main()
{
  int i = 10;
  register static int *a = &i;
  printf("%d", *a);
  getchar();
  return 0;
}



**In C, static storage class cannot be used with function parameter.
 Declaring a global variable as static limits its scope to the same file in which it is defined. A static function is only accessible to the same file in which it is defined. A local variable declared as static preserves the value of the variable between the function calls.
int main()
{
  int x = 10;
  static int y = x;
  
  if(x == y)
     printf("Equal");
  else if(x > y)
     printf("Greater");
  else
     printf("Less");
  return 0;
}  CompiletimeError
In C, static variables can only be initialized using constant literals. This is allowed in C++ though.

#include<stdio.h>
int main()
{
   typedef int i;
   i a = 0;
   printf("%d", a);
   return 0;
}
There is no problem with the program. It simply creates a user defined type i and creates a variable a of type i.   so ans: 0

#include <stdio.h>
int main()
{
    static int i=5;
    if (--i){
        printf("%d ",i);
        main();
    }
}  Since i is static variable, it is shared among all calls to main(). So is reduced by 1 by every function call.


#include <stdio.h>
int main()
{
    if (sizeof(int) > -1)
        printf("Yes");
    else
        printf("No");
    return 0;
}  
In C, when an intger value is compared with an unsigned it, the int is promoted to unsigned. Negative numbers are stored in 2's complement form and unsigned value of the 2's complement form is much higher than the sizeof int.

**#include <stdio.h>
int main()
{
    char a = '\012';

    printf("%d", a);

    return 0;
}The value '\012' means the character with value 12 in octal, which is decimal 10



**Which of the following is not a valid declaration in C?
1. short int x;
2. signed short x;
3. short x;
4. unsigned short x;
All are valid. First 3 mean the same thing. 4th means unsigned.


**strcat() Function concatenates two strings together and has the following form:
strcat(string1,string2);
strcmp( ) is the string comparison function defined in string.h header file. It has
the following form:.
int strcmp ( const char *s1, const char *s2 );
strcpy ( ) function is just like a string-assignment operator which take the
following form:
char *strcpy ( char *dest, const char *src );

**getc( ): reads a character from a file. This function is used to read a character
from a file that has been opened in the read mode. For example, the statement
c=getc(fp2); would read a character from the file whose file pointer is fp2.

ftell( ): gives the current position in the file from the start. ftell takes a file
pointer and returns a number of type long, that corresponds to the current
position. For example:n=ftell(p);

fgets( ): To read a line of characters from a file,we use the fgets() library
function. The prototype is char *fgets(char *str, int n, FILE
*fp);The argument str is a pointer to a buffer in which the input is to be
stored, n is the maximum number of characters to be input, and fp is the pointer
to type FILE that was returned by fopen() when the file was opened.

rewind( ):sets the position to the beginning of the file. It also takes a file
pointer and reset the position to the start of the file. For example:
rewind(fp);
n=ftell(fp); would assign 0 to n because file pointer has been set to the
start of the file by rewind.

fseek( ):sets the position to a desired point in the file. fseek function is used
to move the file position to a desired location within the file. For example:
fseek(fp,m,0); would move the file pointer to (m+1)th byte in the file.


**Linked list: A linked list is a self referential structure which contain a member field
that point to the same structure type. In simple term, a linked list is collections of
nodes that consists of two fields, one containing the information about that node, item
and second contain the address of next node. Such a structure is represented as
follows:
struct node
{ int item;
struct node *next;
};
Info part can be any of the data type; address part should always be the structure
type.
Linked lists are of following types:
1. Linear Singly linked list: A list type in which each node points to the next
node and the last node points to NULL.
2. Circular linked list: Lists which have no beginning and no end. The last
node points back to the first item.
3. Doubly linked list or Two-way linked list: These lists contain double set of
pointers, one pointing to the next item and other pointing to the preceding item.
So one can traverse the list in either direction.
4. Circularly doubly linked list: It employs both the forward and backward
pointer in circular form.
SHARE

About df

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment