Hot answers tagged

8

In C, char is an integer type; if you multiply a character by an integer, you're really multiplying two integer values. But the result may not be what you expect! For instance, try running this code: int x = 'a' * 2; char y = 'a' * 2; printf("x: %d\n", x); printf("y: %d\n", y); On ideone.com, the output is: x: 194 y: -62 Not only that, a string in C is ...


4

Investigate loops. Here's the Loops Short: http://www.youtube.com/watch?v=HHmiHx7GGLE Also, watch Week 2 Monday's lecture for more insights. for (int i = 0; i < 5; i++) { printf("Hi! "); } The output of this is: Hi! Hi! Hi! Hi! Hi! Brenda.


2

You can do this: printf("%0*i", 20, 0); This will print 0 20 times, as shown: 00000000000000000000 However, the 'long way', for loops, is probably the way to go, as the way I showed above is a bit tedious, and complicated even for the later parts of the C section of the course. TheBrainyOne out!


2

It may be hard for you to figure out the cause of the problem assuming that you just finished week 2. The problem deals with concepts like local variables, scope, stack, etc. You may probably know about local variables and scope and you'll get exposed to the rest as you proceed with the course. I'll try to simplify things as much as I can. If you feel like ...


2

When you say return dollars, your program gives an output = "dollars" and ends itself. The line of code after returning dollars is not even read. Just remove the return statement and try your code. Have a great day.. :)


2

Uhhuh. I see that it is doing a nice job of encoding and storing the encoded characters in an array. But have you also stored the end of string marker '\0' at the end of the string? printf will keep printing whatever it finds in memory until it finds that null character which marks the end of the string. If this answers your question, please click on the ...


2

The problem is that argv is an array of strings, and you try to use it as a single string. See this line: if (!isalpha (argv[1][i])) and this: if (isupper (argv[j])) Can you spot the difference?


2

[I posted this on reddit last year but rather than just linking it, I thought I'd repost. In that case, they had printed a few more decimals, but the principle is the same.] A float value has 32 bits to work with: 1 bit to hold the sign 8 bits to hold the offset exponent ( which is the exponent + 127) and 23 bits to hold the significant digits So, in the ...


2

You can print the value of a variable by using placeholders. Each variable type has a different placeholder. Ex: To print out a string called x, write this: printf("%s\n", x); More info: http://shangyilim.blogspot.com/2009/02/placeholders-placeholders-are-used-to.html


1

It's not a floating point problem, it's an integer problem. ;-) Look at this line: int l = 65 / 14 * 100; It contains all integers. Additionally, consider the precedence of operations - what gets done in what order. Since all the numbers are integers, this is going to do integer division. The precedence rules says that all this will be done left to ...


1

Very simple. Look at the line: printf( " xx"\n); Any special control or escape characters, like \n must be inside the double quotes of the string. The compiler just doesn't like it the way you have it. The correct code here would be: printf( " xx\n" ); The \n will now be interpreted as a direction to go to the ...


1

It's all about context. In an assignment, like int I = j % 4;, the % is the modulo operator. However, in the case of a printf statement, it is not. Let's say that you had the statement printf("The value of k = %i \n", k);. Here, inside the double quotes that indicate the string to be printed, the %i is a token that says to 'replace me with the integer ...


1

It does print it, but as you haven't included any '\n' after it, it's printing at the beginning of your command line. printf("%d\n", coins); //Output That ought to fix it. HTH If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the ...


1

Your old code was going through the first n pairs, where n is the number of candidates, then checking if one of the winners of those pairs is the total winner. You can't assume that with n candidates, the overall winner will be the winner of one of the first n pairs. Example: If there are 9 voters and 6 candidates 5 people vote A, B, C, D, E, F 4 people ...


1

This will (hopefully) explain this specific problem in main and perhaps the overriding problem. The output buffer is not "flushed" until a new line (or explicit flush). The answer provided here explains it. If the added debugging printfs are "not executing" it could be for the same reason, since the first "\n" that is encountered would likely be after "...


1

You seem to ignore the value of found. Just disables the duplicate check. You don't have a loop for printing elements, and you seem to use ptr before initialisation. I would expect a loop very similar to that one searching for the element, or that one searching for the last node of the list. Like for (node*ptr=numbers;ptr!=NULL;ptr=ptr->next) ...


1

Could it be the missing line break? The output of printf("Your input: $ %.2f", a); printf(", which amounts to %i total.\n", cents); or equivalently printf("Your input: $ %.2f, which amounts to %i total.\n", a, cents); ends in a newline \n, so the next number would be placed on the next line. Without that second part, whatever you print next gets printed ...


1

In the first case, you use a char variable, which in our case is an signed char by default, supporting values from -128 to 127. The result of your computation does not fit in, so it wraps around, keeping the least significant byte of the result. You can see the numbers differing by 256, or 2 to the power of 8. The second computation is probably returning an ...


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

The i is just a loop counter, and has nothing to do with the character printed. In C, char is an 8-bit integer (well, not 8 bit per standard, but 8 bit in every existing and relevant system), by default either signed char with values from -128 to 127, or unsigned char with values from 0 to 255 (you can explicitly specify "signed" or "unsigned" if you need ...


1

The problem was hiding somewhere else: // Create pword w/ max of 5 chars char pword[6]; pword[5] = '\0'; That means that the first 4 chars are whatever random garbage is in them when the char array is created. So, what's in the first 4 chars as the code progresses? More importantly, where is the end of string marker when you're dealing with 1 and 2 char ...


1

Whenever an unexpected value, particularly 0, suddenly appears in the sorted list and the largest value disappears, look for code that goes beyond the end of the array. This is a somewhat common error. The code is going past the end of the array and picking up what it thinks is another element. Usually, this turns out to be 0, and gets sorted to the ...


1

Here is some details about printf() in C. Long-story-short, %s is known as a string placeholder. And does exactly that: it "leaves room" for a string to be printed out. E.g.: printf("%s\n", "Hello!"); would output: Hello! to your terminal / console. Of course, you could have printed "Hello!" right away, but the use of %s is in order to print ...


1

I went to the lecture and noticed that wasn't "eight such byte", it was "ith such byte". I think this is clear now. Warning: I don't have captions.


1

Have you noticed that when the fifteen program starts up, the entire screen is cleared? Compare that to the other programs before this, which just display the output directly after the command line. The reason for this happening is shown in the greet() function: void greet(void) { clear(); printf("GAME OF FIFTEEN\n"); usleep(2000000); } Here ...


1

The source code for the GNU C standard library implementation of vprintf (which is called by printf) should indicate that it is not a simple operation with a single answer, however, understanding printf works will reveal a lot about the intricacies of languages in general. In the theoretical sense, the formatting parameters used by printf and scanf, and ...


1

No, you can't do this in 'C' as far as I think.In python multiplication of a string by a number is defined as 'repetition' of the string that number of times. One way you can do this in 'C++'(a superset of 'C' language) is through 'operator overloading'.By this you can redefine * operator's functionality for a custom made string or character class.This is ...


1

This may not be the best way, but a simple way to repeat the characters can be done like this (as this answer suggests) Short answer - yes, long answer: not how you want it. You can use the %* form of printf, which accepts a variable width. And, if you use '0' as your value to print, combined with the right-aligned text that's zero padded on the ...


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