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 it called when a program allocates more and more memory but doesn't use the memory it allocates?

You are probably referring to a resource leak, however a resource leak specifically relates to programs that allocate memory on the heap (the free store) but fail to keep track of it. If we lose track of a heap allocation, the programmer cannot release that memory back to the system until the program terminates. The following is an example of a resource leak:

void f (unsigned x) {

void* p = malloc (x); // allocate x bytes // ... use memory

}

Each time we call this function we allocate another s bytes on the heap. But the function never releases that memory and because p is local to the function, we have no way of knowing where that memory was allocated (p will fall from scope when we return from the function). If we call this function often enough, we will eventually run out of memory.

To fix the problem, we must release the memory as soon as we are finished with it:

void f (unsigned x) {

void* p = malloc (x); // allocate x bytes // ... use memory

free (p); // release the memory

p = NULL; // zero the pointer

}

It is not necessary to zero a pointer when the pointer will fall from scope anyway, but it is considered good programming practice. What is important is that we release the memory it refers to before it falls from scope. Keeping our scopes as narrow as possible helps us to manage our resources more easily; the larger the scope, the more easily we can inadvertently introduce resource leaks, particularly when our code is maintained by several different programmers.

The problem you describe isn't necessarily a resource leak but it is clearly wasteful to allocate memory that is left unused even if we don't create a resource leak. Good programming practice dictates that we release memory as soon as we no longer require it, however allocating memory on an as-required basis can impact upon performance so it is not unusual for a programmer to allocate more memory than he needs, thus creating a reserve that can be used as and when required. Not using a reserve isn't in itself a problem, however we should endeavour to keep our reserves to a minimum and return all reserved memory to the system in a timely manner -- as soon as we no longer require it.

What part of a function definition specifies the data type of the value that the function returns?

The function header. The return value is written before the name of the function. This return type must match the type of the value returned in a return statement.

How do you write an output without using printf function in c?

Yes. However, C is a type-sensitive language thus PRINTF and printf would need to be defined as separate functions. However, names using all caps are conventionally used to denote macros in C thus you can easily define PRINTF as an alias for printf:

#define PRINTF printf;

int main (void) {

PRINTF ("%d", 42);

}

The C precompiler will substitute all instances of the symbol PRINTF with printf, thus the code seen by the compiler will become:

int main (void) { printf ("%d", 42);

}

Is 153 a palindrome number?

No.

To be a palindrome the number must read the same forwards as backwards. "153" is not the same number as "351" so this is not palindromic!

Write an algorithm and draw a corresponding flowchart to find the greatest number and its position among the 6 given numbers?

Algorithm

Step1: Read A, B, C

Step2: If A > B is True, then check whether A > C, if yes then A is greatest otherwise C is greatest

Step3: If A > B is False, then check whether B > C, if yes then B is greatest otherwise C is greatest

Give the Flowchart Answer

Define unary operator explain different unary operators used in c language?

A unary operator is one that requires only one operand. The unary operators are the prefix and postfix increment and decrement, positive and negative, logical and bitwise negation, and address and indirection operators, plus the words alignof, typeof, and sizeof.

The first two operators are increment "++", and decrement "--". Its position before or after the variable determines prefix or postfix. Incrementing increases the stored value by 1, and decrementing decreases the stored value by 1. If it is to the left of the variable, the operation occurs before the variable is used; if it is to the right, it occurs after the variable is used.


The positive operator "+" simply asserts the current value, and exists as acomplementto the negation operator "-". The negation operator returns the number with the opposing sign. If x is 5, then -x is -5, and if x is -5, then -x is 5.


The logical negation "!" and bitwise negation "~" operators perform actions on individual bits. C considers zero to be false, and all other values to be true. Using logical negation, it returns true if the value is zero, or false for any other value. The bitwise negation changes all 1 bits to 0 bits, and 0 bits to 1 bits. For an unsigned byte, ~0 becomes 255, and ~255 becomes 0. For signed variables, ~0 would become -1, and ~-1 would become 0. This is because of the two's complement method of signed numbers.


Address "&" and indirection "*" operators take the address or value of the operand. The address operator allows a variable to be passed by reference; modifying the reference will modify the original value. Using the indirection operator treats a variable as a memory address, which allows the programmer to access a specific spot in memory.


Alignof, sizeof, and typeof are all used to determine the alignment, size, and type of objects dynamically. This is necessary when trying to determine how data is laid out when there may be differences in memory accesses across platforms (e.g. when reading a ZIP file's directory contents).

EBNF float data type?

<float_literals> -> <digit> { <digit> } [ . ] [ { <digit> } ]

<digit> -> "0" | <digit excluding zero>

<digit excluding zero> -> "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

hi dude

i wish you are satisfied with my answer

LOL ^^

How can you print percent d on the screen using printf?

printf("Your text here %d", your_data);

or maybe you want to see '%d' as output? Try one of these:

printf ("%%d");

printf ("%cd", '%');

printf ("%s", "%d");

fputs ("%d", stdout);

To display string in a title case in c?

#include<stdio.h>

#include<conio.h>

#include<string.h>

int main()

{

int tmp,i;

char str[30];

printf("Enter any string: ");

gets(str);

for(i=0; str[i]!='\0'; i++) {

if(str[i-1]==' ' i==0)

{

if(str[i]>='a' && str[i]<='z')

str[i]=str[i]-32;

else

if(str[i]>='A' && str[i]<='Z')

str[i]=str[i]+32;

}

printf("%c",str[i]);

}

getch();

return 0;}

Is c language a secure programming language?

C is coming around to being defined as a secure language, but older drafts of C, such as C91 and C99, are inherently insecure. The design philosophy of the day was "trust the programmer," which of course meant that it had to be insecure because the programmer might want to do something unexpected, such as accessing a hardware port directly or dropping in some optimized assembler code that manipulated pointers in an unusual pattern that a specific piece of hardware requires.

Today's C (C11) is more robust and offers new features, and is less likely to "trust the programmer" than older drafts, but it still allows inherently insecure actions. Other languages that are derivatives of C (such as C# or C++) are more secure. For example, C# is staticallyanalyzable-- the Common Language Runtime that underpins this and other .Net languages can assert that the program will not crash, handle memory references incorrectly, or otherwise perform in a way that could cause system instability. Even then, however, a programmer may mark code as "unsafe", which disables static analysis and requires the user to accept the risks of running the application.

C source code for poisson distribution?

/*************************************************************************/

/* POISSON3.C */

/* Calculate poisson statistics given average number of occurences */

/* and the value of interest. */

/* Uses the formula P(X=x) = mu^x * e^-mu/x!, for x = 1,2,3...x */

/* NOTICE: Factorial1() is copyrighted by author */

/* FURTHERMORE: Software is provided as is, with no promise of fitness */

/* for use. */

/* comments or questions: p_duke52@yahoo.com */

/* Have fun... */

/* PAD, May 25, 2008 */

/*************************************************************************/ #include <stdio.h>

#include <math.h> /* protype of "e", exp(x) == exp^x */

#include <conio.h> /* For clrscr() */ /*********************************************************\

// copyrighted by author

// Written by: Jaco van Staden

// Date: 13 July 2002

// Program: Maths Program

// from the file "A_maths_me1067647162002.zip"

// available at Planet Source Code:

// http://www.planet-source-code.com

// changed int to double for size issues

// -- PAD, 5/25/08

\**********************************************************/ double Factorial1 (double a)

{

double answer = 1.0;

while(a>1)

answer *= a--;

return answer;

} int main(void)

{

double result, res1, res2;

double i = 0.0, k = 0.00;

double answer = 0.0; result = res1 = res2 = 0.0; clrscr(); /* makes it easier to see input prompts */ printf("Enter mean: ");

scanf("%lf", &i); printf("Enter value of interest: ");

scanf("%lf", &k); /* factorial of value of interest */ answer = Factorial1(k); /* formula for k */

result = pow(i,k);

res1 = 1/(exp(k));

res2 = (result * res1) / answer; printf("answer is ... : %0.20lf", res2);

return 0;

} /*************************************************************************/

/* POISSON3.C */

/* Calculate poisson statistics given average number of occurences */

/* and the value of interest. */

/* Uses the formula P(X=x) = mu^x * e^-mu/x!, for x = 1,2,3...x */

/* NOTICE: Factorial1() is copyrighted by author */

/* FURTHERMORE: Software is provided as is, with no promise of fitness */

/* for use. */

/* comments or questions: p_duke52@yahoo.com */

/* Have fun... */

/* PAD, May 25, 2008 */

/*************************************************************************/ #include <stdio.h>

#include <math.h> /* protype of "e", exp(x) == exp^x */

#include <conio.h> /* For clrscr() */ /*********************************************************\

// copyrighted by author

// Written by: Jaco van Staden

// Date: 13 July 2002

// Program: Maths Program

// from the file "A_maths_me1067647162002.zip"

// available at Planet Source Code:

// http://www.planet-source-code.com

// changed int to double for size issues

// -- PAD, 5/25/08

\**********************************************************/ double Factorial1 (double a)

{

double answer = 1.0;

while(a>1)

answer *= a--;

return answer;

} int main(void)

{

double result, res1, res2;

double i = 0.0, k = 0.00;

double answer = 0.0; result = res1 = res2 = 0.0; clrscr(); /* makes it easier to see input prompts */ printf("Enter mean: ");

scanf("%lf", &i); printf("Enter value of interest: ");

scanf("%lf", &k); /* factorial of value of interest */ answer = Factorial1(k); /* formula for k */

result = pow(i,k);

res1 = 1/(exp(k));

res2 = (result * res1) / answer; printf("answer is ... : %0.20lf", res2);

return 0;

}

Why program consists more than one object file in c?

why does a program consists of more than one object file in c++

Verify truth table of NAND gate using C programming?

I don't really know what this is supposed to mean, if you want to print the truth-table of the NAND-gate that will be something like this:

for (a=0; a<=1; ++a)

for (b=0; b<=1; ++b)

printf ("%d %d %d\n", a, b, !(a&&b))

Given an integer k write a procedure which deletes the kth element from a linked list?

this question is from the subject data structure and i need answer of this. data structure is the subject ot 3rd semester of bachelor's degree

Sum of 4digit of a number in c?

#include
#include

using std::cin;
using std::cout;
using std::endl;
using std::string

int main()
{
const int numberOfdigits = 5;
string myNumber = "0";
char myNumberChar[numberOfdigits] = {0};
cout << endl << "Enter 4 digit integer: ";
cin >> myNumber;

int sumOfDigits = 0;
int temp = 0;
for (int arrayIndex = 0; arrayIndex < (numberOfdigits - 1); arrayIndex++)
{
temp = atoi(&myNumber.substr(arrayIndex, 1)[0]);
sumOfDigits += temp;
}
cout << endl << "Sum of 4 digits is: " << sumOfDigits << endl;


system("PAUSE");
return 0;

}

Describe about storage allocation?

Storage allocation is a method of saving overall disk space in computer programming terms. This method involves saving similar pieces of data in the same location.

Difference Main functions and user defined functions?

main function in C programming language and almost in every other main language is the main function that launched after the program starts. It takes two arguments, pointer to the first element of a pointers array (arguments) and number of arguments.

If you would write your own functions and link by telling the entry point to your program you would not be able to get arguments any more. There are some code running before actually main function is launched. That code prepares the argument data for main function.

But it is possible to change main function to your defined function, but that would require more knowledge of how linker (ld) and for example objcopy utility works.

Note: There might be a difference in GNU and Microsoft C/C++ versions.

Dry run table in flow-chart?

/* my second program in C++ with more comments */ #include <iostream> using namespace std; int main () { cout << "Hello World! "; // prints Hello World! cout << "I'm a C++ program"; // prints I'm a C++ program return 0; }

C program for implement DFA to all string of abb?

#include<stdio.h>

#include<conio.h>

void main()

{

int state[10];

int str[10],input[10];

char ch;

int x[20];

int s,n,k=0,j,a,i,l,t,q=0,fs,b,nxt;

clrscr();

printf("enter the no. states\n");

scanf("%d",&s);

printf("enter the no.of i/ps\n");

scanf("%d",&n);

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

{

printf("enter the state\n");

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

printf("is it final state?... .y..1/n..0\n");

scanf("%d",&a);

if(a==1)

fs=state[i];

}

printf("enter the i/ps\n");

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

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

printf("transition state\n");

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

{

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

{

printf("(q%d,%d)=q",state[i],input[j]);

scanf("%d",&b);

x[k]=b; k++;

}

}

printf("enter the length of string\n");

scanf("%d",&l);

printf("enter the i/p string\n");

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

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

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

{

t=0;

do

{

if(str[i]==input[t])

{

nxt=x[n*q+t];

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

{

if(nxt==state[j])

q=j;

}

t++;

}

else

t++;

}

while(t!=n);

}

if(nxt==fs)

printf("\n string is accepted\n");

else

printf("\n not accepted\n");

getch();

}