0% found this document useful (0 votes)
240 views

C Practical File

The document discusses various topics in C programming including arrays, pointers, strings, and dynamic memory allocation. Arrays allow storing of homogeneous data and are used to store lists of items. Pointers store the address of another variable and are used for dynamic memory allocation. Strings in C are arrays of characters terminated by a null character. Dynamic memory allocation functions like malloc(), calloc(), and realloc() allow allocating and resizing memory blocks at runtime.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

C Practical File

The document discusses various topics in C programming including arrays, pointers, strings, and dynamic memory allocation. Arrays allow storing of homogeneous data and are used to store lists of items. Pointers store the address of another variable and are used for dynamic memory allocation. Strings in C are arrays of characters terminated by a null character. Dynamic memory allocation functions like malloc(), calloc(), and realloc() allow allocating and resizing memory blocks at runtime.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C Programing

Array – Array is collectoo of homogeoous ( similar data type) data type

Example – where array are used to store employee or studeot oame.

To store marks of studeot.

To store list of oumber or character etc.

Array Declaratoo

Types of array

ooe dimeosiooal array two dimeosiooal array mult dimeosiooal


array

Iotalizatoo Of Array
Data_type array_oame = array_size
// Program to find the average of n (n < 10) numbers
using arrays
1.
2. #include <stdio.h>
3. int main()
4. {
5. int marks[10], i, n, sum = 0, average;
6. printf("Enter n: ");
7. scanf("%d", &n);
8. for(i=0; i<n; ++i)
9. {
10. printf("Enter number%d: ",i+1);
11. scanf("%d", &marks[i]);
12. sum += marks[i];
13. }
14. average = sum/n;
15.
16. printf("Average = %d", average);
17.
18. return 0;
19. }
C Programing

Output

Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39

// C Program to calculate Average Usiog Arrays


1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, i;
6. float num[100], sum = 0.0, average;
7.
8. printf("Enter the numbers of elements: ");
9. scanf("%d", &n);
10.
11. while (n > 100 || n <= 0)
12. {
13. printf("Error! number should in range of (1 to
100).\n");
14. printf("Enter the number again: ");
15. scanf("%d", &n);
16. }
17.
18. for(i = 0; i < n; ++i)
19. {
20. printf("%d. Enter number: ", i+1);
21. scanf("%f", &num[i]);
22. sum += num[i];
23. }
24.
25. average = sum / n;
26. printf("Average = %.2f", average);
27.
28. return 0;
29. }
C Programing

Output

30. . Enter number: 67.5


31. 3. Enter number: -45.6
32. 4. Enter number: 20.34
33. 5. Enter number: 33
34. 6. Enter number: 45.6
35. Average Enter the numbers of elements: 6
36. 1. Enter number: 45.3
37. 2= 27.69

Poioters – Poioters is a variable that stored/poiot the


address of aoother variable. Io C programiog poioters
is used to allocate memory dyoamically that is at ruo
tme. The poioters data type as iotccharcloatcdouble
etc .
Poioter io detail :
 Poioter Airthmatc – There are four airthmatc
operator that cao be used io poioter ++c-c+
 Array of poioter – You cao defoe array to defoe
to hold a oumber of poioter.
 Poioter to poioter – C allow you to have poioter
oo a poioter aod so oo.
 Passiog poioter to fuoctoo io c – Passiog
argumeot by refreoce or by address eoable the
C Programing

passed argumeot to be chaoged io the calliog


fuoctoo by the called fuoctoo.
//Access Array Elements Using Pointers
 #include <stdio.h>

 int main()
 {
 int data[5], i;
 printf("Enter elements: ");

 for(i = 0; i < 5; ++i)
 scanf("%d", data + i);

 printf("You entered: \n");
 for(i = 0; i < 5; ++i)
 printf("%d\n", *(data + i));

 return 0;
 }

Output

Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4
C Programing

Striog - Io c programiog array of character is called


striog. A striog is termioated by oull/0 character
Declaratoo of striog – Striog are declare io similar
maooer as array. Ooly difereoce is thatc striogs are
char type.
Striog Library Fuoctoo
I.
Strcpy() copies striog 2 ioto striog 1
II.
Strcat() coocateoates striog s2 ooto the eod of s1
III.
Strleo() returo the leogth of s1
IV.Strecmp() its compare s1 to s2
V. Strchr() this is character strio
// C Program To Read Striog
1. #include <stdio.h>
2. int main()
3. {
4. char name[20];
5. printf("Enter name: ");
6. scanf("%s", name);
7. printf("Your name is %s.", name);
8. return 0;
9. }

Output

Enter name: Dennis Ritchie


Your name is Dennis.
C Programing

C strlen() function
1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char a[20]="Program";
6. char b[20]={'P','r','o','g','r','a','m','\0'};
7. char c[20];
8.
9. printf("Enter string: ");
10. gets(c);
11.
12. printf("Length of string a = %d \n",strlen(a));
13.
14. //calculates the length of string before null charcter.
15. printf("Length of string b = %d \n",strlen(b));
16. printf("Length of string c = %d \n",strlen(c));
17.
18. return 0;
19. }

Output

Enter string: String

Length of string a = 7

Length of string b = 7

Length of string c = 6

Dyoamic Memory Allocatoo ( Poioter )


C Laoguage required the oumber of elemeot io
ao array to be specifed at compile tme. The
process of allocatog memory at ruo tme is koow
Das dyoamic allocatoo. Dyoamic data structar
C Programing

provide lexibility io addiog deletog aod


rearraogiog data at ruo tme. The dyoamic
memory to realese uowaoted space at ruo tme.
Fuoctoo Task
Malloc Allocate request size of
Byte and return a pointer to
the frst byte of the allocate space
Calloc() Allocaton space for an
array of element initaliie then tto 0 and
then return pointer to the memory
Realloc() Chaoge the size of previous
allocate space

malloc() and free()

This program calculates the sum of n numbers entered by the


user. To perform this task, memory is dynamically allocated
using malloc(), and memory is freed using free() function.
C Programing

1. include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. int n, i, *ptr, sum = 0;
7.
8. printf("Enter number of elements: ");
9. scanf("%d", &n);
10.
11. ptr = (int*) malloc(n * sizeof(int));
12. if(ptr == NULL)
13. {
14. printf("Error! memory not allocated.");
15. exit(0);
16. }
17.
18. printf("Enter elements: ");
19. for(i = 0; i < n; ++i)
20. {
21. scanf("%d", ptr + i);
22. sum += *(ptr + i);
23. }
24.
25. printf("Sum = %d", sum);
26. free(ptr);
27. return 0;
28. }

calloc() and free()

This program calculates the sum of n numbers entered


by the user. To perform this
task, calloc() and free() is used.

1. #include <stdio.h>
2. #include <stdlib.h>
C Programing

3.
4. int main()
5. {
6. int n, i, *ptr, sum = 0;
7. printf("Enter number of elements: ");
8. scanf("%d", &n);
9.
10. ptr = (int*) calloc(n, sizeof(int));
11. if(ptr == NULL)
12. {
13. printf("Error! memory not allocated.");
14. exit(0);
15. }
16.
17. printf("Enter elements: ");
18. for(i = 0; i < n; ++i)
19. {
20. scanf("%d", ptr + i);
21. sum += *(ptr + i);
22. }
23.
24. printf("Sum = %d", sum);
25. free(ptr);
26. return 0;
27. }

realloc()

1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. int *ptr, i , n1, n2;
7. printf("Enter size of array: ");
8. scanf("%d", &n1);
9.
10. ptr = (int*) malloc(n1 * sizeof(int));
11.
12. printf("Addresses of previously allocated memory: ");
13. for(i = 0; i < n1; ++i)
14. printf("%u\n",ptr + i);
15.
16. printf("\nEnter new size of array: ");
17. scanf("%d", &n2);
18. ptr = realloc(ptr, n2 * sizeof(int));
19.
20. printf("Addresses of newly allocated memory: ");
21. for(i = 0; i < n2; ++i)
22. printf("%u\n", ptr + i);
C Programing

23. return 0;
24. }

When you run the program, the output will be:

Enter size of array: 2


Addresses of previously allocated memory:26855472
26855476

Enter new size of array: 4


Addresses of newly allocated memory:26855472
26855476
26855480
26855484

You might also like