In this article, we are sharing the most common C programming interview questions and answers which helps you to crack your next interviews.
1. How to reverse a sentence with a program?
Ans: This is a very simple program having the following logic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h> #include <conio.h> #include <string.h> void main() { char *s = "Life is beautiful", ch; int len = strlen(s), start, end = -1, t = 0, length = 0, i; clrscr(); printf("Original sentence=%s\n", s); *(s + len + 1) = ''; *(s + len) = ' '; while (*(s + length) != NULL) { if (*(s + length) == ' ') { start = end + 1; end = length; //printf("%d %d\n",start,end); t = 0; for (i = start; i < start + (end - start) / 2 + 1; i++) { ch = *(s + i); *(s + i) = *(s + end - t); *(s + end - t) = ch; t++; } } length++; } strrev(s); printf("After processing=%s", s); getch(); } |
2. What is the difference between character array and string in C?
Ans: Below are the comparison table to shows the difference between character array and string in c programming language.
Basis for Comparison | Character Array | String |
---|---|---|
Basic | Character array is collection of variables, of character data type. | String is class and variables of string are the object of class “string”. |
Syntax | char array_name [size]; | string string_name; |
Indexing | An individual character in a character array can be accessed by its index in array. | In string the particular character can be accessed by the function “string_name.charAt(index)”. |
Data Type | A character array does not define a datatype. | A string defines a datatype in C++. |
Operators | Operators in C++ can not be applied on character array. | You may apply standard C++ operator on the string. |
Boundary | Array boundaries are easily overrun. | Boundaries will not overrun. |
Access | Fast accessing. | Slow accessing. |
3.Write a program to reverse a linked list?
Ans: Below is the program to reverse a linked list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include<stdio.h> #include<stdlib.h> struct list { int month; struct list *next; }; typedef struct list node; void init(node * record) { record->next = NULL; } void addnode(node * record, int d) { node *fresh; fresh = (node *) malloc(sizeof(node)); fresh->month = d; fresh->next = record->next; record->next = fresh; } void print(node * record) { node *temp; temp = (node *) malloc(sizeof(node)); for (temp = record->next; temp; temp = temp->next) printf(" %d", temp->month); } void reverse(node * record) { node *temp; node *temp1; node *temp2; temp = (node *) malloc(sizeof(node)); temp1 = (node *) malloc(sizeof(node)); temp2 = (node *) malloc(sizeof(node)); temp = record; temp1 = temp->next; temp2 = temp1->next; temp->next->next = NULL; while (temp2 != NULL) { temp = temp1; temp1 = temp2; temp2 = temp1->next; temp1->next = temp; } record->next = temp1; } int main(void) { node *start; node *start1; start = (node *) malloc(sizeof(node)); init(start); int i = 0; for (i = 10; i >= 0; i--) addnode(start, i); print(start); reverse(start); printf("n"); print(start); return 0; } |
4. How will you change the value of a constant variable in C?
Ans: Constant can be changed by using the pointer. Initialize a pointer to point to the value of a and then change the value using the pointer. See the below c program.
1 2 3 4 5 6 7 8 9 10 11 | #include <stdio.h> int main() { const int a = 5; printf("%d",a); int *k = (int *)&a; *k = 10; printf("%d",*k); printf("%d",a); return 0; } |
6. What are the basic data types associated with C?
Ans: Following are the basic data types associated with C:
7. What is indirection?
Ans: If you have defined a pointer to a variable or any memory object, there is no direct reference to the value of the variable. This is called indirect reference. But when we declare a variable it has a direct reference to the value.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.