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

Who found algebra?

There can be no person who is singlehandedly responsible for the founding of alerbra. This is because algebra is a classification of a certain type of math. Therefor there is a vast amount of people who contributed to the upbringing of algebra.

Which data type can store any data?

There is no such data type.
However, when we use user-defined data types of our own type, then that type of data can be stored in a variable. So as a term, you may say that user-defined data type can store any data. As the data-type used in any variable will be depending upon us.

What are macros in c?

In C macros are a piece of code that is replaced by its value whenever it called from.

Syntax for defining a macro is :

# define Macro_Name Value

As:

# include<stdio.h>

# define three 3

int main(){

printf("%d", three);

return 0;

}

output: 3.

In above example three is a macro and its value is 3.

The place for defining macros are same as including a header file in a program.

What is Size of int variable in c?

The size of an int variable in c is a function of the compiler implementation. It is usually a word in the underlying computer architecture. Original Microsoft C/C++ compilers for the 16 bit platform defined an int as 2 bytes. Current compilers for the 32 bit platform define an int as 4 bytes. The current ANSI C standard provides that an int is at least 4 bytes, but it could be higher, say 8 bytes on a 64 bit platform.

What is a preprocessor?

A preprocessor is a program that proccesses a file before it's passed down to the compiler and the linker. It permits to define some variables so that a programmer can change the program just by changing one line of code. It permits to include header files to.

How you create a table in C programming language?

Well, I don't know what do you mean by table, but here you are:

int main (void)

{

puts ("A3 B3 C3");

puts ("A2 B2 C2");

puts ("A1 B1 C1");

return 0;

}

What is an uninitialized pointer?

It means "nothing", there is no data provided at all; just an empty value.

Contrary to the previous edit it does not mean "zero", "none" or "blank"; as zero is a number and none and blank can be regarded as data.

What would happen if you assign a value to an element of an arry whose subscript exceeds the size of the array?

The simple answer is that the program has undefined behaviour and is therefore an invalid program. There's no easy way to know what will happen because you have (knowingly or otherwise) violated a fundamental language rule; the unconditional access of memory outwith the range of the array. That memory may or may not belong to your program, but there is no way to tell for sure, thus the behaviour is undefined. Perhaps nothing bad will happen at all, or the program will simply crash. But with undefined behaviour, absolutely anything can happen and there's simply no way of knowing in advance. When a user's hard-disk files are deleted by your program, you will quickly learn the peril of allowing undefined behaviour!

Example:

int x[10] = {0, 1, 2, 4, 5, 6, 7, 8, 9];

int* p = x;

p += 10; // one-past-the-end of the array

*p = 42; // undefined behaviour!

Referring to the one-past-the-end of an array is perfectly valid. Indeed, many algorithms that operate upon arrays using a half-closed range of iterators specifically rely upon this validity to determine when the algorithm has reached the end of the sequence and should stop processing. However, we must never dereference this address even if the address is actually part of the array. Consider the following algorithm:

void scale (int* b, int* e, int scalar) {

while (b != e) {(*b) *= scalar; ++b; }

}

This algorithm operates upon any sequence of elements denoted by the half-closed range [b:e), where e refers to the element one-past-the-end of the sequence we wish to scale.

Given our earlier definition of array x, we can scale any sequential portion of the array:

scale (x, x+5, 2); // Scale the first 5 elements (x[0] through x[4]), doubling the values. scale (x+5, x+10, 3); // Scale the last 5 elements (x[5] through x[9]), tripling the values.

scale (x, x+10, 4); // Scale the entire array (x[0] through x[9]), quadrupling the values.

Note that the begin iterator, b, must initially refer to an element within the bounds of x (that is, x[0] through x[9]), but e may refer to any element in the range x[0] through x[10]. Although x[10] is outwith the bounds of x, we never actually access this element, we simply refer to it to check that b is still in range before dereferencing the element and scaling its value.

Undefined behaviour is created when we invoke this algorithm with invalid iterators:

scale (x, x+11); // Undefined behaviour! Range includes x[10] which is outwith the bounds of x. scale (x-1, x+10); // Undefined behaviour! Range includes x[-1] which is outwith the bounds of x. scale (x+9, x-1); // Undefined behaviour! Algorithm does expect reversed sequences.

Note that the algorithm is designed for efficiency and therefore does not check the given range for validity. Although inherently unsafe, efficiency often places the onus of responsibility upon the caller, never the algorithm.

Next Answer

Exceeding the range of elements in an array (or a string) is a common newbie programming error, and is considered improper or "illegal" programming practice in C or any other programming languages and the outcome of doing so is considered "undefined". That means the results cannot be predicted. Compilers attempt to catch such errors and often have options to enable/disable checking for invalid array references during program execution.

So what happens when a range is exceeded? What does undefined get you? Their are several possibilities.

  • If the address is outside of your allocated address space, the system will intercept the attempt and likely terminate your program with a nasty message. Modern systems will not let an errant program violate the integrity of the system or another running program.
  • If your system has separate instruction and data space capabilities, you will access some other data in your program, possibly even a stored constant and that may affect the way the rest of your programs, or not.
  • If both instructions and data share the same space, you might access an actual machine instruction. If you modify that instruction, your program reaches it, and tries to do something it can't do, the system may terminated the program with a nasty message.

Write a program to calculate the volume of cube cylinder and rectangular box using the concept of function overloading?

The following code demonstrates function overloading to calculate volumes of cubes, cylinders and rectangular boxes (cuboids).

#include // required for input/output.

#include // required for atan() function.

using namespace std; // required for console input/output.

// User-input control function (avoids garbage input).

unsigned int GetNum( char * prompt )

{

int iResult = 0;

cout << prompt << ": ";

while( !( cin >> iResult ) iResult < 0 )

{

cin.clear();

cin.ignore( BUFSIZ, '\n' ); // If BUFSIZ not defined, use literal constant 512 instead.

cout << "Please enter numeric characters only." << endl << endl;

cout << prompt;

}

return( iResult );

}

// Forward declarations of overloaded functions.

void volume(unsigned int);

void volume(unsigned int,unsigned int);

void volume(unsigned int,unsigned int,unsigned int);

void volume(unsigned int s)

{

unsigned int cube = s*s*s;

cout << "Volume of cube is " << cube << endl << endl;

}

void volume( unsigned int r, unsigned int h)

{

// For the greatest accuracy across all platforms,

// PI is defined as 4 * arctan(1).

float cylinder = (4*atan((float)1)) * r*r*h;

cout<<"Volume of cylinder is " << cylinder << endl << endl;

}

void volume(unsigned int l, unsigned int b, unsigned int h)

{

unsigned int cuboid= l*b*h;

cout << "Volume of cuboid is " << cuboid << endl << endl;

}

int main()

{

unsigned int s;

s = GetNum( "Enter side of cube" );

volume( s );

unsigned int r, h;

r = GetNum( "Enter radius of cylinder" );

h = GetNum( "Enter height of cylinder" );

volume( r, h );

unsigned int l, b;

l = GetNum( "Enter length of cuboid" );

b = GetNum( "Enter breadth of cuboid" );

h = GetNum( "Enter height of cuboid" );

volume( l, b, h );

return 0;

}

OUTPUT:

Enter side of cube: 4

Volume of cube is 64

Enter radius of cylinder: 2

Enter height of cylinder: 3

Volume of cylinder is 37.6991

Enter length of cuboid: 9

Enter breadth of cuboid: 8

Enter height of cuboid: 6

Volume of cuboid is 432

Press any key to continue . . .

[EDIT] Reasons for changes to original answer:

  1. Code does not compile.
  2. Code strays from the question by including a non-essential class.
  3. Code is not platform-independent.
  4. Code does not cater for invalid input.

Formal and Actual Arguments?

The actual arguments (we call them parameters) to a function are the original copies in the caller's address space. The function prolog code provided by the compiler provides for making copies of all of the parameters. These copies are called the formal parameters. In C and C++, the default calling convention is call by value, which means that the called function only has access to the formal copy. Optionally, you can call by reference, passing instead the address of the actual parameter. Using dereference notation, the called function then has access to the actual parameter, and the formal parameter is simply its address.

One of the things that sometimes confuses people is the name of the parameter. You might, for instance, call something alpha in you main function. It is called alpha, and alpha means the memory location of alpha. In the function, however, you can call the parameter something else, perhaps beta. Within the context of the called function, beta contains the value of or the address of alpha, but it is not alpha, it is beta. To make matters worse, you can have another alpha within a block, or within the function, and that is certainly not related at all to the original alpha.

Recommendation: Always call an object by consistent names. This way, you won't get into scoping rules trouble.

How you pass array elements to a function?

Passing array elements to a function is achieved by passing the individual elements by reference or by value, just as you would any other variable. However, passing the entire array requires that you pass a pointer-to-pointer to the array along with the dimension(s) of the array.

Importance of virtual functions?

Private virtual functions are useful when you expect a particular method to be overridden, but do not wish the override to be called from outside of the base class. That is, the base class implementation and its overrides remain private to the base class.


Private virtual methods are particularly useful in implementing template method patterns, where certain algorithmic steps need to be deferred to subclasses, but where those steps need not be exposed to those subclasses. In many cases the private virtual methods will be declared pure-virtual, thus rendering the base class an abstract base class.


Write a program to exchange the value of two variables?

#include<stdio.h>

void main()

{

int a,b,t;

printf("enter the values of two varaible");

scanf("%d%d",&a,&b);

t=a;

a=b;

b=t;

printf("the exchanged values are",b,a);

}

What are the applications for circular linked lists?

A singly-linked circular list is useful for implementing queue data structures with minimum overhead. Normally we implement a queue with two pointers: one to the tail for insertions and one to the head for extractions. With a circular list we only need to maintain a single pointer to the tail because the tail always points "forwards" to the head (instead of null as it normally would), thus achieving constant-time access to both the head and tail via a single pointer.

Circular linked lists are generally useful wherever "wraparound" is necessary. That is, from any given node in the list, we can traverse forwards with the guarantee that we will eventually arrive back at that same node. With doubly-linked circular lists we have the advantage of traversing in either direction (bi-directional traversal).

What is a traversing process in data structure?

like searching in data structure, move on nodes and check them

What is the difference between enumeration and set of preprocessor define in c?

An enumeration is a group of named integral constants. An enumeration is also a type thus it provides type safety. When passing a constant to a function, an enumeration eliminate values that are not part of the group, thus allowing the compiler to catch errors that would otherwise slip through.

A set or pre-processor definitions (or macro definitions) do not provide any type safety and are never actually seen by compiler (they are processed before the compiler sees the resultant code). Macros are nothing more than a text-replacement system that effectively allow programmers to create a language within the programming language and thus create code that would be difficult if not impossible to produce using the language alone. However, when the language provides a simpler, type-safe mechanism, it is always better to utilise it and thus enlist the help of the compiler to eliminate errors. Macros do have their uses, particularly when debugging, but they must be used only when it is appropriate to do so. Enumerations are preferred over a set of macro definitions every time.

Some languages also permit enumeration classes, which gives stronger guarantees than a "plain" enumeration, not least eliminating the implicit promotion between enumerated types and integral types.

What happens if recursion function is declared inline?

An inline function replaces the call to the function by the body of the function, thus reducing the overhead of saving the context in stack. This is good for functions which are small in size and called occasionally. A recursive function calls an instance of itself and thus can be a deeply nested. Different compilers handle this differently. Some will inline it up to a certain depth and then call a non-inlined instance for further recursion; others will not inline the function at all and generate a normal function call.

What looping process checks the test condition at the end of the loop?

The do..while() loop tests the condition at the end of the loop. Therefore the loop body executes at least once.

The while() loop (without do) tests the condition before entering the loop and before each iteration of the loop.

The for() loop conditional expression is optional but, when specified, is tested before entering the loop and before each iteration of the loop.

How do you create a folder in c lang and take the name from user and how to create a folder inside user's folder?

In ANSI/ISO standard there is no any function that would be capable of doing that. You should need to look for special system function in order to create folder. Systems with support for the POSIX libraries can use the mkdir() function. For example, if you are using UNIX type operating system, you could include "sys/dir.h" and use mkdir().

Example:

#include

#include

#include

int main() {

int code = mkdir("folder");

if (code 0) {

printf("SUCCESS\n");

} else {

printf("FAILURE\n");

}

return 0;

}

In this example we are calling other command line utility in background - mkdir and telling it to create folder named "folder".

How do you get free borland c plus plus software?

There are many vendors of C++ compilers. Your choice depends on what platform you use, and whether or not you want paid support. Two compilers (remember, there are many) are GCC, at gcc.gnu.org, and Visual Studio, at www.microsoft.com/vstudio. Be sure to review the requirements, cost, and license details before you choose.

Why multidimensional array element access using indirection operator?

The number of dimensions is immaterial. All arrays are implemented as a one dimensional array. A multidimensional array is simply an array where every element is itself an array.

The only thing actually known about any array is that its name is a reference to the start address. Unlike an ordinary (non-array) variable, the elements in the array do not have names, we can only refer to them by their memory offsets from the start of the array. As such, in order to obtain the values stored at those offsets, we must dereference them. While the subscript operator gives us notational convenience, it's easy to forget that there's actually pointer arithmetic and dereferencing going on behind the scenes.

What is a binary operator in the C language?

In programming languages, a binary operator is an operator which takes two operands. For example, the divide-by sign between divident and divisor is a binary operator:

x = a / b

Other binary operators include + - * & | ^, among others.

Note that the operator is binary, not the character representing it. Take, for example, the minus sign. The minus sign represents the binary subtraction operator when used between two arithmetic expressions (e.g. x = a - b). However, when used left of an arithmetic expression, it indicates a negative sign (e.g. x = -a). Parentheses may be required to avoid ambiguity or enhance readibility of both effects are combined (e.g. x = a - (-b)).

Why array is a implicit pointer?

An array of pointers is exactly what it sounds like - one or more pointers arranged in order in memory, accessible through a common base name and indexed as needed. Philosophically, there is no difference between an array of pointers and an array of objects...

int a[10]; // 10 integers, named a[0], a[1], a[2], ..., a[9]

int *b[10]; // 10 pointers to int, named b[0], b[1], b[2], ..., b[9]

If you initialize the array of pointers...

int i;

for (i = 0; i < 10; i++) b[i] = &a[i];

... then *b[0] would be the same as a[0], etc.