OS Lab 5
OS Lab 5
OS Lab 5
Output:
#include <stdio.h>
int main( int argc, char *argv [] )
{
printf(" \n Name of my Program %s \t", argv[0]);
if( argc == 2 )
{
printf("\n Value given by user is: %s \t", argv[1]);
}
else if( argc > 2 )
{
printf("\n Many values given by users.\n");
}
else
{
printf(" \n Single value expected.\n");
}
}
Process Environment
Program are invoked in a context through which the environment list
passed to it.
A standard environment list contains information like user's home
directory, terminal type, current locale, and so on. Format:
name=value
Name - upper case
The environment list is also an array of pointers pointing to the address
of a null-terminated string.
And the environment list can be accessed through a global variable
environ, which is defined as a pointer to a pointer to char
Process Environment
Error Handling
• Global Variable errno:
• When a function is called in C, a variable named as errno is automatically
assigned a value which can be used to identify the type of error that has been
encountered
• Its a global variable indicating the error occurred during any function call and
defined in the header file errno.h
Different values for errno mean different types of errors
Error Handling
• Below is a list of few different errno values and its corresponding meaning:
Error Handling
• perror():
• displays the string 's', followed by ':', and the error message associated with
errno
• errno:
• is an integer variable already defined for you in errno.h
• It is set to the latest/last error condition generated by your program
• If your program tries to do something that it isn't allowed to do, an error
condition is reached in such a case
• strerror():
• returns a pointer to the textual representation of the current errno value.
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main ()
{
FILE *fp;
OR
Structure in C #include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the structure
struct person {
• Suppose you want to access char name[50];
int citNo;
the salary of a person2 float salary;
}person1;
int main() {
// Assign values to the fields of person1
Output strcpy(person1.name, "George Orwell");
person1.citNo = 1984;
person1.salary = 2500.00;
• Keyword typedef
• We use the typedef keyword to create an alias name for data types. It
is commonly used with structures to simplify the syntax of declaring
variables
• This can make the code more readable and understandable, especially
in cases where the purpose of the data type is not immediately clear.
#include <stdio.h>
// Define an alias for int
typedef int Integer;
int main() {
// Declare variables using the typedef alias
Integer num1 = 10;
Integer num2 = 20;
// Perform arithmetic operations
Integer sum = num1 + num2;
Integer difference = num1 - num2;
// Print the results
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
return 0;
}