Thursday, July 23, 2015

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another explicitly using the cast operator as follows:

(type_name) expression

Consider the following example where the cast operator causes the division of one integer variable by another to be performed as a floating-point operation:


#include
void main()

{   
           int sum = 17, count = 5;   
           double mean;   
           mean = (double) sum / count;   
           printf("Value of mean : %f\n", mean );
}

When the above code is compiled and executed, it produces the following result:

Value of mean : 3.400000

It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value.
Type conversions can be implicit which is performed by the compiler automatically, or it can be specified explicitly through the use of the cast operator. It is considered good programming practice to use the cast operator whenever type conversions are necessary.

Tuesday, July 21, 2015

C Question Answer

Q1. Difference Between C and ansi C?

Ans.  After the development of C, it got really popular and practically became the default programming language. However, it was not working the same on various different platforms. C had been designed for the UNIX environment; hence, when run in many other platforms, some codes were returning errors. There was a need for a set of standards, which would allow the C programming language to work similarly on all platforms. Hence, the ANSI C was born.

ANSI C is a set of successive standards which were published by the American National Standards Institute (ANSI) for the C programming language. The ANSI specifies the syntax and semantics of programs written in C. It also specifies the C program's interactions with the platform via input and output data. Additionally, it also specifies restrictions and limits imposed upon conforming implementations of C language translators.


Q2. Difference between binary code and machine code?

Ans. Binary IS machine code. When you write a program in C, it's human readable, but not executable. A compiler will compile (translate) it to machine code, which is indeed executable. This machine code is sometimes called a "binary" as it is not human readable, but a stream of instructions for the processor in binary form.

Q3. What ansi stand for & who owes C?

Ans. American National Standards Institute (ANSI)


Q4. Difference between object file and executable file?

Ans. C is a compiled language so you need to translate the source code in a file that the computer can execute. This file is generated by the compiler and is called the object code ( .obj ), but a program like the "hello world" program is composed by a part that we wrote and a part of the C library. The linker links these two parts of a program and produces an executable file ( .exe ).