8

Lets say you have the following code: short myShort = 23948; byte myByte = (byte)myShort; mybyte will contain the value 140. Short is a 2-byte type and a byte is a single byte. When you cast from two bytes to one you're forcing the system to make things fit and one of the original bytes (the most significant) gets dropped and data is lost. What is left ...


6

This is an excellent question. Understanding this behaviour means knowing a bit about the history of the C language, and the design and development of CPUs. The short answer is that the size difference stems from the fact that older computers were much simpler than modern computers. In the early days of microcomputers before PCs (circa 1970), the CPUs at ...


2

Your question is unclear, but I'll take a shot at it. In a linked list, HEAD is usually a var name, not a data type. HEAD is usually a pointer or possibly a data structure that serves as the pointer to the first element in the linked list. In your code, node1 serves as a head. However, if you wanted to insert a new node at the beginning of the list, you'd ...


2

drand48 is a function that takes no arguments and returns a double. It's declared in stdlib.h. You should call it like drand48();


1

The code knows how long each structure element is, thanks to the definition in the header file, so it only needs to add the appropriate number of bytes to the starting address to get to anything. The elements are sequentially ordered, and that order never changes, nor are any other data fields placed in the sequence. For example, BITMAPINFOHEADER.biPlanes is ...


1

Having problems with this? temp_b /= (10 ^ 14); That would be because the ^ operator is not an exponent operator as it is in mathematics. It's the exclusive or operator. You might try a call to pow(). As for float issues, I didn't see any floats in the code at all. If this answers your question, please click on the check mark to accept. Let's keep up on ...


1

According to the man page, printf returns an int Return value Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings). This printf("plaintext:") returns 10 (number of characters printed). get_string expects a string (defined as char *) as it's argument. You should ...


1

you are right, in fact the printf function return value is as follows: Return Value On success, the total number of characters written is returned. If a writing error occurs, the error indicator (ferror) is set and a negative number is returned. If a multibyte character encoding error occurs while writing wide characters, errno is set to ...


1

int chopoffend(long long n) int in C is 4 bytes max, that is 2^32 -> 10 digits max


1

The problem is how numbers are stored. A computer operates with a binary or base-2 number system. The real world operates in a base-10 system. The only fractional numbers that can be stored with perfect accuracy are those where the denominator is a power of 2, like 1/2, 1/4, 3/4, 5/8, etc. Any other fractional part of a floating point number, i.e., the ...


1

You're overthinking this. A struct is not necessary here. Why not try storing your identifiers in an array instead?


1

INT02-C. Understand integer conversion rules Conversions can occur explicitly as the result of a cast or implicitly as required by an operation. Although conversions are generally required for the correct execution of a program, they can also lead to lost or misinterpreted data. Conversion of an operand value to a compatible type causes no ...


1

Looks like C is (internally) casting the constant 2 (or probably more correctly, the result of n - 2) as an int since that is the essential difference between the two printfs. You might search the internet-at-large for "C integer promotion rules". There are a lot of hits on the subject. This might be an interesting place to start.


1

you loop runs infinitely because num never reduces again when it is less than 9. but you else has problem. int mod(int *a, int num) { if (num/10 > 10) { int value = num % 10; printf("value is %i\n", value); *a = *a / 10; return value; } else { int value = num; // you can set it to ...


1

i = 32768; No reason at all why this wouldn't work. First, the size of int is 2^32 at the appliance, not 2^16. Second, even if you enter a higher value, it will never segfault. It will wrap around and go "back". ia = "0, 1, 2, 3, 4, 5" Not sure what you're trying to do there. First, you can't assign an array except upon initialization, that's valid for ...


1

quoting the C99 standard (6.5.3.4 - 20): C89, like K&R, defined the result of the sizeof operator to be a constant of an unsigned integer type. Common implementations, and common usage, have often assumed that the resulting type is int. Old code that depends on this behavior has never been portable to implementations that define the result to ...


1

if you assign a unique number to each dictionary entry and the numbers are assigned sequentially, they run from 0 to 24,999, as you said. Now, it's just a matter of converting 24,999 to binary. If you wanted to represent 11 different entries, you would need a binary number that can represent decimal 11. Decimal 11 = binary 1011. That's 4 bits. Similarly, ...


1

You don't even need to use string.h header file if you don't need to use string specific functions. Try to use following to figure it out by yourself: #include <stdio.h> int main(void) { char greeting[] = "Hello"; printf("%s", greeting); } You need to play bit more in C with strings. E.g. you can't initialize array of characters (string in ...


1

The type string is not a native type in C. It's defined by the CS50 library which you can use by #includeing the cs50.h header file and linking the library's binary file with -lcs50. The actual type that the CS50 library defines as string is char *. So using char * (without using the CS50 library) is equivalent to using string when using the CS50 library.


1

The screen width/height is measured in pixels, but the getX and getY can be any floating number. Modern graphics libraries tend to use floating point numbers because you can draw an image "not on a pixel boundary" by using anti-aliasing. It also allows it all to work if you change the size of the screen and you don't need to worry about that. Here's more ...


Only top voted, non community-wiki answers of a minimum length are eligible