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 dangling pointer reference?

Whenever memory that was in use, and was referred to by a pointer variable, is freed, and the pointer variable is not updated accordingly (setting it to NULL, for example), the pointer variable is considerred to be a dangling pointer reference.

Which data structure in a compiler is responsible for managing information about variables and their attributes?

It is up to the designer of the compiler to decide...

it can be something like this:

struct Type;

struct Block;

typedef struct Variable {

const char *name;

struct Type *type;

struct Block *block; /* refers to the block that is the scope of the variable */

int attributes; /* volatile, const, static etc */

} Variable;

In a header file whether functions are declared or defined?

Ideally, functions should only be declared in a header and defined in a translation unit (source file) that includes the header. However, trivial functions are often defined in a header as they are usually good candidates for inline expansion, but you must remember to declare the function inline. Often it is better to forward declare inline functions so that maintainers are not distracted by the implementation details which can be placed towards the end of the header, out of the way. However, a definition is also a declaration, so forward declaring an inline function is not a requirement unless there is a cyclic dependency issue where a forward declaration is necessary to break the cycle.

In c plus plus Write a program that takes a decimal number input from the user and displays its binary number?

The following code will convert any number in any base to any other base, from binary to hexadecimal, and everything inbetween.

#include<iostream>

#include<string>

#include<sstream>

typedef unsigned long long ull;

typedef unsigned long ul;

const std::string symbols="0123456789abcdef";

std::string inputvalue(ul base)

{

using namespace std;

string value;

while(1)

{

cout<<"Enter a positive value : ";

string s;

getline(cin,s);

if(s.size())

{

for(string::iterator i=s.begin();i!=s.end();++i)

if(*i>='A' && *i<='Z')

*i+=32;

string actual = symbols.substr(0,base);

if(s.find_first_not_of(actual)!=string::npos)

{

cout<<"The value you entered is invalid for the base.\n"

<<"Please enter another value.\n"<<endl;

continue;

}

value=s;

break;

}

}

return(value);

}

ul inputbase(std::string prompt)

{

using namespace std;

ul result=0, min=2, max=16;

while(1)

{

cout<<prompt.c_str()<<" ["<<min<<".."<<max<<"] : ";

string s;

getline(cin,s);

if(s.size())

result=stoul(s,0,10);

if(result<min result>max)

cout<<"The base must be in the range "

<<min<<".."<<max<<"\n"

<<"Please enter another base.\n"<<endl;

else

break;

}

return(result);

}

ull base2dec(std::string value,ul base)

{

ull col=1, num=0;

for(std::string::reverse_iterator i=value.rbegin(); i!=value.rend(); ++i)

{

num+=symbols.find(*i,0)*col;

col*=base;

}

return(num);

}

std::string dec2base(ull dec,ul base)

{

using namespace std;

int len=1;

ull tmp=dec;

while(tmp/=base)

++len;

string value("0",len);

while(dec)

{

value[--len]=symbols[dec%base];

dec/=base;

}

return(value);

}

int main()

{

using namespace std;

ul base=inputbase("Enter the base of the value");

string value=inputvalue(base);

ul newbase=inputbase("Enter the base to convert to");

value=dec2base(base2dec(value,base),newbase);

cout<<"New value:\t"<<value.c_str()<<endl;

return(0);

}

Passing two dimensional arrays into functions in C programming?

The correct way in C++ is to not use arrays, but to use vectors instead. You can then pass vector references to your function just as you would pass references to any other object types.

If you choose to use arrays rather than vectors, then you must pass a reference to the first element in each array, along with the number of elements in each array, just as you would have to if you were writing for C rather than C++.

What is short char in c language?

There is no such thing as "short char"

You either mean char or short int.

a char is a variable declaration that holds one character, usually 8 bits long (1 byte)

short int (or simply short) is a 16 bit (2 byte) integer

Self referential structures in c?

a structure calling a same type of structure is refered to as self refrential ...like This is a very simple function that searches through a linked list using the struct above and returning true if the number is in the list, otherwise false. int list_search(int search, linklist *list) { int ans; if(list list->data) ans = 1; else ans = list_search(search, list->next); } return ans; } by cherry ..... cherrykapata@gmail.com

What does the c in c rations mean?

The letter "C" doesn't actually stand for anything.

Fresh food was given an "A" nomenclature (A-ration).

Packaged but unprepared food was given a "B" nomenclauture (B-ration).

C-rations designated prepackaged, prepared foods which were ready for consumption upon opening.

The survival rations were given a "D" or "K" nomenclature.

What is a pointer in C programming language?

A pointer in C is the address of a variable or function.

The advantages of a pointer over using normal variables are:

If you pass the pointer to a variable to a function, then that function can modify the value of the variable directly; for example, suppose you want a function to convert a string to lower case. If pointers are not available, then a function call would look like this (in BASIC):

NewString = ToLower("This Is A String")

What the BASIC compiler does is make a copy of the "This Is A String" string, pass it to the ToLower() function and return a copy back to the caller to be copied to NewString.

All these copies make the process slow. In C you can do this:

StringPtr = "This Is A String"
ToLower(StringPtr)

StringPtr points to the string, ToLower() receives the pointer and performs the conversion on the original string without any copying taking place.

Function pointers allow C to be used in a more object orientated way; C doesn't support objects, but a structure can contain a collection of data and function pointers so the structure can effectively contain data and the functions needed to process it.

The danger of pointers is that they point directly into computer memory and its impossible for the compiler to be certain that you haven't made a pointer point outside of the program memory; the way you find out is when the program crashes when run with an exception error.

There is a also slight format error with the way pointers are described in C; for example consider the following function:

int Divide(int *a, int *b)
{
int r;
r = *a/*b;
return r;
}

Although it looks harmless, the compiler can't tell if the /*b; is introducing a comment or is part of the calculation and most compilers will produce a missing close comment message - although some will silently compile the program - but commenting out everything after the *a!

  1. Read more on C from : http://www.indiabix.com/c-programming/index.php
  2. Read more on C Pointer from :http://www.indiabix.com/c-programming/question-answer.php?topic=nqvqmuprt

What is maximum length of float in c?

Floating point numbers are always stored according to the underlying architecture. The programming language is immaterial, it must use the same representations as the hardware itself, or at least provide an abstraction of it. C does not provide any abstractions for built-in data types.

Most modern computers use the standard IEEE 754 representation, which caters for single-precision (equivalent to float in C), double-precision (double) and extended-precision (long double).

What is compilation process in programming?

In general a compiler will go through a few steps:

# Lexical analysis - making sure all the symbols in the source code are legal, and turning them into "tokens" for the next step.

# Syntactic analysis - analyze the tokens, ensuring they follow the rules of the language grammar and parsing them into some form of syntax tree. # Code generation - uses the syntax tree to create some form of intermediate language; oftentimes into assembly instructions, or a unique assembly-like language. # Code optimization - may or may not perform optimization on the intermediate language before translating it into executable code. Of course the true process of compilation is almost always much more complex than this, and may involve many more steps.

Write a program in c to implement the depth first traversal?

#include <iostream.h>

#include <conio.h>

class graph

{

private:int n;

int **a;

int *reach;

int *pos;

public:graph(int k=10);

void create();

void dfs();

void dfs(int v,int label);

int begin(int v);

int nextvert(int v);

};

void graph::graph(int k)

{

n=k;

a=new int *[n+1];

reach=new int[n+1];

pos=new int [n+1];

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

pos[i]=0;

for(int j=1;j<=n;j++)

a[j]=new int[n+1];

}

void graph::create()

{

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

{

cout<<"Enter the "<<i<<"th row of matrix a:

";

for(int j=1;j<=n;j++)

cin>>a[i][j];

}

for(int k=1;k<=n;k++)

reach[k]=0;

}

void graph::dfs()

{

int label=0;

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

if(!reach[i])

{

label++;

dfs(i,label);

}

cout<<"

The contents of the reach array is:

;

for(int j=1;j<=n;j++)

cout<<reach[j]<<" ";

}

void graph::dfs(int v,int label)

{

cout<<v<<" ";

reach[v]=label;

int u=begin(v);

while(u)

{

if(!reach[u])

dfs(u,label);

u=nextvert(v);

}

}

int graph::begin(int v)

{

if((v<1)&&(v>n))

cout<<"Bad input

";

else

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

if(a[v][i]==1)

{

pos[v]=i;

return i;

}

return 0;

}

int graph::nextvert(int v)

{

if((v<1)&&(v>n))

cout<<"Bad input

";

else

for(int i=pos[v]+1;i<=n;i++)

if(a[v][i]==1)

{

pos[v]=i;

return i;

}

return 0;

}

void main()

{

clrscr();

int x;

cout<<"Enter the no of vertices:

";

cin>>x;

graph g(x);

g.create();

cout<<"dfs is.....";

g.dfs();

getch();

}

How do you compile software on Linux?

Most programs you can download in source form can be compiled using the following simple steps:

1. Extract the source package (ex. 'tar xzvf programname-version.tar.gz'), this will create directory programname-version. Chdir into it.

2. Run './configure'. This checks the build environment to make sure your compiler works and has the proper libraries installed.

3. Run 'make'. This compiles the program.

4. Run 'make install'. This places the binaries in the appropriate location(s).

5. Depends on GCC version also

What is Lvalue in c?

An lvalue is an object that can be assigned a value, i.e. it can be on the left side of an equals sign, hence the term lvalue.

If the compiler is complaining, you are probably trying to assign a new value to an rvalue, such as an array name or constant.

The area inside the hysteresis loop represents?

It represents the energy lost when work is done by the rubber band, as it is lost as heat.

The loop, means loading minus unloading.

It is the amount the unloading curve SHOULD have covered, as in compress back to original, but it did not, hence the unloading curve 'lags' behind in coming back to original dimensions, and the curve represents that.

Since the unloading is not able to come back, that means that energy is not fully or efficiently converted back, hence it represents loss of energy which is in the form of heat.

Hope it helped :)

Natasha.

In hysteresis materials it represents the energy dissipated in them during the cycle of magnetization & demagnetization (just refer any hysteresis loop diagram) .

This is used in many applications especially in aerospace to damp the oscillations in satellite.

Hope it helped.

Ninad

What is hashing function in data structure?

If you read up on hashing, why hashing is done, what are its uses. Then you will be able to answer your own question. More to the point you will have studied the material that your homework question is intended to make you study. It is educational.

What is visibility mode what are the different inheritance visibility modes support by c plus plus?

There is no such thing as visibility mode in C++. Visibility is a function of information hiding but that relates to the way in which implementation details can be obfuscated within binary executables and libraries where only the interface need be exposed in a plain-text header file. This has nothing whatsoever to do with object oriented programming since information hiding is also possible in C.

You probably meant access specifiers. There are three levels: private, protected and public. Private access limits access to the class and to friends of the class. Protected is the same as private but extends access to derivatives of the class. Public access imposes no limits.

In terms of inheritance, the specified access level determines the accessibility of the protected and public members of the base class (private members are never inherited and will always remain private to the base class). in essence, members with access greater than the specified inheritance are reduced to the specified access. Thus if you specify protected inheritance, all public members of the base class become protected members of the derivative, while private inheritance reduces all public and protected members to private access. You may also reduce access to specific base class members simply be redeclaring them with the appropriate access.

What is the Human readable version of a program called?

Source code. Source code can be written in a high-level like C++, or in a low-level language like assembly. Machine code can also be considered source code if that's what was originally used to write the code, but it is not considered human-readable. In order to read machine code in a human-readable form it must be disassembled, but you cannot reproduce the original source code.

What are the function of c plus plus language?

C++ is merely a tool, it has no specific role in mathematics other than to compute complex mathematical algorithms much faster and more accurately than any human could. But that is more specifically the role of the computer itself: C++ merely makes it possible to program the necessary machine code than would otherwise be possible with low-level assembly, using abstract objects that emulate real-world object behaviours.

Find max and min of given number in c language?

#include<stdio.h>

main()

{

int a[50],max,min,n;

printf("enter the noof digits\n");

scanf("%d",&n);

printf("\nenter the numbers");

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

{

scanf("%d",&a[i]);

}

max=a[0];

min=a[0];

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

{

if(max<a[i])

max=a[i];

if(min>a[i])

min=a[i];

}

printf("\n max is %d \n min is %d",max ,min);

}

Is it possible to have a completely graphical programming language?

Almost any computer language can do GUI manipulations if provided by a GUI library. Some can be add-ons because the language itself does not have a direct method of doing GUI drawing, etc.

Languages such as C and C++ for example do not have a native GUI interface because they are not tied directly to a machine architecture or to an operating systems platform. However, that is not to say that they cannot do GUI manipulations; it just isn't built into the language, but there are 3rd party add-ons that do the manipulations for you.

Other languages, such as C# and Java, have built-in gUI libraries that work the same way regardless of the Operating System they are on. In that way they support GUI operators natively, without the use of an add-on GUI library.

Why there is no object concept in c language?

C is procedural programming language and does not have any object orientated paradigm.

But there is C++ programming language that is C Object-Orientated approach and one of the most popular programming language (rapidly going down).

C++ brought some features to C programming languages. And one of them is support for classes with fours main OO (Object-Orientated) features: encapsulation, abstraction, inheritance and polymorphism.

Object is an instance of the class, which is created at run-time.

Class is like a template for Object. It tells what kind of data inside it should have and what kind of operations are possible with it (abstraction).

Here is example of the Class:

class Point {

public:

Point();

Point(int x, int y);

~Point();

void setPoint(int x, int y);

int getX();

int getY();

private:

int x;

int y;

};

Point::Point() : x(0), y(0) {

}

Point::Point(int x, int y) {

this->setPoint(x, y);

}

Point::~Point() { }

void Point::setPoint(int x, int y) {

this->x = x;

this->y = y;

}

int Point::getX() {

return this->x;

}

int Point::getY() {

return this->y;

}

Here is example of small program that creates two objects and manipulates them:

#include

using namespace std;

int main() {

Point *a = new Point(1, 2); // Object a

Point *b = new Point(3, 4); // Object b

cout << "a.X = " << a->getX() << "; a.Y = " << a->getY() << endl;

cout << "b.X = " << b->getX() << "; b.Y = " << b->getY() << endl;

delete a;

delete b;

return 0;

}

What are the advantages Linked Lists in C?

Array Pros:

* can access any element of an array directly * can be used to create other useful data structures (queues, stacks) * light on memory usage compared to other structures Array Cons:

* rigid structure * can be hard to add/remove elements

* cannot be dynamically resized in most languages Linked List Pros:

* easy to add/remove/change elements * flexible (can contain user defined data types and other fun stuff) * also lighter on memory usage than many other structures Linked List Cons:

* cannot be easily sorted * must traverse 1/2 the list on average to access any element * more complex to create than an array

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.