How to use char in C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm struggling to get Char to work. It keeps returning an error.
#include <stdio.h>
#include <cs50.h>
int main (void)
{
int tower_height;
char #;
// Inputs
do {
printf("Let's build! Give me a number between 0 and 23 'inclusive'.\n");
tower_height = GetInt();
}
while
(tower_height < 0 || tower_height > 23);
// Outputs
for (tower_height = 0; tower_height <= 23; tower_height++)
printf ("%c = tower_height - 2\n");
}

C identifier names may contain letters, underscore, and digits, as long
as the first character isn't a digit, and as long as the identifier
isn't a keyword. They may not contain #.

# is not a valid variable name.

As pointed out, # is not a valid variable name.
You can see how # is properly used in the first line of your code: #include <stdio.h>
Instead, call your char variable something that uses letters and numbers eg: char c;

Related

Guessing Game - Can't figure out why it produces a large negative integer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Cheers guys. I am having trouble with this code I produced. It functions everything fine but when it prints the number when I guessed correctly, it prints a number like -3529583 which I don't understand. Shed some light?
#include <stdio.h>
#include <ctype.h>
#include <time.h>
int main()
{
int y, iRandomNum1; // Declare the three variables
y = 0;
iRandomNum1 = 0;
srand(time(NULL)); // Randomize function
iRandomNum1 = rand() % 10; // Randomize and collect 1 to 10 Value
while (iRandomNum1 != y) {
printf("Guess a number from 1 to 10\n");
scanf("%d", &y);
}
printf("\nCorrect Guess! Congrats! The answer is %d.\n", &y);
return 0;
}
You are display the address of the variable &y instead of the variable itself in your printf just remove the & symbol and it should be ok
https://stackoverflow.com/users/2173917/sourav-ghosh had the answer before me in comment

memchr returns unexpected characters [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying my best to understand the memchr function but having some issues with some simple output. I'm getting extra characters at the end of *newchar.
#include <stdio.h>
#include <string.h>
int main() {
char plus[6] = "12+123";
char *newchar = (char*) memchr(plus,43,3);
printf("%s",newchar);
}
output:
+123( '
I expected to get "+123," why does it give me the extra characters? I noticed the output is consistent which confuses me earlier, it doesn't seem like these were grabbed from somewhere random in memory but were caused by the memchr function.
char plus[6] = "12+123";
You've defined an array of size 6, and initialized it with 6 characters. You haven't left enough room for the NUL terminator. There is garbage at the end of your string, and printf doesn't know when to stop printing it.
Do this instead, allowing the string to be appropriately sized automatically:
char plus[] = "12+123";

Why it doesn't work ( C Lang in Dev C++ ) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Please tell me, what is wrong here,it compiles but console crashes when i enter the number. I don't know what to write next, i will just write something to make my post possible.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
unsigned int i,l,p,w;
printf("Enter natural number excluding 0: ");
scanf("%d",l);
p = 1;
for(i=1;i<=l;i++)
{
p=p*i;
}
w=p;
printf("\nFactorial of entered number %d",w);
return 0;
}
scanf("%d",l); you need to insert the address of l, which is &l.
You should also use %u for unsigned ints, not %d.

#define and then printf not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
For an assignment i must define a variable N as 100, then recall that variable in a printf statement. the code looks like :
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define N 100
int main ( )
{
...
printf("Try to guess a number between 1 and N \n\n") ;
...
}
The N is just coming out as N rather than 100.
This is because everything between double quotes are considered as a character array, i.e. a string. So if you want to show N in the string, you shall use it as an "usual" variable:
printf("Try to guess a number between 1 and %d \n\n", N) ;
#define won't expand in literal character string (block of character between "). You should write:
printf("Try to guess a number between 1 and %d \n\n", N)

Receiving strange number from fscanf - int conversion? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am very new to C and having a problem here. I am attempting to pass a file numbers.in through the below script. numbers.in contains 2 lines as follow:
12,34,56789
123456,789,0123
I am attempting to recognize the comma delineation.
#include <stdio.h>
void main(int argc, char *argv[])
{
int p ,n ,x ; //Converted ints.
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 );
{
printf("got the sequence (%d,%d,%d)\n",x,p,n);
}
}
I am running the script like:
./a.out < numbers.in
Currently my script returns completely different numbers! What am I doing wrong here? Is the file sending them as characters so I need to somehow convert to ints? (I tried saving as chars and then later converting chars to ints and also got strange numbers - but different strange numbers!)
SOLVED, bad semicolon usage >_<
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 ); <-- remove this semicolon

Resources