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 role of stack in calling a subroutine and returning from the routine?

The stack has many purposes, but is chiefly used to enable procedure calls. When you invoke a function, the return address (the next instruction after the function call) is pushed onto the stack. Control is then passed to the function. When the called function returns, it pops the return address off the stack and control is returned to that address. In this way functions can call functions without losing track of where the calls came from because return addresses are popped in the reverse order they were pushed. In other words, it provides an automatic "breadcrumb trail" without which it would be impossible to call and return from functions.

The stack is also used to pass actual arguments to functions by pushing values onto the stack immediately after the return address. The called function can then pop those values off the stack and assign them to its formal arguments.

Finally, the stack is used to store the function's local variables prior to calling a function. These are pushed onto the stack before the return address is pushed. In this way, a function can call itself recursively without losing track of its own local variables.

Since invoking and returning from functions is a time-consuming operation, compilers will try to expand functions inline and thus eliminate the overhead of the function call. However, this needs to be weighed against the increased code size which can affect overall performance. By keeping functions small (one or two very simple statements), programs not only become much easier to read and maintain, there's also a much greater opportunity for inline expansion. Complex functions are never good examples for inline expansion.

Program to copy one string to another?

As usual, you should check official documentation before you ask a question like this.

string.h

// Copies num characters from source into destination.

char* strncpy (char* destination, const char* source, size_t num);

// Copies characters from source into destination.

char* strcpy (char* destination, const char* source);

Write an algorithm and draw corresponding flow chart to check whether a given string is a palindrome or not?

Program

# include<stdio.h> void main() { long n,rev,t; int r; clrscr(); printf("Enter number : "); scanf("%ld",&n); t=n; rev=0; while(t>0) { r=t%10; rev=(rev*10)+r; t=t/10; } if(n==rev) printf("Number is palindrom"); else printf("Number is not palindrom"); getch(); }

algorithm

step 1 : input n

step 2 : s = 0, a=n

step 3 : while(n>0)

begin

rem=n%10

s=s*10+rem

n=n/10

end

step 4 : if(s==a)

print 'it is a palindrome'

else

print 'it is not a palindrome'

step 5 : stop

Program in C language to find factorial value Fibonacci valueGCD value using recursion?

#include<stdio.h>

#include<conio.h>

void main()

{

int i,fact=1,n;

clrscr();

printf("\nEnter the no of terms =");

scanf("%d",&n);

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

fact*=i;

printf("\nFactorial value for %d terms = %d",n,fact);

getch();

}

What is a header node in c plus plus?

A header node, or head node, is a node that marks the start of a series of nodes, usually as part of a list or queue structure. The head node is often a sentinal that holds no data of its own. Sentinels are used to simplify algorithms by ensuring that a list can never be empty, even when it has no data.

What is time complexity and space complexity?

"Running Time" is essentially a synonym of "Time Complexity", although the latter is the more technical term. "Running Time" is confusing, since it sounds like it could mean "the time something takes to run", whereas Time Complexity unambiguously refers to the relationship between the time and the size of the input.

Write a program in c language to reverse elements of a queue?

There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements.

However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack.

You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue.

Your queue will have the exact same elements as in the beginning, but in reverse order.

The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers.

Following is pseudocode:

Queue<Item> reverse (Queue<Item> queue) {

Stack<Item> stack;

Item item;

while (queue.remove(&item)) {

stack.push(item);

}

while(stack.pop(&item)) {

queue.add(item);

}

return queue;

}

How do you display alphanumerics in C program?

#include<iostream.h>

int main()

{

char i,j;

for(i='a';i,<='z';i++)

{

cout<<i<<(char)(i-32);

}

return 0

}

What is the object-oriented version of C programming language that is used to develop software for PCs such as Fractal Design Painter Lotus 123 and games?

There isn't one; C is strictly non object oriented. Although C++ is often considered to be an object-oriented extension for C (it was originally called C with Classes), it would be more accurate to describe them as siblings. The two have evolved separately and while they still retain a high-level of compatibility through their common ancestry, they are not the same language.

Why is Java better than C programming?

Java is very popular among high schools and universities for an introductory language. Java syntax is very similar to the "classic" C and C++ languages, but Java is much more developer-friendly when it comes to error feedback and memory management.

When entire generations of programmers are introduced to the field with the same language, that language tends to become the "popular" one.

How can you pass a function name as argument of another function?

== == Let me correct the Q. Strictly speaking, You never pass a function name to another function, you actually pass function address as argument to another function. However, since the function name automatically resolves into function address, it could be deemed correct to say that you pass function name.

Now, the answer:

If you're talking about function pointers:

void Foo(double (*fptr)(int), int x, int y);

double Bar(int i);

int main()

{

Foo(&Bar, 1, 2);

/*Previous Line passes Bar's address to Foo. The & behind Bar is optional because it's implicit, but I put it there to emphasize that it's the address of Bar being passed.*/

return 0;

}

void Foo(double (*fptr)(int), int x, int y)

{

(*fptr)(10);

/*Previous line will call Bar(10) because main passed it Bar's address. Explicitly declaring the dereference is not required, however I explicitly wrote it in to emphasize that it is a pointer that is getting dereferenced.*/

//do something

}

double Bar(int i)

{

//do something

return 0;

}

Note, the function pointer must have the same parameter list as the function you are trying to set the function pointer equal to.

List the steps how to implement a RPC?

RPC at a Glance 1. Client: invokes stub through a local procedure call

2. Client stub: convert presentation, package parameters, send to server

3. Server stub: unpack parameters, convert presentation

4. Server: executes procedure returns value

5. Server stub: package return values, send to client

6. Client stub: unpack return values and pass to client

Submitted by : Mr. Abdul Malik Y.

sakr232000@yahoo.co.in

Advantages of switch - case over if else?

1.Switch statement looks a lot more tidy and easy to read and understand.

2.Here multiple statements need not to be enclosed within a pair of braces.

3.Switch statement executes faster than if_else because the switch statement checks the condition first and then jumps to the suitable case statement.

Explain the basic datatypes in C?

C datatype can be categories into two part

1) Scalar data type 2) derived data type..

The scalar data type is also called basic data type

they are

int

char

float

double

long

signed or unsigned are key word which effectively change the storing power of these data type.

by default they are signed in nature..

How many types of operators?

The different types of operators are:

Assignment operator- This is used to assign values to variables. Ex: =

Arithmetic Operators - These are used to perform arithmetic operations. Ex: +, -, *, /, %

Logical Operators - These are used to perform logical checks like: I < 10 or x == Y etc.

What is A part of a program in witch a variable may be accessed?

The part of a program in which a particular variable may be accessed is called the 'scope' of the variable.

In most cases, the scope of a variable is limited to the function within which it was created, or any function it is passed to as an argument.

You can also use global variables, which can be accessed from any part of the program and have 'global scope'. However, this is generally considered as poor programming practice, and should be used cautiously and sparingly as it tends to make code difficult to read and maintain.

Is algorithm a pictorial representation of flowchart?

Flow Chart DefinedA flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction. Common Flowchart SymbolsDifferent flow chart symbols have different meanings. The most common flow chart symbols are:
  • Terminator: An oval flow chart shape indicating the start or end of the process.
  • Process: A rectangular flow chart shape indicating a normal process flow step.
  • Decision: A diamond flow chart shape indication a branch in the process flow.
  • Connector: A small, labeled, circular flow chart shape used to indicate a jump in the process flow. (Shown as the circle with the letter "A", below.)
  • Data: A parallelogram that indicates data input or output (I/O) for a process.
  • Document: Used to indicate a document or report (see image in sample flow chart below).

What is pointer and pointer types and uses of pointer and the meaning of pointer?

The main advantages of using pointers are

1.) Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.

2.) In the case of arrays, we can decide the size of the array at runtime by allocating the necessary space.

C has a minimum number of fundamental data types - a single character, a single integer or float, and a few derived data types such as a structure, an enumerated list, and an array. If you want to do much more than that, you make use of pointers.

Pointers allow you to dynamically request memory to store off information for use later. They allow you to create linked lists and other algorithmically oriented data structures.

What mark is used to separate arguments in a function?

That depends on the syntax rules of the language in which you are programming. However, the "," is the most usual separator).

What does a dashed arrow mean in a flow chart?

An arrow is used in a flowchart because it links one part to another. For example, a flow chart describing the process of electric energy:

[Power plant] ---> [Transformer] ---> [Power lines] ---> [Transformer] ---> [House] ---> [Transformer] ---> [Device]

As you can see, the arrows show the direction of where the flow chart is leading, telling the viewer where to begin, and where the to.

Program for stop and wait protocol in c language?

/*************************************************************************************/

/* C program to implement stop and wait protocol*/

/* Download more programs at http://sourcecode4u.com/ */

/*************************************************************************************/

#include<stdio.h>

#include<conio.h>

#define MAXSIZE 100

typedef struct

{

unsigned char data[MAXSIZE];

}packet;

typedef enum{data,ack}frame_kind;

typedef struct

{

frame_kind kind;

int sq_no;

int ack;

packet info;

}frame;

typedef enum{frame_arrival}event_type;

typedef enum{true_false}boolean;

void frame_network_layer(packet *p)

{

printf("\n from network arrival");

}

void to_physical_layer(frame *f)

{

printf("\n to physical layer");

}

void wait_for_event(event_type *e)

{

printf("\n waiting for event n");

}

void sender(void)

{

frame s;

packet buffer;

event_type event;

printf("\n ***SENDER***");

frame_network_layer(&buffer);

s.info=buffer;

to_physical_layer(&s);

wait_for_event(&event);

}

void from_physical_layer(frame *f)

{

printf("from physical layer");

}

void to_network_layer(packet *p)

{

printf("\n to network layer");

}

void receiver(void)

{

frame r,s;

event_type event;

printf("\n ***RECEIVER***");

wait_for_event(&event);

from_physical_layer(&r);

to_network_layer(&r.info);

to_physical_layer(&s);

}

main()

{

sender();

receiver();

getch();

}

How can you calculate the c program running time through c plus plus coding?

This is a big question... but I'll try for a shortish answer. Try to do things in approximately the following order.

1) Use the right algorithms. Look into the Big O efficiency of any algorithms you are using, and make sure there aren't more efficient algorithms available.

2) Think about trading using more memory for a faster algorithm. Perhaps pre-computing tables of intermediate results.

3) Use a profiler to see where the bottle necks are and try improving them.

4) Optimize the inner most loops using assembly language to get the last little bit faster.

5) Run it on a faster computer.

6) Make it run in parallel across many computers or CPUs. This is especially good on the newer Intel chips where you have access to multiple CPUs on one chip. Distributed network computing sometimes is a good idea. Cloud computing is another possibility these days.

7) Make sure that C really is the right answer. It might not be in all cases.

A more specific question might yield a more specific answer.

Why JVM is platform independent?

by creating a jre spesific to each platform programmers can confidently write code in any platform and assume it willl also work in aany other. theirfore java is platform independent as bytecode would look the same on any platform, however will be implemented by a diffrent interpreter for each platform.

What do you call a Program that runs Java byte code instruction?

Get the JDK & Bluej from net and the rest will be done by them.

Java byte codes are stored as *.class ; where "*" represents the class name, in your hard disk.

You can download BlueJ as well as JDK from the related link.