Question

Prints out the size of (there’s a hint, by the way) variables with the following C...

  1. Prints out the size of (there’s a hint, by the way) variables with the following C data types – int, long, unsigned, long long, char, float and double. This is how many bytes of memory a variable of that type occupies on this machine, using your chosen compiler. (Xcode)

  2. Answer the following question: Does adding the static keyword to the declaration of these variables in your program change their size?

  3. Answer the following question: What is the largest number and smallest numbers that can be represented in the variables in your program with the following C data type – int, long, long long, unsigned, char. This is asking about the range of values, not just the size of the variable. Of course, you could Google this, but I strongly recommend that you construct a program that prints out these values, to ensure that they really are the limits of your particular compiler installation. Usually the needed values, such as MAX_INT, can be gotten from the limits.h file supplied with your compiler/IDE.

  4. Answer the following question: Which of these types allow negative numbers to be meaningfully assigned as values? (int, unsigned, char, long)

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

1.

#include <iostream>

using namespace std;

int main() {
    cout << sizeof(int) << endl; // 4
    cout << sizeof(long) << endl; // 8
    cout << sizeof(unsigned) << endl; // 4
    cout << sizeof(long long) << endl; // 8
    cout << sizeof(char) << endl; // 1
    cout << sizeof(float ) << endl; // 4
    cout << sizeof(double) << endl; // 8
}

Does adding the static keyword to the declaration of these variables in your program change their size? => No

==========

#include <iostream>
#include <limits.h>
#include <cfloat>
#include <iomanip>

using namespace std;

int main() {
    cout << fixed << showpoint;
    cout << setprecision(4);    


    cout << INT_MIN << " " << INT_MAX << endl;
    cout << LONG_MIN << " " << LONG_MAX << endl;
    cout << 0 << " " << UINT_MAX << endl;
    cout << LLONG_MIN << " " << LLONG_MAX << endl;
    cout << CHAR_MIN << " " << CHAR_MAX << endl;
    cout << FLT_MIN << " " << FLT_MAX << endl;
    cout << DBL_MIN << " " << DBL_MAX << endl;
}

run this and you will get to see the ranges.

=====================================

every non-unsigned value allows negative numbers.

Add a comment
Know the answer?
Add Answer to:
Prints out the size of (there’s a hint, by the way) variables with the following C...
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: Implement the function ranges that takes no input and prints the ranges (i.e., the...

    Using C: Implement the function ranges that takes no input and prints the ranges (i.e., the minimum and maximum legal values) of the types char, short, int, long, and long long, both signed and unsigned. You should do this by printing appropriate constants defined in the header file /usr/include/limits.h (this is why hw1.c starts with #include <limits.h>). This should print something like: signed char minimum value: -128 maximum value: 127 unsigned char minimum value: 0 maximum value: 255 signed short...

  • 2. Give the minimum size of each of the following C++ types, assuming that char values occupy one...

    2. Give the minimum size of each of the following C++ types, assuming that char values occupy one byte, short values occupy two bytes, int and float values occupy four bytes, double and long values occupy eight bytes, and pointers occupy four bytes. a) union u type ( double a[3]; int *b; char c[10] b) struct s1type float *d [2]; long e[4] char f[6] short *gi c) struct s2 type ( s1_type s; u type u [2]; int *h[3]; short...

  • ***The following is to be written in C:**** ****The following is the sizeof.c program that needs...

    ***The following is to be written in C:**** ****The following is the sizeof.c program that needs to be modified:**** ****Section B11 the question asks to refer to:**** Modify the sizeof.c program (in the sizeof folder on github) and show the data range (min and max) in addition to the size of the data types in a nice table (print one line for each data type). Refer to section B11 in your textbook (page 257) for getting min and max values...

  • Give the minimum size of each of the following C data structures, assuming that char values...

    Give the minimum size of each of the following C data structures, assuming that char values occupy byte, int and float values occupy four bytes, double values occupy eight bytes, and pointers occupy bytes. (a) char str[] = "Curly": (b) double *a[4][4]: (c) char *str[3] = {"Moe", "Larry", "Curly"}: (Include the space occupied by the string literals in your answer.) (d) union { int a: char b: float c[4]: }u: (e) struct { int a: char b: float c[4]: }s:...

  • C PROGRAM The following is code prints the current activation record number, the memory address of...

    C PROGRAM The following is code prints the current activation record number, the memory address of the current array, followed by the estimated size of the current activation record as a distance between the current array address and the array address from the previous activation record. I need it to run until a segmentation fault occurs and also it must print the estimated size of the runtime stack as a product of the size of current activation record and the...

  • Requirement Write pseudocode and translate it to ONE C-program for each the following problems. In your...

    Requirement Write pseudocode and translate it to ONE C-program for each the following problems. In your pseudocode and C-program, use only what you have learned in this class so far. (Menu) Design a menu for question 2 and 3. So far, you use one program to solve all lab questions. But some of you may feel awkward when you want to demo/test only one lab question. To overcome that, your program should show a menu so that the users of...

  • Create a program using Visual Studio 2017 called dataExercise.c that declares the following variables and displays...

    Create a program using Visual Studio 2017 called dataExercise.c that declares the following variables and displays their values: 1. Declare a character variable with value 'a', and display the character using printf with a %c format. Then add a second printf that displays the character with a %d format. 2. Declare a short int variable with a value set to the maximum value of a short int (the maximum value for whatever machine I happen to use to grade your...

  • WRITE THE FOLLOWING IN C (NOT C++ OR JAVA) Problem 6A (5 points 10.8 (Using Unions) Create union integer with members...

    WRITE THE FOLLOWING IN C (NOT C++ OR JAVA) Problem 6A (5 points 10.8 (Using Unions) Create union integer with members char c, short s, int i and long b. Write a program that inputs values of type char, short, int and long and stores the values in union variables of type union integer. Each union variable should be printed as a char, a short, an int and a long. Do the values always print correctly? Sample input/output. To print...

  • C PROGRAM When you print out the old and new bitsets, which are of type unsigned...

    C PROGRAM When you print out the old and new bitsets, which are of type unsigned char, please use the %p control character in the printff statement rather than %x or %d or %c. The compiler will complain, but we don't always listen to them anyway. The difference is it will print the bitset out in hex with a preceeding %0x. unsigned char bitset = 0x14 ; printf("Bitsrt is %p\n", bitset) ; results in Bitset is 0x14, which is what...

  • Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained...

    Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained in a1q4.c and perform the following tasks in your program’s main() function: (8 marks) • Declare and initialize at least one variable of these types: char, int, unsigned int, float, double and long double. When initializing your variables, ensure the constants you use are of the same type as your variable. See the “Constants” section of Topic 7 for more information. • Use printf(3)...

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
ADVERTISEMENT