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 arguments can be made against the idea of a single language for all programming domains?

You cannot. The only programming language understood natively by a machine is its own machine code. Every architecture has its own variant of machine code and for good reason. Just as the machine code for a piano player would make little or no sense to a Jacquard loom, the machine code for a mainframe would be impractical for a smart phone. Each machine has a specific purpose and therefore has its own unique set of opcodes to suit that purpose. Although some of those opcodes will be very similar and may have the same value associated with them, they won't necessarily operate in exactly the same way, so the sequence of opcodes is just as important as the opcodes themselves. Thus every machine not only has its own machine code it also has its own low-level assembly language to produce that machine code.

We could argue that we only need one high-level language, of course, but then that one language would have to be suitable for all types of programming on all types of machine. This is quite simply impossible, because some languages are better suited to certain domains than others. For instance, Java is an incredibly useful language because it is highly portable, but it is only useful for writing application software. It is of no practical use when it comes to writing operating system kernels or low-level drivers because all Java code is written against a common but ultimately non-existent virtual machine. If it were possible to write an operating system in Java, the extra level of abstraction required to convert the Java byte code to native machine code would result in far from optimal performance; never mind the fact you need to an interpreter to perform the conversion in the first place.


C++ is arguably more powerful than Java because it is general purpose and has zero overhead. Other than assembly, there is no other language capable of producing more efficient machine code than C++. However, C++ isn't a practical language for coding artificial intelligence systems; for that we need a language that is capable of rewriting its own source code, learning and adapting itself to new information. C++ is too low-level for that.


The mere fact we have so many high-level languages is testament to the fact we cannot have a single language across all programming domains. Languages are evolving all the time, borrowing ideas from each other. If a domain requires multiple paradigms for which no single language can accommodate, we can easily interoperate between the languages that provide the specific paradigms we need, possibly creating an entirely new language in the process. That's precisely how languages have evolved into the languages we see today.

What is the difference between a null pointer and a null macro?

Using a NULL macro to make C portable

I'll assume that you're asking your question for C type language programming. A NULL pointer is a pointer that's guarnteed to point to nothing. This may be 0 in a UNIX/Linux system or some other address in another system. Using the NULL macro to set/initialize your pointers will make your programs more portable among systems than using something like the 0.

#include

char *c = 0; // initialize to NULL--not portable
char *p = NULL; // initialize to NULL as defined in stdio is portable


AddendumThe code:

char *c = 0;

actually is portable because the compiler converts 0's used in a pointer context (cast to a pointer) to the machine's representation of a NULL pointer, which may or may not be all 0 bits. The NULL macro itself might be defined as something like 0 or (void *)0, and both definitions are portable. As a corollary, the following code is also portable:

if (!c) {
// do something
}

because it is equivalent to:

if (c != 0) {
// do something
}

and the 0 above is converted to a NULL pointer because it is being compared with a pointer.

Can stack be as a pointer?

A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Stack pointer is the pointer that points to the top of the stack or that points the item at the top of the stack and help in adding or deleting the item from the top of stack.

How do you write a programme that prints the sum of all the multiples of 3 or 5 below 1000 in c plus plus?

First work out the algorithm for what you want to do and then encode that using the C++ language.

Alternative Answer

In this case, the algorithm will consist of a loop that counts from 1 to 1000, testing each value to see if it can be divided by 3 or 5 without leaving a remainder. If so, it gets added to a running total. When the loop ends, the running total is the final answer.

To determine a remainder in C++, we use the modulo operator (%). That is; 9%5 is 4 because 9 divided by 5 is 1 remainder 4, whereas 9%3 is zero. Therefore if either operation evaluates to zero, the value can be added to the running total.

int DoSum( void )

{

int total=0;

for(int x=1; x<1000; ++x)

if( !(x%3) !(x%5) )

total+=x;

return(total);

}

An alternative (and more efficient) algorithm will perform the same loop twice, first in steps of 3 and then in steps of 5. Since we're no longer testing for invalid values, we simply sum every value.

int DoSum( void )

{

int total=0;

for(int y=0; y<2; ++y)

{

int step=y?5:3; // if y is 0, step is 3, otherwise step is 5

for(int x=step; x<1000; x+=step)

total+=x;

}

return(total);

}

What is the numerical range of a char?

There is no defined range of values in C. The built-in types all have ranges that are defined in <stdint.h>, <limits.h> and <float.h>. These ranges are implementation-defined, they are not defined by the language or by the standard. The standard only defines minimum guarantees such that a char is always at least 8 bits long (CHAR_BIT) and that an int is at least as long as a short which is at least as long as a char.

Write a c program to add 2 no without using plus operator?

You can make use of pointers to achieve this.

void add( int *a, int *b){

(*a) += (*b);

}


Now if two numbers a and b are given and you need to store the value in variable c, then you would perform:


c = a;

add(&c,&b);


How is the getchar function used in a C program?

The getchar() is used in 'C' programming language because it can read the character from the Standard input(i.e..from the user keyboard),and converts in to the ASCII value.

A design technique that programmers use to break down an algorithm into modules is known as?

When a programmer breaks down a problem into a series of high-level tasks and continues to break each task into successively more detailed subtasks, this method of algorithm creation is called:

What does descending order mean?

Ascending means to increase or rise. You can ascend stairs, for example, by climbing them. a pattern of numbers can be ascending if each number increases, such as 1, 2, 3, 4, 5.

Ascending is used to describe the return of Jesus from earth to heaven after his resurrection.

ok thanks!!
Going upwards. In different contexts it could mean increasing, getting bigger or climbing.

How do you write a program in C to convert temperatures between Fahrenheit and Celsius?

Code Example:/********************************************************************************* MODULE: main.c******************************************************************************** DESCRIPTION:* Program that takes a temperature from the user on the command line, then* displays that temperature as celsius converted to Fahrenheit, and* as Fahrenheit converted to celsius.********************************************************************************/#include #define iARGS_REQUIRED 2#define iARG_EXE 0#define iARG_INPUT 1static floatfCelsiusToFahrenheit( float fCelsius );static floatfFahrenheitToCelsius( float fFahrenheit );/********************************************************************************* MAIN********************************************************************************/intmain( int iArgc, char *acpArgv[] ){ float fFahrenheit = 0.0; float fCelsius = 0.0; float fInput = 0.0; /* user didn't provide a temperature on the command line */ if(iArgc != iARGS_REQUIRED) { fprintf(stderr, "Usage: %s [temperature]\n", acpArgv[iARG_EXE]); return 0; } /* read the given temperature into the fInput variable */ sscanf(acpArgv[iARG_INPUT], "%f", &fInput); /* fInput treated as celsius and converted to Fahrenheit */ fFahrenheit = fCelsiusToFahrenheit(fInput); /* fInput treated as Fahrenheit and converted to celsius */ fCelsius = fFahrenheitToCelsius(fInput); printf( "%.2f degrees Fahrenheit is %.2f degrees celsius.\n", fInput, fCelsius ); printf( "%.2f degrees celsius is %.2f degrees Fahrenheit.\n", fInput, fFahrenheit ); return 0;}/********************************************************************************* STATIC FUNCTION: fCelsiusToFahrenheit******************************************************************************** DESCRIPTION:* Converts a celsius temperature to Fahrenheit.** PARAMETERS:* fCelsius: The temperature in celsius to convert.** RETURNS:* fCelsius converted to Fahrenheit.********************************************************************************/static floatfCelsiusToFahrenheit( float fCelsius ){ return (fCelsius * 1.8) + 32;}/********************************************************************************* STATIC FUNCTION: fFahrenheitToCelsius******************************************************************************** DESCRIPTION:* Converts a Fahrenheit temperature to celsius.** PARAMETERS:* fFahrenheit: The temperature in Fahrenheit to convert.** RETURNS:* fFahrenheit converted to celsius.********************************************************************************/static floatfFahrenheitToCelsius( float fFahrenheit ){ return (fFahrenheit - 32) / 1.8;}

Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable.

Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.

An algorithm to Reversing the order of elements on stack S using 1 additional stacks?

// stack to contain content

Stack sourceStack = new Stack();

// ... fill sourceStack with content

// stack to contain reversed content

Stack targetStack = new Stack();

while (!sourceStack.empty())

{

targetStack.push(sourceStack.pop());

}

// targetStack contains the reversed content of sourceStack

What is An array of wavelengths?

It is a spectrum. I know this because i am currently in physical science class and we just discussed it. Unless my teacher and book are wrong, this is right.

Storage classes available in c language?

There are four types of storage class or variable in c.

1) auto storage class.

2) register storage class.

3) static storage class.

4) external storage class.

Write a java program that finds and displays all the prime numbers less than 1000?

CLS
FOR n = 2 TO 100
FOR k = 2 TO n / 2
flag = 0
r = n MOD k
IF r = 0 THEN
flag = 1
EXIT FOR
END IF
NEXT k
IF flag = 0 THEN PRINT n,
NEXT n
END

How do you Convert binary number 10000001 to decimal?

Every digit in a binary number corresponds to power of two. So 0001 0101 is equal to 0*2^7 + 0*2^6 + 0*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0, which equals 0+0+0+16+0+4+0+1, which equals 21.

What type of data deals with descriptions?

Descriptions are best represented using a character array (string) data type.

What is the most appropriate datastructure to implement priority queue?

Arrays are the most efficient data structure. Memory is allocated to the entire array as a single operation and the total memory consumed is equal to the product of the element size and the number of elements (all elements being of equal size, in bytes). This means that any element in the array can be accessed using simple pointer arithmetic from the start of the array, with the first element at offset 0. All high level languages hide the pointer arithmetic behind an array suffix operator, such that element [5] will be found 5 * sizeof (element) bytes from the start address of the array (the address where element [0] resides). Multi-dimensional arrays are implemented as an array of arrays, such that a two-dimensional array is a one-dimensional array where every element is itself a one-dimensional array. These can be thought of as being a table of rows and columns where the first dimension access a one-dimensional row array, and the second dimension accesses the column within that row. A three-dimensional array can then be thought of as being an array of tables or a cuboid (a stack of tables). A four-dimensional array can therefore be thought of as being an array of cuboids, a table of tables, or a cuboid of arrays. By imagining arrays in this manner it becomes much simpler to imagine arrays with more than 3 dimensions.

By contrast, a list or a tree structure is less efficient because every element requires at least one additional field to maintain the link from that element to another element, thus defining the structure. You also need to maintain an additional field to refer to the first element in the structure. If you have a structure that can dramatically vary in size, lists may be more efficient because there is no need to reallocate the entire structure; you simply allocate and deallocate memory for individual elements and update the links between elements to maintain the structure. However, you lose constant-time random access because you have to traverse the links in the structure to locate an individual element and the additional level of indirection means it will be slower than an array. However, reallocating an array often means copying the array to new memory. One way to minimise reallocations is to reserve more memory than you actually need, thus allowing you to add new elements more quickly at the cost of some memory. You only need to reallocate when you run out of reserve. You can also minimise the cost of reallocation by storing pointers rather than objects in your array. This adds an extra level of indirection, but speeds up the reallocation process by only copying pointers rather than objects being pointed.

How do you write a C program to find the GCD and LCM of two numbers using a switch statement?

The following function will return the GCD or LCM of two arguments (x and y) depending on the value of the fct argument (GCD or LCM).

enum FUNC {GCD, LCM};

int gcd_or_lcm(FUNC fct, int x, int y) {

int result = 0;

switch (fct) {

case (GCD): result = gcd (x, y); break;

case (LCM): result = lcm (x, y); break;

}

return result;

}

Multiple indirection in c language?

C uses pointers for indirection. So, using a pointer to a pointer would be multiple indirection. For example, the following code uses multiple indirection:

int i = 42;

int *pi = &i;

int **ppi = π

**ppi++;

printf("i is now %d\n", i);

What is the default return type in C and C plus plus?

No. There is no default return type for functions, it must be explicitly specified in both the function declaration and in the definition. To specify no return value, return void. To return a variant type, return void* (pointer to void). Otherwise return the exact type.

What is the C program to check whether a given character is vowel or not using if else?

#include<stdio.h> main() { char key; clrscr(); printf("Enter the key"); scanf("\n %c",& key); if (key>='A' key<='Z' )&&(key>='a' key<='z') { printf("%c is an character \n",key); } else { printf("%c is not character",key); } getch(); }

Why is c referred to as middle level language?

C is called a middle level language since it is a higher language than something like assembler, which communicates to the computer through operations that directly manipulate data and uses machine code.

High level languages, are very close to human readable/speakable languages, such as English and French ( and many more), and are therefore more human-oriented.

Unfortunately, the C programming language is neither a low-level language, such as assembler, or a high level language such as English, but somewhere in between. Thus a middle-level language
By mistake. It is a high-level language.

Reverse of a number using do while?

int RevNum( int num )

{

const int base = 10;

int result = 0;

int remain = 0;

do

{

remain = num % base;

result *= base;

result += remain;

} while( num /= base);

return( result );

}