8
votes
Accepted
How to Declare and Initialize Variables in C?
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 ...
4
votes
Accepted
what happens to memory when a variable dies?
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?
...
3
votes
Accepted
compilation error multi-character character constant in caesar
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 ...
3
votes
Global Variables vs #defined Constants
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 ...
2
votes
Accepted
Multiple byte storage and comparison
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 ...
2
votes
Multiple byte storage and comparison
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 ...
2
votes
Accepted
Why does this check for equality fail to trigger in my Scratch project?
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 ...
2
votes
How to initialize multiple types of variables in a FOR loop?
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 ...
2
votes
Accepted
Pset4 Recover not working
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 ...
2
votes
Accepted
scope of local variables
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 ...
2
votes
C: Pset 3: Binary Search - 'expected an exit code of 0, not 1' (not finding n)
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 ...
2
votes
what happens to memory when a variable dies?
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 ...
2
votes
Accepted
Get more than one inputs in same line without pressing enter key
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", &...
2
votes
Accepted
2
votes
Global Variable vs Local variables?
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 ...
1
vote
Declaring a variable in loop or outside
You can do what is shown in both of your examples, but you shouldn't. The first is a very bad practice, while the second depends on how it is used.
In the first case, it's called creating a shadow ...
1
vote
Accepted
Pset7 Sell stockprice not a bound parameter
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 ...
1
vote
Accepted
Printing a character times a variable
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 ...
1
vote
Error using string as a data type while including the cs50 library
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 ...
1
vote
Accepted
C: Pset 3: Binary Search - 'expected an exit code of 0, not 1' (not finding n)
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 ...
1
vote
Accepted
What does "using" a variable entail and why does this inconsistency come up? (pset1, greedy, general question)
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 ...
1
vote
Accepted
Multiplication function error?
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 ...
1
vote
global variables and macros
#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 ...
1
vote
calculations vs variables performance
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 ...
1
vote
Use of functions and variable declarations outside main() on pset1 greedy
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 ...
1
vote
Whats's difference between declaring and defining variables?
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 ...
1
vote
Accepted
Whats's difference between declaring and defining variables?
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
...
1
vote
Accepted
Can I create an element of arbitrary size in a STRUCT?
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 ...
1
vote
Difference between Pass by Value and Pass by Reference (a practical example)
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 ...
1
vote
Accepted
How can I pass an array or a long long unsigned variable as an argument to a function?
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible