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 are the feature of C language?

An array is simply a contiguous allocation of one or more elements of a given type.

int i; // a single element of type int

int j[100]; // an array of 100 elements of type int

The elements of an array are anonymous (we can't refer to them by name because they have no names). However we can access them by their offset address from the start of the array. The array name serves as a reference to the start of the array and will implicitly convert to a pointer:

int* p = j;

Each element of an array is offset by sizeof (type) bytes. However, the offset is known to the compiler so we do not need to specify it when accessing array elements, we only need to know the zero-based index of the element we wish to access:

int k = j[42];

Note that the suffix operator [] does not perform a range-check. It is up to the programmer to ensure the given index is in range. The array named j has 100 elements so all indices must be in the range 0 through 99. Access beyond the range of an array has undefined behaviour:

int m = j[100]; // undefined behaviour

Given j is a reference, we can also use pointer arithmetic to access elements:

int n = j + 42;

From this we can see that the following holds true:

j[42] 42[j]

It often surprises people to learn that j[42] and 42[j] are equivalent, however it's only because the suffix operator is sugar-coating for the underlying pointer arithmetic so the compiler sees 42 + j, not 42[j]. However, low-level trickery such as this has no place in production code.

The elements of an array are uninitialised at the point of instantiation. However we can use an initialiser to set initial values:

int j[5] = {5, 10, 15, 20, 25};

When we pass arrays to functions, the array implicitly converts to a pointer. This means information is lost in the exchange because the function cannot know how many elements are being referred to by the pointer. Thus we must pass the length of the array as a separate argument:

void f (int* arr, unsigned size) {

for (unsigned idx=0; idx<size; ++idx) {

arr[idx] = 0;

}

}

Given that arrays implicitly convert to pointers, variable-length arrays are treated exactly the same as fixed-length arrays, the only difference is that the programmer is responsible for allocating and releasing the memory:

void g (unsigned size) {

int* p = malloc (size * sizeof (int)); // allocate

f (p, size); // use

free (p); // release

}

Note that variable-length arrays cannot be initialised at the point of instantiation; they must be instantiated then initialised, as shown above.

Fixed-length arrays (where the length is known to the compiler) can be allocated in static memory, on the call stack or on the heap. Global arrays are best allocated statically while small local arrays are best allocated on the stack. Large local arrays should be allocated on the heap. Variable-length arrays must always be allocated on the heap.

How do you write a c program to read two numbers and perform addition and subtraction of two matrices?

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int a,b,c,n;

printf("enter the numbers");

scanf("%D",&n);

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

a=b;

a=c;

c=a+b;

printf("the sum is %c",);

getch();

}

Why are data structures important?

Data structures are important because they allow us to aggregate different data types and treat them as a single entity. For instance, if we have separate variables such as first name, last name and age to represent a person, embedding them into a data structure means we can handle multiple people much more easily:

typedef struct person {

char* first_name;

char* last_name;

unsigned age;

};

struct person[100] people; // An array of 100 person objects.

Difference between dos and c programming?

Answer: In dos you have to create the complete application, yourself in other words the complete .EXE file, that can be done in any of the many different programming languishes IE. GWBasic, QBasic, C, or ASM etc., the last is the most difficult but the result is the fastest application. Where Windows is a platform for your application, most of the things you want to do is already created in Windows, all you have to do, is to use it from within your application, not that it means less programming, you actually have to do more coding than with Dos, for instance in Dos GWBasic, if you want to read data from a comm. port you just open it as follow

[code]

open "comm1:" for input as #1

input #1, var

close #1

[/code]

But in Windows it is a long story

What is a tree data structure as used in programming?

DATA STRUCTURES AS ITS NAME IMPLIES DATA MEANS "VALUE' AND STRUCTURE MEANS THE WAY IT IS ORGANISED AND THE WAY IT IS ARRANGED INTO MATHEMATICAL AND LOGICAL WAY.

Just like array...array helps you to store elements without declaring multiples variable

eg: num[100] can store 100 variables which are integers. Here if you are not using array and want to store data in 100 variables then you must declare 100 unique variables names. This can be done effectively using data structures.

What is a large array?

one of the world’s largest radio observatories

When is quick sort better than selection sort?

Because the quick sort can be used for large lists but selection not.

selection sort is used to find the minimum element ,but quick choose element called pivot and move all smaller nums before it & larger after it.

Difference Between interpreter and compiler in java application?

Due to platform independence, a Java compiler will interpret Java source code into Java Byte Code and pass to the JVM, which will pass machine understandable code through to cpu. (clarification needed).A conventional compiler converts source code directly to machine code.(clarification needed).

Disadvanatges of object-oriented programming languages?

  • OOP is a more complicated and structured paradigm than, say classic procedural programming.
  • More planning and design is required on the programmer's part.
  • OOP is less efficient in terms of a computer's resources than most other paradigms.
  • OOP is a complex idea - no language including Java and C++ implements it perfectly.
  • OOP may be overkill for smaller, simpler programs.

What is the difference between local and global variable of C language?

4m Sai. Gloabal Variable is variable which is declared before main (). int a=10; main() { } here a is a global variable. where ever u r using this variable ... we'll get the value as 10. Local Variable is a variable which is declared inside the main(). int a=10; main() { int a=5; ........ ....... } here a=5 is a local variable and a=10 is called as global variable. then if u want to get a value... printf("%d",a); then result 'll be a=5; B'cos compiler gives main preference to local variables.. if any local declareation is not there then it 'll prefer global variable.

Character stuffing in C?

#include

#include

main()

{

char a[30],fs[50]="",t[3],sd,ed,x[3],s[3],d[3],y[3];

int i,j,p=0,q=0;

clrscr();

printf("Enter characters to be stuffed : ");

scanf("%s",a);

printf("\nEnter a character that represents starting delimiter : ");

scanf(" %c",&sd);

printf("\nEnter a character that represents ending delimiter : ");

scanf(" %c",&ed);

x[0]=s[0]=s[1]=sd;

x[1]=s[2]='\0';

y[0]=d[0]=d[1]=ed;

d[2]=y[1]='\0';

strcat(fs,x);

for(i=0;i

{

t[0]=a[i];

t[1]='\0';

if(t[0]==sd)

strcat(fs,s);

else

if(t[0]==ed)

strcat(fs,d);

else

strcat(fs,t);

}

strcat(fs,y);

printf("\nAfter stuffing : %s",fs);

getch();

}

Why c is preferred over Java for embedded system?

Embedded systems typically run on extremely limited hardware. Even the smallest implementation of Java (Micro Edition) can't compete with a small C implementation both in terms of memory footprint and execution speed.

What is the extraction operators in C plus plus?

This operator (>>) applied to an input stream is known as extraction operator. It performs an input operation on a stream generally involving some sort of interpretation of the data (like translating a sequence of numerical characters to a value of a given numerical type).

Three groups of member functions and one group of global functions overload this "extraction operator" (>>) applied to istream objects:

  • The first group of member functions are arithmetic extractors. These read characters from the input data, and parse them to interpret them as a value of the specific type of its parameter. The resulting value is stored in the variable passed as parameter.
  • The streambuf version copies as many characters as possible to the stream buffer object used as right-hand parameter, either until an error happens or until there are no more characters to copy.
  • Those in the last group of member functions have a pointer to a function as parameter. These are designed to be used with manipulator functions.

    Manipulator functions are functions specifically designed to be easily used with this operator.

  • The global functions overload the operator when the parameter is either a character or a c-string, and, as expected they extract either one character or a sequence of characters from the input stream.

What is the function of scanner in the microscope?

Document Scanners have a glass plate and a cover. There is also a lamp used to illuminate the document. The scanner moves a mirror, reflecting the light across the document. The light is picked up by a simple CCD camera chip and digitizes the data line by line, like a slow scan TV.

----------------------------------------------------------------------------------------------

Scanners have become an important part of the home office over the last few years. Scanner technology is everywhere and used in many ways:

  • Flatbed scanners, also called desktop scanners, are the most versatile and commonly used scanners. In fact, this article will focus on the technology as it relates to flatbed scanners.
  • Sheet-fed scanners are similar to flatbed scanners except the document is moved and the scan head is immobile. A sheet-fed scanner looks a lot like a small portable printer.
  • Handheld scanners use the same basic technology as a flatbed scanner, but rely on the user to move them instead of a motorized belt. This type of scanner typically does not provide good image quality. However, it can be useful for quickly capturing text.
  • Drum scanners are used by the publishing industry to capture incredibly detailed images. They use a technology called a photomultiplier tube (PMT). In PMT, the document to be scanned is mounted on a glass cylinder. At the center of the cylinder is a sensor that splits light bounced from the document into three beams. Each beam is sent through a color filter into a photomultiplier tube where the light is changed into an electrical signal.

The basic principle of a scanner is to analyze an image and process it in some way. Image and text capture (optical character recognition or OCR) allow you to save information to a file on your computer. You can then alter or enhance the image, print it out or use it on your web page.

----------------------------------------------------------------------------------------------

A scanner is used to scan in printed photographs and other documents so they

can be put into documents or web pages. It uses a light source,

typically a cold cathode lamp to illuminate the scanned imaged. The

light is then reflected off the object and into Charged Coupled Device

(CCD). The Charged Coupled Device collects the information, and through

a series of electronic devices converts the analog signal into a series

of digital signals which can then be read and processed by the internal

electronics of the scanner and subsequently a computer.

----------------------------------------------------------------------------------------------

What do you compile?

Compiling is the act of translating human-readable source code to machine-readable byte code.

In Java, the compiler program is javac

Write a program to generate the Fibonacci series using non-recursive method?

In c:

int fibr(int n) { // Find nth Fibonacci number using recursion.

if (n<=2) return 1; // the first two Fibonacci numbers are 1 and 1

return (fibr(n-2)+fibr(n-1));

}

int fibi(int n) { // Find nth Fibonacci number using iteration.

int temp,last=1,f=1;

int i;

for (i=3;i<n;++i) { // the first two Fibonacci numbers are 1 and 1

temp=f;

f+=last;

last=temp;

}

return f;

}

Algorithm for infix to prefix conversion?

Algorithm to Convert Infix to Prefix Form

Suppose A is an arithmetic expression written in infix form. The algorithm finds equivalent prefix expression B.

Step 1. Push ")" onto STACK, and add "(" to end of the A

Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty

Step 3. If an operand is encountered add it to B

Step 4. If a right parenthesis is encountered push it onto STACK

Step 5. If an operator is encountered then:

a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator.

b. Add operator to STACK

Step 6. If left parenthesis is encontered then

a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd)

b. Remove the left parenthesis

Step 7. Exit

Write a program to Print pyramid of numbers using loops in c?

#include <stdio.h>

#include <conio.h>

void main()

{

int height, line, i;

clrscr();

scanf("%d", &height);

for (i = 0; i < height - 1; ++i)

printf(" ");

printf("1\n");

for (line = 1; line < height; ++line)

{

for (i = 0; i < height - line - 1; ++i)

printf(" ");

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

printf("%d", line + 1);

printf(" ");

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

printf("%d", line + 1);

printf("\n");

}

getch();

}

Why are the array and linked list used as stack queue and others?

Arrays are beneficial whenever you need constant-time, random-access to any element within a set of data and the number of elements doesn't change too much (enlarging an array may require the entire array be copied to a larger block of free memory, which is expensive in terms of performance). Linked lists are beneficial when the number of elements is highly variable and you also need constant time access to the head (as per a stack) or both the head and tail (as per a queue). Neither a queue nor a stack requires any traversal whatsoever, so the lack of random access is immaterial. If a set of data is variable and must remain in sorted order (such as a priority queue), then a heap or self-balancing binary tree is the most efficient structure to use.

What are the advantages and disadvantages of arrays in c sharp?

Advantages:

1. Can store "n" number of same data types in different indexes

2. Can be accesses using reference.

Disadvantage:

1. No boundary check, if the array boundary is crossed run time

2. No facility to increase the size of array in Run time

How do you determine the size of an array of int passed to a method?

It really depends on the language. In Java, you can use the .length property.

Can you return multiple values from a function in C?

There are many approaches to this 'problem'. Since a C function is only allowed to return one value as the name of the function you have to provide parameters to the routine if you want multiple values to be returned.

For example, traditionally you return a value thus:

int Today () ;

if (Today() == 2) ....

int Today ()
{
/* Some logic */
return value ;
}


So if you want multiple values, send parameters to the routine that can be changed:

int Today (int * val1, int * val2, char * charValue) ;


Then, call it:

int first ;
int second ;
char third ;

if (Today (&first, &second, &third) == 2)

In this case first, second, and third can be changed inside the Today routine and return multiple values.

int Today (int * val1, int * val2, char * charValue)
{
*val1 = 5 ;
*val2 = 10 ;
*charValue = 'Y' ;
return 2 ;
}

Why there is a need for converting high level language into machine level language............ why cant we use high level language directly in computers?

Machine code is the native language of the machine. The machine does not "understand" any language other than its own native language. As such, all other languages, including low level assembly languages, must be compiled or interpreted in order to produce the required machine code.