Skip to main content

Posts

The Basics Of C Programming - Part 3.

There are a number of different C input commands, the most useful of which is the scanf command. To read a single integer value into the variable called a you can use: scanf("%d",&a); When the program reaches the scanf statement it pauses to give the user time to type something on the keyboard and continues only when users press Enter or Return, to signal that he, or she, has finished entering the value. Then the program continues with the new value stored in a. In this way, each time the program is run the user gets a chance to type in a different value to the variable and the program also gets the chance to produce a different result! The final missing piece in the jigsaw is using the printf function, the one we use to print the value currently being stored in a variable. To display the value stored in the variable a you can use: printf("The value stored in a is %d",a); Note: the scanf function does not prompt for an input. You should ge...

The Basics Of C Programming - Part 2.

A variable is just a named area of storage that can hold a single value (numeric or character). C is very fussy about how you create variables and what you store in them. It demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it. There are five basic data types associated with variables: int - integer: a whole number. float - floating point value: i.e. a number with a fractional part. double - a double-precision floating point value. char - a single character. void - valueless special purpose type. One of the confusing things about the C language is that the range of values and the amount of storage that each of these types takes is not defined. This is because in each case the 'natural' choice is made for each type of machine. You can call variables what you like, although it helps if you give them sensible names that give you a hint of what they're being u...

The Basics Of C Programming - Part 1.

Your first program is going to do is print the message "Hello World" on the screen. The program is a short one, to say the least. Here it is: #include <stdio.h> main() { printf("Hello World\n"); } The first line is the standard start for all C programs - main() . After this comes the program's only instruction enclosed in curly brackets {} . The curly brackets mark the start and end of the list of instructions that make up the program - in this case just one instruction. Notice the semicolon marking the end of the instruction. You might as well get into the habit of ending every C instruction with a semicolon - it will save you a lot of trouble! Also notice that the semicolon marks the end of an instruction - it isn't a separator as is the custom in other languages. If you're puzzled about why the curly brackets are on separate lines I'd better tell you that it's just a layout convention to help you spot matching b...

C Interview Questions And Answers - Part 5.

What will be output of the following program? #include<stdio.h> int main() { int a = 2, b = 7, c = 10; c = a == b; printf("%d",c); return 0; } Output: 0 == is a relational operator which returns only two values. 0: If a == b is false 1: If a == b is true Since a = 2 and b = 7, so a == b is false, thus c = 0 What will be output of following program? #include<stdio.h> #include<string.h> int main() { char *ptr1 = NULL; char *ptr2 = 0; strcpy(ptr1," c"); strcpy(ptr2,"questions"); printf("\n%s %s",ptr1,ptr2); return 0; } Output: Run time error.  W e cannot assign any string constant in null pointer by strcpy function.

C Interview Questions And Answers - Part 4.

What is the output of following c program? #include<stdio.h> int main()  { int goto=5; printf("%d",goto); return 0; } Output:  Compilation error.  Because  it declared an invalid variable name. goto is a keyword in c language and variable name cannot be any keyword. What is the output of following c program? #include<stdio.h> int main() { long int 1a=5l; printf("%ld",1a); return 0; } Output: Compilation error. Because variable name cannot start with numeric value.

C Interview Questions And Answers - Part 3.

What is the meaning of NULL? Answer: NULL is a macro constant which has been defined in several header files such as stdio.h, alloc.h, mem.h, stddef.h and stdlib.h as #define NULL 0 Example 1: What is the output of following c program? #include "stdio.h" int main()`{ if(!NULL) printf("I know preprocessor"); else printf("I don't know preprocessor"); } Output: I know preprocessor Explanation: !NULL = !0 = 1, i.e. any non-zero number means true. Example 2:   What is the output of following c program? #include "stdio.h" int main() { int i; static int count; for(i=NULL;i<=5;) { count++; i+=2; } printf("%d",count); } Output: 3  What is the output if you execute following c code? #include<stdio.h> int main() { int i; for(i=0;i<5;i++) { int i=10; printf(" %d",i); i++; } return 0; } Output: 10 10 10 10 10 Explanation: The default sto...

C Interview Questions And Answers - Part 2.

What is printf()? Answer: printf() is an inbuilt library function in C library by default. This function is declared and related macros are defined in “stdio.h” header file. printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. What is scanf()? Answer: scanf() is an inbuilt library function in C library by default. This function is declared and related macros are defined in “stdio.h” header file. scanf() function is used to read character, string, numeric data from keyboard. What is void in C? Answer: Void is an empty data type that has no value. We use void data type in functions when we don’t want to return any value to the calling function. Example: void sum (int a, int b); – This function won’t return any value to the calling function. int sum (int a, int b); – This function will return value to the calling function. We use void data type in pointer like “void *p”. It means, po...

C Interview Questions And Answers - Part 1.

What is the difference between pass by value and pass by reference? Answer: We can pass the parameters in a function in two different ways in C programming. Pass by value: In this approach we pass the copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable. For example: #include<stdio.h> int main() {     int a=5,b=10;     swap(a,b);     printf("%d %d",a,b);     return 0; }   void swap(int a,int b) {     int temp;     temp =a;     a=b;     b=temp; } Output: 5  10 Pass by reference: In this approach we pass the memory address of actual variables in function as a parameter. Hence any modification on parameters inside the function will reflect in the actual variable. For example: #incude<stdio.h> int main() {...

The History Of C Language.

The C programming language was devised in the early 1970s by Dennis M. Ritchie, an employee from Bell Labs (AT&T). In the 1960s, Ritchie worked with several other employees of Bell Labs (AT&T) on a project called Multics. The goal of the project was to develop an operating system for the large computer that could be used by many users. Bell Labs withdrew from the project in 1969, as it could not produce an economically useful system. Hence the employees of Bell Labs (AT&T) had to search for another project to work on (mainly Dennis M. Ritchie and Ken Thompson). Ken Thompson began to work on the development of a new file system for the DEC PDP-7, in assembler. Soon they began to make improvements and add expansions. They used the knowledge from the Multics project to add improvements. After a while a complete system was born. Brian W. Kernighan called the system UNIX, a sarcastic reference to Multics. The whole system was still written in assembly code.

C Program To Implement AVL Tree Operations.

The AVL tree is a self-balancing binary search tree in which the heights of the two child sub-trees of any node differ by at most one. If at any time they differ by more than one, rebalancing is done by one or more tree rotations to restore this property. Basic operations such as lookup, insertion, deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. There are four ways to rotate nodes in an AVL tree (graphically represented):

C Program To Implement Red Black Tree Operations.

A red–black tree is a self-balancing binary search tree which supports the operation search, find predecessor, find successor, find minimum, find maximum, insertion and deletion in O(log n)-time. In addition to the requirements imposed on a binary search tree, the following must be satisfied to be a red–black tree: 1)    A node is either red or black . 2)    The root is black. 3)    All leaves (NIL) are black. 4)    If a node is red, then both its children are black. 5)    Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.  If there is a modification in RB tree due to insertion or deletion of any element, the new tree is then rearranged and repainted to restore the properties.

C Program To Implement Binary Search Tree Operations.

A binary search tree (BST), also known as an ordered binary tree, is a node-based data structure in which each node has no more than two child nodes. Each child must either be a leaf node or the root of another binary search tree. The left sub-tree contains only nodes with keys less than the parent node; the right sub-tree contains only nodes with keys greater than the parent node. Example: Insert 20 into the Binary Search Tree. Tree is not available. So create root node and place 20 into it.                                                    20 Insert 23 into the given Binary Search Tree. Since 23 > 20, then 23 will be inserted in the right sub-tree of 20.           ...

C Program To Generate Prime Numbers.

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Remember two is the only even and also the smallest prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications in computer science and mathematics. Any number greater than 1 can be factorized into prime numbers; for example 540 = 22*33*51. This fact makes primes very significant to communications. Most modern computer cryptography works by using the prime factors of large numbers. The large number that was used to encrypt a file can be publicly known and available, because the encryption works so only the prime factors of that large number can be used to decrypt it again. Though finding those factors is technically only a matter of time, it’s a matter of so much time that we say it cannot be done. A modern super-computer could chew on a 256-bit factorization problem for longer than the current age of the universe,...