Sunday, 28 August 2011

Question Bank of C



Qu 20. Write a shortcut keys of C


1. Alt + F  -  open file


2. Alt + F + N  -  new file


3. Alt + F9  -  compile programme


4. Ctrl + F9  - execute a program


5. Ctrl + F5  - out put screen


6. F6  - to change between programme screen and error screen


7. F2  -  save programme


8. Alt + ENTER  -  full screen mode


9. Alt + F + L  -  to check programme load or not


10. F3  -  load programme


11. Alt + X  -  exit turbo c

Qu 19. What is important of Comment line in C

All programming languages let you include comments in your programs. Comments can be used to remind yourself (and others) of what processing is taking place or what a particular variable is being used for. They can be used to explain or clarify any aspect of a program which may be difficult to understand by just reading the programming statements. 

This is very important since the easier it is to understand a program, the more confidence you will have that it is correct. It is worth adding anything which makes a program easier to understand.

Remember that a comment (or lack of it) has absolutely no effect on how the program runs. If you remove all the comments from a program, it will run exactly the same way as with the comments.

Each language has its own way of specifying how a comment must be written. In C, we write a comment by enclosing it within /* and */, for example:
/* This program prints a greeting */

A comment extends from /* to the next */ and may span one or more lines. The following is a valid comment:
/* This program reads characters one at a time
and counts the number of letters found */
C also lets you use // to write one-line comments. The comment extends from // to the end of the line, for example:
a = s * s; //calculate area; store in a
In this series, we will use mainly one-line comments.

Qu 18. what is token in c? Explain all type of tokens avalible in C

 In a C source program, the basic element recognized by the compiler is the "token." A token is source-program text that the compiler does not break down into component elements.

Syntax

token:
    keyword

identifier constant string-literal operator punctuator

The keywords, identifiers, constants, string literals, and operators described in this section are examples of tokens. Punctuation characters such as brackets ([ ]), braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens.


C token is an individual word present in the c-language. There are 6 types
  • Keywords: keywords are defined by the software with the specified meaning. 
  • In c language 32 keywords are there
                          Example: double,int,struct, etc
  • Identifiers:User defined variable names.
  • Constants: The value which is not changed during the execution
  • Strings: Group of caharacters enclosed in " "
  • Operators: The following are the operators available in c
  • 1.Arthematic Operators: [+,-,*,/,%] 
    2.Assignment Operators: [=] 
    3.Relational Operators: [<,<=,>,>=,==,!=] 
    4.Logical Operators: [and,or,not] 
    5.increment/decrement Operators:[++,--] 
    6.Bitwise Operators: [&,|,~,^,>>,<<] 
    7.Conditional or terinary Operator:[?:]
  • Special characters: ,(comma) -to seperate 
  • 2 variables sizeof Operators -returns the size of the variable

Qu 17. What is backslash in c?

The backslash is a trigger. It says the next character is special. \n is new line, \t is tab, \b is backspace. So logically, the backslash alone can't be used to display a backslash, so you use \\ to display one backslash. 
Backslash Commands 

When used in a character or string expression, the backslash character (\) is used to
give instructions to cout.   For example, \n has the identical effect of endl.      For example:

cout << “This is on one line.” << ‘\n’ << “This is on another line.”;

will produce the output

                         This is on one line.
                         This is on another line

The only advantage of \n is that it can be included in a string.      For example:

                 cout << “This is on one line.\nThis is on another line.”;

will also produce the output

                         This is on one line.
                         This is on another line 

Qu 16. what is trigraph in c

In the C family of programming languages, a trigraph is a sequence of three characters, the first two of which are both question marks, that represents a single character.

The reason for their existence is that the basic character set of C (a subset of the ASCII character set) includes nine characters which lie outside the ISO 646 invariant character set. This can pose a problem for writing source code when the keyboard being used does not support any of these nine characters. The ANSI C committee invented trigraphs as a way of entering source code using keyboards that support any version of the ISO 646 character set.

Trigraphs might also be used for some EBCDIC code pages that lack characters such as { and }.

Trigraphs are not commonly encountered outside compiler test suites. Some compilers support an option to turn recognition of trigraphs off, or disable trigraphs by default and require an option to turn them on. Some can issue warnings when they encounter trigraphs in source files. Borland supplied a separate program, the trigraph preprocessor, to be used only when trigraph processing is desired (the rationale was to maximise speed of compilation).

The C preprocessor replaces all occurrences of the following nine trigraph sequences by their single-character equivalents before any other processing.
Trigraph Equivalent
??= #
??/ \
??' ^
??( [
??) ]
??! |
??< {
??> }
??- ~

Note that ??? is not a trigraph sequence.

Note also that the problematic characters are nevertheless required to exist within the implementation, in both the source and execution character sets.

Qu 15. Write Syntax and Example of printf and scanf.

1. printf

The printf function is the main method used for outputting text in C programs. printf is a library function contained in the stdio library. The general form of printf is shown below.
Syntax: printf ( <format string>, arg1, arg2, arg3, ... ) ;
The format string can contain text, control characters and also markers that indicate where the value of a variable should be displayed within the output text. Following the format string are a list of variables (or constants) that should be output as part of the message. These variables are separated with commas. For each variable marker in the format string, there should be a corresponding variable included in the argument list. The following examples will show how printf is used.

Display text:
                printf("Hello World!");
Display text and move to the next line:
                printf("Hello World!\n");

The character sequence \n indicates that you should move to the next line. If this is not included, the next printf will print on the same line as this one. The newline character \n is a single character. It is not possible to enter the character directly. The \ indicates that you are specifying a special character, and the following letter, an n in this case, specifies which special character.
Display text and the value of an integer:

                printf("We have been in school %d days.\n", dayCount); 
/*dayCount is a variable*/

The %d in the format string indicates that the printf command should insert the value of the first variable in the argument list, in this case, the value dayCount.

A constant can be used instead of a variable:
                printf("We have been in school %d days.\n", 23);

This constant could also be put directly in the format string instead of the %d.
Different control charactes correspond to different variable types. The %d character can be used with integer variables. Control characters for the various data types are shown in the table below.
int %d float %f double %lf string %s single character %c a format string may reference several variables. Example statements and their output are shown in the sample code below.


#include <stdio.h>
int main(int argc, char* argv[])
{
    int count = 9;
    float fract = 3.5;
    double percent = 37.6389;
    char singleChar = 'A';
    char * testString = "happy";
    printf("The student's average was: %lf\n", percent);
    /*Output:  The student's average was: 37.638900;*/
    printf("A fraction %f, a string %s and a character %c\n", fract,testString, singleChar);
    /*Output:  A fraction 3.500000, a string happy and a character A*/
    printf("Numbers without spaces %d%lf%f", count, percent, fract);
    /*Output:  Numbers without spaces 937.6389003.500000;*/
    getch();
}

2. scanf
The command scanf can be used in C programs to input data. scanf has similar syntax to printf. The general form is shown below.
                scanf ( <format string>, arg1, arg2, arg3, ... ) ;
With scanf, the format string specifies the type of data the program is expecting to get from the user. The list of arguments are the variables where the data should be stored. printf and scanf use the same control characters for the various variable types.
scanf will store the data it receives in the memory location where the variable is stored. In order to do this, it needs the memory location of the variable. Preceding the variable name with an & indicates the address of the variable, rather than the variable itself. For instance, the following command will read an integer and store it in the variable index.
int index;
    scanf("%d", &index);
The sample code below shows various examples of scanf usage.
#include <stdio.h>
int main(int argc, char* argv[])
{
    int sample;
    float fl;
    char string[33]; /*string that will hold 32 characters plus the termination character*/
    printf("Type an integer: \n");
    scanf("%d", &sample);
    printf("Type a string: \n");
    scanf("%s", string);
    printf("Type a float: \n");
    scanf("%f", &fl);
    printf("You typed the integer %d, the string %s, the float %f\n", sample, string, fl);
    /*you can also capture multiple values with one scanf*/
    /*scanf returns the number of values it receives, so a
                  program can check to make sure all the desired data was
                  received*/
    int dataReceived = 0;
    int testInt;
    float testFloat;
    printf("Enter an int and a float separated by a space\n");
    dataReceived = scanf("%d %f", &testInt, &testFloat);
    printf ("Scanf received %d items of data.  You entered the int %d and the float %f .\n", dataReceived, testInt, testFloat);
    getch();
}
Output:
Type an integer:
45
Type a string:
happy23
Type a float:
6.879
You typed the integer 45, the string happy23, the float 6.879000
Enter an int and a float separated by a space
51 1.235
Scanf received 2 items of data.  You entered the int 51 and the float 1.235000 .


Qu 14. Explain The #define Directive

The #define Directive
When you compile a program the compiler first uses a preprocessor to analyze the code. The #define directive can be used to either define a constant number or function or to replace an instruction in your code. For instance:

#define for_ever_do while(1)
This means you can use for_ever_do instead of while(1) and the effect will be the same because the preprocessor first replaces for_ever_do with while(1) and then the program is compiled. With #define you can also create function or more precisely called, a macro. Here's another example:

#define sqr(x) (x*x)
When you call sqr(6), the preprocessor will first replace sqr(6) with (6*6) and then compile the program. Another thing that you can do with #define is concatanate two variables. For example:

#define conc(a,b)
int main() {
int xyz=123,try=125;
cout<<conc(xy,z)<<" "<<conc(t,ry);
}
This will display 123 125 because the preprocessor replaces conc(xy,z) with xyz and then evaluates it.

Qu 13. Explain #include directive.

The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. 

You can organize constant and macro definitions into include files and then use #include directives to add these definitions to any source file. Include files are also useful for incorporating declarations of external variables and complex data types. You need to define and name the types only once in an include file created for that purpose.


#include  "path-spec"
#include  <path-spec>

The path-spec is a filename optionally preceded by a directory specification. The filename must name an existing file. The syntax of the path-spec depends on the operating system on which the program is compiled.
Both syntax forms cause replacement of that directive by the entire contents of the specified include file. The difference between the two forms is the order in which the preprocessor searches for header files when the path is incompletely specified.



Syntax Form Action
Quoted form This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.
Example #include "defs.h"
 
 
 






Angle-bracket form This form instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then, when compiling from the command line, along the path specified by the INCLUDE environment variable.
Example #include <stdio.h>

Qu 12. What is variable? Explain rules how to declair and intilisation a variable

Variable

A variable is a named memory, that can be used to read and write information. In this sense, variables are nothing more than "buckets" that can be used to store whatever values are needed for a specific computation. However, as different materials require different containers, we also have different types of variables for the various contents that can be stored in it.



Declare Variable initialization

There are two C Standards documents. The original (ANSI-89/ISO-90) did not allow mixed declarations and (executable) code within a block. In fact declarations can occur at the beginning of any block (that is, after the opening { brace and before any code), not just at the beginning of a function. This has been true of all releases of any serious compilers, even before the first standard. This standard is sometimes called C89, and sometimes it's called C90.

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
    int i = 1;
    printf("i = %d\n", i);
    {
        int j = 2;
        printf("i = %d, j = %d\n", i, j);
    }
    return 0;
}

Output:
Code:
i = 1 i = 1, j = 2

The variable j is only visible inside the block in which it is declared. This behavior was also carried forward into C++, so the above program is legal C and C++ for all compilers that I have tested (and all C and C++ compilers should work with that code).

There is a second standard, ISO-99, which allows declarations just about anywhere in a block. (This is commonly called the C99 Standard.)

Many commonly used compilers (Borland and Microsoft to name two with I am familiar) do not allow the mixed declarations of the ISO-99 standard.

So the following would not work with these C compilers, but it would be OK with GNU gcc:
CPP / C++ / C Code:
#include <stdio.h>
int main()
{
    int i = 99;
    printf("i = %d\n", i);
    int j = 42;
    printf("j = %d\n", j);
    return 0;
}
 Rules

• Variable name may be a combination of alphabets, digits or underscores.
 
• Sometimes, compilers in which case its length should not exceed 8 characters impose an additional constraint on the number of characters in the name.
 
• First character must b e alphabet or an underscore.
 
• No. comma or blank space is allowed.
 
• Among the special symbols, only underscore can be used in variable name.
 
• No word, having a reserved meaning in C can be used for variable name.
  


Qu 11. Why "C" is called Middle Level Language?

C Programming language is called as Middle Level Language because 

(i) it gives or behaves as High Level Language through Functions - gives a modular programming and breakup, increased efficiency for re-usability

(ii)it gives access to the low level memory through Pointers. Moreover it does support the Low Level programming i.e, Assembly Language.

As its a combination of these two aspects, its neither a High Level nor a Low level language but a Middle Level Language.

Qu 10. Explain What is programming language with all types.

 What is Programming language ?

A programming language is used to write computer programs such as
  • Applications
  • Utilities
  • Servers
  • Systems Programs
A program is written as a series of human understandable computer instructions that can be read by a compiler and linker, and translated into machine code so that a computer can understand and run it. 

Type of programming language

1. Low level Language

2. Middle Level Language

3. High Level Language



1. Low level Language


Of all of the categories, it’s probably easiest to define what it means to be a low-level language.  Machine code is low level because it runs directly on the processor.  Low-level languages are appropriate for writing operating systems or firmware for micro-controllers.  They can do just about anything with a little bit of work, but obviously you wouldn’t want to write the next major web framework in one of them (I can see it now, “Assembly on Rails”).

Characteristics


  • Direct memory management
  • Little-to-no abstraction from the hardware
  • Register access
  • Statements usually have an obvious correspondence with clock cycles
  • Superb performance

C is actually a very interesting language in this category (more so C++) because of how broad its range happens to be.  C allows you direct access to registers and memory locations, but it also has a number of constructs which allow significant abstraction from the hardware itself.  Really, C and C++ probably represent the most broad spectrum languages in existence, which makes them quite interesting from a theoretical standpoint.  In practice, both C and C++ are too low-level to do anything “enterprisy”.

2. Middle Level Language


This is where things start getting vague.  Most high-level languages are well defined, as are low-level languages, but mid-level languages tend to be a bit difficult to box.  I really define the category by the size of application I would be willing to write using a given language.  I would have no problem writing and maintaining a large desktop application in a mid-level language (such as Java), whereas to do so in a low-level language (like Assembly) would lead to unending pain.


This is really the level at which virtual machines start to become common-place.  Java, Scala, C# etc all use a virtual machine to provide an execution environment.  Thus, many mid-level languages don’t compile directly down to the metal (at least, not right away) but represent a blurring between interpreted and compiled languages.  Mid-level languages are almost always defined in terms of low-level languages (e.g. the Java compiler is bootstrapped from C).

Characteristics


  • High level abstractions such as objects (or functionals)
  • Static typing
  • Extremely commonplace (mid-level languages are by far the most widely used)
  • Virtual machines
  • Garbage collection
  • Easy to reason about program flow

3. High Level Language


High-level languages are really interesting if you think about it.  They are essentially mid-level languages which just take the concepts of abstraction and high-level constructs to the extreme.  For example, Java is mostly object-oriented, but it still relies on primitives which are represented directly in memory.  Ruby on the other hand is completely object-oriented.  It has no primitives (outside of the runtime implementation) and everything can be treated as an object.


In short, high-level languages are the logical semantic evolution of mid-level languages.  It makes a lot of sense when you consider the philosophy of simplification and increase of abstraction.  After all, people were n times more productive switching from C to Java with all of its abstractions.  If that really was the case, then can’t we just add more and more layers of abstraction to increase productivity exponentially?


High-level languages tend to be extremely dynamic.  Runtime flow is changed on the fly through the use of things like dynamic typing, open classes, etc.  This sort of technique provides a tremendous amount of flexibility in algorithm design.  However, this sort of mucking about with execution also tends to make the programs harder to reason about.  It can be very difficult to follow the flow of an algorithm written in Ruby.  This “obfuscation of flow” is precisely why I don’t think high-level languages like Ruby are suitable for large applications.  That’s just my opinion though.  

Characteristics


  • Interpreted
  • Dynamic constructs (open classes, message-style methods, etc)
  • Poor performance
  • Concise code
  • Flexible syntax (good for internal DSLs)
  • Hybrid paradigm (object-oriented and functional)
  • Fanatic community


Qu 8. Difference between Low-Level & High-Level Language

High-level Language

1. Learning
High-level languages are easy to learn.

2 Understanding
High0level languages are near to human languages.
 
3. Execution
Programs in high-level languages are slow in execution.
 
4. Modification
Programs in high-level languages are easy to modify.
 
5. Facility at hardware level
High-level languages do not provide much facility at hardware level.
 
6. Knowledge of hardware Deep
Knowledge of hardware is not required to write programs.
 
7. Uses
These languages are normally used to write application programs.



Low-level languages
 
1. Learning
Low-level languages are difficult to learn.
 
2 Understanding
Low-level languages are far from human languages.
 
3. Execution Programs in low-level languages are fast in execution.
 
4. Modification
Programs in low-level languages are difficult to modify.
 
5. Facility at hardware level
Low-level languages provide facility to write programs at hardware level.
 
6. Knowledge of hardware Deep
Deep knowledge of hardware is required to write programs.
 
7. Uses
These languages are normally used to write hardware programs.

Qu 7.Difference between compiler and interpreter

Computer does not understands our human languages. It understands only binary language. All the input we feed in the computer is converted into binary language.
The programming languages e.g. Basic, C, C++ are either compiler based or interpreter based.

Interpreter:


An Interpreter is a program which converts the higher level language in lower level language or assembly language or binary language i.e. the language of 0’s and 1’s. It reads one line of code at a time, converts it into binary language and then runs the code on the machine. So the initial start up time to run the program is almost negligible. If there is any error on the middle of the program then the program is interrupted in between. After removing the error the programs needs to run again from the beginning. Languages like BASIC is interpreter based languages. So in this interpreter converts one line of code at a time, and then runs it.

Compiler:


A compiler is also a program which convert the higher level language in lower level language. In case of compiler, it reads a whole block of code at a time, converts it into executable code, and runs the code. The initial start up time to run program is more as compared to Interpreter. Once the code is compiled, then the initial start up time to run the program is negligible. Languages like C, C++ are compiler based languages.

Qu 6. Explain FLOW CHART.

FLOW CHART

Flow char is a graphical representation of algorithm. It is used to represent algorithm steps into graphical format. It also gives us an idea to organize the sequence of steps or events necessary to solve a problem with the computer. In other words flow charts are symbolic diagrams of operations and the sequence, data flow, control flow and processing logic in information processing. These charts are used to understand any working sequence of the program.

Advantages of flowchart:-

1. It provides an easy way of communication because any other person besides the programmer can understand the way they are represented.
2. It represents the data flow.
3. It provides a clear overview of the entire program and problem and solution.
4. It checks the accuracy in logic flow.
5. It documents the steps followed in an algorithm.
6. It provides the facility for coding.
7. It provides the way of modification of running program.
8. They shows all major elements and their relationship.


 

 

1.Terminator

This symbol represents the beginning and end point in a program. We use start and stop option in it.

2.Input/Output Symbol

This symbol is used to take any input or output in the algorithm.

3.Process Symbol

A rectangle indicates the processing, calculation and arithmetic operations

4.Decision Symbol

It is used when we want to take any decision in the program.

5.Connector Symbol

This symbol is used to connect the various portion of a flow chart. This is normally used when the flow chart is split between two pages

6.Data Flow Symbol

This symbol is used to display the flow of the program. It shows the path of logic flow in a program.