Question: QUESTION: Write a function to check if a machine uses little endian or big endian notation. The basic structure of the program is given. You
QUESTION: Write a function to check if a machine uses little endian or big endian notation.
The basic structure of the program is given. You are expected to write the function checkEndianess(), complete the main function implementation to print the value returned by the function and print if the machine is Little Endian or Big Endian, compile and run the program.
The function checkEndianess() should return the following values:
0 if the architecture is "Little Endian"
1 if the architecture is "Big Endian".
The C code containing the function checkEndianess() written by you. The File should be named as Endian.c. Please do not turn is main file.
KEY POINTS:
1. Make sure to call the function name correctly (same function name as given in this document) in the main program.
2. Comment out all non-C statements.
3. Follow all instructions mentioned above.
We will check your function by calling the function in the following main function.
#include sdtio.h
int main()
{
int check; // Variable: "check" will hold the value returned by the function
//Call the function to check the Endianness here
// This is where your function will be called to return endianness
check = checkEndianess ();
// TODO for TAs and Students who will use this function to check the student function
// TODO Write the code to check the value returned by the function
// TODO print if the system uses little Endian or big Endian notation
return(0);
}
/* Function "checkEndianess" checks the Endianness of the machine on which this program is executed. It takes an integer value as input and returns an integer value.
The function should return 0 if the architecture is "Little Endian" and return 1 if the architecture is "Big Endian". */
int checkEndianess ()
{
}
//Program Ends Here
SUPPLEMENTARY MATERIAL READING
UNIONS:
A union is a special data type available in C that enables you to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multi-purpose. The union statement defines a new data type, with more than one member for your program. The format of the union statement is as follows:
union [union tag]
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
The union tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the union's definition, before the final semicolon, you can specify one or more union variables but it is optional. An example is shown below:
union Data
{
int i;
float f;
char str[20];
} data;
A variable of Data type can store an integer, a floating-point number, or a string of characters. This means that a single variable ie. Same memory location can be used to store multiple types of data. The memory occupied by a union will be large enough to hold the largest member of the union. For example, in above example Data type will occupy 20 bytes of memory space because this is the maximum space, which can be occupied by character string.
To access any member of a union, we use the member access operator (.). The member access operator is coded as a period between the union variable name and the union member that we wish to access.
#include stdio.h
#include string.h
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %d ",data.i);
data.f = 220.5;
printf( "data.f : %f ", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s ", data.str);
return 0;
}
STRUCTURES:
Structure is another user defined data type available in C programming, which allows you to combine data items of different kinds. To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program. The format of the struct statement is shown below:
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. An example is shown below:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access.
Bit Fields allow the packing of data in a structure. This is especially useful when memory or data storage is at a premium. Typical examples include:
Packing several objects into a machine word. e.g. 1 bit flags can be compacted.
Reading external file formats -- non-standard file formats could be read in. E.g. 9 bit integers. C allows us do this in a structure definition by putting :bit length after the variable.
For example:
struct packed_struct
{
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
} pack;
Here, the packed_struct contains 6 members: Four 1 bit flags f1..f3, a 4 bit type and a 9 bit my_int.C automatically packs the above bit fields as compactly as possible, provided that the maximum length of the field is less than or equal to the integer word length of the computer. If this is not the case then some compilers may allow memory overlap for the fields whilst other would store the next field in the next word.
POINTERS:
Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. Every variable is a memory location and every memory location has its address defined, which can be accessed using ampersand (&) operator, which denotes an address in memory. A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. The general form of a pointer variable declaration is:
type *var-name;
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries. To check for a null pointer you can use an if statement as follows:
if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */
Some key concepts:
Concept Description
C - Pointer arithmetic => There are four arithmetic operators that can be used on pointers: ++, --, +, -
C - Array of pointers => You can define arrays to hold a number of pointers.
C - Pointer to pointer => C allows you to have pointer on a pointer and so on.
Passing pointers to functions in C => Passing an argument by reference or by address both enable the passed argument to be changed in the calling function by the called function.
Return pointer from functions in C => C allows a function to return a pointer to local variable, static variable and dynamically allocated memory as well.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
