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

Write a C program to implement dequeue?

Write a C program for Dequeue

#include

#include

struct dll

{

struct dll *llink;

int data;

struct dll *rlink;

};

typedef struct dll node;

node *first=NULL, *new;

void create(),insertbeg(),insertmid(),insertend(),insert();

void delbeg(),delmid(),delend(),delete();

void display();

void main()

{

char ch='y';

int c;

clrscr();

while(ch=='y')

{

printf("1. create 2. insert 3. delete 4. display 5.exit\n");

printf("enter u'r choice");

scanf("%d",&c);

switch(c)

{

case 1: create();break;

case 2: insert();break;

case 3: delete();break;

case 4: display();

}

printf("do u want to continue(y/n)");

fflush(stdin);

scanf("%c",&ch);

}

}

void create()

{

node *temp;

char ch='y';

temp=first;

do

{

printf("enter the data for new node\n");

new=(node *)malloc(sizeof(node));

scanf("%d",&new->data);

if(first==NULL)

{

first=new;

temp=new;

new->llink=NULL;

new->rlink=NULL;

}

else

{

temp->rlink=new;

new->llink=temp;

new->rlink=NULL;

temp=new;

}

printf("do u wnat to create another node(y/n)");

ch=getchar();

fflush(stdin);

}while(ch!='n');

}

void insert()

{

char ch='y';

int c;

while(ch=='y')

{

printf("1.insertbeg 2: insertmid 3. insertend 4. exit\n");

printf("enter u'r option");

scanf("%d",&c);

switch(c)

{

case 1:insertbeg();break;

case 2:insertmid();break;

case 3:insertend();

}

printf("do u want to continue(y/n)");

fflush(stdin);

ch=getchar();

}

}

void insertbeg()

{

printf("enter data for node to aded in the beginnning");

new=(node *)malloc(sizeof(node));

scanf("%d",&new->data);

if(first==NULL)

{

first=new;

new->llink=NULL;

new->rlink=NULL;

}

else

{

new->rlink=first;

first->llink=new;

new->llink=NULL;

first=new;

}

}

void insertmid()

{

int pos,i=1;

node *temp;

temp=first;

printf("enter the positon to insert a node");

scanf("%d",&pos);

printf("enter data for node to be added at a positon %d",pos);

new=(node *)malloc(sizeof(node));

scanf("%d",&new->data);

while(i {

temp=temp->rlink;

i++;

}

temp->rlink->llink=new;

new->rlink=temp->rlink;

temp->rlink=new;

new->llink=temp;

}

void insertend()

{

node *temp;

temp=first;

printf("enter data for node to be aded at end");

new=(node *)malloc(sizeof(node));

scanf("%d",&new->data);

while(temp->rlink!=NULL)

{

temp=temp->rlink;

}

temp->rlink=new;

new->llink=temp;

new->rlink=NULL;

}

void delete()

{

char ch='y';

int c;

while(ch=='y')

{

printf("1. delbeg 2. delmid 3. delend 4. exit\n");

printf("enter u'r option");

scanf("%D",&c);

switch(c)

{

case 1: delbeg();break;

case 2: delmid();break;

case 3: delend();break;

}

printf("do u want to continue(y/n)");

ch=getchar();

}

}

void delbeg()

{

node *temp;

if(first==NULL)

{

printf("deletion not possible");

exit();

}

else

{

first=first->rlink;

first->llink=NULL;

}

}

void delmid()

{

int pos,i=1;

node *temp;

temp=first;

printf("enter the position to delete a node");

scanf("%d",&pos);

while(i {

temp=temp->rlink;

i++;

}

temp->rlink->rlink->llink=temp;

temp->rlink=temp->rlink->rlink;

}

void delend()

{

node * temp;

temp=first;

while(temp->rlink->rlink!=NULL)

{

temp=temp->rlink;

}

temp->rlink->llink=NULL;

temp->rlink=NULL;

}

void display()

{

node *temp;

if(first==NULL)

{

printf("linked list is empty");

exit();

}

else

{

printf("the contents of dd are :\n");

temp=first;

while(temp->rlink!=NULL)

{

printf("%d",temp->data);

temp=temp->rlink;

}

}

}

What are the fields of a node in a linked list?

usually we have two fields they are data field and node i.e. pointer field.it also depends on type of linked list.the above said is for single linked list.And for double linked list it sontains three fields first pointer that pointes to previious node and data field and another pointer that point to next node

Why do you use conio.h?

You include stdio.h when writing C code. If you are writing C++ code then you must include cstdio instead. Both headers implement the C standard input and output library but cstdio is compliant with C++.

Consider the following :

#include

int main()

{

printf("Hello world\n");

std::printf("Hello world\n"); // ERROR!

}

The error occurs because there's no guarantee the printf symbol resides in the std namespace. There's a small possibility it does reside there, but it is only guranteeed to exist in the global namespace. Thus the risk of error is high.

Now consider the following:

#include // also includes stdio.h

int main()

{

using std::printf;

printf("Hello world\n");

std::printf("Hello world\n");

}

The cstdio header guarantees all symbols defined in stdio.h now appear in the std namespace (where all standard library symbols rightly belong). Although there's no guarantee those same symbols also reside in the global namespace, the using keyword can be used to inject those symbols into the global namespace. In this case the using keyword is unnecessary; the code will compile with or without it.

It's important to recognise the difference between C++ standard library headers (those with no extension) and the equivalent C headers (those with a .h extension). C headers guarantee all symbols are imported to the global namespace and possiblythe std namespace. C++ headers guarantee all symbols are imported to the std namespace and possibly the global namespace. In most cases, the C++ header simply modifies the equivalent C header to render it compliant with C++. This isn't always the case, but ultimately it is much easier to inject symbols to the global namespace (with using) than it is to inject to the std namespace. The C++ headers take care of all the messy stuff for you and are less problematic in the long run.

Write a program to count the number of times the digit is present?

#include<stdio.h>

#include<conio.h>

void main( )

{

int a[50],i,n,s,t=0;/* the digit 50 can be any integer, its a user defined function a[limit]*/

clrscr( );

printf("\n\nenter the number of elements u want to enter");

scanf("%d",&n);

printf("\n\nenter the numbers u desire");

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

scanf("%d",&a[i]);

printf("enter the element to be counted");

scanf("%d",&s);

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

{

if(s==a[i])

t++;

}

printf("the number u entered has occured %d number of times",t);

getch( );

}

Is java interpreter or compiler?

Java has both a compiled and an interpreted stage.

1) The programmer writes his source codes (.java extension); a compiler will compile this to bytecode (.class extension).

2) When the end-user runs the .class program, the JVM (Java Virtual Machine) will interpret this.

What are data structures and algorithms used for?

Data structures are a way of storing and organizing data on a computer so that it can be used in a way that is most efficient and uses least resources. Algorithms are step by step processes for calculations which are used for data structures.

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 ] );

}