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 composite data type in c?

Composite datatypes are the datatypes which can be constructed with in a program by using prmitive datatypes and other composite types.the act of constructing a composite data type is called composition..............

What are local variables and global variables?

Local variables:

These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called.

Global variables:

These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.

Why are troubleshooter bugs more difficult with global variables?

Global variables are accessible to any code making it much more difficult to determine when and where a global is being modified. They can also lead to data-races whenever two threads try to write to the same global at the same time, or one tries to read the global while another is modifying it. The more complex the global the greater the risk.

Globals are not necessarily bad provided they are thread-safe and genuinely represent a truly global concept. If not, use a local variable instead.

Why is programming language named C?

C derives from a programming language called B, that existed before it. Since C was something of a successor to it, to indicate the advancement from B., the language is called C, the next letter.

What is the container object that holds a fixed number of values of single type?

There are multiple answers to this question, but the most basic one common to both Java and C is the array.

An array is a simple structure that you initialize to a certain size and fill with data. Think of an array as a sort of list, where each element in the list is numbered starting from zero up to the list size minus one (or the array is zero-based, as it's also called).

In Java:

// 10 is the number of elements the array can hold

int[] myIntArray = new int[10];

myIntArray[0] = 2; // The first element in the array

myIntArray[9] = 4; // The last element in the array

Referencing myIntArray[10] or higher will cause a runtime error in Java, which may stop your program.

In C:

// 10 is the number of elements the array can hold

int myIntArray[10];

myIntArray[0] = 2; // The first element in the array

myIntArray[9] = 4; // The last element in the array

Referencing myIntArray[10] or higher results in a buffer overflow in C (and C++). Note that in C, this won't throw errors like they do in Java, and this can and very likely will cause your program to have random bugs and possibly even crash from a segmentation fault, so be a bit more careful about using arrays in C.

C plus plus code of calendar using loop statement?

#include<iostream>

#include<iomanip>

#include<string>

#include<sstream>

#include<cassert>

using std::cout;

using std::cin;

using std::endl;

using std::setw;

using std::string;

using std::stringstream;

#ifndef NDEBUG

#define assert_year(year) do { assert (1900 <= (year)); } while(0)

#define assert_month(month) do { assert ((month) && ((month) <= 12)); } while(0)

#define assert_day(day) do { assert ((day) && ((day) <= 31)); } while(0)

#else

#define assert_year(year) ((void)0)

#define assert_month(month) ((void)0)

#define assert_day(day) ((void)0)

#endif NDEBUG

bool is_leap_year (size_t year)

{

assert_year (year);

if (year%4) return false; // If not a multiple of 4 then common year.

if (year%100) return true; // If a multiple of 4 but not of 100 then leap year.

if (year%400) return false; // If a multiple of 4 and 100 but not of 400 then common year.

return true; // If a multiple of 4, 100 and 400 then leap year.

}

size_t days_in_month (size_t month, size_t year)

{

assert_month (month);

switch (month)

{

case 2:

return is_leap_year (year) ? 28 : 29;

case 4: case 6: case 9: case 11:

return 30;

}

return 31;

}

size_t day_of_year (size_t day, size_t month, size_t year)

{

assert_day (day);

assert_day (month);

size_t days = day;

while (--month)

days += days_in_month (month, year);

return days;

}

size_t days_in_previous_years (size_t year)

{

assert_year (year);

size_t days = 0;

while (1900 < --year)

days += is_leap_year (year) ? 366 : 365;

return days;

};

size_t days_since_epoch (size_t day, size_t month, size_t year)

{

// Epoch is 1st January 1900

return days_in_previous_years (year) + day_of_year (day, month, year) - 1;

}

size_t day_of_week (size_t day, size_t month, size_t year)

{

return days_since_epoch (day, month, year) % 7;

}

void print_calendar (size_t month, size_t year)

{

assert_month (month);

cout << endl;

switch (month)

{

case 1: cout << "January"; break;

case 2: cout << "February"; break;

case 3: cout << "March"; break;

case 4: cout << "April"; break;

case 5: cout << "May"; break;

case 6: cout << "June"; break;

case 7: cout << "July"; break;

case 8: cout << "August"; break;

case 9: cout << "September"; break;

case 10: cout << "October"; break;

case 11: cout << "November"; break;

case 12: cout << "December"; break;

}

cout << ' ' << year << endl;

cout << "\n Su Mo Tu We Th Fr Sa\n";

size_t start_day = day_of_week (1U, month, year);

size_t days = days_in_month (month, year);

size_t count = start_day % 7U + 1U;

for (size_t i = 1; i <= count; i++)

cout << setw (4) << ' ';

for (size_t i = 1; i <= days; ++i)

{

cout << setw (4) << i;

++count;

if (count == 7)

{

cout << endl;

count = 0;

}

}

if (count)

cout << endl;

cout << endl;

}

size_t ask (string prompt)

{

for (;;)

{

cout << prompt;

string input;

cin >> input;

stringstream ss;

ss << input;

size_t reply;

if (ss >> reply)

return reply;

cout << "Invalid input.\n";

}

}

int main (void)

{

for (;;)

{

enter_month:

size_t month = ask ("Enter a month (1 - 12, 0 to exit): ");

if (!month)

break;

if (month > 12)

{

cout << "Invalid month." << endl;

goto enter_month;

}

enter_year:

size_t year = ask ("Enter a year (>= 1900, 0 to exit): ");

if (!year)

break;

if (year < 1900)

{

cout << "Invalid year." << endl;

goto enter_year;

}

print_calendar (month, year);

}

}

Enumerate the types of selection constructs in c plus plus?

Selection constructs in C++

  1. if...else
  2. switch/case
  3. conditional ternary operator (?:)

Who sorted the elements?

Дми́трий Ива́нович Менделе́ев

Dmitri Ivanovich Mendeleev

How arrays are used in synon?

Synon array processing is totally intuitive for any synon user. You do not need to open a manual. If you can use a file, you can use an array. The main difference is that there are only 4 function types: CRTOBJ, CHGOBJ, DLTOBJ, RTVOBJ, and only 1 "ACP" (set of Key fields)

The array processing really behaves more like an indexed file than an array. The file portion (Records) is a multiple occurrence data structure and the index portion is an array. The data structure contains the fields as defined in the *ARRAY file. The array(Index) is the fields defined as the Key concatenated with the occurrence# of the data structure. When you create an element in the array (a record in the file), the program takes the next available occurrence of the data structure and loads the fields. The program also creates the Index element by concatenating the key fields with the occurrence# of the newly added data structure. The program then does a Sort Array to sequence the Index so that the records may be accessed in key sequence. Any time a record is deleted, the Index element is blanked out and the Index is Sorted (Sort Array). This is a very clean solution and the performance is surprisingly good considering that the array is sorted after each add or delete.

When accessing the data, a LOKUP is performed on the array using the key value with blanks instead of the occurrence# and looking for GT in the array. This allows processing of partial keys as positioners or restrictors. The program then reads the data structure for the occurrence# taken from the array element to get the data(fields). For the next occurrence, it is not necessary to do another LOKUP (which is slow) because the array is sorted. The program only has to read the next element in the array and continue the processing as long as the partial key matches the key in the array. From the point of view of the Synon user, this processing is identical to a file RTVOBJ.

Synon loads the array from the bottom (an RPG technique to improve performance in large arrays) because the LOKUP command may be given a starting position, but cannot be stopped until every element is checked when no match is found. By loading the array backwards from the bottom and counting the elements, the search may be started at the last element loaded and only elements which contain data are read.IF No Match is Found

An array is defined for 1000 elements.

Only 10 elements are loaded.

These are loaded to occurrences 991 thru 1000.

When the LOKUP is performed, it starts at 991 and only 10 compares are made.

** If the array is loaded from the top, 1000 compares are performed.

How do you easily create Excel files in c?

You might be able to use C to extract data from an Excel file, but there is no easy way to write a program to create an Excel file.

What is the definition of prefix function?

A function that is used before an variable to increase or decrease its value

How can you type different brackets?

If you can't find them on your keyboard, use charmap.exe

() ASCII 28H and 29H

[] ASCII 5BH and 5DH

{} ASCII 7BH and 7DH

Write a program which takes the temperature in farhenheightthe program should display the farhenheight temperature as well as centigrade C equals?

Write a program which takes the temperature in farhenheight.the program should display the farhenheight temperature as well as centigrade.

C= (f-32)*5/9

Using while loop write a program which calculates the product of digits from 1 to 5 and also show these nos vertically?

Using while loop, write a program which calculates the product of digits from 1 to 5 and also show these no's vertically.

Code palindrome in c using queue and stuck?

  1. /*
  2. * C Program to Check String is Palindrome using Stack.
  3. */
  4. #include
  5. #include
  6. void push(char);
  7. char pop();
  8. char stack[100];
  9. int top = -1;
  10. void main()
  11. {
  12. char str[100];
  13. int i, count = 0, len;
  14. printf("Enter string to check it is palindrome or not : ");
  15. scanf("%s", str);
  16. len = strlen(str);
  17. for (i = 0; i < len; i++)
  18. {
  19. push(str[i]);
  20. }
  21. for (i = 0; i < len; i++)
  22. {
  23. if (str[i] == pop())
  24. count++;
  25. }
  26. if (count == len)
  27. printf("%s is a Palindrome string\n", str);
  28. else
  29. printf("%s is not a palindrome string\n", str);
  30. }
  31. /* Function to push character into stack */
  32. void push(char c)
  33. {
  34. stack[++top] = c;
  35. }
  36. /* Function to pop the top character from stack */
  37. char pop()
  38. {
  39. return(stack[top--]);
  40. }

Write a program to multiply two polynomials using an array?

#include<stdio.h>

#include<malloc.h>

int* getpoly(int);

void showpoly(int *,int);

int* addpoly(int *,int,int *,int);

int* mulpoly(int *,int,int *,int);

int main(void)

{

int *p1,*p2,*p3,d1,d2,d3;

/*get poly*/

printf("\nEnter the degree of the 1st polynomial:");

scanf("%d",&d1);

p1=getpoly(d1);

printf("\nEnter the degree of the 2nd polynomial:");

scanf("%d",&d2);

p2=getpoly(d2);

printf("Polynomials entered are\n\n");

showpoly(p1,d1);

printf("and\n\n");

showpoly(p2,d2);

/*compute the sum*/

d3=(d1>=d2?d1:d2);

p3=addpoly(p1,d1,p2,d2);

printf("Sum of the polynomials is:\n");

showpoly(p3,d3);

/*compute product*/

p3=mulpoly(p1,d1,p2,d2);

printf("Product of the polynomials is:\n");

showpoly(p3,d1+d2);

}

int* getpoly(int degree)

{

int i,*p;

p=malloc((1+degree)*sizeof(int));

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

{

printf("\nEnter coefficient of x^%d:",i);

scanf("%d",(p+i));

}

return(p);

}

void showpoly(int *p,int degree)

{

int i;

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

printf("%dx^%d + ",*(p+i),i);

printf("\b\b\b ");

printf("\n");

}

int* addpoly(int *p1,int d1,int *p2,int d2)

{

int i,degree,*p;

degree=(d1>=d2?d1:d2);

p=malloc((1+degree)*sizeof(int));

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

if((i>d1) && (i<=d2))

*(p+i)=*(p2+i);

else if((i>d2) && (i<=d1))

*(p+i)=*(p1+i);

else

*(p+i)=*(p1+i)+*(p2+i);

return(p);

}

int* mulpoly(int *p1,int d1,int*p2,int d2)/* this is the function of concern*/

{

int i,j,*p;

p=malloc((1+d1+d2)*sizeof(int));

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

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

p[i+j]+=p1[i]*p2[j];

return(p);

}

What is Top down design implementation of algorithm?

Top down approach to design a program.

For example you want to have a program to cook a cake. First you write down big blocks in just a few words (high level of abstraction).

--

Go to the shop

But ingredients

Get back home

Cook the cake

--

After you have everything ready at high level of abstraction, you work on details of each block/module

--

Go to the shop

Go out

Sit in you car

Drive to the shop

bla bla

...

But ingredients

Get back home

Cook the cake

--

Can keyword be used as variables?

No, any keyword could be used as a identifier (a method, class or variable name). These keywords have a special meaning in the language and the compiler can not identify if they are used as a variable name or as a keyword,

Write a program that uses a for loop to compute and print the sum of a given no of squares?

#include
#include
#include
void main()
{
int i,n,sum=0;
clrscr();
printf("n=");
scanf("%d",&n);
for (i=1;i<=n;i++)
sum=sum+pow(i,2);
printf("sum=%d",sum);
getch();
}