Hot answers tagged

8 votes
Accepted

Can you print more than one char in C by using "x" * 5 like you can in Python?

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

How does a person make a character print more than once?

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! "); } ...
user avatar
  • 18.4k
3 votes
Accepted

trail of strange characters after printing character array

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

Can you print more than one char in C by using "x" * 5 like you can in Python?

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

printf is changing string contents

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

Pset1 How to print variable to make sure input is correct?

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

I have written this code for vigenere and it is giving 02 errors

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?
user avatar
  • 7,326
2 votes
Accepted

Why the printf function is able to print 28 decimal digits, if the float has only 24-bits mantissa?

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

How do I print the values of variables?

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://...
user avatar
  • 1,211
1 vote

printf printing an @ after a string

From man printf s If no l modifier is present: The const char * argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (...
user avatar
1 vote
Accepted

Numbers with printf -- sanity check

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

pset 1 Mario - clang error - printf syntax error? won't allow /n at end of printf line - error code

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

what does %d or %i do?

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

Greedy not printing

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

Tideman, my tests work but check50 says there is an error

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

speller- tried using printf to debug -printf not working

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

Help in linked datastructure

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

Why removing a printf function messes up with my code? CS50 pset1 cash greedy challenge

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

addition operation difference using printf in c

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

The question mark (????) example | Mario

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

Pset2 Crack - Including printf causes passwords I am generating to change

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

Puzzled by Pset3 - find less confortable- sorting output

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

what (%s) mean in printf in c

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

Why "the eight such byte" would be bracket four in printf("%c", s[4]) ? -lecture 2

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

fifteen.c : printf() not printing anything to the screen

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

How does printf and scanf works behind the scene?

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

Can you print more than one char in C by using "x" * 5 like you can in Python?

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

How does a person make a character print more than once?

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 %*...
user avatar
  • 11

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