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 the scope of any variable?

The Scope of a variable defines the areas of a program where this variable would be visible and can be used. For ex:

a. Method variables - are visible only inside the method where they are declared and hence their scope is only the method

b. Class variables - are visible inside the class and can be used by any method inside the class and hence their scope is the whole class.

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

  1. #include
  2. main()
  3. {
  4. char ch;
  5. clrscr();
  6. pritnf("\nEnter any character:");
  7. fflush(stdin);
  8. scanf("%c",&ch);
  9. if (ch 'U' ch = 'u')
  10. {
  11. printf("\n The entered character %c is Vowel.\n", ch);
  12. }
  13. else
  14. {
  15. printf("\nThe entered character %c is not Vowel.\n",ch);
  16. }
  17. }

What is the difference between C and Linux?

There is very little difference in the C compiler between Unix and Linux; in some cases (the gcc compiler) it is the same.

The differences come in when using system calls; some system calls do not exist in Unix or Linux, although most do. The program I work on compiles the same way (for the most part) between all commercial versions of Unix and several variants of Linux. In other words, the code is fairly portable across platforms.

What is the benefit of using a stack-linked list?

The basic advantage of the Link list is when you have to store the data dynamically and the number of data to be stored is not known in advance.

Example: The word document stores all the data(words) in form on link list. In this scenario you never know how many words will a document contain initally.

Why getch is used at the end of every c program?

getch() takes one char from keyboard but it will be invisible to us. in other words we can say that it waits until press any key from keyboard.

getch() is a function which has its protype defined in conio.h header file.

it is basically used to take input a single characterfrom keyboard. and this char is not displayed at the screen.

it waits until itn gets a input that's why it can be used as a screen stopper.

Difference between fixed loop and variable loop?

Fixed loop: this is the loop where the number of iterations are known.

Variable loop: Here the number of iterations are not known

Example for a variable loop.

The pseudocode for variable whille loop

begin

character cchoice

display"enter the choice"

accept cchoice

while(cchoice='y')

begin

//execute the statements

end

end

Rkarthikeyan

What is yytext?

the return value of yytext is single token that was just recognised..

Why do you use decimal number instead of binary number?

The octal and hexedecimal numbering system allows you to specify the contents of an object with fewer characters, making it easier to read and write the values. An example is 0001001000110100 is 123416 or 110648. It is also 466010 but that requires a non-trivial conversion, something you can not easily do by sight.

Program to find the sum of harmonic series?

Let's assume that you want the sum of the general harmonic series:

sum(n=0,inf): 1/(an+b)

Since we know that the harmonic series will converge to infinity, we'll also assume that you want the sum from 0 to n.

double genHarmonic(const double n, const double a, const double b) {

double sum = 0.0;

// perform calculations

int k;

for(k = 0; k <= n; ++k) {

sum += 1.0 / (a * k + b);

}

return sum;

}

Difference between Fibonacci heap and binomial heap?

Like a binomial heap, a fibonacci heap is a collection of tree. But in fibonacci heaps, trees are not necessarily a binomial tree. Also they are rooted, but not ordered.

If neither decrease-key not delete is ever invoked on a fibonacci heap each tree in the heap is like a binomial heap.

Fibonacci heaps have more relaxed structure than binomial heaps.

Why you use curly braces in C programming?

Only the designers Brian Kernighan and Dennis Ritchie can answer this question. But I would guess it was because of the handy notation on the drawing board to represent the concept of BEGIN and END.

How do you write java program to generate the number triangle?

Save the following file as PascalTriangle.java:

public class PascalTriangle {

public static void main(String args[]) {

int x = 6;

int triangle[][] = new int[x][x];

for (int i = 0; i < x; i++) {

for (int j = 0; j < x; j++) {

triangle[i][j] = 0;

}

}

for(int i = 0; i < x; i++) {

triangle[i][0] = 1 ;

}

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

for (int j = 1; j < x; j++) {

triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];

}

}

for (int i = 0; i < x; i++) {

for(int j=0;j<=i;j++) {

System.out.print(triangle[i][j]+ " ");

}

System.out.println();

}

}

}

answer 2:

import java.util.*;

class pascal

{

protected static void main()throws Exception

{

Scanner sc=new Scanner(System.in);

System.out.print("Enter the limit: ");

byte a=sc.nextByte();

sc.close();

String l[]=new String[a];

for(byte i=0;i<a;i++)

l[i]="";

l[0]="1";

l[1]="1 1";

for(byte i=2;i<a;i++)

{

for(byte j=0;j<(i+1);j++)

{

if(j==0j==i)

l[i]+="1 ";

else

{

StringTokenizer st=new StringTokenizer(l[i-1]);

byte k=(byte)st.countTokens(),n=0;

String w[]=new String[k];

for(byte f=0;f<w.length;f++)

w[f]="";

for(byte f=0;f<l[i-1].length();f++)

{

if(l[i-1].charAt(f)==' '){

n++;

continue;

}

w[n]+=l[i-1].charAt(f);

}

n=0;

int x=Integer.parseInt(w[j-1])+Integer.parseInt(w[j]);

l[i]+=(String.valueOf(x)+" ");

}

}

}

for(byte i=0;i<l.length;i++)

{

System.out.println();

for(byte j=a;j>=i;j--)

System.out.print(" ");

for(byte j=0;j<l[i].length();j++)

System.out.print(l[i].charAt(j));

}

}

}

How make a calculator in c?

#include

int main(){

int a, b, choice;

printf("This program implements a calculator\n");

do{

printf("Enter your choice:\n1-Addition\n2-Subtraction\n3-Multiplication\n4-Division\n5-Modulus\n6-Quit\t:");

scanf("%d", &choice);

switch(choice){

case 1: printf("Enter two numbers :");

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

printf("Sum = %d\n", a+b);

break;

case 2: printf("Enter two numbers :");

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

printf("Subtraction = %d\n", a-b);

break;

case 3: printf("Enter two numbers :");

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

printf("Product = %d\n", a*b);

break;

case 4: printf("Enter two numbers :");

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

printf("Quotient = %d\n", a/b);

break;

case 5: printf("Enter two numbers :");

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

printf("Remainder = %d\n", a%b);

break;

case 6: break;

default: printf("Invalid choice\n");

}

}while(choice != 6);

return 0;

}

What are some examples of assembly level language?

These days very few programs are written in assembly language. Some parts of operating system kernels are written in assembly language usually because they need to perform some function very specific to a particular microprocessor architecture. Other programs written in assembly language include programs written for very cheap microprocessors in embedded systems. Such systems have very little resources and do not run operating system and compilers. Finally some specific parts of applications programs may be written in assembly language for performance optimization, but examples of those today are quite rare indeed.

What is precedence between relational operator and arithmetic operator?

Arithmetic operators (+, -, *, /, % ) have greater precedence over relational operators (<, >, <=, >=, ==, !=) in C language.

Do while loop uses?

The DO WHILE loop is like the WHILE loop very much, except for one thing. The WHILE loop is like an IF loop but it keeps on going. The WHILE loop needs to start, so the beginning, when you are setting the conditions, is exactly the same. When the WHILE loop is done, it goes back to right before the conditions. The DO WHILE loop starts the WHILE loop so that you can have a WHILE loop that does not start with conditions, but instead, needs conditions to keep on going. This might not be the exact syntax, because if it doesn't work try it without the semicolon after the while. And anyways, if that doesn't work, I started with C++, not C.

do

CONTENT

while(CONDITIONS);

The WHILE loop goes back to the DO if the conditions are matched. The DO happens without any conditions.

:) :) :)

How do you write a magic square program with basic?

Answer

You're better off using a program like Visual Basic or C++ to do that. QBasic doesn't have very many capabilities.


AnswerQBasic is quite capable. It is certainly capable of solving a magic squares problem.

How do you write a program in c to get the factorial of a number?

There are two ways to implement a factorial function. The first is the conventional method using a constexpr function:

constexpr fct (unsigned n) {

return (i < 2) ? 1 : n * fct (n - 1);

}

This has the advantage in that it can be used for both compile-time and runtime computation:

constexpr unsigned x = fct (5); // compile-time computation

A good compiler will perform the complete calculation at compile-time, thus the declaration of x is equivalent to:

constexpr unsigned x = 120; // e.g. x = 5 * 4 * 3 * 2 * 1

Conversely, the declaration of y is not constexpr, thus the calculation must be performed at runtime. The same is true of any non-const variable.

However, if we only required compile-time computation, then we can use template-metaprogramming instead:

template<unsigned N>

constexpr unsigned fac (void) {

return N*fac<N-1>();

}

template<>

constexpr int fac<2> (void) {

return 2;

}

Note that we cannot use variables in a template intended for compile-time computation, hence we use a template parameter and a specialisation to enable the recursion. The specialisation for N=2 represents the end-point of recursion. We could also provide a specialisation for N=1 and N=0, however this would be superfluous given that fac<1> and fac<0> both equate to 1 and we'd never deliberately invoke these in code.

Note also that the largest integer we can represent with an unsigned integer is UINT_MAX (defined in <limits>). For a 32-bit integer this is (2^32)-1, thus fct<12> is the largest factorial we can calculate. If we use uint64 instead, then we can calculate factorials up to fct<21>. However, the larger the value of N, the less likely we can make use compile-time computation, but we can still make use of partial compile-time computation in conjunction with runtime computation if we use the constexpr version of the function.

If we wish to accommodate larger factorials, then we must either use a long double or use a user-defined type that can handle larger integers, however the latter would mean losing compile-time computation altogether.

Programming to calculate a factorial number?

double factorial(double N)

{

double total = 1;

while (N > 1)

{

total *= N;

N--;

}

return total; // We are returning the value in variable title total

//return factorial;

}

int main()

{

double myNumber = 0;

cout << "Enter the number: ";

cin >> myNumber;

cout << endl << "Factorial of N is: " << factorial(myNumber);

return 0;

}

Write a Fibonacci function then takes an input from the user in main program and pass to function which prints Fibonacci series up to this number using function in c plus plus language?

We use long int or long instead of int as for larger terms the number is also larger.

Code is as follows:

#include

#include

long fib(long n);

void main()

{

long res,n;

int i;

printf("Enter the number of terms of the Fibonacci Series:\t");

scanf("%d",&n);

for(i=0;i

{

res=fib(i);

printf("\n %d",res);

}

_getch();

}

long fib(long n)

{

long res;

if(n==0n==1)

return (n);

else

res=fib(n-1)+fib(n-2);

return(res);

}

What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.


Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).



How many pointers can be used in a c program?

Answergenerally we use simple pointer, void pointer,null pointer, structure pointer. Answerzero or more (unlimited).

Why is a just-in -time compiler useful for executing Java programs?

I'm not sure if it's "useful" as much as it is the fact of it being how the Java compiler works.

However, there's a GCC frontend for compiling Java to native machine code rather than bytecode.