answersLogoWhite

0

📱

C Programming

Questions related to the C Computer Programming Language. This ranges all the way from K&R to the most recent ANSI incarnations. C has become one of the most popular languages today, and has been used to write all sorts of things for nearly all of the modern operating systems and applications. It it a good compromise between speed, power, and complexity.

9,649 Questions

Inventor of C?

A Bell Labs researcher named Dennis Ritchie (who was one of the driving forces behind Unix) created the C programming language.

How do you write a C program that interchanges the odd and even elements of an array?

This is easier to achieve using C++ than C, because we can make use a single template function that can cater for arrays of any type in a type-safe manner. In C, we must use macro trickery to cater for different array types, but macros are not type-safe. However, if we limit ourselves to arrays of integers, we can provide a concrete implementation:

void swap_oddeven (int* p, size_t size) {

if (size<2) return;

for (size_t index=1; index<size; index+=2) swap (p[index], p[index-1]);

}

Is C compiled or interpreted?

Assembler, COBOL, PL/I, C/C++ are all translated by running the source code through a compiler. This results in very efficient code that can be executed any number of times. The overhead for the translation is incurred just once, when the source is compiled; thereafter, it need only be loaded and executed.

Interpreted languages, in contrast, must be parsed, interpreted, and executed each time the program is run, thereby greatly adding to the cost of running the program. For this reason, interpreted programs are usually less efficient than compiled programs.

Some programming languages, such as REXX™ and Java™, can be either interpreted or compiled.

Defference between c plus plus and c?

these are difference in between c and c++: a) C is a SPL and C++ is a OOP. b) C has not concept of object but C++ has this feature. c) C has not 'class' name data type but C++ has.

How do you write c file arguments?

Using parameters argc and argv, and library functions fopen, fprintf, fclose

What is work of continue statement in c?

ANSWER

Continue is used to jump to the end of an iteration in a loop. It is often used in an if statement within a while or for loop to skip the remainder of the loop code.

Example (note: not the easiest way to write this code):

for ( i=0; i<10; i++ )

{

if ( array[i] <= 0 )

{

continue; // do not add this value to the total

}

total += array[i];

}

How do you pass an array to a function in C programming?

The declaration of an array of 3 int pointers will be:

int *p[3];

The declaration of a function that receive an array of int pointers will be:

int my_func(int **p);

or

int my_func(int *p[]);

and you can pass it to other function simply by passing p.

Difference between semantic error and logical errors?

Syntax Error: error due to missing colon, semicolon, parenthesis, etc. Syntax is the way in which we construct sentences by following principles and rules.

Example: In C++, it would be a syntax error to say

int x = "five";

This will not compile because it does not follow the syntax of the language and does not make any sense to the compiler.

Semantic Error: it is a logical error. it is due to wrong logical statements. Semantics is the interpretations of and meanings derived from the sentence transmission and understanding of the message. Semantics errors are Logical, while Syntax errors are code errors.

Example: A semantic error would compile, but be incorrect logically:

const int pi = 12345;

Your program will likely compile and run without error but your results will be incorrect. (Note that these types of errors are usually much harder to debug)

Characteristics of constructor?

Contains an access modifier followed by the name of the class and some parameters. More specifically:

public class MyClass {

//Constructor

public MyClass() {

}

}

What is Array in Programming C?

Arrays allow similar types of data to be stored within a contiguous block of memory such that every data element is accessible in constant time, regardless of its physical location within the array. This is achieved through simple pointer arithmetic treating each element as a memory offset from the start of the array. Since every element is the same length (in bytes), locating any element is simply a matter of calculating its offset from its index. Indices are zero-based thus the third element can be found at index 2. The memory offset for that element is therefore the product of the element size and 2. However, C permits indices to be specified directly, while the pointer arithmetic is done in the background. Thus array_name[2] automatically returns a reference to the third element.

Arrays with large and complex variable length data elements need to store those elements separately from the array, usually non-contiguously. This is achieved by using a pointer array. Pointer arrays are particularly useful when sorting extremely large data lists as it is much easier and more efficient to implement a sorting algorithm with an array than it is with a linked list, particularly when constant-time random-access is essential to the algorithm. The time and effort in building the array is generally more than compensated for by the efficiency of the algorithm.

Arrays can also be divided and subdivided to better model the data they represent. For instance, a chessboard might be implemented as a one-dimensional array of 64 elements, however it makes more sense to model the chessboard in a two-dimensional array of 8x8 elements. Although the array is still allocated contiguously and can be thought of as being 8 rows and 8 columns, it's actually better to think of this two-dimensional array as being a one-dimensional array of 8 elements, where each element is another one-dimensional array of 8 elements.

By thinking this way it makes it possible to allocate extremely large arrays in non-contiguous memory (as completely separate one-dimensional arrays) and also makes comprehension of a four-dimensional array in a three-dimensional world that much easier (unless you actually want to model time and space of course).

A four-dimensional array can be thought of in a variety of ways: as being a one dimensional array of three-dimensional arrays, or as a two-dimensional array of two-dimensional arrays, or as a three-dimensional array of one-dimensional arrays, or even as a one-dimensional array of one-dimensional arrays of one-dimensional arrays of one-dimensional arrays. Whichever method you use to imagine your array is immaterial, so long as it makes sense to you that's all that really matters.

What is a recursive function?

A recursive function is one that calls upon itself until a given result in the original call is met.

Take a look at this example.

Program Recursion;

Uses

crt;

Var

number:longint;

Function Factorial(number:longint):longint;

Begin

if number > 0 then

factorial:=number*factorial(number-1)

else

factorial:=1;

End;

Begin

clrscr;

readln(number);

writeln(factorial(number));

readln;

End.

Note how the function factorial calls itself.

Why use in c programming void main?

The compiler needs to know where a program starts. This is called the entry point of a program. The main function is just the standard way of denoting where the entry point of your program is.

How many different binary trees can be made from three nodes?

As far as i Know, just one.

Do you know any formula to calculate how many binary search trees are possible?

--

answer:

(2n C n) / (n+1) = ( factorial (2n) / factorial (n) * factorial (2n - n) ) / ( n + 1 )

where 'n' is number of element (integer/string)

like:

N Number of BST

1 1

2 2

3 5

4 14

5 42

6 132

and so on

How typedef feature used with structure?

You use typedef to declare a synonym for an existing type. It's generally just a way of reducing a complex or cumbersome declaration outside your code to a more simplified, more easily understood declaration that you can use inside your code.

Cumbersome example:

void DoStuff( void (*)(int&, char& ), int&, char&); // Huh? Do what?

Simplified example:

typedef void (*pFunc) ( int&, char& );

void DoStuff( pFunc, int&, char& ); // Aha! It's a function pointer!

In relation to C structures, typedef provides a way to declare and name user-defined types, primarily so you don't have to use the struct keyword in the variable declaration. C++ structures are more flexible and the typedef keyword is optional.

Structure examples for C:

struct hard

{ int i;double f;

};

// typedef is optional in C++, but required in C.

typedef struct

{

int i;

double f;

} easy;

int main()

{

struct hard hs; // Requires struct keyword

easy es; // Same as C++.

}

Type of binary tree?

  • A rooted binary tree is a tree with a root node in which every node has at most two children.
  • A full binary tree (sometimes proper binary treeor 2-tree or strictly binary tree) is a tree in which every node other than the leaves has two children. Sometimes a full tree is ambiguously defined as a perfect tree.
  • A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level, and in which every parent has two children.[1] (This is ambiguously also called a complete binary tree.)
  • A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.[2]
  • An infinite complete binary tree is a tree with a countably infinite number of levels, in which every node has two children, so that there are 2d nodes at level d. The set of all nodes is countably infinite, but the set of all infinite paths from the root is uncountable: it has the cardinality of the continuum. These paths corresponding by an order preserving bijection to the points of the Cantor set, or (through the example of the Stern-Brocot tree) to the set of positive irrational numbers.
  • A balanced binary tree is commonly defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1,[3] although in general it is a binary tree where no leaf is much farther away from the root than any other leaf. (Different balancing schemes allow different definitions of "much farther"[4]). Binary trees that are balanced according to this definition have a predictable depth (how many nodes are traversed from the root to a leaf, root counting as node 0 and subsequent as 1, 2, ..., depth). This depth is equal to the integer part of where is the number of nodes on the balanced tree. Example 1: balanced tree with 1 node, (depth = 0). Example 2: balanced tree with 3 nodes, (depth=1). Example 3: balanced tree with 5 nodes, (depth of tree is 2 nodes).
  • A rooted complete binary tree can be identified with a free magma.
  • A degenerate tree is a tree where for each parent node, there is only one associated child node. This means that in a performance measurement, the tree will behave like a linked list data structure.

Note that this terminology often varies in the literature, especially with respect to the meaning of "complete" and "full".

Disadvantages of circular link list as compared to singly link list?

As compared with doubly-linked lists, one disadvantage is that singly-linked lists can only be efficiently traversed in one direction. Finding the (n - 1)th element, given only a pointer to the nth, requires scanning the list from the first element up to the (n - 1)th. Thus (if memory is too limited to permit tricks such as creating a reversed list) to iterate over the entire list in reverse order would require n + (n - 1) + ... + 1 = n(n + 1)/2 link traversals, which grows quadratically with n. Obviously for a doubly-linked list the time merely grows linearly with n.

A down-side of all linked lists versus arrays is that random access can be inefficient; the time taken to find an element with a randomly chosen index grows in direct proportion to the list's length (since we must scan from one of the ends). In contrast, an array allows us to index directly to the elements with simple pointer arithmetic, in a time independent of the array's size - at least in an idealised environment.

What is malloc function in C?

The malloc function is a function used in C/C++ and various other high-level programming languges to add memory to the heap, it is essentially a pointer

int* X = malloc(sizeof(int));

Where the parameter to malloc is the number of bytes you want to make room for. Because we are using an integral variable we want to make as much room as an integer takes.

How do you convert digit to word in c?

#include<iostream>

class expand

{

public:

expand(unsigned long long num):value(num){}

std::ostream& operator()(std::ostream& out)const;

private:

unsigned long long value;

static const char * const units[20];

static const char * const tens[10];

};

const char * const expand::units[20] = {"zero", "one", "two", "three","four","five","six","seven",

"eight","nine", "ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",

"eighteen","nineteen"};

const char * const expand::tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",

"eighty","ninety"};

std::ostream &operator<< (std::ostream &out, expand number)

{

return(number(out));

}

std::ostream &expand::operator()(std::ostream &out) const

{

const unsigned long long quintillion=1000000000000000000;

const unsigned long long quadrillion=1000000000000000;

const unsigned long long trillion=1000000000000;

const unsigned long long billion=1000000000;

const unsigned long long million=1000000;

const unsigned long long thousand=1000;

const unsigned long long hundred=100;

const unsigned long long twenty=20;

const unsigned long long ten=10;

unsigned long long multiple=quintillion;

unsigned long long remain;

if(value>=thousand)

{

while(multiple>value&&(multiple!=quintillionmultiple!=quadrillion

multiple!=trillionmultiple!=billionmultiple!=millionmultiple!=thousand))

multiple/=1000;

out<<expand(value/multiple);

switch(multiple)

{

case(quintillion):out<<"-quintillion"; break;

case(quadrillion):out<<"-quadrillion"; break;

case(trillion):out<<"-trillion"; break;

case(billion):out<<"-billion"; break;

case(million):out<<"-million";break;

case(thousand):out<<"-thousand";break;

}

if(remain=value%multiple)

{

if(remain<hundred)

out<<"-and";

out<<"-"<<expand(remain);

}

}

else if(value>=hundred)

{

out<<expand(value/hundred)<<"-hundred";

if(remain=value%hundred)

out<<"-and-"<<expand(remain);

}

else if(value>=twenty)

{

out<<tens[value/ten];

if(remain=value%ten)

out<<"-"<<expand(remain);

}

else

out<<units[value];

return(out);

}

int main()

{

for(unsigned long long ull=10000000; ull<100000000; ++ull)

std::cout<<expand(ull)<<std::endl;

return(0);

}

What is the difference between breadth first search and best first search?

The main difference is that depth-first uses a stack while breadth-first uses a queue.

To illustrate, imagine a binary tree where every node has up to two child nodes and some data. We begin at the root in both cases. With breadth-first, we enqueue the root. We then begin an iterative process. First, we dequeue a node. If the node contains the data being sought then we're done. Otherwise we enqueue the node's immediate children. If the queue is empty, the data being sought does not exist and we're done. Otherwise we begin a new iteration.

With depth first we do the same thing except we stack the nodes (push and pop rather than enqueue and dequeue). Queues are a FIFO structure (first in, first out) while stacks are LIFO (last in, first out). This dramatically alters the sequence in which nodes are examined. Breadth-first examines nodes in sequence, row by row, whereas depth-first examines the depths of the left hand side of each node before examining the depths of the right hand side of each node.

Depth-first is ideally suited to brute force backtracking algorithms (particularly NP-complete problems) as well as for rapidly building sorted lists from unsorted sequential data. Breadth-first search is better suited to creating diagrams from binary trees because a single pass can determine the number of levels and the maximum width required to display the tree, while a second pass can build the diagram one row at a time (typical breadth-first implementations will maintain the width and height as internal members to avoid recalculating them).

Because depth-first employs a stack, implementations often make use of recursion rather than iteration, thus taking advantage of the call stack to provide the necessary backtracking. However, an iterative approach is usually more efficient, particularly if the tree depth exceeds the compiler's ability to inline expand the recursions.

What is the difference between sign and unsign in Java?

Normally, signed and unsigned data types just refer to whether or not a value can be negative or not.

An unsigned 4-bit value can be the values 0 to 15

A signed 4-bit value can be the values -8 to 7

However, there is no such thing as an unsigned value in Java*. All primitive types are signed by default and cannot change.

*Note that technically a char value can be considered an unsigned type. The only way to see this is to declare a char with value '\uffff' (or 65535) and try to print it out as both a short and an int. If you try this with any other data types, the larger values will display the same as the smaller values. Not so with the char example.

Can you use the same function name for a member functoin of a class and an outside function in the same program file?

Yes you can use the same function name for a member function and an external function. They are primarily distinguished by the number and type of arguments they accept (the function signature). If they match exactly, then the scope resolution operator (::) is used to differentiate them by namespace. The class namespace is the class name itself. The external function uses global scope unless scoped to another namespace. When the scope is not explicitly stated, then the scope is implied by the call site.

Note that whenever there is any ambiguity about which function is implied, the compiler will emit an error indicating where the ambiguity lies, and the program will ultimately fail to compile.