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

Is the array size is fixed after it is created?

Generally, a array is fixed in size. With some libraries, however, they are extensible, either by reallocation/copying strategies (C/C++/STL), or by linking/referencing strategies (JAVA).

What is a variable that is used within a function?

If the variable is declared within the function body, it is a local variable, one that is local to the function. Local variables fall from scope when the function returns, they are only accessible within the function. However, local variables can be returned by value, which creates an automatic variable that is returned to the caller. If the caller does not store the return value, the automatic variable falls from scope when the expression containing the function call ends. However, the expression may evaluate the return value without storing it. Note that functions cannot return local variables by reference since the local variable falls from scope when the function returns.

If the variable is passed as an argument to the function, then the variable is a parameter of the function. Arguments may be passed by value or by reference, depending upon the function signature. Passing by value means the function parameter is a copy of the argument (if the argument is an object, the object's copy constructor is invoked automatically). Thus any changes made to the parameter within the function are not reflected in the argument that was originally passed, and the parameter will fall from scope when the function returns. However, the value of the parameter can be returned as previously explained.

Passing by reference means the function parameter refers directly to the argument that was passed. Thus any changes made to the parameter are reflected in the argument.

Parameters that are declared as constant references assure the caller that the reference's immutable members will not be altered by the function. If the parameter is a non-const reference but the caller does not wish changes to be reflected in the argument, the caller should pass a copy of the argument instead.

Is array index out of bound allowed in C language?

No, it is not allowed to exceed the allocated size of an array. However, few compilers check, so if the programmer fails to check, he or she can get in trouble, by corrupting other memory or throwing a bus exception.

Write a program of using recursion to create a table of any number?

/*mycfiles.wordpress.com

Program to prepare Table of any no. using while loop*/

#include

#include

void main()

{

int n,t,count=1;

clrscr();

printf("Enter any number\n\n");

scanf("%d",&n);

while(count<=10)

{

t=n*count;

printf("\n%d*%d=%d",n,count,t);

count++;

}

getch();

}

How many bytes are required to store the pointer?

It depends on the platform...

In a 16 bit environment, such as DOS or Windows 3.x, a near pointer is two bytes, while a far pointer is 4 bytes.

In a 32 bit environment, such as Win32, a pointer is 4 bytes.

In a 64 bit environment, such as Win64, a pointer is 8 bytes.

If you want to find out in your particular environment, look at sizeof(ptr), where ptr is declared as a pointer to something.

char* ptr;

std::cout << sizeof(ptr) << std::endl;

Note that the size of the pointer is not the same as the size of the object to which it points. If you looked at sizeof(*ptr), you would get 1.

Difference between C and C Plus programming languages?

C++ allows you to mix object oriented programming with generic programming and C-style programming. C has none of these features.

C-style programming and C programming are broadly similar, with only minor differences, most of which are irrelevant. As such, it is very rarely necessary to use a lower-level language than C++.

The static type system in C is inherently weak; filled with loopholes and inconsistencies. C++ is much more robust by comparison.

What are the operations in singly linked lists?

The main operations on singly linked list are:

1:-Insertion

2:-Deletion

3:-Searching

4:-Display

Insertion can be done in done ways:

1)Insertion at beginning

2)Insertion at end

3)Inserting at a specified position

Deletion can be performed in 3 ways:

1)Deletion from beginning

2)Deletion from end

3)Deletion from a specified position.

C program to accept a string and search a character?

#include<stdio.h>

#include<conio.h>

int main()

{

char str[20],charsrc;

int i,j=0,re;

printf("Enter the string\n");

gets(str);

printf("Enter the character to be searched : ");

charsrc=getche();

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

{

if(charsrc==str[i])

{

re=i;

j=1;

}

}

if(j==1)

printf("Character is at position %d",re);

else

printf("Not found.");

return 0;

}

Why do global variables make a program difficult to debug?

The term variables imply that those things may be changed or assigned with new value (at numerous places). This make the program harder to debug because it will be difficult to know when and where a global variable being changed and what was the consequence of that value changed.

What is the input statement for a program?

Input statements extract data from an input stream. For example:

int x;

std::cin >> x;

Output statements insert data to an output stream. For example:

std::cout << x;

You cannot insert data into an input stream and cannot extract data from an output stream. However, streams that are both input and output streams (such as read-write files) can insert and extract data as required, depending on whether you are reading or writing to the stream.

Difference between simple queue and circular queue?

Simple queue is a linear queue having front & rear var to insert & delete elements from the list.But there is a boundary that we have to insert at rear & have delete from front.For this reason instead of having space in the queue if there is a single element in the rear,the queue is full.the other space is wasted.
To utilize space properly,circular queue is derived.In this queue the elements are inserted in circular manner.So that no space is wasted at all.

Why float value cannot be used id case and switch?

It is by design.

Speaking of floating-point values, please remember that they are never accurate, so be careful. Example:

wrong: if (f==4.1) printf ("Equals to 4.1");

good: if (fabs (f-4.1)<0.0000001) printf ("Equals to 4.1");

What is push operation in data structure?

Push inserts a value onto the top of the stack.

Pop extracts the top value from the stack.

These are the two primary operations that can be performed upon a stack. Prior to popping a value, you will first check the stack is not empty, store the top value, then pop the stack. For a stack of type T, you might use the following:

if (!stack.empty()) {

T value {stack.top()}; // copy top value

stack.pop(); // remove value from stack

// use value...

}

Write a program to find the L.C.M. and HCF of two number using function where numbers are passed as parameter use function with return value?

For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.

The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).

If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.

Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.

For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).

For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.

The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).

If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.

Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.

For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).

For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.

The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).

If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.

Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.

For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).

For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.

The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).

If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.

Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.

For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).

Which operator is use to find address of a variable?

The dereference operator. In C, we use the * operator for this. Note that the * symbol has several meanings in C (multiplication, pointer declaration and pointer dereferencing). The meaning is determined from the context in which it is used.

int x, y; /* integer variables */

int* p; /* pointer variable (pointer to int) */

x = 42; /* assign a value to the x variable */

p = &x; /* assign the address of the x variable to the pointer variable p */

y = *p; /* assign the value pointed to by p (42) to the variable y */

How to Reverse a string letter by letter in C language?

#include

#include

void pr_reverse(char *s);

int main(void)

{

pr_reverse("I like C");

return 0;

}

void pr_reverse(char *s)

{

register int i;

for(i = strlen(s) - 1; i >= 0; i--)

putchar( s [ i ] );

}

Algorithm for implementing push operation on stack?

1. If TOP = MAXSTK THEN [Stack already filled?]

Print "OVERFLOW"

Go to step 4

Endif

2. TOP = TOP + 1 [Increase TOP by 1]

3. Set ST[STOP] = ITEM [Insert ITEM in new TOP position]

4. End

What is borland c plus plus?

Borland C++ was a C++ development environment originally released by the Borland Software Corporation but is now owned by Embarcadero Technologies who purchased the Borland CodeGear division in 2008.

It is now formally known as Embarcadero C++ Builder, or informally as just C++ Builder. At the time of writing, the latest stable release is C++ Builder XE2.

Write a program in qbasic to print the multiplication table of an input number?

This is a homework assignment. write homework assignments for you because you need to do this yourself or you will not learn the skills that the assignment is trying to teach you.

However if, while trying to do your assignment, you find a specific problem that you need help with, WikiAnswers will help you with these specific questions (e.g is this 'xxxxx' qbasic statement correct).

====

Both the above examples can be adjusted to print out the 15 x tables/or else, the 15 times tables square.

HINT:

A> In the first case the user types in the number 15; then, presses Enter.

B> In the 2nd case you simply change both tablesNo%=/timesNo%= to say 15 instead of 12.

What are the differences between Bresenham's line algorithm and Bresenham's circle algorithm?

These two algorithms are almost completely different. The only real similarity is that they are each designed to use only integer addition/subtraction and multiplication, avoiding expensive division and floating point operations.

What is 2's complement of 10002?

Surprise: it is -10002.

(If you wanted to ask 1000(2), then it is 11111000(2))

How do you write a program to find magic numbers?

#include<stdio.h>

unsigned sum_row (unsigned* sq, const unsigned width, const unsigned row) {

unsigned sum, col;

sum = 0;

for (col=0; col<width; ++col)

sum += sq[row*width+col];

return sum;

}

unsigned sum_col (unsigned* sq, const unsigned width, const unsigned col) {

unsigned sum, row;

sum = 0;

for (row=0; row<width; ++row)

sum += sq[row*width+col];

return sum;

}

unsigned sum_diag (unsigned* sq, const unsigned width) {

unsigned sum, row, col;

sum = 0;

for (row=0, col=0; row<width; ++row, ++col)

sum += sq[row*width+col];

return sum;

}

unsigned sum_anti (unsigned* sq, const unsigned width) {

unsigned sum, row, col;

sum = 0;

for (row=0, col=width-1; row<width; ++row, --col)

sum += sq[row*width+col];

return sum;

}

bool is_magic (unsigned* sq, const unsigned width) {

unsigned magic, row, col;

magic = sum_row (sq, width, 0);

for (row=1; row<width; ++row)

if (magic!=sum_row(sq, width, row))

return false;

for (col=0; col<width; ++col)

if (magic!=sum_col(sq, width, col))

return false;

if (magic!=sum_diag(sq, width))

return false;

if (magic!=sum_anti(sq, width))

return false;

return true;

}

int main () {

const unsigned width = 3;

unsigned a[width][width] {{2,7,6},{9,5,1},{4,3,8}};

unsigned row, col;

printf ("Square:\n\n");

for (row=0; row<width; ++row) {

for (col=0; col<width; ++col) {

printf ("%d ", a[row][col]);

}

printf ("\n");

}

printf ("\n");

if (is_magic((unsigned*)&a, width))

printf ("The square is magic with a magic constant of %d\n", sum_row((unsigned*)&a, 3,0));

else

printf ("The square is not magic\n");

return 0;

}

Binary search using pointer in c?

#include
#include
#include
void main()
{
int a[10]={1,2,3,10,11,13,16,21,46,73};
int mid,lower=0,upper=9,num,flag=1;
clrscr();
printf("enter the number to search");
scanf("%d",&num);
printf("\n the list of the data is");
printf("%-20s","\ndata);
for(num=0;num<=upper;num++)
printf("%3d",a[num]);
printf("%-20s","\nindexno");
for(num=0;num<=upper;num++)
printf("%3d",num);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(a[mid]==num)
{
printf("the number is at position %d in array",mid);
flag=0;
break;
}
if(a[mid]>num)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
printf("element is not present in the array");
getch();
}