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 Objectives Oriented Evaluation Approach to program evaluation?

Objectives Oriented Evaluation Approach is the means by which the worth or merit of a program is assessed based on the extent to which the objectives or purposes of the program are being achieved.

When to use Inheritance in C?

Assume the question was for C#, not C.

":" is syntax to extend a type. If the type extended from is another class, they form a class hierarchy and the "inheritance" is established:

For example:

class Base {}
class Derived : Base {}

Derived extends Base, and thus inherits from Base.


How do you empty a file using C programming?

That depends on the type of file and what you mean by "empty". Generally, fopen() called on an existing file, with the "w" option instead of the "a" option will truncate the file to zero length. To delete a file completely is a different process. Function truncate is your friend.

Can a structure be assigned to another using assignment operator?

Yes, provided both structures are of the same type:

struct S {/*...*/};

S a, b;

// ...

a = b;

The built-in structure assignment operator is typically implemented as a memory copy (memcpy) operation as this is the most efficient means of performing member-wise copy, as opposed to performing individual assignment operations upon each member.

Be aware that pointer members are shallow-copied not deep-copied:

struct S {

void* ptr;

size_t size;

};

S a;

a.size = 100 * sizeof(int); // allow for storage of 100 integers

a.ptr = malloc (a.size); // allocate memory

At this point, object 'a' owns the memory allocated to a.ptr.

S b;

b = a;

At this point, ownership of the memory is ambiguous because both a.ptr and b.ptr are referring to the same memory address. Shared memory is best avoided because releasing that memory will invalidate all objects that share the same memory:

free (a.ptr);

free (b.ptr); // Undefined behaviour: releasing the same memory twice!

To avoid this, we must manually perform a deep-copy:

S b;

b.size = a.size; // copy the size member

b.ptr = malloc (b.size); // allocate new memory

memcpy (b.ptr, a.ptr, a.size); // deep-copy

Note that the onus is entirely upon the programmer to ensure resource ownership is unambiguous. The compiler cannot help you.

In object oriented languages like C++, we use "smart" pointers (resource handles) rather than "naked" pointers. In this way, the onus of resource ownership shifts to the language itself.

Why should a function that accepts an array as an argument and processes that array also accept an argument specifying the array?

Basically in c++ passing an array as an argument only provides a pointer to the first value and that function won't know how many values it has.

If you read beyond the size you will just get garbage from memory.

What is the Advantages of selection sort?

+ reasonable fast in worst and average cases, n lg n + O(n)

+ in place

- best case still n lg n

How can the value of an expression be converted to a different data type in c?

There are different ways, there is one straight forward though. For instance you have a variable of type char: var1. And you want to convert it to another variable of type int: var2. You can use casting for it:

var2 = (int) var1;

You have to be very careful using casting because it cause buffer overflow. Newer versions of C have many different function to assure correctness of parsing, for instance, TryToParse...

Can a function be called from more than one place in a program in c plus plus?

In C and C++, as well as in many (all?) languages, a function can be called from more than one place in a program. That's the purpose of functions - to encapsulate pieces of code that are needed in more than one place in the program.

What is complete binary tree in c?

The basic idea (you write functions CountChildren, GetChild, NewBinTree):

struct GenTree;

struct BinTree;

typedef struct GenTree GenTree;

typedef struct BinTree BinTree;

int CountChildren (const GenTree *from);

int GetChild (const GenTree *from, int which);

BinTree *NewBinTree (DATATYPE data, BinTree *left, BinTree *right);

BinTree *Convert (const GenTree *from)

{

BinTree *bt, *tmp;

int i, n;

if (from==NULL) return NULL;

n = CountChildren (from);

bt = NewBinTree (from->data, NULL, NULL);

if (n 1) return bt;

tmp = Convert (GetChild (from, n));

for (i=n-1; i<=2; --i) {

tmp= NewBinTree (<NODATA>, Convert (GetChild (from, i)), tmp);

}

bt->right = tmp;

return bt;

}

Explain the scope and visibility of variables in c plus plus function?

If you define a variable inside of your function, the variable can be referred and used only inside of that function. It means that you will not able to use the variable in another function (including main). Area of code where your variable can be used after declaration is usually called visibility of the variable.

What is the difference between data record and data base?

A data record is a collection of various fields of data (like name, organisation, sex, age etc), whereas a database is a collection of records tables. (And views, indexes, stored procedures, and other stuff;)

Describe the four basic data types in c?

Describe the basic data types in C

Describe the basic data types in C

How is C program portable?

Your program is portable if you can compile and execute it on different platforms.

Which tools can be used to create prototypes?

Prototypes are normally built in a similar way to the final product however parts may differ slightly as they aren't always produced on a production line they are one off's made by hand or 'compatible' parts may be used instead to lower the cost of prototyping as it is a very expensive process.

Rapid prototyping uses 3D printing technologies to make prototype parts, this process also allows for no assembly of parts as they can be printed in place thus further speeding up the prototyping process. In some cases the material used isn't final but the process takes only hours compared to traditional weeks-months and can be used to test the fitting, visual appeal and dimensional properties of the product wanting to be prototypes.

In some cases computer modelling is used to make a prototype as you can test a design without ever having to build it physically. This has its draw backs of requiring large amounts of computing power and the models may require refinement to accurately reflect real scenarios.

What kinds of programming languages may be developed and used in the future?

It is always very hard to predict the future, especially in an area which has seen very rapid development over the past two decades.

In software engineering of applications interfacing with human users, powerful new declarative approaches are the modern choice, such as Microsoft's Windows Presentation Framework (WPF) and its XAML language, or Adobe's Flex (with MXML), and others.

It is probably fair to assume that the declarative part of application design will grow in the near and mid-term future, taking even more responsibility for the mundane tasks related to graphical user interface development off the programmer. This allows to focus more implementing the application's core algorithm and logic.

It's probably also fair to assume that the dividing line between desktop applications, web applications and mobile applications continue to diminish both in term of design as well as in terms of usability: applications will increasingly work within a web browser, and it will be common to have a (possibly feature reduced) mobile app on a smart phone or tablet computer, or a desktop app all resulting from the same development effort.

It is fair to assume that programming languages will continue supporting this goal in even more comprehensive hardware abstractions and increased automation of the housekeeping work.

What are the disadvantages of bubble sort?

The advantage of sorting is that it is quicker and easier to find things when those things are organised in some way. The disadvantage is that it takes time to sort those things. In computing terms, the cost of searching a few unsorted items is minimal, but when searching a large number of items (millions or perhaps billions of items), every search will have an unacceptable cost which can only be minimised by initially sorting those items. The cost of that sorting process is far outweighed by the speed with which we can subsequently locate items. Inserting new elements into a sorted set also incurs a cost, but no more than the cost of searching for an element, the only difference is we search for the insertion point rather than a specific element.

What is used to translate source code instructions into appropriate machine language instructions?

Languages are either "Compiled languages" or "interpreted languages":

- A compiled language will use a compiler which is another program that checks your code and then converts it to the correct machine code for the machine it is intended to run on. You can only run the program after you have compiled it. A compiler can help spot syntax errors and certain semantic errors and will give you a "compilation error".

- An Interpreted language can be ran directly as long as you have another program called the interpreter which translates your code into machine code whilst it is running. This means certain errors will not be caught before runtime (There is no concept of a compilation error) and so you won't know until runtime if certain errors are present in your code

How string constants are declared in c?

Some strings are constants, others aren't; some constants are strings, other aren't. So these are unrelated things. Examples:

"text" -- constant string

123 -- constant number

char s[40] -- variable string

Which type is generally faster and holds data and instructions while the data is being processed Which type of storage is generally slower but more permanent?

In a computer, there are many forms of media storage. We can catagorize them into 3 different categories - ROM (such as BIOS), RAM (memory), and Storage (flash drives, hard disk drives, etc.). Keep in mind that RAM retains information only when the power is on.

ROM is usually stored in the BIOS chip. It basically gives your computer the most basic instructions to tell your computer to do a system check and boot your operating system.

RAM is used by the computer to do just about everything. When your computer executes a program, for example, it loads the program into the RAM, interprets it, and runs it. It is generally faster than storage devices, but it cannot retain information once power is cut from it.

Storage medium, as the name implies, for storing data. All data, programs, and files must be present on it for it to do anything useful.

Where are data structures used?

It would probably be easier to ask where they aren't used. Even a one-dimensional array is a data structure, thus all strings are data structures. There are relatively few programs that don't require strings and even fewer that don't require arrays. But even they will use one or more data structures because a program without data of any kind would be completely useless.

Sum of geometric series using c program?

there are different ways of writing dis program... 1+x+(x*x)+(x*x*x*)+....has a formula for its sum... the sum for a geometric series with a as initial value and x as common ratio is (a*(pow(r,n)-1))/(r-1).... where a=1;r=x.. accept the values of x and n through keyboard remember to take x as a float value!! apply the formula and be careful about the parantheses. happy programming!!!

Types of programming language that is machine independent?

FORTRAN (FORmula TRANslator) is the best-known earliest example of machine independent language. This is where the language is not dependent on the characteristics of the computer.

COBAL (COmmon Business-Orientated Language) is the other type of programming language that is machine independent. COBAL was developed by the US Navy for business applications.