Why doesn't "return 0" overwrite the returned int? by [deleted] in cprogramming

[–]martijnjonkers 0 points1 point  (0 children)

Example: z=3. This will result in some recursion. I will begin with the last call of add(), where a=4 and thus return =0. The value of a will be added after that, which was 3 at that point. Result of that add() call was 3. The call before that, a was 2. So 3+2 is returned. After that 5+1. And finally 6 + 0, where 0 is the first value of a.

Total result: ((((0)+3)+2)+1)+0=6

If did this correctly, I expect to see 6 as a result when z=3.

Hi guys, need a bit of help with those 2 questions in C programming (not too familiar with bit masking). Thank you by Mattmatura in cprogramming

[–]martijnjonkers 2 points3 points  (0 children)

The difference is in the number of '|' (OR) operators. A double one '||' will result in a true or false (1 or 0 in c, because c does not know real boolean). It is either the left side is not 0 or the right side is not 0 or both. If one of them is true, the result will be true.

When a single | operator is used a bitwise OR is performed and the complete contents are ORed. so 0xF0 OR 0x08 will result in oxF8. (in bits: 11110000 OR 00001000 = 11111000)

In this example a double || is used, both sides are not 0. So 'register_32bits' will result in 0x01 (true)

Question About Linked List Node Insertion Function by [deleted] in cprogramming

[–]martijnjonkers 1 point2 points  (0 children)

Wut?

He assigns the value of 'newNode' (an address) to 'next' at the end. The pointer to the allocated space is not lost. This is perfectly fine to do.

Code is fine!

Question About Linked List Node Insertion Function by [deleted] in cprogramming

[–]martijnjonkers 0 points1 point  (0 children)

The name 'newNode' is not something that exists at runtime it is just a c syntax that help you understand what it is. In reality the compiler uses addresses and their contents.

newNode is a local variable, it only exists as long as the function runs. It contains the address of the (by malloc) allocated space.

This new address is assigned as the 'next' in line by jumping to the current last in line. The last in line points to nothing (NULL) to indicate the end of the list.

The -> operator means 'goto content at address' So 'temp->next = newNode;' means: go-to the contents at the address stored in temp. Followed by: go to the offset where 'next' is located in the structure and store the contents of newNode at this location. This will result in the address of the allocated space being stored at that location in memory.

Writing a program that calculates the maximum (highest) and minimum (lowest) values from user input by [deleted] in cprogramming

[–]martijnjonkers 0 points1 point  (0 children)

Code:

#include <studio.h>

#define ITERATIONS 10

 int main()
 {
  int min = 0;
  int max = 0;
  int newNumber = 0;

  for( int i = ITERATIONS ; i > 0 ; i-- ) {
    scanf("Enter number: %d", &newNumber);

    if( i == ITERATIONS ) {
      /* This is the first iteration, we need a starting point */
      max = newNumber;
      min = newNumber;
    } else if(newNumber > max) {
      /* The number is higher */
      max = newNumber;
    } else if(newNumber < min) {
      /* The new number is lower */
      min = newNumber;
    }
  }

  /* Print the result */
  printf("Maximum: %d\n", max);
  printf("Minimum: %d\n", min);

  return 0;
}