c faq

7
1. What is a compiler? Ans: A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program. The name "compiler" is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language or machine code). 2. What is cross-compiler? Ans: If the compiled program can run on a computer whose CPU or operating system is different from the one on which the compiler runs, the compiler is known as a cross-compiler. 3. What is compilation? Ans: In computer programming, the translation of source code into object code by a compiler. 4. Differences between native compiler and cross compiler. Ans: A native or hosted compiler is one which output is intended to directly run on the same type of computer and operating system that the compiler itself runs on. The output of a cross compiler is designed to run on a different platform. Cross compilers are often used when developing software for embedded systems that are not intended to support a software development environment. 5.What is an interpreter? Ans: In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language. An interpreter may be a program that either executes the source code directly translates source code into some efficient intermediate representation (code) and immediately executes this explicitly executes stored precompiled code[1] made by a compiler which is part of the interpreter system

Upload: rajesh-chowdary

Post on 21-Jul-2016

1 views

Category:

Documents


0 download

DESCRIPTION

c questions

TRANSCRIPT

Page 1: c faq

1. What is a compiler?

Ans: A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program.

The name "compiler" is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language or machine code).

2. What is cross-compiler?

Ans: If the compiled program can run on a computer whose CPU or operating system is different from the one on which the compiler runs, the compiler is known as a cross-compiler.

3. What is compilation?

Ans: In computer programming, the translation of source code into object code by a compiler.

4. Differences between native compiler and cross compiler.

Ans: A native or hosted compiler is one which output is intended to directly run on the same type of computer and operating system that the compiler itself runs on. The output of a cross compiler is designed to run on a different platform. Cross compilers are often used when developing software for embedded systems that are not intended to support a software development environment.

5.What is an interpreter?

Ans: In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language. An interpreter may be a program that either

executes the source code directly

translates source code into some efficient intermediate representation (code) and immediately executes this

explicitly executes stored precompiled code[1] made by a compiler which is part of the interpreter system

6. Differences between compiler and interpreter.

Ans: Programmers usually write programs in high level code, which the CPU cannot execute; so this source code has to be converted into machine code. This conversion is done by a compiler or an interpreter. A compiler makes the conversion just once, while an interpreter typically converts it every time a program is executed (or in some languages like early versions of BASIC, every time a single instruction is executed).

7. What is a translator?

Page 2: c faq

Ans: A Translator is a computer program that translates one programming language instruction(s) into another programming language instruction(s) without the loss of original meaning.

8. What is an assembler?

Ans: Typically a modern assembler creates object code by translating assembly instruction mnemonics into opcodes, and by resolving symbolic names for memory locations and other entities.

9.What is a linker?

Ans: In computer science, a linker or link editor is a program that takes one or more objects generated by a compiler and combines them into a single executable program.

10. What is a preprocessor?

Ans: In computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers. Preprocessors are capable of performing relatively simple textual substitutions and macro expansions.

11. Who calls main() in c program?

Ans: Main function is called from C startup assembly which is called by operating system during creation of a new task or process.

A task or process starts from the entry point of the executable binary. Programs written in C/C++ languages are linked with a startup assembly routine and the entry point is startup or start. This startup routine has some responsibilities before it can go to main function. Startup assembly does the following steps before it jumps to main function.

Setup stack segment,

Setup data segment,

Setup BSS segment and clean BSS,

setup task related to platform,

Parse command line arguments,

jump to main function

12. Is main is a keyword?

Ans: main is not a keyword, it is an identifier (it usually the name of a function in c program).

13. Where the definitions of printf(), scanf() presents?

Ans: printf(), scanf() are declared in header files but their definition is presented in operating system. i.e at /lib/libc.so

Page 3: c faq

14 . what is volatile in c?

The volatile keyword acts as a data type qualifier.

The volatile qualifier alters the default behaviour of the variable and does not attempt to optimize the storage referenced by it.

15. What is modifier in c?

Ans: C Modifiers To modify the properties of basic data types ,C programming language supports some

modifiers(generally they are keywords). According to the properties of these modifiers, we can categorize the modifiers in

following groups:

Modifier Group Modifiers1.Size modifier: short, long, nothing(not a keyword)2.Sign modifier: signed, unsigned3.Constant modifier: const, non_const(not a keyword)4.Storage Class: auto, register, static, extern5.Volatile modifier: volatile, non_volatile(not a keyword)6.Pointer modifier: near, far, huge7.Function modifier: pascal, cdecl8.Interrupt modifer: interrupt, non_interrupt(not a keyword)

Important Concepts: "nothing" modifier must be default modifier of size modifier group.

"Pointer modifier" are not present in LINUX gcc compiler.

"Interrupt modifier" are mostly used in testing of a C program.

"Storage Class modifier" are very important in regards to Dynamic Memory Allocation Concept in C programming language.

Pointer modifier,function modifier and interrupt modifiers are rarely used in programming.

The list of default modifier in each group discussed above: Group Modifier1. Size modifier nothing(not a keyword)2. Sign modifier Signed3. Constant modifier non_const(not a keyword)4. Storage Class auto or extern5. Volatile modifier non_volatile(not a keyword)6. Pointer modifier near7. Function modifier cdecl8. Interrupt modifer non_interrupt(not a keyword)

Very Important Terms:

Page 4: c faq

In storage class modifiers, the default modifier are auto and extern. There are two cases for default modifier in storage class:

1. Variable declared locally (inside the functions) : auto 2. Variable declared globally (outside the functions): extern

The default storage class modifier for C function is “extern”. The default size modifier is nothing(it is not a keyword) but we can specify it be using

a C keyword “void”.

The default pointer modifier depends upon the memory model(TINY, SMALL, MEDIUM, COMPACT, LARGE, HUGE) of C language.

Important Concept:Default modifiers and default data types both are different,don’t be confused.

Compiler assumes "int" as the default data type for SIZE, SIGN, STORAGE CLASS, CONSTANT and VOLATILE modifiers.

Examples:1. short x=10;It has similar meaning to: short int x=10;2. signed x=10;It has similar meaning to: signed int x=10;3. register x=10;It has similar meaning to: register int x=10;

Another Important Concept:We cannot declare such type of statements:

1.far *p; 2.pascal ps; 3.interrupt itp;

Rules to use C modifiers Rule-1: Two modifiers of the same group cannot be used to declare a

data type of C. Example: Short long int x; signed unsigned int x; extern auto char x; long long int x; etc. Following declaration are valid: const volatile long x; unsigned static long volatile int x; Rule-2: We can write modifier either before the data type or after the

data type, both are valid. Example: Unsigned float x; Or float unsigned x; Rule 3: The Order of modifier(including data type )does not affect the

meaning of declaration. Example: int const long static x;

Page 5: c faq

int static const long x; long int static const x; Rule 4: POINTER, FUNCTION and INTERRUPT modifier must be

written after the data type. Example: unsigned char far *c; char far unsigned *c; char far unsigned *c; far char unsigned *c; unsigned far char *c; First three declarations are valid as well as equivalent. But last

two declarations are invalid. Data types with allowed modifiers

In C, all modifiers cannot be used with each type of data type. Following is a list that specify the names of data types with allowed modifiers.

Data Type Allowed Modifier char: sign, constant, volatile, storage-class int: size, sign, constant, volatile, storage-class float: constant, volatile, storage-class double: long, constant, volatile, storage-class void: no any modifier enum: no any modifier array: size, sign, constant, volatile, storage-class function: storage-class, pointer, function pointer: size,sign, constant, volatile, storage-class,pointer structure: constant, volatile, storage-class union: constant, volatile, storage-class

16. what is type casting?

Ans: Casting represents a request by the programmer to do an explicit type conversion. In standard C programming, casts are done via the () operator, with the name of the type to cast to inside. For example:

int nValue1 = 10;

int nValue2 = 4;

float fValue = (float)nValue1 / nValue2;

In the above program, we use a float cast to tell the compiler to promote nValue1 to a floating point value. Because nValue1 is a floating point value, nValue2 will then be promoted to a floating point value as well, and the division will be done using floating point division instead of integer division!

Page 6: c faq

Why The Array Index Should Start From 0?

Ans : In C, the name of an array is essentially a pointer, a reference to a memory location, and so the expression array[n] refers to a memory location n-elements away from the starting element. This means that the index is used as an offset. The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0]. Most programming languages have been designed this way, so indexing from 0 is pretty much inherent to the language.