Difference between revisions of "CSC231 Mystery C Program with signed numbers"

From dftwiki3
Jump to: navigation, search
(Mystery Program)
(Mystery Program)
Line 28: Line 28:
 
#include <stdio.h>
 
#include <stdio.h>
  
main() {
+
int main() {
  
 
     short int x = ???? ;
 
     short int x = ???? ;
Line 38: Line 38:
 
     x = x+y;
 
     x = x+y;
 
     printf( "%d\n", x );
 
     printf( "%d\n", x );
 +
 +
    return 0;
 
}
 
}
  

Revision as of 07:59, 13 November 2014


Mystery Program

Figure out what the two variables x and y are initialized with when the program starts.

You may use this converter to help you out...

/* mystery.cpp
// D. T.
// mystery program
// To compile and run this program:
//
//     g++ mystery.cpp
//     a.out
//
// The output of the program is the following
//
// 32740
// -32756
//
// With what positive values were x and y initialized
// at the beginning of the program.  Explain why.
// (a short int contains 16 bits)
*/

#include <stdio.h>

int main() {

    short int x = ???? ;
    short int y = ???? ;

    x = x+y;
    printf( "%d\n", x );

    x = x+y;
    printf( "%d\n", x );

    return 0;
}


For reference, 215 = 32768.




















...