C sample programs for interview
It can serves as a reminder on what the program is all about, or a description on why a certain code or function was placed there in the first place. Comments can be a single line, or can even span several lines. It can be placed anywhere in the program. Debugging is the process of identifying errors within a program. During program compilation, errors that are found will stop the program from executing completely.
At this state, the programmer would look into the possible portions where the error occurred. Debugging ensures the removal of errors, and plays an important role in ensuring that the expected program output is met. When using this operator, all conditions specified must be TRUE before the next action can be performed.
There is no single command or function in C that can check if a number is odd or even. However, this can be accomplished by dividing that number by 2, then checking the remainder. If the remainder is 0, then that number is even, otherwise, it is odd. You can write it in code as:. This format is used for two things: to set the number of spaces allotted for the output number and to set the number of decimal places.
The number before the decimal point is for the allotted space, in this case it would allot 10 spaces for the output number. If the number of space occupied by the output number is less than 10, addition space characters will be inserted before the actual output number. Program that contains logical errors tend to pass the compilation process, but the resulting output may not be the expected one. This happens when a wrong formula was inserted into the code, or a wrong sequence of commands was performed.
Syntax errors, on the other hand, deal with incorrect commands that are misspelled or not recognized by the compiler. There are 3 main control structures in programming: Sequence, Selection and Repetition. Sequential control follows a top to bottom flow in executing a program, such that step 1 is first perform, followed by step 2, all the way until the last step is performed.
This also means that not all codes may be executed, and there are alternative flows within. Repetitions are also known as loop structures, and will repeat one or two program statements set by a counter. The is also known as the OR operator in C programming.
For comparing string values, there is another function called strcmp that deals specifically with strings. Preprocessor directives are placed at the beginning of every C program. This is where library files are specified, which would depend on what functions are to be used in the program. Another use of preprocessor directives is the declaration of constants. Preprocessor directives begin with the symbol. The outcome will be TRUE. Just the same, s! Order of precedence determines which operation must first take place in an operation statement or conditional statement.
On the top most level of precedence are the unary operators! Instead, use the strcpy function. To get the length of a string value, use the function strlen. This is because C language is rich in features that make it behave like a high level language while at the same time can interact with hardware using low level methods.
The use of a well structured approach to programming, coupled with English-like words used in functions, makes it act as a high level language. On the other hand, C can directly access memory structures similar to assembly language routines. Source codes in C are saved with. C file extension. Header files or library files have the. H file extension. Every time a program source code is successfully compiled, it creates an.
OBJ object file, and an executable. EXE file. Reserved words are words that are part of the standard C language library. This means that reserved words have special meaning and therefore cannot be used for purposes other than what it is originally intended for. Examples of reserved words are int, void, and return. A linked list is composed of nodes that are connected with another.
In C programming, linked lists are created using pointers. Using linked lists is one efficient way of utilizing memory for storage.
In C programming, there is a data structure known as queue. A queue represents a line wherein the first data that was stored will be the first one that is accessible as well. Binary trees are actually an extension of the concept of linked lists. A binary tree has two pointers, a left one and a right one.
Each side can further branch to form additional nodes, which each node having two pointers as well. All reserved words must be written in lowercase; otherwise the C compiler would interpret this as unidentified and invalid. In the first expression, the increment would happen first on variable a, and the resulting value will be the one to be used. This is also known as a prefix increment. In the second expression, the current value of variable a would the one to be used in an operation, before the value of a itself is incremented.
This is also known as postfix increment. C language is a case sensitive language. An endless loop can mean two things. One is that it was designed to loop continuously until the condition within the loop is met, after which a break function would cause the program to step out of the loop. Another idea of an endless loop is when an incorrect loop condition was written, causing the loop to run erroneously forever. Endless loops are oftentimes referred to as infinite loops.
A flowchart provides a visual representation of the step by step procedure towards solving a given problem. Flowcharts are made of symbols, with each symbol in the form of different shapes. The word void is a reserved word in C language. You cannot use reserved words as a user-defined variable.
Assuming that INT is a variable of type float, this statement is valid. One may think that INT is a reserved word and must not be used for other purposes. However, recall that reserved words are express in lowercase, so the C compiler will not interpret this as a reserved word.
When you create and use functions that need to perform an action on some given values, you need to pass these given values to that function. The values that are being passed into the called function are referred to as actual arguments. This is used to insert a new line when displaying data in the output screen. A newline escape sequence can be placed before the actual output expression or after.
It is the process of transferring data to an alternative output source other than the display screen. Output redirection allows a program to have its output saved to a file. These are errors that occur while the program is being executed. We use the idea of recursively calling main function. Skip to content. Change Language. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. Take a step-up from those "Hello World" programs. Click here to check out 10 java programs for technical interview.
Now there are a number of ways you can do this program but there is one way you can really impress the interviewer which uses this cool technique using bitwise operators. What is XOR gate? Show some other ways to do the swapping. Or you can multiply and then divide.
Often you might make a very simple mistake which the interviewer was actually looking for. If you are still having problems you can visit my post on patterns. The function must have a recursive definition or in simple words can be expressed in its own form.
There is a terminating value for which we know the return value of the function. We have to make a function fibonacci in a recursive form. Can we use this function to print the whole Fibonacci series? What if we pass a negative number to the fibonacci? Take some rest and come back later but if u want a challenge then continue.
Now what is an Armstrong number? If a number has n digits and the sum of the nth power of its digits is the number itself then the number is an Armstrong number. Surely you got confused. Here are some examples to help you out. The code is given below. Feel free to peek. Is 0 an Armstrong number? How many 2 digit numbers are Armstrong numbers?
We are so used to using strcat when it comes to concatenate two strings but how can we do that without using strcat? To answer that you have to know what is string in C and how it is printed. Strings in C are represented by character arrays. The end of the string is marked by a special character known as the null character. So to concatenate two strings you just have to add the characters of the second character array after the first and place a null character at the end.
What if we use scanf instead of gets? Use scanf instead of gets to find the difference. Sorting is a one of the most favorite topic asked during the interviews. It is usually accompanied by something or the other like here sorting is done indirectly using pointers.
There are many sorting techniques but usually bubble sort or selection sort is asked. Sorting the elements of the array using pointers makes the program a bit challenging.
0コメント