Array
An array is a variable that can store multiple values. For example, if you want to store 100
integers, you can create an array for it.
How to declare an array?
dataType arrayName[arraySize];
int data[100];
Program to find the average of n numbers using arrays
#include <stdio.h>
#include<conio.h>
void main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
getch();
}
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
String
#include <stdio.h>
#include<conio.h>
void main()
{
char s[10];
int count = 0;
printf("Input a string\n");
gets(s);
while (s[count] != '\0')
count++;
printf("Length of the string: %d\n", count);
getch();
}
output
Computer
Length of string 8
unction Work of Function
strlen() computes string's length
strcpy() copies a string to another
strcat() concatenates(joins) two strings
strcmp() compares two strings
strlwr() converts string to lowercase
strupr() converts string to uppercase
strrev() Reverse the string
Pointer
C Pointers
Pointers (pointer variables) are special variables that are used to store addresses rather than
values.
int* p;
#include <stdio.h>
#include<conio.h>
void main () {
int var1;
char var2[10];
printf("Address of var1 variable: %x\n", &var1 );
printf("Address of var2 variable: %x\n", &var2 );
getch();
}
Output
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6