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 a library file and give examples?

A library file on a computer is the user's personal file. It could be a series of pictures downloaded to the computer, documents scanned into the computer, or a music file.

Where is C used?

C is ideally suited to low-level programming, but has been used to write operating system kernels, device drivers, subsystems programming and applications software of all types. C++ now dominates across all these domains as it is capable of producing far more efficient machine code much more easily. However, even C++ programmers will make use of C-style programming in combination with its object-oriented and generic programming paradigms.

Why a for loop is more appropriate here than a while loop?

There is insufficient information in the question to properly answer it. You did not provide the example "here". Please restate the question.

What does this mean to copy data from a temporary file to a more permanent location?

Temporary files are used a lot in computers. Most of the time, a temporary file is created when changes to a permanent file cannot be made until a certain process is complete.

A simple analogy might be painting a wall. Before you can paint the wall, anything hanging on the wall must be moved to a temporary holding area while the wall is being painted. After the painting process is complete, you move the items from their temporary location back to the permanent location - the wall that they hang on.

Computers actually follow a very similar process whenever a file is updated. Often times, the original file is copied to a temporary folder where the computer will make any changes needed to the file. When all the changes are complete, the computer might request your approval before the information is permanently stored in the permanent location where the file belongs.

Many modern word processing software programs (like Microsoft Word, Corel Wordperfect, or OpenOffice Writer) will create temporary files for new documents before the user actually saves the document to its permanent folder. This way, if the power fails suddenly, many of these programs can then check for any temporary files (such as a 30 page essay you've been working on for the past three hours) and "restore" your work. When you actually tell the program to save the file and give it a file name, the file is then moved to its permanent location.

How do you write a c program for reader writer problem?

/* program to study Reader/Writer problem with Semaphore along with output*/

#include<stdio.h>

#include<pthread.h>

#include<semaphore.h>

void reader1();

void writer1();

void reader2();

void writer2();

sem_t rsem;

sem_t wsem;

sem_t x,y,z;

int rc=0, wc=0,p=0;

int main()

{

int r,w,i=0,ch=2;

pthread_t t1[5],t2[5];

printf ("\n\nEnter the number of READERS: ");

scanf ("%d", &r);

printf ("\n\nEnter the number of WRITERS: ");

scanf ("%d", &w);

printf ("\n\nMENU\n\n\t1.READERS HAVE PRIORITY");

printf ("\n\n\t2.WRITER HAVE PRIORITY");

printf ("\n\n\tENTER YOUR CHOICE:");

scanf ("%d", &ch);

sem_init(&rsem, 0, 1);

sem_init(&wsem, 0, 1);

sem_init(&x, 0, 1);

sem_init(&y, 0, 1);

sem_init(&z, 0, 1);

if (ch==1)

{

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

pthread_create(&t1[i], NULL, (void *)reader1, (void *)i );

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

pthread_create(&t2[i], NULL, (void *)writer1, (void *)i);

//pthread_create(&t1[w-1], NULL, (void *)reader1, (void *)(w-1) );

//pthread_create(&t2[r-1], NULL, (void *)writer1, (void *)(r-1) );

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

pthread_join(t1[i], NULL);

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

pthread_join(t2[i], NULL);

}

else if (ch==2)

{

//pthread_create(&t2[0], NULL, (void *)writer2, 0);

//pthread_create(&t1[0], NULL, (void *)reader2, 0);

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

pthread_create(&t2[i], NULL, (void *)reader2, (void *)i );

for (i=1;i<w;i++)

pthread_create(&t1[i], NULL, (void *)writer2, (void *)i );

//pthread_create (&t2[r-1], NULL, (void *)writer2, (void *)(r-1) );

//pthread_create (&t1[w-1], NULL, (void *)reader2, (void *)(w-1) );

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

pthread_join(t1[i], NULL);

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

pthread_join(t2[i], NULL);

}

else

printf ("\nINVALID CHOICE....PROGRAM TERMINATED.");

sem_destroy(&rsem);

sem_destroy(&wsem);

return 0;

} // main function close

void reader1 (void *i)

{ int i1;

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

{

sem_wait(&x);

rc++;

if (rc==1)

sem_wait(&wsem);

sem_post(&x);

printf ("\n\t \t \t \tREADER %d IS READING NOW %d", (int)i,p);

sem_wait(&x);

rc--;

if(rc==0)

sem_post(&wsem);

sem_post(&x);

sleep(2);

//printf ("\nREADER %d HAS FINISHED READING\n");

}

}

void writer1(void *i)

{ int i1;

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

{ sem_wait(&wsem);

p++;

printf ("\nWRITER %d IS WRITING NOW %d", (int) i,p);

sem_post(&wsem);

if ((int)i % 2 == 0)sleep(1); else sleep(6);

// printf ("\nWRITER %d HAS FINISHED WRITING\n");

}

}

void reader2(void *i)

{

sem_wait(&z);

sem_wait(&rsem);

sem_wait(&x);

rc++;

if(rc==1)

sem_wait(&wsem);

printf ("\nREADER %d IS READING NOW", (int)i);

sem_post(&x);

sem_post(&rsem);

sem_post(&z);

sleep(1);

sem_wait(&x);

rc--;

if (rc==0)

sem_post(&wsem);

sem_post(&x);

//printf ("\nREADER %d HAS FINISHED READING\n");

}

void writer2(void *i)

{

sem_wait(&y);

wc++;

if (wc==1)

sem_wait(&rsem);

sem_post(&y);

sem_wait(&wsem);

printf ("\nWRITER %d IS WRITING NOW", (int) i);

sem_post(&wsem);

sem_wait(&y);

wc--;

if(wc==0)

sem_post(&rsem);

sem_post(&y);

sleep(1);

//printf ("\nWRITER %d HAS FINISHED WRITING\n");

}

/*

student@student-desktop:~/Documents$ ./rdwrt

Enter the number of READERS: 4

Enter the number of WRITERS: 4

MENU

1.READERS HAVE PRIORITY

2.WRITER HAVE PRIORITY

ENTER YOUR CHOICE:1

READER 0 IS READING NOW

READER 1 IS READING NOW

READER 2 IS READING NOW

READER 1 IS READING NOW

READER 2 IS READING NOW

READER 3 IS READING NOW

READER 1 HAS FINISHED READING

READER 0 HAS FINISHED READING

READER 1 HAS FINISHED READING

READER 2 HAS FINISHED READING

READER 2 HAS FINISHED READING

READER 3 HAS FINISHED READING

WRITER 0 IS WRITING NOW

WRITER 0 HAS FINISHED WRITING

WRITER 1 IS WRITING NOW

WRITER 1 HAS FINISHED WRITING

WRITER 2 IS WRITING NOW

WRITER 2 HAS FINISHED WRITING

WRITER 3 IS WRITING NOW

WRITER 3 HAS FINISHED WRITING

student@student-desktop:~/Documents$ ./rdwrt

Enter the number of READERS: 4

Enter the number of WRITERS: 4

MENU

1.READERS HAVE PRIORITY

2.WRITER HAVE PRIORITY

ENTER YOUR CHOICE:2

WRITER 0 IS WRITING NOW

WRITER 0 HAS FINISHED WRITING

WRITER 1 IS WRITING NOW

WRITER 1 HAS FINISHED WRITING

WRITER 2 IS WRITING NOW

WRITER 2 HAS FINISHED WRITING

WRITER 3 IS WRITING NOW

WRITER 3 HAS FINISHED WRITING

READER 1 IS READING NOW

READER 0 IS READING NOW

READER 2 IS READING NOW

READER 3 IS READING NOW

READER 2 HAS FINISHED READING

READER 1 HAS FINISHED READING

READER 0 HAS FINISHED READING

READER 3 HAS FINISHED READING

*/

Written by: Fabianski Benjamin

What are the requirements to be a heavy machine operator?

There are a number of requirements to be a heavy machine operator. Depending on the type of machinery that is being operated, firstly a good level of health is required as the place of work could require some minimal lifting and carrying of smaller goods, in addition to this a college or apprenticship scheme may need to have been completed as will a commercial drivers license test that sometimes has a fee attatched to it.

Print your name ten times on the screen using a do-while loop?

#include<stdio.h> #include<conio.h> void main() { clrscr(); int x=0; do { printf("Kaushal"); printf("\n"); x++; }while(x<10); getch(); }

Write a C program to find the sum of all prime numbers in an array?

Borrowing the isPrime function from another answer of mine. Note that this will result in terrible performance for large arrays of numbers. (You should look into one of the sieve algorithms for this)

// returns 1 if n is prime, 0 otherwise

void isPrime(const int n) {

// We know that if n is composite, then we will find a factor in the range [2,sqrt(n)]

// so we compute the square root only once to limit our number of calculations.

const int sqrt_n = sqrt(n);

// Iterate through possible factors

int i;

for( i = 2; i <= sqrt_n; ++i ) {

// If n is divisible by i (n%i==0) then we have a factor

if(!(n % i)) {

return 0;

}

}

// If we get here, we know n has no factors other than itself and 1

return 1;

}

// returns the sum of all prime numbers in nums

int findPrimeSum(const int numsLength, const int[] nums) {

int sum = 0;

// iterate through nums and add up all the primes

int i;

for(i = 0; i < numsLength; ++i) {

if( isPrime(nums[i] ) {

sum += nums[i];

}

}

return sum;

}

How do you display the square root symbol on the output screen using Borland C Plus Plus?

Its Unicode value is 221A according to System tool Character map Advanced view Unicode subrange Math operators.

But I haven't done C in awhile, so I don't know how to or if you can.

ASCII value of root symbol is 251. In C we can print this symbol by printing the character value as below

printf("%c",251);

this will print the root symbol

Write a 'c' program to accept 'n' numbers from user store these number into an array count the numbers of occurrence of each numbers?

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],i,j,k;

int count=1,num[10],pos=0;

clrscr();

printf("Enter the Array Element ");

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

{

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

}//close the for loop

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

{

count=1,pos++;

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

{

if(a[i]==a[j])

{

for(k=j;k<10;j++)

a[k]=a[k+1]

}//close the if

count++;

}//close the for loop

num[pos] = count;

}//close the for loop

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

printf("Repeted Number of IN Arrary %d",num[i]);

}//close the main

How do you embed PHP code in C code?

PHP and C are two different programming languages that cannot directly interact with eachother. You can have a program made with C call a PHP script, using its output (an indirect interaction), but there's no way to "embed" PHP into C code.

How do you control hardware in C?

Platform-dependent. If you have an OS on your computer, then most likely you cannot access hardware directly from userland programs.

How many dimensions can an array be created in c?

There is no language limit to "How many dimensions can an array be created in c?". The limit will depend on available memory.

How to write printf statements for print 1.234 in a 9-digit field with preceding zeros?

#include<stdio.h>

#include<conio.h>

void main()

{

char b=0;

float a;

clrscr();

a=1.234;

printf("\n%d%d%d%d%.3f",b,b,b,b,a);

getch();

}

When you use decimal which type of data is it?

Decimal numbers are real numbers. In C and C++ we use the float, double and long double data types to represent real numbers.

How do data types and formulas work in the programming language C?

The C language provides many basic types. Most of them are formed from one of the four basic arithmetic type specifies in C (char, int, float and double), and optional specifies (signed, unsigned, short, long). Functions in computer programming and spreadsheets are very similar to those in math.

What is the work of compiler?

A compiler converts a program in one programming language into a program in another programming language. Often the conversion is into a language that can be understood directly by the hardware.

Program that takes a single integer argument n from the command line and creates a binary tree of processes of depth n?

#include

#include

#include

#include


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

{

int process = 1;

int child[2];

int numChilds = 0;

int state;

int depth;

assert(argc 0)

{

numChilds = 0 ;

procId *= 2 ;

if (! --depth)

exit(0);

}

else

{

numChilds++ ;

process++ ;

}

}

while (numChilds) wait(&state);

}

What is the name for queue insertion and deletion?

The names given to these functions are implementation-specific. Below are some common examples.

Adding an element to the end: enqueue, push, add

Removing an element form the top: dequeue, pop, remove