C Programs Asked In Tcs Interview
What is the difference between declaration and definition of a variable/functionAns: Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function.
So that’s all about declaration. Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a super set of declaration.
(or declaration as a subset of definition). From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function). // This is only declaration. Y is not allocated memory by this statementextern int y;// This is both declaration and definition, memory to x is allocated by this statement.int x.
Tcs Interview Questions For Experienced
FilternoneSee.When should we use pointers in a C program?1. To get address of a variable2. For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.3. To pass large structures so that complete copy of the structure can be avoided.4.
To implement “linked” data structures like linked lists and binary trees.What is NULL pointer?Ans: NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.What is Dangling pointer?Ans: Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. Following are examples.
FilternoneWhat are static functions? What is their use?Ans:In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files. See for examples and more details.
Practices on C. CYou may also like:Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.