C Programming Questions and Answers
1. What is the difference between arrays and structures?
Array: Collection of elements of the same data type.
Structure: Collection of elements of different data types grouped together.
2. Differentiate between arrays and list of variables.
Array: Uses indices to store multiple values in a single variable.
List of variables: Requires separate declarations for each variable.
3. Write a program to calculate the mean of an array of integers.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}, sum = 0;
float mean;
for (int i = 0; i < 5; i++) {
sum += arr[i];
mean = sum / 5.0;
printf("Mean = %.2f\n", mean);
return 0;
4. How do you access a member of a structure?
Use the dot (.) operator with structure variable.
Example:
struct student { int roll; char name[20]; };
struct student s;
s.roll = 101;
C Programming Questions and Answers
5. Write C program to concatenate two strings without using inbuilt function.
#include <stdio.h>
int main() {
char str1[100] = "Hello ", str2[] = "World";
int i = 0, j = 0;
while (str1[i] != '\0') i++;
while (str2[j] != '\0') {
str1[i++] = str2[j++];
str1[i] = '\0';
printf("Concatenated string: %s\n", str1);
return 0;
6. State & explain Array. State different ways of initialization of the same.
Array: Collection of similar data types stored in contiguous memory.
Initialization:
1. int a[3] = {1, 2, 3};
2. int a[] = {1, 2, 3};
3. int a[3]; a[0]=1; a[1]=2; a[2]=3;
7. Illustrate String. State different ways of initialization of the same.
String: A 1D array of characters ending with a null character '\0'.
Initialization:
1. char str[] = "Hello";
2. char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
3. char str[20]; gets(str);