8 votes
Accepted

What happens under the hood when performing a cast?

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 ...
user avatar
  • 483
7 votes
Accepted

sizeof(int) is 2 bytes in some computers, while it is 4 bytes in most modern ones. Why is it so?

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 ...
user avatar
  • 2,625
2 votes
Accepted

How does HEAD in linked list work?

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 ...
user avatar
  • 63.4k
2 votes
Accepted

Pset4 error: invalid operands to binary expression ('double' and 'double (*)(void)')

drand48 is a function that takes no arguments and returns a double. It's declared in stdlib.h. You should call it like drand48();
user avatar
  • 17.4k
1 vote
Accepted

Segmentation fault: Data types for key and entered text by user while program operated through CLI

The program does not validate the number of arguments passed to it, therefore if program is run with none or one argument, it will fail on one of the atoi calls. From the spec: If your program is ...
user avatar
1 vote
Accepted

Filter - Structures & Headers

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 ...
user avatar
  • 63.4k
1 vote
Accepted

ProblemSet1 - Another Credit.c issue - Can't convert the floating value of my variables into an integer value

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()....
user avatar
  • 63.4k
1 vote
Accepted

datatype string is malfunctioning in pset2 2020

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 ...
user avatar
1 vote
Accepted

Return type of printf in C

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 ...
user avatar
  • 5,172
1 vote
Accepted

pset1 credit.c – is this a data type conversion error?

int chopoffend(long long n) int in C is 4 bytes max, that is 2^32 -> 10 digits max
user avatar
  • 1,139
1 vote
Accepted

Limits of data type as explained in Week-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 ...
user avatar
  • 63.4k
1 vote
Accepted

Pset4 recover: can't seem to use struct to identify the jpg

You're overthinking this. A struct is not necessary here. Why not try storing your identifiers in an array instead?
user avatar
  • 3,035
1 vote
Accepted

C data types and format specifiers

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 ...
user avatar
1 vote

C data types and format specifiers

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 ...
user avatar
1 vote
Accepted

Pointers & Integer Arithmetic Question

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; ...
user avatar
  • 549
1 vote
Accepted

difference between string, pointer, array and int

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 ...
user avatar
  • 1,677
1 vote

return value of sizeof()

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, ...
user avatar
  • 17.4k
1 vote

Data compression: dictionary encoding

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 ...
user avatar
  • 63.4k
1 vote

string variable is not working

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) { ...
user avatar
1 vote

string variable is not working

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 ...
user avatar
  • 17.4k
1 vote
Accepted

Why using variable of double type for storing number of pixels? pset4

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 ...
user avatar
  • 18.3k

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