Lab 04
Lab 04
Lab 04
Programs written in C are compiled with the gcc compiler in a Linux environment. A C file
should have extension .c e.g. test.c. The general format to compile a C program by gcc compiler
is given below:
$ ./first
40
Example 1
#include <stdio.h>
int main()
{
printf(“\n\t One OS to Rule Them All !! \n”);
return 0;
}
4.4 Step-by-Step Compilation Using gcc:
The compilation is a multi-stage process involving several tools, including the GNU Compiler
itself (gcc), the GNU Assembler as, and the GNU Linker id. The sequence of commands
executed by a single invocation of GCC consists of the following stages:
$ gcc -S first.i
However, we didn’t specify a ‘–o’ option. This is because gcc would automatically name the output
file ‘first.s.’
41
$ gcc first.o –o first
This links the object file ‘first.o’ to the C standard library and produces an executable file ‘first.’
4.5 C Output
In C programming, printf() is one of the main output function. The function sends formatted
output to the screen. For example
Example : C output
#include <stdio.h>
int main()
{
//Displays the string inside quotations
printf("C Programing");
return 0;
}
All valid C programs must contain the main() function. The code execution begins
from the start of the main() function.
The printf() is a library function to send formatted output to the screen. The
function prints the string inside quotations.
To use printf() in our program, we need to include stdio.h header file using
the #include <stdio.h> statement.
The return 0; statement inside the main() function is the "Exit status" of the
program. It's optional.
4.6 C Input
In C programming, scanf() is one of the commonly used functions to take input from the
user. The scanf() function reads formatted input from the standard input, such as
keyboards.
Example 2
#include <stdio.h>
int main()
{
int testInteger;
printf("Ënter an integer");
scanf("%d", &testInteger);
printf("Number = %d", testInteger);
return 0;
}
42
Here, we have used %d format specifier inside the scanf() function to take int input
from the user. When the user enters an integer, it is stored in the testInteger variable.
Notice that we have used &testInteger inside scanf(). It is because &testInteger gets
the address of testInteger, and the value entered by the user is stored in that address.
%d for int
%f for float
%lf for double
%c for char
Here's a list of commonly used C data types and their format specifiers.
43
Example 3
#include <stdio.h>
int main()
{
int a;
float b;
printf("Ënter an integer and then a float");
//Taking multiple inputs
scanf("%d %f",&a,&b );
printf("You entered %d and %f", a , b);
return 0;
}
4.9 C malloc()
The name "malloc" stands for memory allocation. The malloc() function reserves a
memory block of specified bytes. And it returns a pointer of the void, which can be casted
into pointers of any form.
The above statement allocates 400 bytes of memory. It's because the size of the float is 4
bytes. And the pointer ptr holds the address of the first byte in the allocated memory.
4.10 C free()
Dynamically allocated memory created with malloc() doesn't get freed independently.
You must explicitly use free() to release the space.
44
This statement frees the space allocated in the memory pointed by ptr.
Example 4:
In this code, we dynamically allocate memory using the malloc() function based on the
integer value entered by the user. The allocated memory is released using free()function at
the end of the code execution.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int i;
int *ptr;
int sum=0;
int n;
printf("Ënter number of elements:");
scanf("%d", &n);
ptr = (int*)malloc(n * sizeof(int));
//If memory cannot be allocated
if(ptr == NULL){
printf("Error! memory not allocated,");
exit(0);
}
printf("Enter elements: ");
for(i=0; i<n; ++i){
scanf("%d", ptr + i);
sum +=*(ptr + i);
}
printf("Sum = %d", sum);
printf(“\n”);
//Deallocating the memory
free(ptr);
return 0;
}
45
Here, we have dynamically allocated the memory for n number of int.
46
Lab Task
Write a C program that:
Prints a welcome message and then prompts the user to enter an integer (e.g., age) and a float (e.g., height
in meters) and displays the entered values back to the user with appropriate labels.
Extend the program to Prompt the user to enter the number of elements for a float array (e.g., temperatures
recorded over a week). Next you will dynamically allocate memory for this array using malloc() and
perform a NULL pointer check after allocation. If the allocation fails, display an error message and exit the
program gracefully. Prompt the user to enter values to populate the array. Calculate and display the
maximum and minimum values from the array and then Free the dynamically allocated memory at the end.
Ensure the program includes a check for NULL pointers after every dynamic memory allocation
Provide screenshots for each stage of compilation, ie Preprocessing, compilation, assembly and linking.