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

How do you implement a will?

You really need legal advice to do this so that it meets laws in your state or country. Basically, state your name, place of residence, the date, and declare that this is your last will and testament. Then state what property you want to leave to who, and sign it in front of witnesses (people that are NOT in the will) who will date and sign it as witnesses. Usually 3 witnesses.

Write a program in c to find even and odd nos?

#include<iostream>

using namespace std;

void main() {

int odd,even,number;

odd=0;

even=0;

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

cout<<"enter a number\n";

cin>>number;

if(number%2==0){

even++;

}

else {

odd++;

}

}

cout<<"even number has:"<<even;

cout<<"odd number has:"<<odd;

}

How do you solve undefined references error in c?

Linker errors are many and varied. Without knowing the error it is impossible to say how you solve it.

Most linker errors relate to undefined symbols. For instance, if you declare a function but do not implement it, the function is undefined. This can also happen when you include a function library header but do not link to the function library containing the definitions. The compiler uses the header declarations to ensure all calls to the functions are valid even if the definition hasn't yet been compiled. It is the linker's job to ensure all declarations are defined. The only exception is templates which must be defined before they are used. This is why all template definitions are placed in the header alongside the declarations.

How does a high level language relate to the microprocessor's instruction set?

Generally speaking, it doesn't relate at all. If it did, it wouldn't be high-level, it would be machine-dependent. The relationship between the high-level code and the machine-code is ultimately determined by the language translator (compiler and/or interpreter) but, unlike assembly language which maps 1:1 with the microprocessor instruction set, compilers and interpreters are code generators and there is seldom a 1:1 relationship between the high-level source code and the machine code.

What is the syntax to declare the variable in c?

A variable is declared by declaring its name and type. Once a variable is declared you can use it in expressions, whether to assign a value or use the value. The name is also a reference to the memory address allocated to the variable, thus you can take the address of the name if required.

Need of constructor in c plus plus?

There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor.

A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.

Write a program to read any five numbers and print average value?

#include<stdio.h>

#include<conio.h>

#define p printf

#define s scanf

int main()

{

clrscr();

int a,b,c,d,e;

int ans;

p("\nEnter first no.:");

s("%d",&a);

p("\nEnter second no.:");

s("%d",&b);

p("\nEnter third no.:");

s("%d",&c);

p("\nEnter fourth no.:");

s("%d",&d);

p("\nEnter fifth no.:");

s("%d",&e);

ans=(a+b+c+d+e)/5;

p("\nThe average is: %d",ans);

getch();

return 0;

}

What is the advantage of a binary search tree over an array based structure?

In a binary search tree, insertion, deletion and lookup are O(log n) (i.e. fast) when balanced.
With unsorted arrays, insertion and deletion are O(1) (i.e. very fast) but lookup is O(n) (i.e. slow).
With sorted arrays, insertion and deletion are O(n) (i.e. slow) and lookup is O(log n) (i.e. fast).

Binary search trees are good if you do all three operation (insertion, deletion, lookup) often and have enough data to justify the added burden of more complex structures and algorithms.

Why there is a need for C plus plus and how does it overcome the drawbacks of the C language?

Absolutely! C++ and Java are by far the two most popular programming languages in use today. Java is the most popular due to its ease of use, particularly with cross-platform development, but it compiles to byte code rather than native machine code, thus it is nowhere near as efficient as C++ and is therefore unsuitable for time-critical applications. Even its predecessor, C, is still in use today due to the fact that procedural C can be mapped 1:1 with the underlying machine code making it easier to develop small-scale, cross-platform applications such as driver software. However, since C++ is backward-compatible with C, the ability to mix low-level machine code with intermediate object-oriented code is advantageous when developing more complex applications, including operating systems. Java is simply too high level for this.

Would you write a program for trapezoidal rule in c language?

yes. /* trapezoid.c */ #include #include float f(float); float a; float b; float x; float h; float sum; int n; int i; int main() { printf("Enter value for a: "); scanf("%f", &a); printf("Enter value for b: "); scanf("%f", &b); printf("Enter number of rectangles: "); scanf("%d", &n); h = (b - a) / n; sum = (0.5 * h) * (f(a) + f(b)); printf("%f\n", sum); for (i = 1; i < n; i++) { sum = sum + h * f(a + (i * h)); printf("%f\n", sum); } printf("The value of the integral is: %f\n", sum); } float f(float x) { float value; /* define function here */ value = x*x + 3; return value; }

Which is the easiest sorting method?

There are generally eight sorting algorithms that are studied in school by computer science students. They are as follows: insertion, bubble, quick, quick3, merge, shell, heap, and selection sorting.

There are different types of sorting algorithms. One would be considered good if it is accurate and efficient. Different types of sorting includes; sequential, ascending, and descending.

What always contains a pointer variable?

pointer variable in c contains the address or the location of the other variable.

eg- if a=2 and address of a is 2345. b=&a then b is a pointer which contains 2345 which is the address of a. *b gives value of that is 2.

Write a c program to find the binary addition of two 8 bit numbers?

#include <stdio.h>

#include <iomanip>

int main()

{

int binary1[8] = {0,1,1,1,1,0,1,1}; //8 element array

int binary2[8] = {1,0,1,0,1,1,1,1}; //8 element array

int binarySum[9];

int overflow [9];

int i;

for (i=0; i<9; i++) overflow[i] = 0;

for (i=9; i >= 0; i--)

{

binarySum[i] = binary1[i] + binary2[i] + overflow[i];

if ( binarySum[i] > 1)

{

overflow[i-1] = 1;

binarySum[i] %= 2;

}

}

printf("Binary Sum is: ");

for (i = 0; i<9; i++) printf("%i", binarySum[i]);

printf("\n");

printf("\n");

printf("Carry Bit is: ");

for (i = 8 ; i>=0 ; i--) printf("%i", overflow[i]);

printf("\n");

return (0);

}

Time space trade off in data structure?

IN COMPUTER SCIENCE, A SPACE-TIME OR TIME-MEMORY TRADEOFF IS A SITUATION WHERE THE MEMORY USE CAN BE REDUCED AT THE COST OF SLOWER PROGRAM EXECUTION (OR, VICE VERSA, THE COMPUTATION TIME CAN BE REDUCED AT THE COST OF INCREASED MEMORY USE). AS THE RELATIVE COSTS OF CPU CYCLES, RAM SPACE, AND HARD DRIVE SPACE CHANGE HARD DRIVE SPACE HAS FOR SOME TIME BEEN GETTING CHEAPER AT A MUCH FASTER RATE THAN OTHER COMPONENTS OF COMPUTERS. THE APPROPRIATE CHOICES FOR SPACE-TIME TRADEOFFS HAVE CHANGED RADICALLY. OFTEN, BY EXPLOITING A SPACE-TIME TRADEOFF, A PROGRAM CAN BE MADE TO RUN MUCH FASTER.

THE MOST COMMON SITUATION IS AN ALGORITHM INVOLVING A LOOKUP TABLE: AN IMPLEMENTATION CAN INCLUDE THE ENTIRE TABLE, WHICH REDUCES COMPUTING TIME, BUT INCREASES THE AMOUNT OF MEMORY NEEDED, OR IT CAN COMPUTE TABLE ENTRIES AS NEEDED, INCREASING COMPUTING TIME, BUT REDUCING MEMORY REQUIREMENTS. A SPACE-TIME TRADEOFF CAN BE APPLIED TO THE PROBLEM OF DATA STORAGE. IF DATA IS STORED UNCOMPRESSED, IT TAKES MORE SPACE BUT LESS TIME THAN IF THE DATA WERE STORED COMPRESSED (SINCE COMPRESSING THE DATA REDUCES THE AMOUNT OF SPACE IT TAKES, BUT IT TAKES TIME TO RUN THECOMPRESSION ALGORITHM). DEPENDING ON THE PARTICULAR INSTANCE OF THE PROBLEM, EITHER WAY IS PRACTICAL. ANOTHER EXAMPLE IS DISPLAYING MATHEMATICAL FORMULAE ON PRIMARILY TEXT-BASED WEBSITES, SUCH AS WIKIPEDIA.

Storing only the LaTeX source and rendering it as an image every time the page is requested would be trading time for space - more time used, but less space. Rendering the image when the page is changed and storing the rendered images would be trading space for time - more space used, but less time. Note that there are also rare instances where it is possible to directly work with compressed data, such as in the case of compressed bitmap indices, where it is faster to work with compression than without compression. Larger code size can be traded for higher program speed when applying loop unrolling. This technique makes the code longer for each iteration of a loop, but saves the computation time required for jumping back to the beginning of the loop at the end of each iteration. Algorithms that also make use of space-time tradeoffs include:

BABY-STEP GIANT-STEP ALGORITHM FOR CALCULATING DISCRETE LOGARITHMS.

RAINBOW TABLES IN CRYPTOGRAPHY, WHERE THE ADVERSARY IS TRYING TO DO BETTER THAN THE EXPONENTIAL TIME REQUIRED FOR A BRUTE FORCE ATTACK. RAINBOW TABLES USE PARTIALLY PRECOMPUTED VALUES IN THE HASH SPACE OF A CRYPTOGRAPHIC HASH FUNCTION TO CRACK PASSWORDS IN MINUTES INSTEAD OF WEEKS. DECREASING THE SIZE OF THE RAINBOW TABLE INCREASES THE TIME REQUIRED TO ITERATE OVER THE HASH SPACE.

THE MEET-IN-THE-MIDDLE ATTACK USES A SPACE-TIME TRADEOFF TO FIND THE CRYPTOGRAPHIC KEY IN ONLY 2N + 1 ENCRYPTIONS (AND O(2N) SPACE) VERSUS THE EXPECTED 22N ENCRYPTIONS (BUT ONLY O(1) SPACE) OF THE NAIVE ATTACK.

DYNAMIC PROGRAMMING, WHERE THE TIME COMPLEXITY OF A PROBLEM CAN BE REDUCED SIGNIFICANTLY BY USING MORE MEMORY.

C program to find the year is leap year or not?

Algorithm:

If the year is not divisible by 4 then it is not a leap year.

Else if the year is not divisible by 100 then it is a leap year.

Else if the year is not divisible by 400 then is not a leap year.

Else it is a leap year.

Implementation:

bool is_leap_year (unsigned year) {

if (year%4) return false;

if (year%100) return true;

if (year%400) return false;

return true;

}

Given a linked list of integers sorted in an ascending order and a pointer to a single node containing an integer write a C program that insert the node P in the linked list so that remains sorted?

InsertNode(NODE **q,int num)

{

NODE *r,*temp ;

temp = *q;

r= malloc(sizeof(NODE));

r->data = num;

//if it's fisrt node to be inserted

if ( *q == NULL num < (*q)->data)

{

*q = r ;

(*q)->link=temp;

}

else

{

while(temp)

{

if ( (num > temp->data) && (num < temp->link->data ) )

{

r->link = temp->link;

temp->link = r;

return;

}

temp = temp->link;

}

r->link = NULL;

temp->link = r;

}

}

Which translator translate low level language 1010111 to machines language?

Only assembly languages are generally considered low-level programming languages, so one could argue that there is only one low-level language.

However, there are as many assembly languages as there are processor models or families, as each processor family implements its own set of machine code instructions. Different manufacturers not only invent different mnemonics for similar machine code operations to suit conventions and hardware architecture (e.g. MOVE vs LOAD/SAVE), but also support different notations.

Therefore, conceptually, the assembly language is the only low level programming language. However, the standpoint of view of learning the language, or from that of tools to support the language, there are numerous assembly languages, thus numerous low-level languages.

What Example of structured programming?

C is a structured programming language. PHP, COBOL is also a structured programming language. These languages follow a top down approach.

What is switch case?

switch (expression)

{

case constant-value1:

statements

break [optional];

case constant-value2:

statements

break [optional];

.

.

.

default:

statements;

}

How do you use strlen function in c language?

It is very difficult, because it has as many as oneparameter, so it is very easy to get confused...

try this:

const char *s= "example";

printf ("length of '%s' is %d\n", s, (int)strlen (s));

strlen just tells you how many charters there are before the the first zero value. If you are going to create a buffer based on strlen then you must add 1 to the value that strlen returnes. The needs to be a place to store the terminating zero.

char *s= "example";

char *buffer;

buffer = (char*)malloc(strlen(s)+1);

Why Two pointers cannot be added in c language?

Because no-one knows what the sum of two pointers should be...
of course you can convert them to integers and then sum them, but why on earth would you do that?

What is single line comments as used in C programming?

single line comment are comment written in single line.in c there are two types of comment single line and multiple line.single line comment is written using // and multiple line comment is written between /*comment*/.compiler does not compile comments.it is used for better understanding of program.

What does it mean when a variable is static?

A static variable is a variable that is allocated at compile time and that remains in memory for the entire duration the program remains in memory. Contrary to the misleading answer given below, static variables ARE variables, they are NOT constants; the value is initialised at compile time but can be modified at runtime whenever the variable is within scope. Static constants are also possible, but they must be explicitly marked constant -- otherwise they are implicitly variable.

As with ordinary variables, the scope of a static variable is dependant upon where it is declared. This could be at global scope, file scope, function scope, namespace scope or class scope. However, the scope only determines the visibility of a static variable. The variable exists at all times even when not in scope so its value can persist across scopes, but the value can only be modified when the variable is currently in scope.

Global scope means the static variable has external linkage and is accessible to all code with access to the definition. File scope limits visibility to the translation unit that defines it. Function scope limits visibility to the function that defines it. Namespace scope limits visibility to the namespace that defines it, but you can still gain external access via the scope resolution operator (::).

Class scope is the most complex scope because as well as scope resolution, the variable is also subject to the class access specifier (public, protected and private) associated with it. However, another key difference is that static class member variables are scoped to the class itself, not to any instance of the class. Thus they are visible (if not accessible) even when no instances of the class exist.

***************PREVIOUS ANSWER*********************

A common misconception about and misuse of the static qualifier is:

A static variable is program variable that does not vary... go figure.

Static variables have the same value throughout run time. They can be changed at design time only.

This is actually a better description of the const modifier.

'static' is used in C programs to declare a variable that should exist throughout the lifetime of the program. When the program starts executing, all allocations are made for static variables. It has the added side-effect of making the variable not visible outside the scope of the module in which is declared. The explanation in the answer below this one describes this well.

For more information specific to java see: http://mindprod.com/jgloss/static.html Answerstatic is an access qualifier that limits the scope but causes the variable to exist for the lifetime of the program. This means a static variable is one that is not seen outside the function in which it is declared but which remains until the program terminates. It also means that the value of the variable persists between successive calls to a function. The value of such a variable will remain and may be seen even after calls to a function. One more thing is that a declaration statement of such a variable inside a function will be executed only once.

For usage try following: void fun() { static int fnvalue=0;//Executed once printf(" \n%d",fnvalue++);// Value changed retains for next call }

this code behaves like a sequence generator.

One more advantage of static variables is that, since they are created once and then exist for the life of the program, the address of the variable can be passed to modules and functions that aren't in the same C file for them to access the variable's contents. This has several advantages in programming.

For further confusions or details write me: rupesh_joshi@sify.com,rupesh.joshi@gmail.com

C++ and Java In Object-oriented languIages like C++ and Java, the static keyword has additional practical value. If you define a class-level variable as static, that variable is accessible whether there is an instance of the class or not. Furthermore, all instances of the class share the same single value. This is used in a number of ways.

Sometimes you'll have a class where you want all the instances to share a single variable (EG a bus class where all buses in the system shared the same totalRiders variable.) Often you'll define constants as static final, because they are frequently used as parameters for the constructor of the class.

Methods can also be static, which means they can be called without an instance of the class. Most often this is used to make a class act like an old-school function library. The Java Math class is a great example of this. You never instantiate the Math class, but all of its methods are static.

A static member is almost always called with the class name rather than the instance name.
"static" in programming and databases means "constant--never changing". So, a static variable could be the "node name" or "database name". Once those are set, they cannot be changed.

How do you distinguish between a text file and a binary file?

You can distinguish between binary and text files, and for the most part even identify what type of binary file, by using the "file" command. For example:

~$ file unknownfile

unknownfile: PNG image data, 155 x 155, 8-bit/color RGBA, non-interlaced

This tells you that the file is a PNG file by reading metadata and/or magic numbers in the file. When used on a text file, the command will return "ASCII text" or "Unicode text."