Chapter - 9: Arrays

Point out the errors, if any, in the following program segments:


B
Sections
1
Exercises

(a)

/* mixed has some char and some int values */
#include<stdio.h>
int char mixed[100];
int main()
{
	int a[10], i;
	for (i = 1; i <= 10; i++)
	{
		scanf("%d", a[i]);
		printf("%d", a[i]);
	}
	return 0;
}

Error: 
1. int followed by char  is illegal
2. No ‘&’ sign in scanf function


(b)

#include<stdio.h>	
int main()
{
	int size;
	scanf("%d", &size);
	int arr[size];
	for (i = 1; i <= size; i++)
	{
		scanf("%d", arr[i]);
		printf("%d", arr[i]);
	}
	return 0;
}

Error: 
1. Index of the array must be a constant, "size" is a variable.
2. No ‘&’ sign in scanf function


(c)

#include<stdio.h>	
int main()
{
	int i, a = 2, b = 3;
	int arr[2 + 3];
	for (i = 0; i < a + b; i++)
	{
		scanf("%d", &arr[i]);
		printf("\n%d", arr[i]);
	}
	return 0;
}

Error: No any error

 

© 2020 Garbage Valuegarbage value logo