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 external variable in c language?

The "extern" declaration in C is to indicate the existence of, and the type of, a global variable or function. A global variable, or a global function, is one that is available to all C modules (a single C module is typically a single .c file). An extern is something that is defined externally to the current module. In many cases, you can leave off the extern qualifier and not notice any difference because the linker can collapse multiple definitions to one. But the intent is then unclear in the code, and the code is error prone in case of typos. It is much clearer to define the global in one place, and then declare extern references to it in all the other places. When refering to globals provided by a library, especially a shared library, this is even more important in order to ensure you are talking about the correct, common instance of the variable. Declaring a variable as extern will result in your program not reserving any memory for the variable in the scope that it was declared. For instance (as example) if a program's source code declared the variable var as a global volatile int in foo.c, to properly use it in bar.c you would declare it as extern volatile int var. It is also not uncommon to find function prototypes declared as extern.

A good C manual will certainly answer this more completely.

What is bottom up approach in C programming?

Top down and bottom up programming are two approaches or methodologies employed for designing structured programs in C.

TOP DOWN APPROACH

In a top down approach a program(structured) is designed by using the top down methodology as follows..

First the overall structure of the program is designed and it is defined and then it is followed by the designing of individual functions..

BOTTOM DOWN APPROACH

The bottom down approach is just the opposite of the top down approach..

ie, the program is designed by first designing the individual functions followed designing of overall program structure

Why arrays are using?

1. An array cuts down on the number of variables that need to be declared.

For example, if you wanted to store the numbers 1 to 10 without using an array you would literally have to declare ten separate variables.

Dim int1 as Integer = 1

Dim int2 as Integer = 2

Dim int3 as Integer = 3

Dim int4 as Integer = 4

Dim int5 as Integer = 5

Dim int6 as Integer = 6

Dim int7 as Integer = 7

Dim int8 as Integer = 8

Dim int9 as Integer = 9

Dim int10 as Integer = 10

Which is monstrously difficult to read and use further along in your code. A better way of doing it would be to use one variable; an integer array.

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Using this example you use the same variable throughout your code and access different parts of it as follows;

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Dim intTemp as Integer

MsgBox(intArray(0))

MsgBox(intArray(4))

This example displays two textboxes which will display the number 1 and 5 to the user (Note that array starts at zero)

2. An array can be used in itterative procedures such as a For... Next loop.

For example if we use the previous example of an integer array containing numbers 1 through 10, and with this array we want to display every number to the user.

Without an array this is an almighty block of code with a minimum of 20 lines, 10 declaring each variable and 10 outputting it to the user. With an array this can be reduced to four lines.

Dim intArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

For i = 0 to intArray.Length

MsgBox(intArray(i))

Next

The importance of this is that in our example we only have ten numbers stored, in the real world you could have hundreds. Declaring hundreds of variables and messing around with them will take up memory, obfuscate the code immensely and make it virtually unchangeable.

With the above example that array could be any length and contain a thousand variables and still function the same.

3. An array can be declared as a variable length.

Whilst it's all well good saying we have an example array containing numbers 1 through 10, it is unlikely to ever be that simple; we will always want to store an unknown amount of data at runtime.

Just declaring variables in the code wouldn't work for this because they would need to be hardcoded.

In this example we will pretend a form has been made with a listview containing a hundred items, when a user clicks a button we want to store everything they selected in the listview. (If you don't know what a listview is I would ask that question as well)

Dim intVarArray(ListView1.SelectedItems.Count - 1) as Integer

For i = 0 to ListView1.SelectedItems.Count - 1

intVarArray(i) = ListView1.SelectedItems(i).ToString

Next

To do this using nothing but variables would be near impossible as you wouldn't know how many items the user would select, as such you would have to declare a hundred variables and write hundreds of lines of code to do something that can be covered in four lines.

4. An array can be resized on the fly, either destroying or preserving existing data.

In the above example we created an array of variable length and then stored some data from a made up listview control, but it's possible when working in the real world we may need to add things to that variable.

We can Redim an array to change its length.

Dim intArray(9) as Integer

Redim intArray(14)

This declares a variable of an array of integers, sets the length to 10 and then redefines that array as an array of length 15. This will completely remake the variable destroying any data held within it.

Dim intArray(9) as Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Redim Preserve intArray(14)

intArray(10) = 11

intArray(11) = 12

intArray(12) = 13

intArray(13) = 14

intArray(14) = 15

This declares a variable of length 10 and stores the number 1 to 10 inside it, it then redefines the variable as length 15 but preserves the data within it (It then adds the number 11 to 15 to that variable).

5. An array can have multiple dimensions.

Dim intArray(, ) as String = {{"White", "Rabbit"}, {"Brown", "Bear"}, {"Blue", "Water"}}

This declares something more unique than any other type of single variable, a multidimensional array, and stores some data within it.

--
Ac532 Answer:

Arrays can be disposed by using the .Dispose which means all allocated memory toward using the array will be lost meaning that your program will run faster.

What is the difference between getcher and getch statements in C?

If you are referring to getchar(), it returns a single character as an 'int'.

'scanf' allows the user to input many different types of variables, and converts them accordingly using a format specified by the user.

If you type 'man getchar' and 'man scanf' you will get additional information on how to use them.

How is memory allocated to arrays?

It depends where the array is declared and whether or not it is fixed-length or variable-length. The only thing all arrays have in common is that the memory is allocated contiguously. Fixed length arrays are arrays where the length is known at compile time while variable length arrays have unknown length and are allocated at runtime.

Fixed-length arrays can be allocated statically, on the call stack (local memory) or on the heap (the free store) while variable length arrays must always be allocated on heap. Fixed-length arrays should use a constant variable to refer to the array length consistently while variable-length arrays should use a (mutable) variable. Arrays allocated on the heap must also use a pointer variable to keep track of the start address. The array (or pointer) and its length should always be declared in the same scope. Storing both in a data structure makes it easier to keep track of the array and its length.

Static arrays are always fixed length and are allocated at load-time, before entry to the main function (even if declared in the main function or in another function entirely). The memory remains allocated until the program terminates. If there is insufficient memory available, the program itself will fail to load. It is recommended you use static memory sparingly and avoid the use of globals whenever possible.

Local arrays are also fixed length but are allocated on the call stack at runtime as and when they come into scope. The memory is released automatically when the array falls from scope. The stack is fixed-length and therefore has limited capacity, so local arrays should be used sparingly.

Variable length arrays and fixed-length arrays allocated on the heap are allocated and released manually (programmatically) at runtime. It is the programmer's responsibility to keep track of the array's start address. If there is insufficient memory available, it is the programmer's responsibility to handle the exception.

Examples:

// Fixed-length array at global scope // Note that all globals are implicitly static:

const int a=10;

int A[a]; // static array of 10 integers

// Fixed-length array at file scope

// Note that all file scope variables are explicitly static:

static const int b=20;

static int B[b]; // static array of 20 integers

// Fixed-length arrays at function scope:

void f ()

{

const int size=100;

static int C[size]; // static array of 100 integers (allocated at load-time)

int D[size]; // local array of 100 integers (allocated when f comes into scope)

int* E = malloc (size * sizeof (int)); // heap array of 100 integers

// Note: if E is NULL, the allocation failed!

// ...use arrays...

free (E); // release the allocation before E falls from scope!

E = NULL; // good housekeeping!

}

// D is released here

// C, D and E will fall from scope here, however C is not released

// Variable-length arrays at function scope:

void g (const int n)

{

int* F = malloc (var * sizeof (int)); // heap array of n integers

// ...use array...

free (F);

F = NULL;

}

// F falls from scope here.

How a C program to find LCM using while loop?

Problem Statement for LCM:

Enter Two digits by a user and get its LCM

Answer:

#include

#include

void main()

{

int x,y,i,j;

clrscr();

printf("Enter Two Digits");

scanf("d",&x,&y);

for(i=1;i<=x;i++)

{

for(j=1;j<=y;j++)

{

if(y*i==x*j)

{

printf("LCM is %d\n",y*i);

i=i+x;

}

}

}

getch();

}

OR

there will be another easy way to solve it...

Answer:

#include

#include

void main()

{

int x,y,i,;

clrscr();

printf("Enter Two Digits = ");

scanf("d",&x,&y);

for(i=1;i<=x*y;i++) {

if(i%x==0&&i%y==0)

{

printf(LCM is %d",i);

break;

}

}

getch();

}

What is balanced binary search tree?

A balanced tree is a tree which is balanced - it has roughly the same height on each of its sub-nodes. A balanced tree will have the lowest possible overall height.

For example, a balanced binary search tree will have equal heights (plus or minus one) on the left and right sub-trees of each node. This ensures that operations on the tree always are guaranteed to have O(lg n) time, rather than the O(n) time that they might have in an unbalanced tree.

Certain tree algorithms are designed for ensuring that the tree stays balanced at all times, while maintaining the O(lg n) time for all operations. Such algorithms, such as red-black trees, AVL trees, and others, are generally used in standard library implementation of binary search trees.

How to write printf statement for read characters into array str until the letter p is encountered?

In pseudo-code:

while ( not(end of string) and letter(string at position X) is not 'P' ){

add(array, newposition) = letter(string at position X);

}

How many dimensions you can declare in array?

It seems that the number of allowed array dimensions is implementation specific and not set by the Java specifications. I'm sure that any Java implementation will allow a reasonable number of dimensions for any project you have.

After a quick test, it seems that Java is not limited by an arbitrary number so much as a practical value. If you add hundreds of array dimensions, Java will allow you to do so as long as you have enough memory allocated for Java. After a bit of copy-pasting the program no longer ran, exiting with a StackOverflowError.

Write a C program to display odd numbers from 1 to n where n is accepted from user?

#include<stdio.h>

main()

{

int i=0,j=0,n;

printf("Enter a number:");

scanf("%d",&n);

printf("The first %d odd numbers are:-\n");

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

{

if(j%2!=0)

printf("%d\n"j);

j=j+1;

}

}

Who was Dennis Ritchie?

Dennis Ritchie is the creator of C programming language and, with Ken Thompson, the creator of the Unix operating system. He is also co-author of 'The C Programming Language' (Kernighan & Ritchie), which gave rise to what is now known as K&R C, the original version of C prior to what is now known as Classic C.

What is the use of realloc in c language?

Realloc is a function in C. It is used to change the size of a block of memory by expanding it. There are various ways realloc can change the memory size, depending on if there is enough space in the block of memory.

Write a c program to convert from cartesian to polar coordinates?

the equation that convert from cartesian to polar coordinates and vice versa

r = sqrt (x*x+y*y);

phi = atan2 (y, x);

x = r*cos (phi);

y = r*sin (phi);

What is faster access the element in an array or in a list?

Array is always faster to read from disk/access any element in the array is quicker since elements in a array are stored in contiguous location in the memory, you need the pointer to the head or 0th element in the array and then it much quick to navigate to the next on index based. But you need to know INDEX of the element for best results

List (say linked list) will be slower since not always elements are stored in contiguous location in the memory as well it involves a function call which is can be assembler/cpu expensive.

However getting an individual object from an array is faster if you know the index of the object. Walking through a linked list is faster than walking through an array, if you use a non-recursive algorithm.

--Vinay Solanki

What are the Differences between array and queue?

from the abstraction: list

you go to the implementation details (concrete):

arraylist,vector or linkedlist,circularlinkedlist and maybe more

when somebody asks you for a special list in this case (queue)

speciality 1 enqueue -add to botton

speciality 2 dequeue - remove from top

you are free to pick up arraylist,and/or linkedlist and/or circularlists and/or vector to implement your new specialized list called queue.

and/or means you might want to combine as well

What is the function of radioisotopes C-14?

Used in carbon dating that is to find the age of fossils.

Far pointer size in C plus plus?

If you are talking "far pointer", then you are probably talking about real mode in a 16 bit environment such as DOS or Windows 3.1, or in Virtual 8086 mode in Windows 95 or higher. In this mode, addressing is segmented into 65536 segments of 65536 bytes each, but each segment overlaps the next by only a 16 byte offset. This gives you addressability to 1048576 bytes. A far pointer is a 32 bit object, containing a 16 bit segment and a 16 bit offset. int __far *p; /* a far pointer called p which points to an int */

What is node in c language?

No such predefined type, so you can define it as you wish.

What does cout stand for in c?

It serves to send usually text-based information on the monitor (in currently running application, window), for instance:

...

int myVariable;

cout >> "Please enter a number: ";

cin >> myVarible;

//You entered number 5

cout << "You entered the number: " << myVariable;

...

In the application window you will see:

Please enter a number:

5

You entered the number: 5

Write a program to add two numbers using oop?

#include<iostream.h>

#include<conio.h>

void main()

{

int a, b, c;

clrscr();

cout<<"enter the two numbers";

cin>>a;

cin>b;

c=a+b;

cout<<"Addition of two numbers="<<c;

getch();

}

Who invented data structures?

Data structure is a very basic concept.

I don't think it's possible to trace it back to a single person who invented it...

Difference between Data Type and Abstract Data Type?

A data type tends to mean a primitive data type. Primitive data are built-in data types, such as integers, characters and Booleans. They are basic constructs of the language (that is, they are built into the language).

Primitive data also tends to be of a strict data type, meaning you can't treat characters like integers or Booleans like integers, etc., although some languages will support implicit casting of primitive data types (for example, will treat Booleans like integers if you use a Boolean in an arithmetic operation).

Abstract data types are generally constructed by the user or by a higher level language. For example, you might create a currency data type, which generally acts like a float but always has a precision of 2 decimal places and implements special rules about how to round off fractions of a cent.

Abstract data types also often contain the ability to either be treated as a specific type of primitive data in certain circumstances (for example, many languages allow you to treat strings as character arrays); or contain certain rules / methods to manipulate their data (such as a programming language allowing you to cast a float as an integer).

A data structure is a gathering together of many different data types. For example, objects and arrays are data structures. Data structures usually can contain information of many different types (such as strings, integers, Booleans) at the same time, and in more complex structures -- namely, classes -- can contain specific methods, properties and events to manipulate that data, change its type, etc.