Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
When I run this program it prints a strange character in the terminal. Can someone tell me what that is?
int main(){
char x=1;
printf("%c\n",x);
return 0;
}
This is because you are assigning 1 (ASCII value of a character) to x . Assign '1' to x. It will give output 1.
char x = '1';
printf("%c\n",x);
Its printing character 1 from the ASCII table
(making reasonable assumptions about your platform, that you aren't on an EBCDIC platform or something)
Characters in C should usually be assigned using characters, not by using ASCII encoding. For example:
char x = 'A';
printf("%c\n", x);
Will print the character 'A' on the terminal. By giving the character the ASCII index of 1, you're assigning it the START OF HEADING character (SOH). If you were looking for 'A' or '1', that would be:
char x = 65; // x = 'A'
char y = '1'; // y = '1'
But like I said, this is very awkward and requires ASCII memorization to read, so assigning numbers is very bad practice.
You can find an ASCII table at: http://www.asciitable.com/
It depends on what you want as output. If you think that your code is giving something weird as output, you certainly want '1' as output. And for that, you should replace your statement
char x = 1;
to
char x = '1';
Thats it! Your problem would be solved!
Related
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 last year.
Improve this question
I've been messing with turning characters into integers and I wrote this function to do that. (I know that Unicode could break this code but let's assume we won't use any unicode characters here.)
int ctoint( char fCHAR ) {
int cVALUE = fCHAR-'0';
return cVALUE;
}
When I insert character 'A' to this function, it will return '17', when I insert 'B', 18 ... 'Z' 42 etc. So, what does C take reference when it is converting chars into integers when using this kind of code?
Thanks in advance :3
Edit: I tried it with some symbols and it returned -5 for a '+', -3 for a '-', -6 for a '*' and -1 for a '/'.
Edit 2: It seems like the it decrements 48 from the ASCII value of that character. '65-48' is 17 so that is an A and '43-48' is -5 which is + as I tried it in the previous edit. Does anyone know, why 48? I guess because it is the start of numbers so numbers' characters will get the value of themselves (e.g. 48-48 is 0)
Edit 3: Thank you all!!!
In ASCII, '0' has code 48 and 'A' has code 65, which is why 'A'-'0' evaluates to 17. See the full ASCII table on Wikipedia.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
char hi[10] = "bye";
char a = 'a';
strcat(hi, a);
Like the example above. How would I do this in C? Is there a more general string I cant let hi be?
a is a char type while strcat expects it's both arguments of type char *. To append the char to an array of characters you can do this
int index = strlen(hi);
if(index < sizeof(hi)-1)
hi[index] = a;
Note that in this particular case the initializer will initialize the first three elements of hi to b, y and e respectively. The rest of the elements will be initialized to 0. Therefore you do not need to take care of the null termination of the array after appending each character to it. But in general you have to take care of that.
int index = strlen(hi);
if(index < sizeof(hi)-1){
hi[index] = a;
hi[index+1] = '\0';
}
strcat(hi, (char[]){a,0});
This would append the a.
Or you can do this
char s[]={a,0};
strcat(hi,s);
Or simply
#define MAXLEN 10
...
size_t len = strlen(hi);
if( len+1 <= MAXLEN-1)
hi[len]=a,hi[len+1]=0;
else
// throw error.
In your case hi[len+1]=0 is not required as it is already filled with \0. Also as mentioned by Serge that you can use simply used the string literal as the second parameter to the strcat function.
strcat(hi,"a");
There is a subtle difference in this two as mentioned by Serge again, that string literals are const but the compound literals are not.
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;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following scenario where any input string will be converted into an integer.
Example:
result = get_integer_from_string("100");
result == 100; // true
How can I write this function without using any libraries? I am able to do it by using libraries.
Follow these Steps:
Parse the input string.
Check if the character is a digit or not.
Use some logic to convert digit in character format to integer format.
Also, you can implement Exception in case, input is not integer string.
I cannot tell you the code, it would not help you in learning, try implementing code yourself, it is very easy!!
Loop over the string, from the end to the start. Get each digit, and convert it to a decimal value. Multiply the first (in the backward loop) by 1 and store the result. Multiply the second by 10 and add to the result of the previous. And so on.
This is very prone to error conditions, but should work if string is valid integer:
int str2int(const char* str) {
int result = 0;
char* p = str;
for (;;) {
char c = *p++;
if (c < '0' || c > '9')
break;
result *= 10;
result += c - '0';
}
return result;
}
It has behavior close to atoi() - stop processing on any non-digit, and return 0 for empty input.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have doubt in this line flag[str[i]-'a']++; how this line work.
For full program visit http://www.programmingsimplified.com/c/source-code/c-anagram-program
char str[44];
int flag[26],i=0;
gets(str);
while(str[i]!='\0')
{
flag[str[i]-'a']++; // How this line work
i++;
}
i=0;
while(str[i]!='\0')
{
printf("\n%d, %d ",str[i]-'a');
i++;
}
Lowercase 'a' resolves to decimal 97. Subtracting 97 basically allows you to use characters 'a', 'b', 'c', etc. as indexes to the flag array. Once you have that, the ++ is incrementing the appropriate letter slot in the array.
So flag[0] is for the letter 'a', flag[1] is for the letter 'b', and so on.
flag[str[i]-'a']++;
post incrementing flag[someindex] value
someindex vale is counted str[i]-'a'
if str[i]='c' then
someindex='c'-'a' ==> someindex=2
post incrementing flag[2];
i'm guessing you have a lower case letter in str[i] and in that case str[i]-'a' will mean the letter's number, as in
a=0
b=1
c=2
.
.
.
and at the end you have
flag[str[i]-'a']++;
so it's an array of letters, incrementing each iteration the current letter's cell and generally it counts how many times each letter appears
for example if you have the string "aaccdvb
you'll get:
str[0] = 2
str[1] = 1
str[2] = 2
str[3] = 1
str[21] = 1
and all the rest are 0
Without really parsing the rest of the program... flag[str[i]-'a']++ translates to:
Take a value str[i] and subtract it from 'a'. 'a' resolves to 97 in ASCII, so you're taking whatever value is in str[i] and subtracting it from 97.
Take the result of the difference and use it as an index into the flag array.
Apply a post-increment operation of the resulting value determined by the previous two steps.