C - Questions and Answers
- What is C language?
- Why C is called a middle level programming language?
- Why C is known as a mother language?
- Who is the founder of C language?
- When C language was developed?
- What are the features of C language?
- Why C is a also known as procedural language?
- What is the use of printf() and scanf() functions?
- What is token?
- What is new line escape sequence?
- What is the maximum length of an identifier?
- What is the difference between local variable and global variable in C?
- What is the use of static variable in C?
- What is auto keyword in C?
- What is infinite loop?
- What is array in C?
- What is the acronym for ASCII?
- What is the difference between getch() and getche()?
- What is the use of function in C?
- What is the difference between call by value and call by reference in C?
- What is recursion in C?
- What is pointer in C?
- What is the usage of pointer in C?
- What is pointer to pointer in C?
- Can we access array using pointer in C language?
- What is static memory allocation?
- What is dynamic memory allocation?
- What is structure?
- What is union?
- What is command line argument?
- What are the functions to open and close file in C language?
- Can we compile a program without main() function?
- Write a program to print "hello world" without using semicolon?
- Write a c program to find the sum of n numbers given as input by the user.
C is a middle level and procedural programming language.
It supports the feature of both low-level and high level languages that is why it is known as a middle level programming language.
Low level language specific machine dependent, fast to run, But it is not easy to understand.
High Level language is not specific to one machine i.e. machine independent. It is easy to understand.
C is known as a mother language because most of the compilers, kernels and JVMs are written in C language and most of the languages follows c syntax.
It provides the core concepts like if condition, loops, array, functions, file handling etc. that is being used in many languages like C++, Java, C#, Python etc.
C language was developed in 1972 at bell laboratory of AT&T.
The main features of C language are given below:
• Machine Independent
• Rich Library
• Fast Speed
• Memory Management
A procedure is known as function, method, routine, subroutine etc. A procedural language specifies a series of steps or procedures for the program to solve the problem. A procedural language breaks the program into functions, data structures etc. C is a procedural language. In C, variables and function prototypes must be declared before being used.
The printf() function is used for output and scanf() function is used for input.
Token is an identifier. It can be constant, keyword, string literal etc.
The new line escape sequence is represent by "\n". It inserts a new line on the output screen.
It is 32 characters ideally but implementation specific.
Local variable: A variable which is declared inside the function or block is known as local variable.
Global variable: A variable which is declared outside the function or block is known as global variable.
int a=10; //global variable
int b=20; //local variable
A variable which is declared as static is known as static variable. The static variable retains its value between multiple function calls.
int x=10;//local variable
static int y=10;//static variable
printf("%d\n",x);//will always print 11
printf("%d\n",y);//will always increment value, it will print 11, 12, 13 and so on
In C, every local variable of a function is known as automatic (auto) variable. Let's explain with an example:
int i ;
auto int j;
Here, both 'i' and 'j' variables are automatic variables.
A loop running continuously for indefinite number of times is called infinite loop.
Infinite For Loop:
printf("\nInfinitive for loop");
Infinite While Loop:
printf("\nInfinitive while loop");
printf("\nInfinitive do while loop");
Multiple initializations in for loop
for (i=1,j=1; j<=10; i++, j++)
printf("%d %d\n", i, j);
Array is a group of similar types of elements. It has contiguous memory location. It makes the code optimized, easy to traverse and easy to sort.
American Standard Code of Information Interchange.
The getch() function reads a single character from keyboard. It doesn't use any buffer, so entered data is not displayed on the output screen.
The getche() function reads a single character from keyword and data is displayed on the output screen.
A function in C language provides modularity. It can be called many times. It saves code and we can reuse the same code many times.
We can pass value to function by one of the two ways: call by value or call by reference.
In call by value, a copy of value is passed to the function, so original value is not modified.
But in the case of call by reference, an address of a variable is passed via function argument, so original value is modified.
Calling the same function, inside the function is known as recursion.
A pointer is a variable that refers to the address of a variable. It makes the code optimized and makes the performance fast.
• Accessing array elements
• Dynamic memory allocation
• Call by Reference
• Data Structures like tree, graph, linked list etc.
In case of pointer to pointer concept, one pointer refers to the address of another pointer.
Yes, by holding the base address of array into pointer, we can access the array using pointer.
In case of static memory allocation, memory is allocated at compile time and memory can't be increased while executing the program. It is used in array.
In case of dynamic memory allocation, memory is allocated at run time and memory can be increased while executing the program. It is used in linked list.
Structure is a user-defined data type that allows to store multiple types of data in a single unit. It occupies the sum of memory of all members.
Like Structure, union is a user-defined data type that allows to store multiple types of data in a single unit. But it doesn't occupies the sum of memory of all members. It occupies the memory of largest member only.
The argument passed to the main() function while executing the program is known as command line argument. For example:
void main(int count, char *args)
//code to be executed
The fopen() function is used to open file whereas fclose() is used to close file.
Yes, we can compile but it can't be executed.
But, if we use #define, we can compile and run C program without using main() function. For example:
#define start main
There are various ways to do so. Let's see a program to print "hello world" using if.