Question

I have already posted this before and I am looking at different answer. using C outputs...

I have already posted this before and I am looking at different answer. using C
outputs and comments will be appreciated.


QUESTION: Write a function to check if a machine uses little endian or big endian notation.The basic structure of the program is givenbelow.
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: •0if the architecture is "Little Endian" •1if the architecture is "Big Endian".

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<stdio.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. The function should return 0 if the architecture is "Little Endian" and return 1 if the architecture is "Big Endian". */
int checkEndianess ()
{

}

Please note that if you implement the function checkEndianess in a separate file other than the file containing the main function, you need to declare this function as an extern in the file containing the main function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int checkEndianess ();

int main(void) {
        int check;
        
        check = checkEndianess();
        
        if (check)
                printf("machine is big endian.\n");
        else
                printf("machine is little endian.\n");

}

int checkEndianess ()
{
        // Declaring a unsigned character pointer, because dereferencing 
        // the character pointer can give the value at particular memory 
        // without the signed notation.
        unsigned char *ptr;
        
        // variables to 
        int x, i, bigEndian;
        
        x = 0x12345678;
        
        // Get the address of the first byte of x.. int is 4 byte. &x makes ptr point to first byte only.
        ptr = (unsigned char *)&x;

        /*
        The Big Endian machine stores the Lowest byte of the numbers on the higher memory locations, and highest byte on the lower memory locations.. 
        So if 0x12345678 need to be stored in big endian machine starting from address 100, then 
        location 100 contains: 12
        location 101 contains: 34
        location 102 contains: 56
        location 103 contains: 78       
        */
        // x >> 24, Shifts the bytes to left side on user input integer. 
        // So we basically compare the value of first memory location with the 
        // value of left 8 bits of the x.
        // similarly for other bytes.
        
        bigEndian = (*ptr == (unsigned char)(0xff & (x >> 24))) &&
        (*(ptr + 1) == (unsigned char)(0xff & (x >> 16))) &&
        (*(ptr + 2) == (unsigned char)(0xff & (x >> 8))) &&
        (*(ptr + 3) == (unsigned char)(0xff & x));

        return bigEndian;
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
I have already posted this before and I am looking at different answer. using C outputs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming...

    Write a text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer numbers. The program will call one function to determine if all the numbers on the main diagonal are the same.The function is called checkdiag (int checkdiag (int matrix[][100], int...

  • Write a C++ program that will count the number of words and vowels in a sentence....

    Write a C++ program that will count the number of words and vowels in a sentence. Create a bool function called isVowel that accepts a character as a parameter and returns a true if it’s a vowel (aeiou) and false otherwise. Create an int function named countVowels that will accept a string variable as a parameter and will return the number of vowels in it. Call the isVowel function as part of this process. Write an int function named countWords...

  • My module page is correct but I don't believe I am using the main() function properly....

    My module page is correct but I don't believe I am using the main() function properly. One of my .py files is supposed to contain my definitions, which is the module file. My second .py file is supposed to properly call a main function that does what my program asks it to: First name, last name, age, and respond based off of input. For some reason I am struggling with using the main() function so that my program works. Any...

  • Using C programming

    Write another function called checkdiag2 (int checkdiag2 (int matrix[][100], int size)) that will return 1 if all the numbers on the antidiagonal are the same and 0 otherwise.Rewrite your main program to use 2D dynamic allocation instead for your array. Your array will have rows and columns depending of the first integer read from the file.Call your function from the main program. A typical report would look like The matrix is 8x8 and all the numbers on the antidiagonal are the same.Check your...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • Using C Write a function that takes two ints and returns the minimum (smallest) of those...

    Using C Write a function that takes two ints and returns the minimum (smallest) of those two ints. Include a function prototype. In main, call the function with two int inputs of your choice and print the returned value to the output window. The function should not print, just return the smallest number (int). If the numbers are the same, the minimum is just the number. Please use notes to explain, thanks!

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT