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

How do you write a c program which accepts a positive integer and displays the number of digits?

#include
#include

void main()
{
unsigned long int num;
int i=0;
clrscr();
printf("Enter the digit\n");
scanf("%lu",&num);
while(num!=0)
{
num=num/10;
++i;
}
printf("Length=%d",i);
getch();
}

Can you overload destructor for your class?

No. Classes can only have one destructor, whether you define one yourself or allow the compiler to generate one for you. The compiler-generated destructor is public by default, does not release any memory allocated to any class' member pointers, and is non-virtual, which are the three main reasons for defining your own.

Functions in C Language?

Functions have many uses. Although it is possible to write all code within a single function (the main function), code becomes harder to read. By separating the code into smaller, simpler functions with meaningful descriptive names, complex code becomes that much easier to both read and maintain. That is, the code becomes self-documenting so there is less need for comments which would otherwise become a distraction.

Each function should be designed such that it does the absolute minimum amount of work required to fulfil its purpose and does that work as efficiently as possible. Although functions can be expensive to call and return, a well-written function can be inline expanded by the compiler thus eliminating the function call altogether. Thus functions become a programming tool which can drastically reduce the amount of duplicate code we need to write.

Ideally, a function should be small enough so that all the code within that function will fit on screen at once. This usually means writing a huge number of low-level functions that do very little work by themselves, and then using these low-level functions as the building blocks for more complex, higher-level functions.

How do you draw a rhombus in c program?

C++ has no built-in graphics functions as graphics are platform-specific. You need a graphics library that provides a suitable API for your platform and its hardware in order to output graphics in C++. The code you use will therefore be dependant upon that library.

What is anonymous union in c?

union is similar to structure .The distinction is in terms of storage. In structure each member has it own storage location but in union all the members use the same location. It can handle only one member at a time.

example.

union u

{

int a;

char b[4];

};

A union may contain structure(s) and a structure may contain union(s).

A structure (or an union) may contain unnamed unions (and structures), a useful feature and worth getting to know.

Example:

typedef union MYUNION {

. struct {

. . int x;

. . int y;

. } int_coord;

. struct {

. . double x;

. . double y;

. } dbl_coord;

} MYUNION;

It's a fragment of memory shared by multiple variables.

What is self referential structure?

It is exactly what it sounds like: a structure which contains a reference to itself. A common occurrence of this is in a structure which describes a node for a linked list. Each node needs a reference to the next node in the chain.

struct linked_list_node {

int data;

struct linked_list_node *next; // <- self reference

};

Write a source code using C programming to find the area of a trapezium using functions?

#include<stdio.h>

#include<conio.h>

# define PI 3.14

void main()

{

float radius,area;

clrscr();

printf("Enter the Radius of the Triangle:");

scanf("%f",&radius);

area=PI*radius*radius;

printf("Area = %f",area);

getch();

}

What will happen if you do not use break statement?

Everywhere in the code when there is no 'break', the running will continue with the next instruction. Example:

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

printf ("i=%d: ", i);

switch (i) {

case 2: printf ("two ");

case 1: printf ("one ");

case 0: printf ("zero ");

}

printf ("\n");

}

The output:

0: zero

1: one zero

2: two one zero

How do you write a c program to sort ten names in descending order?

If you are using the String class to store your names then you can use the < or > operators to compare the order, alphabetically, they appear in.

String names[3];

int numberOfNames = 3;

names[0] = "abc";

names[1] = "dfg";

names[2] = "hij";

printf("befores%s\n", names[0],names[1],names[2]);

for( int i = 0; i < numberOfNames-1; i++ )

{

for( int j = 0; j < numberOfNames; j++ )

{

if( names[j] < names[j+1] )

{

names[j].swap( names[j+1] );

}

}

}

printf("afters%s\n", names[0],names[1],names[2]);

output:

before abcdfghij

after hijdfgabc

*********************************************************************

this will give correct answer

#include

int main(){

int i,j,n;

char str[20][20],temp[20];

puts("Enter the no. of string to be sorted");

scanf("%d",&n);

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

gets(str[i]);

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

for(j=i+1;j<=n;j++){

if(strcmp(str[i],str[j])>0){

strcpy(temp,str[i]);

strcpy(str[i],str[j]);

strcpy(str[j],temp);

}

}

printf("The sorted string\n");

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

puts(str[i]);

return 0;

}

What is the difference between datatype and keyword in c?

data type refers to the kind of value that is held by a particular variable. For ex: an int variable contains integer value, a string holds a alpha numeric value etc.

variable refers to the name of a value using which we can refer to this value.

Ex:

public int age = 28;

here int is the data type and age is the variable.

Creating a program using switch statement and if and else statement?

Not that difficult...

int main (int argc, char **argv)

{

if (argc==1) printf ("No arguments\n");

else printf ("%d arguments\n", argc-1);

goto END;

END:

return 0;

}

Is encapsulation a characteristic of procedural or object oriented programming?

Encapsulation is one of the four pillars of object-oriented programming. The other three are inheritance, polymorphism and abstraction.

How do you write mainframe program?

Nothing special. Of course before you start, you should know how to compile, link, run and debug your programs, but this goes for every platform.

Explain the linked list and multi linked list structures?

I tried my best to explain all Linked List.

For Single Linked List http://www.fansonnote.com/2012/02/single-linked-list/

For Double Linked List http://www.fansonnote.com/2012/02/double-linked-list/

For Multi Linked List http://www.fansonnote.com/2012/02/multi-linked-list/

Hope it will help.

Thanks.

How do you extract the contents of a file in C?

Open it (open of fopen), read the data (read or fread), then close it (close or fclose).

Use of Percent in C programming?

%p is used in a printf statement to show the memory address value of a pointer. It does not show the address of the pointer itself. You can see this with the following program:

#include

int main(void){
int* p;
printf("p: %p x: %x x&: %x\n",p,p,&p);
}

This gave me the following output:

p: 0x33d6d0 x: 33d6d0 x&: bf9a8c10

So %p is a way of formatting the value in a pointer to make it obvious that you are referring to a memory location.

C program to determine sum of squares of all even numbers between 10 to 1000?

main()

{

int i,sumo=0,sume=0,oddno,evenno;

for(i=1,oddno=1,evenno=2;i<=100;++i,oddno=oddno+2,evenno=evenno+2)

{

sumo=sumo + oddno;

sume=sume + evenno;

}

printf("Sum of odd nos = %d And Sum of even nos = %d,sumo,sume);

}

Why should you be careful not to place a statement in the body of a for loop that changes the value of the loop's counter variable?

By changing the loop counter variable within the loop can cause the loop to exit early or to loop more times than the limit.

The is nothing that says that you can not change the loop counter variable within the loop, you may indeed want to do that. Just be careful and be sure of what will happen.

What is the difference between macro and inline function in c plus plus?

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point.

Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself.

Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.

Why timers are used?

If you need to get something done at certain time or periodically.

What are multiple constructors in c?

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.

When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.

A constructor is declared without a return value, that also excludes void.

Therefore, when implemented, do not return a value:

Constructor Exampleclass rectangle { // A simple class int height; int width; public: rectangle(void); // with a constuctor, ~rectangle(void); // and a destructor }; rectangle::rectangle(void) // constuctor { height = 6; width = 6; }

sagar sainath samant. sybsc (computer science)

Why C program is called as 'C'?

The language was called the "C" language because it was a kind of successor of the "B" language.

What is the summary of c plus plus and sql in a programming language?

Your question is unclear. C++ IS a programming language. SQL is not (it is a specialized language for defining relational databases, inserting/retrieving/modifying data and specifying user access.)

Hexa to octal?

The best way is to first convert hexadecimal to binary and then to octal.

For example the hexadcimal number B4EA is in binary:

B 4 E A

1011 0100 1110 1010

Thus B4EA (hexadecimal) = 1011010011101010 (binary)

We add leading zeros to the binary number in order to be able to divide the number in sets of 3 digits. Then we convert easy to octal as follows:

001 011 010 011 101 010

1 3 2 3 5 2

Therefore 1011010011101010 (binary) = 132352 (octal)

and from here you have your result:

B4EA (hexadecimal) = 132352 (octal)