8

Variables can be declared and initialized in different ways. Variable Declaration There are two common methods for declaring variables. Suppose we want to declare two int variables, x and y Method 1: int x; // declare x int y; // declare y Method 2 int x, y; // declare x and y Notice that method 2 works only if x and y (and probably more if we want) ...


4

accessing a variable after it went out of scope is not defined in C. I assume by "erased" you mean "zero-filled" or something and, in this case, no, the memory is not zero-filled because why bother? what happens is that the memory that this variable used to identify is again available to use after this variable goes out of scope and it may be reused when ...


3

Simple fix. Look at the line: int letter = 'plaintext[i]' ; You are trying to put the integer (ASCII) value of a letter stored at plaintext[i] into the variable letter. The idea is right, but look at what you did. int letter = 'something' ; When you use the single quotes around something, it has to be a single character. If you had used double quotes, ...


3

at the end of the day there is really not much difference between the two Not really.. #defined CONSTANTS are simply "names" that are to be replaced (as in "copy&paste") by the preprocessor right before compiling. But in the end, they're just that: constants. There's no way a function or subroutine can access them and change them at runtime, because ...


2

That is not technically correct syntax. There are least two ways that this could be interpreted into working code, depending on the variable type of nf, which can either be a byte or some other larger value type. A common scenario is if nf refers to bytes, for example if nf is a char array or pointer, then the if statement needs a comparison for each byte, ...


2

The values 0xff, 0xd8, 0xe0 and 0xe1 are int values that are written in hexadecimal notation. Since this is for pset5 and, as the pset specification page states, a block is 512 bytes long, we can conclude that none of these values will exceed 0xff(hex) or 255(dec) since each value will be of size 1 byte (8 bits). As a result, we can use uint8_t (aka BYTE) ...


2

The problem is that the condition Lifes = 0 is only checked once, when the game starts. At that point in the game Lifes = 3, always, so the first condition is ignored because it is false, and the else statement is triggered, which leads to an infinite loop for the rest of the game. In order to fix your script, you must check if Lifes = 0 throughout the ...


2

Short answer is no. There's no way to do it the way you want because declaring variables of two different types requires a semicolon between the two declarations and the for loop can't have more than three sections. A quick fix would be declaring them before the for loop as follows char a, b; for (int i = 0, n = strlen(oldStr), a = 'a', b = 'b'; i < n; ...


2

Actually what you had written is not a binary search as in each move its not changing the size of array to half of original size. By the way mistake in your code is in line no. 19 and 30 as you are writing min = 0; max = n - 1; (values[mid])-1 which is wrong, it should be mid - 1 (line 19) (values[mid])+1 which is wrong, it should be mid - 1 (line 30)


2

What happens to memory varies from one programming language to another, and sometimes from one platform to another. With some languages, a variable is discarded from the program, such as when a variable goes out of scope. A good example is when you call a function in C. Vars are created inside the function and data is stored there, but later, you leave the ...


2

Yes there is. You can use scanf() from stdio.h. As an example imagine you want to input 2 + 2. An option can be the this: int a, b; char c; scanf("%d", &a); scanf(" %c", &c); scanf("%d", &b); Notice the space before the char placeholder, this is to ignore the spaces, don't needed if the input is 2+2, or the same in one line: scanf("%d %c%d",...


2

You need to concatenate the strings '%' + sometext + '%'


2

candidate_count is not declared in main. It's a global variable (declared outside of main) on line 20 in the distribution file plurality.c at the time of this writing. The definition of main starts later on line 26. That's why you're able to see candidate_count in other functions. argc, on the other hand, is a local variable that you can reference only ...


1

You forgot the : for :stockprice in VALUES (:userID, :symbol, :shares, :amount, stockprice). And to quote Cliff B: If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


1

Nope, can't do it that way. If you want to print multiple copies of a char based on a var's value, think for loop. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


1

It's a scope issue. You have declared outptr inside of a pair of curly braces - the curly braces that contain the code for the if statement's code block. As soon as the code exits that set of curly braces, the var outptr ceases to exist. If you had declared it before starting the do loop, it would persist through the rest of the program because the scope ...


1

Variables are declared in block-scope, available in the current and any of its inner blocks. Variables "shadow" outer ones of the same name. Function parameters work like variables declared for the whole function. By default they hold a copy of the value passed to the function (so main's a,b,c are different from multiply's a,b,c even if they have same ...


1

Clang is telling you exactly what is going wrong..."unused variable 's'". Your code gets input from the user and stores it in a string variable s, then does nothing with it. If you do something with s the error goes away.


1

When you write int min; int max; you're creating two int variables, but their value is undefined, it's the value that happened to be at that place in memory when they were created. You don't assign anything to them, so they remain in that undefined state. Then you write int mid = (max + min) / 2; calculating a value from two undefined values. Also, ...


1

The root of this problem lies in the order of operations. In C, postfix increment (++) binds more tightly than dereference (*). Thus, what the code *coins++; does is not to dereference coins and then increment the stored value; rather, it increments the pointer coins and then dereferences this new pointer. The compiler is complaining of the fact that the ...


1

Welcome to the point of this exercise! ;-) Floats are very rarely stored as exact values in a computer because they have to be converted from base 10 to base 2. If you were to print out the value stored in x, you would see that it is not 2.10, but something very close, like 2.0999999998735 (or something very similar), but ever so slightly less than 2.10. ...


1

#define a 1 Pre-processor will simply replace every occurrence of a in your source code with 1 before compilation. For instance, if you had a variable called myVar in your source file after preprocessing it will be changed to myV1r. One more time, this statement tells the preprocessor to literally replace every symbol a in your source file with a symbol 1. ...


1

Like almost everything in the programming, the use of recursion must be made in appropriate cases, you are right regarding the excessive memory usage, and the successive return statements are pending evaluating until the base case, which can be inefficient . However we can not exclude recursivity completely, in many cases the code for an iterative program ...


1

The difference between where a variable is declared is scope. Strictly for this discussion, consider main to be a function, even though it is a special one. A local variable is a variable declared inside of a function is local to that particular function. It exists only inside the function where it was declared and cannot be directly accessed by any ...


1

when you #define DIM_MIN 3, you are defining a constant not a variable. what's the difference? well, there are many differences actually, but probably the most important one is that, in case of a defined constant, you don't have a piece of memory that can be accessed by the name DIM_MIN during runtime (program's execution). so you can't change the value ...


1

You can use these values throughout your entire code, but you cannot change their values. Therefore it's a useful tool for creating global constants in your code. A bit more info: #define DIM_MIN 3 Is created by the preprocessor and does not use memory. While the usual int x = something will use memory. While you can use the value DIM_MIN in your code, ...


1

There are any number of ways to implement what you want. The most obvious would be to declare a struct element that is a 508 byte array. This assumes that you really want to implement a struct. But wouldn't it be far simpler to implement an array of 512 bytes instead of a struct? BYTE buffer[512]; You would be able to check the first 4 bytes ...


1

For set_int() function, it's not complicated. You understand it correctly. However, things are confusing for C arrays. Actually, to me, accessing array member via bracket notation is just a syntactic sugar. Please consider the following example. // allocate an array of integer with size of 2 int *dynmem_array = malloc(sizeof(int) * 2) // assign values ...


1

You ask: "However, my question remains: what would it take to properly pass a long long as an argument to a function?" Pass the argument to the funcion. Pass it with the correct type. Your function doesn't have a type in the prototype and it doesn't have it in the definition. The prototype is this: int checkCC(); That's an empty argument list. In C, ...


1

Your instincts are more or less correct. You have actually declared key twice, once as a global and a second time as a local var inside main(). Specifically, the line int key = atoi(argv[1]); is the second declaration and it takes precedence over the global var, so any usage of key inside of main will use the local version. The actual error is that once ...


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