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 program to use recursive and non-recursive functions to perform linear search operation for a key value in a given set of integers?

/* Write C programs that use both recursive and non recursive functions to perform the following searching operation for a Key value in a given list of integers : i) Linear search */

#include <stdio.h>

#define MAX_LEN 10

void l_search_recursive(int l[],int num,int ele);

void l_search(int l[],int num,int ele);

void read_list(int l[],int num);

void print_list(int l[],int num);

void main()

{

int l[MAX_LEN], num, ele;

int ch;

printf("======================================================");

printf("\n\t\t\tMENU");

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

printf("\n[1] Linary Search using Recursion method");

printf("\n[2] Linary Search using Non-Recursion method");

printf("\n\nEnter your Choice:");

scanf("%d",&ch);

if(ch<=2 & ch>0) {

printf("Enter the number of elements :");

scanf("%d",&num);

read_list(l,num);

printf("\nElements present in the list are:\n\n");

print_list(l,num);

printf("\n\nElement you want to search:\n\n");

scanf("%d",&ele);

switch(ch) {

case 1:

printf("\n**Recursion method**\n");

l_search_recursive(l,num,ele);

getch();

break;

case 2:

printf("\n**Non-Recursion method**\n");

l_search_nonrecursive(l,num,ele);

getch();

break;

}

}

getch();

}

/*end main*/

/* Non-Recursive method*/

void l_search_nonrecursive(int l[],int num,int ele)

{

int j, f=0;

for(j=0; j<num; j++) if( l[j] ele) {

printf("\nThe element %d is present at position %d in list\n",ele,num);

f=1;

}

else {

if((num==0) && (f==0)) printf("The element %d is not found.",ele);

else l_search(l,num-1,ele);

}

getch();

}

void read_list(int l[],int num) {

int j;

printf("\nEnter the elements:\n");

for(j=0;j<num;j++) scanf("%d",&l[j]);

}

void print_list(int l[],int num) {

int j;

for(j=0;j<num;j++) printf("%d\t",l[j]);

}

(Note: Edited for clarity.)

Write a c program the extracts and prints the rightmost digit of the integral portion of the float?

#include<stdio.h>

int main (void)

{

float x,y;

int z,w;

printf("enter a number:");

scanf("%6.2f",&x);

y=x/10.0;

z=x-y;

w=z%10;

printf("the rightmost digit is %d\n",w);

return 0;

}//main

Can laser pointers kill you?

I suppose a crate full of them could fall on your head ...

Or someone could shine one in your eyes when you were driving ...

But barring something like that, laser pointers do not have enough power to harm skin. You shouldn't look directly at them (as the sign on more than one research laser I've seen says, "Do not stare directly into beam with remaining eye"), but you can shine it at your hand for as long as you like.

Disadvantages of enumeration in c?

The big disadvantage I've found is that you might mistake it for a user-defined type. It's not; you still have to use int for your variable type and compilers will not do anything to help restricted assignment to values from the enumeration.

What is a callback URL?

A "callback URL" is the web address an API needs to send information for use in a different web site.

For example, a web developer is building a web application for use in conjunction with another web service's API (such as Facebook or Last.fm). The application is hosted on the web developer's server, and the other web service stores the callback URL so that it knows where to send information when it is requested by the developer's application.

See Also: http://en.wikipedia.org/wiki/Callback_(computer_science)

How do you use arrays in biginteger?

In Java, you would create an array of type BigInteger, then initialize each object (each array element) with the newoperator. I believe it would be something like this: BigInteger[] myArray = new BigInteger[5]; for (int i = 0; i

How do you convert ASCII to BCD?

void main ()

{

unsigned char hrs,mins,secs;

char ch1, ch2;

puts("\nEnter the hours to update: ");

ch1=getche();

ch2=getch();

hrs = ASCIItoBCD(ch1, ch2);

puts("\nEnter the minutes to update: ");

ch1=getche();

ch2=getch();

mins = ASCIItoBCD(ch1, ch2);

puts("\nEnter the seconds to update: ");

ch1=getche();

ch2=getch();

secs = ASCIItoBCD(ch1, ch2);

*tm = 0;

_CH = hrs;

_CL=mins;

_DH= secs;

_DL=0;

_AH =3;

geninterrupt(0x1a);

puts("Time Updated");

}

Write a c program whether a given word is palindrome do using function?

int main(int argc, char* argv[])

{

char a[10],b[10];

printf("enter any string\n");

scanf("%s",a);

printf("a=%s",a);

strcpy(b,a);

strrev(b);

printf("\nb=%s",b);


if(strcmp(a,b)==0)

printf("\nstring is palindrome");

else

printf("\nnot pelindrome");

return 0;

}


How do you avoid garbage values in c plus plus programs?

Initialization. Hint: your compiler might warn you, neverignore warnings if you aren't absolutely sure what they mean.

The elements in an array must be of the same basic data type but why must they be stored in contiguous memory?

An array is nothing more than a block of contiguous memory addresses divided into one or more units of equal length. We call these units elements. As a result, all elements of an array must be allocated contiguously.

Other than padding for memory alignment purposes, an array offers the most compact storage for multiple elements of the same type. And because the elements are allocated contiguously, it is trivial to traverse the array from one element to the next using nothing more than a pointer variable of the same type as the array. Each increment or decrement of the pointer increases or decreases the address by 1 element. The array suffix operator [] does the same thing, except the index is a zero-based offset from the start of the array. The first element is index 0 because it is 0 elements from the start of the array while the nth element is found at index n-1.

When we create an array we have the choice of storing the objects themselves in the array or storing a pointer to the objects. With the latter, the objects need not be allocated contiguously, however we consume more memory because the array of pointers consumes additional memory that we wouldn't need to consume were all the objects allocated in the array itself. We also have the additional cost of dereferencing those pointers in order to access the objects being referred to. However, arrays of pointers are advantageous when the objects are larger than a pointer (in bytes) or are of polymorphic type. Copying or moving a large object in memory is more expensive than copying or moving a pointer, so if we need to sort an array of large objects, an array of pointers is generally more efficient despite the indirection. But with polymorphic types we have to use pointers because polymorphic types are not guaranteed to be the same length so we must store a pointer to the common base type if we wish to retain polymorphic behaviour.

Which data types can be thrown as exceptions in a c program?

C does not provide native support for exception handling.

There are basically three error handling strategies in C. The first is to design all functions such that the return value indicates success or failure. The second is to use an output parameter to indicate success or failure. The third is to set a global variable to indicate success or failure.

Integer values are usually used to indicate success or failure. Typically, the all-zeros bit-pattern indicates success while the all-ones bit pattern indicates failure (equating to 0 and -1, respectively, on a twos-complement system). However, any non-zero value can be used to indicate an error because all non-zero values implicitly convert to true (an error has occurred). Thus the error code can be used to indicate a specific type of error.

The problem with this is that there is often a lot of code between the point where an error is detected and the point where it can be handled. With global error codes this can be extremely problematic (particularly in multi-threaded applications) because the error code could easily change if other errors occur en-route to the error-handling code. But even for "local" return values and output parameters, this usually means storing the error code somewhere that the error-handling code can easily retrieve it. The only alternative is to deal with every error as and when it occurs, which is often impractical, but also leads to error handling code being duplicated and widely-dispersed.

In C++ we do not have these problems. If we invoke a C library function that returns or sets an error code that cannot be handled there and then, we can simply convert that code into an exception object and throw the object for a suitable handler to deal with. Once an exception is thrown by a thread, the call stack unwinds, immediately terminating functions (without returning a value) until a suitable handler is found. Unhandled exceptions that make it all the way back to the main function can be caught with a catch-all exception handler. By making consistent use of resource handles and "smart" pointer objects rather than "naked" pointers, all resources consumed between the error handling code and the point where the error was initially detected are automatically released, thus minimising the amount of manual housekeeping operations required en-route to the exception handling code. This technique is known by the unwieldy term Resource Acquisition Is Initialisation (RAII) but all this means is that named objects that utilise RAII will release their acquired resources automatically when they fall from scope.

As with exception-handling, RAII is not supported in C because objects in C have no constructors or destructors. When a "naked" pointer falls from scope, the object it was pointing at is not automatically released; it is up to the programmer to manually release the resource at the appropriate time. With multiple pointers referring to the same object (a shared resource), it can be difficult to determine which pointer actually "owns" the resource. With smart pointers we do not have this problem.

What is the difference between the put and the mput?

Both will operate same but i recommend you that if there is only single file use "PUT" and if there are multiple files then you can use "MPUT"...... if there is only single file and if we use MPUT command thgen it will bit slower than PUT command.

Can Header file be called in main function or not?

No, main.c does not require a header file. When you have a ".c" and ".h" pair, the ".h" file is to declare the existence of functions that are defined in the ".c" files so that these functions can be called in other files. since "main.c" needs the headers of the other modules (to access their data types and functions) but usually doesn't have a header file itself.

Header files aren't "called", they are "included",but usually not inside any function.

What are Delimiting characters in c progrmme?

I would say the semi colon ; and theese {}

Any integer is input through the keyboard write a c program to find out whether it is an odd number or even number?

printf(\n "ENTER THE NUMBER\t");

scanf{"%d",&a);

while(a!=0);

{

r=a%2;

if(r==0)

printf("\n\n THE NUMBER IS EVEN \t");

else

printf("\n\n THE NUMBER IS ODD \t");

printf ("\n ENTER THE NUMBER \t");

scanf("%d",&a);

}

getch();

}

C program to find addition of two long integer numbers?

#include<stdio.h>

main()

{

long int num1, num2;

long int sum=0;

printf("Enter Value=");

scanf("%ld", &num1);

printf("Enter Value=");

scanf("%ld", &num2);

sum=num1+num2;

printf("The Result is=%ld", sum);

}

Probability search in data structure and algorithm?

manu_del In Probability Search , the list is ordered with most probable search element at the beginning of the list and least probable at the end.

Useful when a relatively small number of elements are the targets for most of the searches. To ensure that the probability ordering is correct over time, in each search we exchange the located element with the element immediately before it in the list.

This is a sample ProbabilitySearch program for a list of integers

INPUT:

list[] : reference of integer array

last : index of last item

target: target to be found

ref_locn: reference to the location of target

OUT:

If target is found

location of target is stored in ref_locn

found = 1 is returned

target is moved up in priority

Else

last is stored in ref_locn

found = 0 is returned

int ProbabilitySearch (int list[],int last, int target, int *ref_locn)

{

int looker = 1;

int found = 0;

int temp;

while(looker <= last && target != list[looker])

{

looker = looker + 1;

}

if(target == list[looker])

{

found = 1;

if(looker > 1)

{

temp = list[looker -1];

list[looker -1] = list[looker];

list[looker] = temp;

looker = looker -1;

}

}

else

found = 0;

*ref_locn = looker;

return found;

}

Is two items a list?

A list can contain any number of items, including no items (an empty list).

C plus plus program that display an employees name by entering an id?

Ideally you'd want to use a DBMS to store and retrieve the employee data, but we'll keep it simple and use some static data instead.

#include

#include

#include

#include

struct employee

{

std::string m_name;

unsigned m_id;

employee(std::string name, unsigned id): m_name(name), m_id(id) {}

};

std::vector employees;

void load_data ()

{

employees.push_back (employee ("Alan", 1));

employees.push_back (employee ("Brian", 2));

employees.push_back (employee ("Charles", 3));

employees.push_back (employee ("David", 4));

employees.push_back (employee ("Eric", 5));

}

unsigned input_id (std::string prompt)

{

unsigned id = 0;

while (1)

{

std::cout<

std::string input="";

getline (std::cin, input);

std::stringstream ss (input);

if (ss>>id)

break;

std::cout<<"Invalid input.\n";

}

return (id);

}

employee* locate (unsigned id)

{

for (unsigned i=0; i

{

if (employees[i].m_id == id)

return (&employees[i]);

}

return (NULL);

}

int main()

{

load_data ();

unsigned id=0;

do

{

if( id=input_id("Enter an employee ID (0 to exit)"))

{

if (employee* emp = locate( id ))

std::cout<m_name<<'\n'<

else

std::cout<<"There is no employee with that ID.\n"<

}

} while( id );

std::cout<

}

Example output

Enter an employee ID (0 to exit): 6

There is no employee with that ID.

Enter an employee ID (0 to exit): 5

Eric

Enter an employee ID (0 to exit): 2

Brian

Enter an employee ID (0 to exit): 0

How many types of logical operators in c only?

There are three logical operators in C; AND (&), OR (|), and NOT (^). These are the bitwise versions. The combinatorial versions are &&, , and !.

What is Kevin in ASCII values?

Determining the ASCII values for any string of characters is simple, requiring a for loop and an understanding of strings.

All a string is in C is an array of characters terminated with ASCII 0 (also known as a zero-terminated string or NUL-terminated string). The following block of code assumes that "sz" is a pointer to your string data and "psz" is a character pointer:

for (psz=sz; *psz; psz++) printf("%02X:%03d ", *psz);

printf("\n");

Step by step, what this code does is:

- Initializes "psz" to the same value as "sz", which means they both point to the address of the first character in the string;

- Checks if the character at the address in "psz" is not zero (meaning not at the end of the string) and, if so, breaks out of the loop;

- Displays a two-character hexadecimal (base 16) and a three-digit (base 10) ASCII representation of the character followed by a space;

- Loops back to the character check

- Displays a newline to move the cursor to the beginning of the next line.

Before the code, and inside your desired function block, define "psz" and "sz" as char* types, and set "sz" to the string "Kevin".

The full listing is as follows:

int main()

{

char *psz, *sz="Kevin";

for (psz=sz; *psz; psz++) printf("%02X:%03d ", *psz);

printf("\n");

return 0;

}

Note that if you're using C++ you'll need to change "char" to "const char".

How do you write a c program for hospital database using priority queue?

#include<stdio.h>

#include<conio.h>

typedef struct prque

{

char p_name[20];

int prn;

struct prque *next;

}prque;

prque * insert(prque *);

prque * delet(prque *);

void display(prque *);

int main()

{

int i,ch,n,prn1;

char p_name1[20];

prque *front;

clrscr();

front=NULL;

do

{

printf("\n1)Patient Entry");

printf("\n2)Delete");

printf("\n3)display Patiant List.");

printf("\n4)Exit");

printf("\nEnter your choice:");

scanf("%d",&ch);

switch(ch)

{

case 1:

front=insert(front);

break;

case 2: printf("\nPatient Name Deleted:%s\n",front->p_name);

front=delet(front);

break;

case 3: display(front);

break;

case 4:exit(0);

}

}while(ch!=4);

getch();

return 0;

}

prque *insert(prque *front)

{

char p_name[20];

int prn;

prque *temp,*current,*prev;

temp=(prque*)malloc(sizeof(prque));

printf("\nEnter patient name:");

scanf("%s",temp->p_name);

printf("\nEnter Status:\n1)General Check up\t2)Non Serious\t3)Serious.\t");

scanf("%d",&prn);

temp->prn=prn;

temp->next=NULL;

if(front==NULL)

{

front=temp;

}

else

{

if(prn>front->prn)//Insert at front

{

temp->next=front;

front=temp;

}

else if(prn<=front->prn) //insert at middle

{

current=front;

while(current->prn>=prn && current!=NULL)

{

prev=current;

current=current->next;

}

temp->next=prev->next;

prev->next=temp;

}

}

return(front);

}

prque * delet(prque *front)

{

prque *temp;

if(front==NULL)

{

printf("\nQueue is Underflow.");

return(front);

}

else

{

temp=front;

front=front->next;

free(temp);

}

return(front);

}

void display(prque *front)

{

prque *p;

p=front;

printf("\n--------------------------------------------------");

printf("\n\tPatient Name\t\tPriority\n");

printf("--------------------------------------------------");

while(p!=NULL)

{

printf("\n\t%s",p->p_name);

if(p->prn==3)

printf("\t\t\tSerious");

else if(p->prn==2)

printf("\t\t\tNon serious");

else if(p->prn==1)

printf("\t\t\tGenaral Checkup");

p=p->next;

}

}

/*

Output:

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:1

Enter patient name:Afridi

Enter Status:

1)General Check up 2)Non Serious 3)Serious. 3

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:1

Enter patient name:Ponting

Enter Status:

1)General Check up 2)Non Serious 3)Serious. 2

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:1

Enter patient name:Sangakara

Enter Status:

1)General Check up 2)Non Serious 3)Serious. 3

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:

1

Enter patient name:Clerke

Enter Status:

1)General Check up 2)Non Serious 3)Serious. 1

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:2

Patient Name Deleted:Afridi

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:1

Enter patient name:Vittori

Enter Status:

1)General Check up 2)Non Serious 3)Serious. 2

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:1

Enter patient name:Dilshan

Enter Status:

1)General Check up 2)Non Serious 3)Serious. 3

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:1

Enter patient name:Malinga

Enter Status:

1)General Check up 2)Non Serious 3)Serious. 1

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:2

Patient Name Deleted:Sangakara

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:3

--------------------------------------------------

Patient Name Priority

--------------------------------------------------

Dilshan Serious

Ponting Non serious

Vittori Non serious

Clerke Genaral Checkup

Malinga Genaral Checkup

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:2

Patient Name Deleted:Dilshan

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:2

Patient Name Deleted:Ponting

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:2

Patient Name Deleted:Vittori

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:2

Patient Name Deleted:Clerke

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:2

Patient Name Deleted:Malinga

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:2

Patient Name Deleted:(null)

Queue is Underflow.

1)Patient Entry

2)Delete

3)display Patiant List.

4)Exit

Enter your choice:4

*/

Written by: Fabianski Benjamin

What is the difference between C and C Sharp?

#1) C is unmanaged code, and C# is managed by the .NET CLR (Common Language Runtime)

Managed code means that many of the low-level activities that one has to worry about with C, such as memory management, garbage collection, pointers, etc, are automatically handled for you.

#2) C# is an object orienated language. C is a structed language. However, if you were referring to C++, then both C# and C++ have similar (although not identical) object orienated capabilities.

AnswerC is a procedural programming language developed in AT&T-Bell labs. It has been in use for about four decades for system and application programming. It has been ported to lots of CPUs and operating systems.

C-sharp (C#) is an object oriented programming language developed by Microsoft for use in its .NET framework. It is much newer, and only works on Microsoft operating systems. C#, like many other programming languages, is a descendant of C - they share some syntactic conventions.

What is the function of the crontab file in Linux?

Unix Cron is a daemon ('service') that runs in the background and runs the commands specified in the crontab file at the intervals specified within. This allows someone to easily run a program or script every hour, day, week, etc.

The crontab file can be found in two places. Typically, it is located in the /etc directory. Cron also supports multi-user setups, in which case you will also find other 'crontab' files in some distribution-specific location (I believe /var/spool/cron, named after the user that owns the file).

More information on Cron can be found by Googling "cron" or "crontab".