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

What is ampersand in C Language?

The ampersand '&' has many uses in the C++ language:

The single ampersand can be used to reference the pointer address of a pointer:

int* pointer;

int* anpointer;

anpointer = &pointer;

This example, although perhaps not valid, shows that the anpointer reference is now the same as the reference to the pointer memory address.

The single ampersand can also be used to reference an object in a function:

void function( int param1, int param2, int &reference );

If this function were to be called, and the reference object altered within the function, the actualy object that was passed into the function would be altered.

The double ampersand '&&' specifies that the left AND the right concepts must both be true before the whole statement is true. For example:

if( conceptA true )

{

conceptC = true;

}

Why is C plus plus such a robust programming language?

Borland's implementation of C++ was never regarded as the Bible of Computer Programming (whatever that means). If it were we'd probably still be using it today, but it was rendered obsolete in 1997 when Borland C++ Builder superseded it.

What is difference between constant in c plus plus and final in java?

The final keyword can be used to modify a class, method, or variable.

When used on a variable, it means that variable cannot be changed once set.
When used on a method, it means that no subclasses may override that method.

What is Runtime Error?

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination (compare compile time). The term is also used as a short form when referring to a runtime library (without a space), a program or library of basic code that is used by a particular computer language to manage a program written in that language while it is running. A runtime environment is a virtual machine state which provides software services for processes or programs while a computer is running. It may pertain to the operating system itself, or the software that runs beneath it.

How many operators in c?

Quite a few. Some of them are:

, () [] & * . ->

+ ++ += - -- -=

* / % *= /= %=

! == <= >= < > !=

<< >> >>= <<=

& | ^ ~

&&

What are the disadvanteges of high level programming language?

Depends on the purpose of the program your creating. Generally speaking high level languages require far more system resources than low level languages. So if you were writing embedded code on a microcontroller, writing in Java would be a big no no as you'd barely be able to do anything whereas in C or assembler you could write a functional and useful program. (C is often used for faster development and high accuracy whereas assembler can be moulded to high performance low accuracy).

What is the importance of an array?

The primary use of an array is create a collection of anonymous variables of the same type.

Although we can name all our variables at compile time, we can only do so if we know precisely how many variables we require at compile time. Even then, if the number of variables is large enough, naming and referring to each one by name can be impractical; imagine naming a million integer variables individually!

Arrays allow us to refer to a collection of variables through a single name; the array name. Every variable in an array must be of the same type. The variables are known as "elements" and the elements must be stored contiguously (one after the other). The array name serves as a reference to the start address of the array and thus to the first element of the array. All other elements are anonymous.

For any given type T, the type T[n] is an array of n elements of type T, where n is a constant. The number of elements in an array is limited only by available memory, bearing in mind that memory must be allocated as a single contiguous block. Arrays can also be disk-based however the same principals apply to both disk-based and memory-based arrays.

Consider the following declaration:

int x[1000];

The name x refers to an array of 1000 integer elements. The amount of memory physically allocated to this array is 1000 * sizeof (int) bytes. If we suppose that sizeof (int) is 4 bytes, the amount of memory allocated will be exactly 4000 bytes. The compiler can work this out from the declaration alone.

The compiler can also work out the address of every element within the array using a zero-based index. We use the subscript operator to refer to individual elements by index:

x[10] = 42;

Here, we've assigned the value 42 to the 11th element (index 10). The compiler calculates the address of the 11th element using trivial pointer arithmetic:

sizeof (int) * 10 + x;

Note that x, &x and &x[0] all refer to the same memory address (the address of the 1st element).

Arrays may be fixed-length or variable-length. We use fixed-length arrays when we know exactly how many elements we need at compile time. Fixed-length arrays may be allocated statically (in the program's data segment), on the call stack or on the heap (the free store). Variable-length arrays can only be allocated on the heap.

To create an array on the heap we need to use a pointer variable to keep track of the start address:

void f (const int elements) {

int* p1 = malloc (1000 * sizeof (int)); // fixed-length array on the heap

int* p2 = malloc (elements * sizeof (int)); // variable-length array on the heap

// use arrays...

p1[10] = 42;

// ...

free (p2);

free (p1);

}

Note that we can use the array subscript operator with any pointer type except void*. This is because the compiler needs to know the length of the type being pointed at, but sizeof (void) is meaningless.

Whenever we pass arrays to functions we must always pass the length of the array through a separate argument. This is because an array implicitly converts to a pointer to the first element and we cannot determine how many elements are actually being referred to from the pointer alone:

void f (int a[], unsigned len) {

for (int i=0; i<len; ++i) printf ("%d\n", a[i]);

}

The only exceptions to this are null-terminated arrays or arrays that use some pre-determined value to mark the end of the array. Character arrays (strings) are a typical example:

void g (char* str) {

printf ("%s", str); // Note: str must be null-terminated!

}

Note that, by convention, we use char* when referring to a null-terminated string rather than char[]. The latter is conventionally used to denote a non-null terminated string in which case we must also pass the string's length:

void h (char str[], unsigned len) { for (int i=0; i<len; ++i) printf ("%c", str[i]);

}

How can you pass variable arguments to a function in c?

## posted BY Pulkit ok. so to declare variable argements in arguments use this: ret_type function_name( . . .); using these three dots will tell compiler to include variable aarguments. Note: this feature has been included in New version of C,, this is not available in old compiler. thankx guys n grls ## posted BY Pulkit ok. so to declare variable argements in arguments use this: ret_type function_name( . . .); using these three dots will tell compiler to include variable aarguments. Note: this feature has been included in New version of C,, this is not available in old compiler. thankx guys n grls

Can a queue be represented by a circular linked list with only one pointer pointing to the tail of the queue?

Yes.

With a linked list, each node in the list points to the next node, except the tail which points to nothing. To maintain the list we must keep track of the head node at all times. In order that all insertions and extractions occur in constant time, all insertions and extractions must occur at the head. Thus when we insert a new node, it simply points to the head and then becomes the head. And to extract the head, the next node from the head becomes the head before we delete the old head. Since all insertions and extractions occur at the head, linked lists can be used to implement a stack (last in, first out). We can also traverse the list from the head and insert at any point in the list in order to maintain a sorted order, however this cannot be done in constant time and there are more efficient ways of maintaining order than by a linked list.

In order to implement a queue (first in, first out) using a linked list, we need to maintain a pointer to the tail as well as the head. Extractions still occur at the head, as before but insertions now occur at the tail, where the tail points to the new node which then becomes the tail. However, rather than maintaining a separate pointer for the head, we can simply point the tail at the head, thus creating a circular linked list. Since the tail always points at the head we have constant time access to both through a single pointer. Insertions are only slightly more complicated in that new nodes must first point at the head node (which they can copy from the tail node) before the tail node points to the new node which then becomes the tail.

Area of square?

A "square" is a flat shape, depicted on a piece of paper or other flat surface.

Since it has only two dimensions, it has no volume. In other words, a square

can't hold any water.

Why do need Unicode when you have Ascii?

ASCII only has 127 standard character codes and only supports the English alphabet. While you can use the extended ASCII character to provide a set of 256 characters and thus support other languages there's no guarantee that other systems will use the same code page, so the characters will not display correctly across all systems (the characters you see will depend upon which code page is currently in use). Moreover, some languages, particularly Chinese, have thousands of symbols that simply cannot be encoded in ASCII.

UNICODE encoding supports all languages and the first 127 symbols are also the same as ASCII, so all characters appear the same across all systems. UTF8 is the most common UNICODE encoding in use today because it uses one-byte per character for the first 127 characters and is therefore fully compliant with non-extended ASCII. If the most-significant bit is set then the character is represented by 2 or more bytes, the combination of which maps to the UNICODE encoding.

How do you make flowchart of ascending order using raptor?

Hey sunil i have an idea about mode that is more repeated data is mode ok

na

Example in the series 2,4,5,2,4,5,2,4,24,22,2,2,7,8,9,2 Mode for this data is 2 i.e. Answer is 2

How are Structure passing and returning implemented by the complier?

When structures are passed as arguments to functions, the entire structure is typically pushed on the stack, using as many words as are required. (Programmers often choose to use pointers to structures instead, precisely to avoid this overhead.) Some compilers merely pass a pointer to the structure, though they may have to make a local copy to preserve pass-by-value semantics. Structures are often returned from functions in a location pointed to by an extra, compiler-supplied ``hidden'' argument to the function. Some older compilers used a special, static location for structure returns, although this made structure-valued functions non-reentrant, which ANSI C disallows

How do you Write a program in c to calculate the sum of two integers?

/*use "c-free" compiler*/

#include <stdio.h>

main()

{

int a,b,c;

printf("enter the value of a & b");

scanf("%d%d",&a,&b);

c=a+b;

printf("sum of the two numbers is a+b- %d",c);

getch();

}

What is non linear array?

What is ARRAY, explain its typesARRAY: -

It is the combination of same data type or if same data is needed more then none time then we use array.

Types Normally there are two types of array.

(1) One dimensional array

(2) Two dimensional array

(1) One dimensional array: -

This array is in form a list (vertical\Horizontal) the data input output or process in one dimensional array with the help of loop.

The syntax of one dimensional array will be as:

Array name [strength]

For example

A [5];

The structure of above syntax will be as:

A (0)

A (1)

A (2)

A (3)

A (4)

(2) Two dimensional array: -

Two dimensional array is also called a table because it consist of rows and columns. The syntax of two dimensional arrays will be as:

Data type array name [rows] [columns]

For example

Int [3] [2];

The structure of above syntax will be as:

A [0] [0] A [0] [1]

A [1] [0] A [1] [1]

A [2] [0] A [2] [1]

The data input output are process in this array with the help of nested loop.

Write a program to print first 'n' Fibonacci 100 numbers?

#include<stdio.h> #include<conio.h> void fibo(int); void main() { int num; clrscr(); printf("\n\t Enter number of elements in series : "); scanf("%d",&num); if(num>0) fibo(num); else printf("\n\t Please enter positive number "); } void fibo(int num) { int a=0,b=1,c=0; if(num==1) printf("\n%d",a); if(num>=2) printf("\n%d\t%d\t",a,b); for(;num>2;num--) { c=a+b; a=b; b=c; printf("%3d\t",c); } getch(); }

Does The statements within a while loop execute at least once?

No. If the loop condition is not satisfied then the loop would not be executed even once.

while(condition)

{

.....

/statements

.....

}

here,when the condition is true then the statements will be executed. Otherwise they would be skipped without being executed.

Only for do.. while loops this execution at least once holds good.

Give me if else statement?

Use an else if immediately after the body of an ifwhen you have more than one expression to evaluate. Expressions are evaluated in sequence (top to bottom) and the first evaluation that is true (non-zero) will be the one that executes the statements in its body. Any and all of the remaining expressions are ignored, even if they would have evaluated true.

In the following example, x, y and z are random integers that could be 0 or 1. We evaluate them one by one looking for the first that is non-zero. The comments show what we know to be true about X, Y, and Z at that point, based upon the evaluations that came before.

if( x=1 )

{

// X is definitely 1, but Y and Z might also be 1.

// do something..

}

else if( y=1 )

{

// X is not 1, but Y is definitely 1. Z might also be 1.

// do something else..

}

else if( z = 1 )

{

// Neither X nor Y is 1, but Z definitely is 1.

// do something else..

}

else

{

// X, Y and Z are all 0.

// when all else fails...

}

How do you write a C program to print the diagonal element of a NxN matrix?

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3],b[3][3],c[3][3],r,c;

for(r=0;r<=3;r++)

{

for(c=0;c<3;c++)

{

printf("\n enter the value=");

scanf("%d%d",&a[r][c],&b[r][c]);

}

}

printf("\n first matrix=\n");

for(r=0;r<=3;r++)

{

for(c=0;c<3;c++)

{

printf("%d\t",a[r][c]);

}

printf("\n");

}

printf("\n scond matrix=\n");

for(r=0;r<=3;r++)

{

for(c=0;c<3;c++)

{printf("%d\t",b[r][c]);

}

printf("\n");

}

printf("\n sum of given matrix=\n");

for(r=0;r<=3;r++)

{

for(c=0;c<3;c++)

{

c[r][c]=a[r][c]+b[r][c];

printf("%d\t",c[r][c]);

}

printf("\n");

}

getch();

}

How many times will a for loop be executed?

As many times as necessary. There is no practical limit because all loops rely on a control expression that can only evaluate true or false. So long as the control expression evaluates true, the loop will just keep iterating. The canonical example is the infinite loop:

while (true) {

// ...

}

The control expression never evaluates false, so the only way to break out of this loop is from within the body of the loop itself. Although we typically use infinite loops when the control expressions are far too complex to be expressed as a simple expression in the while statement, we can easily avoid this by placing those control expressions in a function. For example, if the control expressions depend on three variables, a, b, c, we can pass those variables to a predicate function that returns true or false:

while (evaluate (a, b, c));

Here, the overall complexity of the control expression is hidden within the evaluate function, greatly simplifying the calling code.

Write a c program to find the maximum of three numbers using parameter passing?

#include<stdio.h> #include<conio.h>

main()

{

int a,b,c;

clrscr();

printf("enter the values of a,b,c");

scanf("%d,%d,%d",&a,&b,&c);

if(a>b)

{if (a>c)

{

printf("a is big");

}

else

{

printf("c is big");

}

}

else

{

if (b>c)

{

printf("b is big");

}

else

{

printf("c is big");

}

}

printf("a=%d/nb=%d/nc=%d",a,b,c);

getch();

}

/*or you can make it in another way*/

#include<stdio.h>

main()

{

int a,b,c;

printf("enter the values of a:");

scanf("%d",&a);

printf("enter the values of b:");

scanf("%d",&b);

printf("enter the values of c:");

scanf("%d",&c);

if(a>b)

{if (a>c)

{

printf("a is big");

}

else

{

printf("c is big");

}

}

else

{

if (b>c)

{

printf("b is big");

}

else

{

printf("c is big");

}

}

return 0;

}

Write a program to remove left recursion?

#include"stdio.h"
#include"conio.h"
int fabo(int);
void main()
{
int result=0,a=1,b=1,c;
printf("enter upto which you want to generate the series");
scanf("%d",&c);
result=fabo(c);
printf("%d\n%d\n",a,b);
printf("the fabonnaci series is %d\n",result);
getch();
}
int fabo(int n)
{
if (n==1);
return 1;
else if(n==2);
return 1;
else
return fabo(n-1)+fabo(n-2);
}

What is a quick sort?

Quicksort is a recursive algorithm.

Given an array and indices to the lower and upper bounds of a subset within the array, divide the subset into two subsets around a pivot value then recursively apply the algorithm to each of these subsets until a subset has fewer than 2 elements.

The algorithm begins by checking the size of the given subset. If there are fewer than 2 elements then the subset is sorted. This represents the end condition for this instance of the algorithm since a subset of 0 or 1 elements can always be regarded as being sorted.

If the set has 2 or more elements, however, a pivot element is selected . the pivot is typically the mode of the first, last and middle elements. The pivot is then swapped with the last element so it's out of the way.

Two indices are then instantiated, left and right, one referring to the first element (the lower bound), the other referring to the penultimate element (we ignore the pivot at the end for now).

While the left index is less than the right index, repeat:

1. While left is less than right and the value referred to by left is less than the pivot, increment left. (In other words, search for the first value from the left but no further than the right that is not less than the pivot).

2. While right is greater than left and the value referred to by right is not less than the pivot, decrement right. (In other words, search for the first value from the right but no further than the left index that is less than the pivot).

3. If the left and right indices are not equal, swap the values at those indices.

When the indexes are the same, or left crosses right, the loop ends.

Swap the value at the left index with the last element (the pivot).

At this point, the pivot is now in place and all values less than the pivot are to its left, with all others to its right. The values on either side are not yet sorted, thus we recursively invoke the same algorithm to sort each of these two subsets (excluding the pivot, of course).

Since the set is divided into two smaller subsets upon each invocation of the algorithm, each recursion takes less time because there are fewer elements to divide. The recursions effectively form a binary tree with one recursion per node. Some nodes will terminate earlier than others so the tree will probably be unbalanced. But since the sorting is done in-place, each recursion will unwind automatically leaving behind a sorted subset within the set. When the initial instance of the algorithm terminates, the entire set is sorted.

Is call by reference and pass by reference same thing?

Strictly speaking there is no such term as call by value. Functions are called while parameters are passed. When someone uses the term call by value they really mean pass by value, so in that sense they are the same. However call by value is incorrect terminology.