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

Why should you be careful not to place a statement in the body of a for loop that changes the value of the loop's counter variable?

By changing the loop counter variable within the loop can cause the loop to exit early or to loop more times than the limit.

The is nothing that says that you can not change the loop counter variable within the loop, you may indeed want to do that. Just be careful and be sure of what will happen.

What is the difference between macro and inline function in c plus plus?

Macros are not actually part of the C++ language; they are nothing more than a simple text-replacement system intended to simplify your coding. Macros do not adhere to C++ type safety and cannot be debugged because macros are preprocessed, prior to compilation. Your compiler can only see the preprocessed code, not the original source code, and therefore cannot debug macros because the macros no longer exist at that point.

Inline functions are functions that can be debugged like any other function, but the compiler is able to eliminate the overhead of function calla by replacing those calls with inline expanded code. This is not unlike a macro, which is by definition inline expanded, but retains the built-in type safety and debugging capabilities of the C++ language itself.

Typically, if you can use an inline function (or C++ is general) then that is always the preferred option. But if a macro can achieve more than can be achieved with C++ alone, or can otherwise simplify the equivalent C++ code, then use a macro. Just keep in mind that macros are not type-safe and cannot be debugged by the C++ compiler.

Why timers are used?

If you need to get something done at certain time or periodically.

What are multiple constructors in c?

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.

When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.

A constructor is declared without a return value, that also excludes void.

Therefore, when implemented, do not return a value:

Constructor Exampleclass rectangle { // A simple class int height; int width; public: rectangle(void); // with a constuctor, ~rectangle(void); // and a destructor }; rectangle::rectangle(void) // constuctor { height = 6; width = 6; }

sagar sainath samant. sybsc (computer science)

Why C program is called as 'C'?

The language was called the "C" language because it was a kind of successor of the "B" language.

What is the summary of c plus plus and sql in a programming language?

Your question is unclear. C++ IS a programming language. SQL is not (it is a specialized language for defining relational databases, inserting/retrieving/modifying data and specifying user access.)

Hexa to octal?

The best way is to first convert hexadecimal to binary and then to octal.

For example the hexadcimal number B4EA is in binary:

B 4 E A

1011 0100 1110 1010

Thus B4EA (hexadecimal) = 1011010011101010 (binary)

We add leading zeros to the binary number in order to be able to divide the number in sets of 3 digits. Then we convert easy to octal as follows:

001 011 010 011 101 010

1 3 2 3 5 2

Therefore 1011010011101010 (binary) = 132352 (octal)

and from here you have your result:

B4EA (hexadecimal) = 132352 (octal)

What converts and executes one statement at a time in computers?

There are number of compilers that are used . A compiler that executes and translates on statement at time is called interpreter. An interpreter is a compiler that executes one line at a time and if an error occurs it need to be corrected at the same point of time else the further execution will stop.

Why can you not declare void variables in c?

All names must be declared in C so that the compiler knows what each name represents even if the definition of that name has not yet been compiled. Without a declaration, the compiler cannot know what operations are permitted upon the name and therefore cannot notify the programmer of syntax errors.

What is the size of void data type?

Data-type void has some special features:

- it doesn't have values

- it doesn't have size

- you cannot declare variables with it

- void *pointers cannot be dereferenced

How a program in c that will add 2 numbers without using any plus operator?

int main()

{

int a=10,b=20;

while(a--)

b++;

printf("%d",b);

}

or: a -= -b;

How do you use fseek function in c?

Answer

First a few points.

I do not know if you intended it but you are asking this of a C++ expert not a C expert. I mention this as fseek forms part of the standard C library file operations. Of course the C++ standard library inherits these functions, but they are not directly compatible with C++ IOStreams. You therefore have to have opened the file using the C fopen function, and close it using the C fclose function.

Next, you have to be careful when using fseek with text files. Things like end of line character sequences and the size of characters used can make a difference to the position values you need to pass to fseek via its offset parameter. The fseek function positions to the requested byte position within the referenced file, it knows and cares nothing about characters or their encoding making up the data of the file.

You also have all that silliness to do with character 26 (ctrl-Z) being interpreted as end of file in text mode - a Microsoft hangover from the days of MS-DOS. Then there is the effect caused by skipping over the byte order mark (if there is one), which as far as I know has to do with text files of characters in various Unicode encodings (so hopefully can be ignored in simple cases). See http://unicode.org/unicode/faq/utf_bom.html#BOM for more information on byte order marks.

You ask about positioning to the letter positions. Not all characters are letters and not all characters are printable. As I said fseek knows and cares nothing about the data or meaning of the values in a file and therefore cannot tell a letter from any other character type.

So assuming you meant characters when you mention letters in your question and assuming characters are all the same size and are byte sized then to position to the 45th character when starting with the file pointer positioned at the 20th character you can do the following:

status = fseek( file, 44L, SEEK_SET );

The fseek call shown above assumes file is a FILE * pointing to a valid open FILE structure. 44L specifies the position as a long integer constant, SEEK_SET is a value indicating that 44L is used to specify the position counting from the beginning of the file. As the beginning of the file is at position 0 and not position 1 we have to subtract 1 from the byte number we want if we count the first character as character 1.

The status variable is an int and is used to hold the returned value which is 0 on success and some non-zero value if fseek failed. If fseek fails you should check errno for more details or call a function such as perror as in the MSDN example, which converts the errno value to a string, prefixes the passed text and displays it on stderr (as associated with std::cerr in C++).

Alternatively you could specify the position to move to relative to the current position which would be the value given by the difference between the new position from the current position, thus 45-20 = 25:

status = fseek( file, 25L, SEEK_CUR );

Here the way the position value is interpreted is specified as SEEK_CUR, meaning to count from the current file position. This assumes of course that the file pointer position is in fact at position 19 (i.e. the 20th byte counting from 0) to start with. If it were at (say) position 112 then it would end up at position 137.

The reverse would be either:

status = fseek( file, 19L, SEEK_SET );

That is move to position 20 as counted from 0, the beginning of the file.

Or:

status = fseek( file, -25L, SEEK_CUR );

Which means move to the current position less 25, which assuming the file pointer is positioned at position 44 (i.e. byte 45 counted from 0) to start with would position it to position (44-25) = 19 (i.e. byte 20 counted from 0).

If you look carefully at the signature for fseek then you would notice that the offset parameter is specified as a long value - that is it is a signed value and so can be negative.

I should note that we could also position relative to the end of the file by specifying SEEK_END as the third parameter value. However this would mean we would need to know how long the file was in bytes so we can calculate the distance from the end of the file to the positions we require and so seems to be more effort than it is worth in this case.

Hope this helps. If you require further information then please ask another question.

Write a program to find a square of first ten integers?

#include<iostream.h>

#include<conio.h>

void main()

{

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

{

cout<<i*i;

}

getch();

}

How do you pass an array as a parameter?

Answer
Unfortunately your question is to broad. All progamming languages vary in the way things are done. I will just give a general way of doing it.
You have to pass the multidimensional array into the function by including it with calling the function. On the receiving end you have to declare another multidimensional array so the information can be passed into it. Depending on the language, you may not be passing in a multidimensional array, instead that array may be stored in an object which you can pass instead.
Hope this helps some.



-Ashat-
in C, when passing two dimensional arrays the compiler needs to know the width so it can calculate memory offsets.

passing a 2d array of width 4:

void
Func(type array[][4]);

What is the difference between cc plus plus and java?

C is a procedure oriented language ,Where C++ & java are object oriented language.
But java is platform independent.
So generally C is called POP.
C++ is called OOP.
But java is OOP , which is platform independent.
If java does not support primitive data type then it is called as pure object oriented language.

What do you call the process of subdividing a problem into smaller sub problems?

what do we call the process of subdividing a problem into smaller sub-program.c programing

What is the difference between strcpy and strncpy?

strcpy - copy a string to a location YOU created (you create the location, make sure that the source string will have enough room there and afterwards use strcpy to copy)

strdup - copy a string to a location that will be created by the function. The function will allocate space, make sure that your string will fit there and copy the string. Will return a pointer to the created area.

To solve sin series in c language?

# include <conio.h>

# include <stdio.h>

unsigned int factorial(int);

void main()

{

unsigned int f;

int a,i,g=0,c=1,j=-1;

clrscr();

scanf("%d",&a);

printf("sine series up to %d terms",a);

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

{

f=factorial(c);

j=j*(-1);

printf(" [(%d(x^%d))/(%d)] ",j,g,f);

if(i!=a)

printf("+");

c=c+2;

g=g+2;

}

getch();

}

unsigned int factorial(int x)

{

int fact=1,i;

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

fact=fact*i;

return fact;

}

How do you write algorithm of Program in C to print hello 10 times?

#include<stdio.h>

int main()

{

int i;

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

{

printf("Hello Sok Sabay\n");

}

}

How many copies of the Common Language Runtime CLR can be executing on a machine?

As many as required by the programs that require a specific version, up the maximum of 4 different version. DotNet v1.0 programs require CLR v1.0. DotNet v1.1 requires CLR v1.1. DotNet 2.0, 3.0 and 3.5 require CLR v2.0 and DotNet v4.0 and 4.5 require CLR v4.0.

How do you save a C program?

One does not normally save programs. Programs are usually binary files containing a list of executable instructions and internal data. The C++ programs are a suite of executables that allow the creation and management of source code files to create new executables. We do not need to save these programs -- they were stored on disk when the suite was first installed.

How do you subtraction two numbers using bitwise operator?

#include<stdio.h>

#include<conio.h>

void main()

{

int x,y;

clrscr();

printf("\n enter the elements\n");

scanf("%d%d",&x,&y);

printf("\n before swaping x=%d,y=%d",x,y);

x=x^y;

y=y^x;

x=x^y;

printf("\n after swaping x=%d y=%d",x,y);

getch();

}

What are the advantages and disadvantages of a compiler?

The advantages are that the compiler can see each translation unit as a whole and can therefore optimise the resultant machine code accordingly. For instance, trivial functions that are accessed often, such as array suffix operators, can benefit from inline expansion, thus eliminating the need for an otherwise costly function call.

Also, constant expressions can be evaluated at compile time using compile-time evaluation. For instance, consider the following code:

constexpr int fac (constexpr num) {

return num==1?1:fac (num-1);

}

void f() {

int x {fac (7)};

// ...

}

A good compiler will replace the recursive function call fac(7) with the constant value 5040.

In addition, compilers (along with the corresponding linker) help eliminate many common errors such as static type errors, syntax errors and linkage errors. They can also test programmer's assumptions through static assertions. The more errors detected and eliminated at compile time, the fewer errors there will be at runtime, which will primarily consist of logic errors.

The disadvantage of compilers is that compilation can take a long time, particularly with large projects. Each time the program is modified, it must be recompiled. compilation times can be reduced by precompiling headers that are not expected to change very often, creating intermediate files that are much faster to compile. There is also no need to recompile translation units that are unaffected by changes in code, most of which only occur due to changes with a common header. Nevertheless, a non-trivial application can still take several minutes to produce a working executable and the more time spent compiling the less time you can spend debugging the runtime logic.

Interpreted languages are executed in real-time without the need for compilation. However, execution times are very much slower due to the need to constantly translate high-level source code into low-level machine code.

There are, however, languages that are both compiled and interpreted. Java is a typical example. The source code is first compiled to an intermediate code known as Java byte code which can then be interpreted by the Java virtual machine to produce the machine code. Unlike traditional compilers which produce native machine code that only runs one the architecture it was intended for, Java byte code is completely portable and can be executed upon any machine with a suitable Java virtual machine implementation, which is pretty much everything these days. However, interpreted languages (including Java) are not suitable for low-level programming, they are only suitable for applications programming. Compiled languages such as C and C++ are suitable for all types of programming, hence they are termed general purpose languages.