Skip to main content

G-Fact


  • In C language, sizeof( ) is an operator. Though it looks like a function, it is an unary operator.




  • To know the IP address(es) of a URL/website, nslookup can be used at the shell/command prompt (cmd.exe). It works on both types of operating systems i.e. Linux/Windows.


                                     For example : nslookup www.geeksforgeeks.org 



  • In ISO C, you can define main either to take no arguments, or to take two arguments that represent the command line arguments to the program, like this:

int main (int argc, char *argv[])
 Other platform-dependent formats are also allowed by the C and C++ standards; for example, Unix (though not POSIX.1) and Microsoft Visual C++ have a third argument giving the program’s environment, otherwise accessible through getenv in stdlib.h:



  • In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.




  • A large proportion of programming languages are bootstrapped, including BASIC, C, Pascal, Factor, Haskell, Modula-2, Oberon, OCaml, Common Lisp, Scheme, and more.





Example for inline functions
inline int max(int a, int b)
{
  if (a > b)
    return a;
  else
    return b;
} 

a = max (x, y); 
/*
  This is now equivalent to 
  if (x > y)
    a = x;
  else
    a = y;
*/
Example for variable length arrays
float read_and_process(int n)
{
    float   vals[n];
 
    for (int i = 0; i < n; i++)
        vals[i] = read_val();
    return process(vals, n);
}


  • “Pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different”– Wayne Throop




  • To uniquely construct a Binary Tree, Inorder together with either Postorder or Preorder must be given (See this for details). However, either Postorder or Preorder traversal is sufficient to uniquely construct a Binary Search Tree. To construct Binary Search tree, we can get Inorder traversal by sorting the given Preorder or Postorder traversal. So we have the required two traversals and can construct the Binary Search Tree.










  • Enumeration constants (enum values) are always of type int in C, whereas they are distinct types in C++ and may have size different from that of int.






  • Following relationship holds in any n-ary tree in which every node has either 0 or n children.

                                                              L = (n-1)*I + 1

                     Where L is the number of leaf nodes and I is the number of internal nodes.


Proof:

The tree is n-ary tree. Assume it has T total nodes, which is sum of internal nodes (I) and leaf nodes (L). A tree with T total nodes will have (T – 1) edges or branches.

In other words, since the tree is n-ary tree, each internal node will have n branches contributing total of n*I internal branches. Therefore we have the following relations from the above explanations,

n*I = T – 1
L + I = T

From the above two equations, it is easy to prove that L = (n – 1) * I + 1.






  • In C, struct keyword must be used for declaring structure variables, but it is optional in C++.

For example, following program gives error in C and works in C++.
struct node {
   int x;
   node *next; // Error in C, struct must be there. Works in C++
};
int main()
{
   node a;  // Error in C, struct must be there. Works in C++
}
And following program works in both C and C++.
struct node {
   int x;
   struct node *next;  // Works in both C and C++
};
int main()
{
   struct node a;  // Works in both C and C++
}



  • const Behaviour in C and C++

In C, the const qualified identifiers will have external linkage, where as in C++ it will have internal linkage. For example,
In C++, the following statement
float const interest_rate = 9.25;
is implicitly defined as
static float const interest_rate = 9.25;
i.e. the scope of interest_rate is limited to the block in which it is defined.
In C, the above statement will have external linkage when defined at file scope, i.e. it will be visible outside the current translation unit (source file).
The internal linkage of const qualified variables have some advantages in C++. 



  • .In C, a structure cannot have static members, but in C++ a structure can have static members.

For example, following program causes compilation error in C, but works in C++.
#include<stdio.h>

struct test {
  static int i;  // Error in C, but works in C++.
};

int main()
{
  struct test t;    
  getchar();
  return 0;
}






  • Predict the output of following program.

#include <stdio.h>
int main() 
{
   int x = 012;
   printf("%d",  x);
   getchar();
   return 0;
}
The program prints 10. Putting a 0 before an integer constant makes it an octal number and putting 0x (or 0X) makes it a hexadecimal number. It is easy to put a 0 by accident, or as a habit. The mistake is very common with beginners.







Comments

Popular posts from this blog

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…

Hackerrank > SQL > Basic Select

Select
01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as