How function can be nested in c?
A function can call other functions (or itself), but a function-definition cannot be nested in another function-definition:
int main (void)
{
void wont_compile (void) { puts ("Won't compile"); }
wont_compile ();
return 0;
}
Why should you declare variables?
Declaring of a variable in Java refers to creating a variable of a particular data type.
Example:
int x = 10;
Here we are declaring an integer variable X and initializing it to a value of 10
What is the difference between array of pointers and pointer to an array?
An array of pointers is that for eg if we have array of 10 int pointers ie int *a[10] then each element that which is stored in array are pointed by pointers. here we will have ten pointers. In pointer to an array for eg int(*a)[10] here all the elements that is all the ten elements are pointed by a single pointer.
What is multidimensional arrays?
A two-dimensional array is the simplest multi-dimensional array and is implemented as a one-dimensional array where every element is itself a one-dimensional array. We can imagine a two-dimensional array as being a table of rows and columns where every row is an array in its own right.
A three-dimensional array is simply a one-dimensional array of two-dimensional arrays, which can be imagined as being an array of tables. Extending the concept, a four-dimensional array is a table of tables.
Multi-dimensional arrays may be jagged. That is, a two-dimensional array may have rows of unequal length. Unlike regular arrays, jagged arrays cannot be allocated in contiguous memory. Instead, we use the outer array (the first dimension) to store pointers to the inner arrays. An array of strings (character arrays) is an example of a two-dimensional jagged array.
What is the c plus plus program that finds max value in the array of size n?
#include
using std::cout;
using std::endl;
double maxValue(double arr, const intarrSize);
int main()
{
const int arrSize = 10;//Size of the array you are going to use
double arr[arrSize] = {0.0};
cout << endl << "Enter the array elements..."
for ( int i = 0; i < arrSize; i++ )
{
cout << endl << "Enter " << (i + 1) << " element:";
cin >> arr[i];
}
cout << endl << "Max value is: " << maxValue;
system("PAUSE");
return 0;
}
double maxValue(double arr, const intarrSize)
{
double max = arr[0];
for ( int j = 0; j < arrSize; j++ )
{
if ( max < arr[j])
{
max = arr[j];
}
}
return max;
}
What is the meaning of conio in C?
Hi,
Conio.h is a header file which have functions declaration. One of them is clrscr() function. This function have its prototype in conio.h.
clrscr() function is used to clear the screen. Thus, whenever u called clrscr() function u r required to define conio.h header file in your program.... Similarly there are many functions in conio.h....
Hope it will help u....
Write a program to accept n from user and to display all even numbers between 1 to n in javascript?
man mnn g gt grand theft auto or grand theft walrus
What is outgoing tour operators?
Outgoing tour operators are companies that organize and sell travel packages for clients traveling from their home country to international destinations. They handle all aspects of travel arrangements, including transportation, accommodations, tours, and activities, often providing a complete travel experience. These operators typically work with various suppliers, such as airlines and hotels, to create appealing itineraries for their customers. Their services aim to make the travel planning process seamless and enjoyable for travelers.
Program demonstrate Fibonacci series using recursion?
#include<stdio.h>
#include<conio.h>
int fib(int);
int f=1,j=0,k=0,i=0,c;
void main()
{
int l;
clrscr();
printf("Enter limit\n");
scanf("%d",&l);
f=0;
printf("%d\n",f);
f=1;
printf("%d\n",f);
for(c=0;c<l;c++)
{
f=fib(l);
printf("%d\n",f);
}
getch();
}
int fib(int n)
{
while(i<n)
{
if(i<=n)
{
i++;
j=k;
k=f;
f=k+j;
fib(1);
return f;
}
}
}
output-
enter limit
4
0
1
1
2
3
5
Advantages of register variable?
Register variables are a special case of automatic variables. Automatic variables are allocated storage in the memory of the computer; however, for most computers, accessing data in memory is considerably slower than processing in the CPU. These computers often have small amounts of storage within the CPU itself where data can be stored and accessed quickly. These storage cells are called registers. Normally, the compiler determines what data is to be stored in the registers of the CPU at what times. However, the C language provides the storage class register so that the programmer can ``suggest'' to the compiler that particular automatic variables should be allocated to CPU registers, if possible. Thus, register variables provide a certain control over efficiency of program execution. Variables which are used repeatedly or whose access times are critical, may be declared to be of storage class register. Also these register variables are used in huge projects the tiny program developers are not interested to include these register variables, because the tiny programs never requires more time complete its job. These register variables may be used to store constant values so as to make use of it anywhere in the programs. main{ register float a=0;}
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i,a[100],sum=0;
float avg;
printf("Enter number of students");
scanf("%d",&n);
printf("Enter the marks score by students one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=sum/n;
printf("The average is %f", avg);
getch();
}
Do while loop program in c for printing odd numbers?
#include<stdio.h>
main()
{
int a,i=1;
printf("Enter max no");
scanf("%d",&a);
do
{
printf("%d",i);
i=i+2;
}while(i<=a);
}
What are the differences between a struct data type and a class data type in c plus plus?
A struct declares all members public by default, whereas a class declares all members private by default. That is really the only difference. Structs are from C and classes from C++, you can use both structs and classes in C++ but only structs in C.
/* ellipses, ..., used to emulate indentation */
int strcmp (const char* s1, const char* s2) {
... while (s1 != NULL && s2 != NULL) { /* scan both strings */
... ... if (*s1 < *s2) return -1; /* first string less */
... ... if (*s1 > *s2) return 1; /* first string greater */
... ... s1++;
... ... s2++;
... }
... if (s1 == s2) return 0; /* strings equal or both empty */
... if (s1 != NULL) return 1 else return -1; /* first string longer(1) / shorter(-1) */
}
A slightly less computation-intensive version is:
int strcmp(const char* s1, const char* s2)
{
... if (s1==s2)
... ... return 0;
... while (*s1)
... ... if (*s1++^*s2++)
... ... ... if (--s1<--s2)
... ... ... ... return -1;
... ... ... else
... ... ... ... return 1;
... return 0;
}
What are the rules of defining variable?
All variable names must begin with a letter of the alphabet or an underscore ( _ ). The rest of the characters may be any of those previously mentioned plus the digits 0-9.
Which is the keyword used to access the variable declared as global variable in another file?
To be able to access a variable declared in one file/class from another file/class you need to declare that variable as "public".
Doing this is a very bad coding practice because exposing a variable of a class to be available publicly to other classes is totally against the Java object oriented concepts of Encapsulation and Data hiding. So, preferably you shouldn't be doing this.
However, a better way to do this is, declare the variable as private and have public accessor methods through which you can access this variable. This will ensure that your data is protected even if it is available outside the class
Is it better to use malloc or calloc to allocate memory?
In general using malloc is faster, since calloc initializes the allocated memory to contain all zeroes. If this is what you want, however, then calloc can be used. The results can vary among different operating systems and environments, though. Memory allocation in an OS that uses floating blocks in heaps, such as Microsoft Windows and MacOS, should use the OS-native memory allocators instead. "Use malloc() almost always and calloc() almost never." The reason is that the initialization to zero that calloc() performs is usually not very helpful: - The initialization to "all-bits-zero" is not necessarily the same as initialization to "all-data-zero." C says very little about the representation of values in memory, nothing at all for floating-point or pointer values. On many machines all-bits-zero representations will in fact correspond to f.p. zeroes or null pointers, but this is not guaranteed by the language and there have been machines where the correspondence did not hold. If you get in the habit of using calloc() to initialize f.p. and pointer items, you may be heading for trouble. - Usually, one allocates a chunk of dynamic memory in order to store something in it -- and when you store something in it, you'll overwrite whatever was there before. Thus, the initialization performed by calloc() is usually not needed anyhow. There are occasional exceptions where all- bits-zero initialization is helpful, but they are unusual.
What is stdio.h means in c language?
#include <stdio.h> means that the compiler needs to grab all function definitions, implementations, variables, etc. from that file. In this case, stdio stands for "Standard Input/Output". An example of a function in stdio.h is "printf".
What is operator precedence in c programming?
Operator precedence is often associated with order of evaluation, however order of evaluation and operator precedence are actually undefined in C. Expressions may be evaluated in any order while operator precedence is derived from the language grammar. The concept of operator precedence merely simplifies our understanding.
When we think of operator precedence we can imagine a table with 15 rows. When parsing an expression, any operator on a given row takes precedence over those on lower rows. If we also number the rows in ascending order then we can associate each row with a precedence level, such that level 1 has the highest precedence and lower rows have lower precedence.
In addition to precedence, each row also has an associativity. Rows 2, 13 and 14 have right-to-left associativity while all others have left-to-right. For instance, the assignment operator appears on row 14 thus the expression a=b=c is evaluated as if it were actually written a=(b=c) rather than (a=b)=c.
What is the use of header file in c?
A header file is used to define constants, variables, macro's and functions that may be common to several applications. When a system consists of multiple applications that all access the same data it becomes essential that each application uses identical definitions and it is safer for all of those applications to use the same methods to read that data. updating data should only be performed by a single function, from a single application, but reading data can be safely performed by using the common defintions found in header files. you use header files in the programming language C to declare struct's functions and other things that could be usefull for your program.. it makes thing's simpler and easy to use...
What is the importance of recursion in computer programming?
Recursion is important because many algorithms are naturally recursive, particularly those that involve a divide-and-conquer technique. That is, we divide a complex problem into smaller instances of the same problem. Each division divides and reduces the problem further until the divisions are small enough to be resolved. We then aggregate those individual solutions to solve the original problem.
As a simple example, factorials can be calculated such that f(n) = n * f(n-1) for all n>1 where f(1) and f(0) are both 1. We can implement this function recursively as follows:
unsigned f(unsigned n) {
return n<2 ? 1 : n * f(n-1);
}
While the function itself is naturally recursive, that doesn't mean we must use a recursive solution. In this case, an iterative solution would actually be more efficient:
unsigned f(unsigned n) {
unsigned a = 1;
while (1<n) a *= n--;
return a;
}
However, in languages that allow compile-time computation (such as C++), recursive functions can be advantageous:
constexpr unsigned f(unsigned n) {
return n<2 ? 1 : n * f(n-1);
}
With this function, a good compiler will convert f(6) to the literal constant 720, completely eliminating the runtime overhead of invoking the function recursively. However, for values that cannot be computed at compile time due to excessive recursions, the runtime function will still be invoked, such that f(10) might be replaced with the constant expression 10 * f(9).
Languages that support template metaprogramming can provide the means to completely eliminate the runtime overhead associated with recursion:
template <unsigned N>
constexpr unsigned f () {
return N*f<N-1>();
}
template<>
constexpr unsigned f<1>() {
return 1;
}
template<>
constexpr unsigned f<0>() {
return 1;
}
Note that the terminating conditions are handled through specialisation rather than through recursion. At compile time, f<1>() invokes the first specialisation while f<0>() invokes the second. For all values N>1, the general function is invoked recursively at compile time.
constexpr unsigned x = f<10>();
Compile-time computation will effectively replace the above expression with:
constexpr unsigned x = 3628800;
Note that no code is generated for these template functions so we cannot invoke them at runtime, they are used purely for compile-time computation.
In languages that do not support template metaprogramming or constexpr, we can still make use of recursion, particularly where an iterative approach is too complex to implement efficiently. Divide-and-conquer algorithms are a typical example.
The Quicksort algorithm is a relatively simple recursive algorithm that can be implemented as follows:
void qsort (int a[], int lo, int hi) {
if (hi<=lo) return;
unsigned pivot = partition (a, lo, hi);
qsort (a, lo, pivot-1);
qsort (a, pivot+1, hi);
}
The partition() function does most of the actual work. Given the lower and upper bounds of the array it will select a pivot value and position this value such that all values to the left of it are less than it and all those to the right are not less than it. Once the array is partitioned, the index of the pivot value is returned. This value is used to determine the upper bound of the left sub-array (pivot-1) and the lower bound of the right sub-array (pivot+1).
What are the 3 turbo c language components?
Borland Turbo C came with an editor, compiler, linker and debugger, all of which were tightly integrated into the Turbo C IDE (integrated development environment). The professional version also came with standalone versions of the Turbo Assembler and Turbo Debugger.
Note that Turbo C is 27 years old. As such it is redundant. All Borland development tools are now owned by Embarcadero. Turbo C is now classed as "antique".
What are the difference between array declaration in java and c plus plus?
There is no difference, other than that declarations in C++ can also initialise the variable in a single instruction.
C example:
int x; // declaration
x=5; // initialisation
C++ example:
int y=5; // declaration and initialisation combined.
Structure of If-Then statement?
if (condition) statement1
[else statement2]
example:
if (i==j);
else if (j==k) printf ("i!=j, j==k\n);
else printf ("i!=j, j!=k\n);
here statement1 is an empty-statement, statement2 is another if-statement
There are three forms of statements
IF-THEN
IF-THEN-ELSE
IF-THEN-ELSIF
Sequence of statements is executed only if the condition evaluates to TRUE
If condition evaluates to FALSE or NULL, it does nothing
In either case control passes to next statement after the IF-THEN structure
IF THEN
statements;
END IF;
Sequence of statements in the ELSE clause is executed only if the condition evaluates to FALSE or NULL
IF THEN
statements;
ELSE
statements;
END IF;