error passing a character to a function in C - c

I have the following function
int namecomp(char c);
Part of the function code
else if (c == 'b' || 'B')
i=2;
The way I am calling it in main()
j= namecomp(s);
and s is defined as char s = 'B';
There is an error and whenever I am trying to use j the value is always 1 in the main. Please help me to know where exactly the error is. Thanks!
EDIT:Sorry Folks none of it worked., I am posting the complete code for help
int main (int argc, char* argv [])
{
int i;
int j;
char s = 'B';
j= namecomp(s);
printf ("%d",j);
}
int namecomp(char c)
{
int i;
if (c == 'a'||'A')
i=1;
else if ((c == 'b' || c == 'B'))
i=2;
return i;
}

c == 'b' || 'B'
always evaluates to 1, because it's parsed as
(c == 'b') || 'B'
I'm betting you want
(c == 'b') || (c == 'B')

This
(c == 'b' || 'B')
Should be:
(c == 'b' || c == 'B')
Otherwise, you're testing this:
((c == 'b') || 'B')
Which is the same as
((c == 'b') || true)
since 'B' is non-zero.
Remember that the logical and/or symbols can't be used inside a logical test, only to join logical tests together.

You wrote
(c == 'b' || 'B') // this can be ( (c=='b') || 'B') in your compiler
Did you mean
(c == ('b' || 'B'))
or
( (c == 'b') || (c=='B') )
you should use the latter.
You should approach these conditions as a paranoid with paranthesis to make sure it satisfies your conditions. Then you can try without paranthesis if it works for every condition.

c == 'b' || 'B' is always evaluate to 1 because 'B' is a nonzero value, so the second operand is always true.
You need to test your condition both.
if (c == 'b' || c == 'B')

Related

how can I make this run multiple times using do-while loop?

I'm trying to make a program that will scan the character I entered and will end only if the character inputted is a vowel or if the number of inputted characters has already reached 5
when I executed the code it only run once, and even if I inputted a vowel it does not terminate/end.
I tried adding c++ but it did not change anything
I feel like something is missing but I can't figure it out.
I've also tried watching tutorials about do-while loop.
here it is:
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c",&c);
do {
printf("%c",c);
} while (c == 'a' && c == 'e' && c == 'i' && c == 'o' && 'u' && c == 'A' && c == 'E' && c == 'I' && c == 'O' && 'U' && c == 5);
return 0;
}
Maintain a count and check if count < 5 and it is not any vowel
string ch ;
cin >> ch ;
int count = 1 ;
while(count < 5 && ch != "A" && ch!= "E" && ch != "I" && ch!= "O" && ch != "U" && ch != "a" && ch!= "e" && ch != "i" && ch!= "o" && ch != "u" ){
cout << ch << '\n';
cin>>ch;
count ++ ;
}

c how to check the first char of an array

I am trying to take in 10 characters over a serial console and add them to an array called buffer. The first character needs to be 'L' or 'S' and the next characters either '1' or '0'.
The code passes the first if statement ok. But the line if((buffer[0] != 'L') || (buffer[0] != 'S')) doesn't seem to work even when I enter 'L' OR 'S'.
Is there anything wrong with using the buffer[0] != notation?
int main(void)
{
char ch;
char buffer[10] = "";
putstring("Enter 9 characters beginning with 'L' or 'S' and 8 digits\r\n");
for (int i = 0; i < 9; i++) {
ch = getcharacter();
if ((ch == '0') || (ch == '1') || (ch == 'L') || (ch == 'S')) {
buffer[i] = ch;
//check first character
if((buffer[0] != 'L') || (buffer[0] != 'S')) {
printf("First letter must be L or S\r\n");
goto error;
}
error:
return -1;
}
}
}
int getcharacter(void) {
char c = 0;
const uint32_t recieve_ready = 1 << 7;
//disable interrupt for a read ready
*uart_control_reg = 0;
while (1) {
//check if RRDY bit is set
if ((*uart_status_reg) & recieve_ready) {
c = *uart_rxdata_reg;
break;
}
}
return ((char) c);
}
if((buffer[0] != 'L') || (buffer[0] != 'S'))
is wrong, you need
if((buffer[0] != 'L') && (buffer[0] != 'S'))
or
if (!(buffer[0] == 'L' || buffer[0] == 'S'))
Your original code was "if the char is not L or the char is not S" which is always true. If the char is L, then the second part was true, making the whole if statement true.
Just noticed Chris Turner's comment above. The return -1 is always executed, move it to replace the line that says goto error.
Try using
if((buffer[0] != 'L') && (buffer[0] != 'S'))
instead of
if((buffer[0] != 'L') || (buffer[0] != 'S'))
Just some logic problem here. According to your code, the char needs to be equal to 'L' AND 'S' to avoid the condition, which is never the case !

I'm learning C language but I cannot run the 'y' to continue the program

Here i get the answers for vowels but not for continue it says error for the continue and also for break.
#include < stdio.h >
main() {
char c, d;
printf("say your word to find the vowels\n");
scanf("%c", & c);
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U')
printf("you got a vowel\n");
else
printf("cool no vowel word\n");
printf("continue\n");
printf("(y/n)\n");
scanf("%c", & d);
if (d == 'y' || d == 'Y')
continue;
else
break;
return 0;
}
if (d == 'y' || d == 'Y')
continue; // where?
else
break; // what?
You have nothing to continue or break in your main function.
Both continue and break only make sense within a loop, so you should add a while(true) loop around the code in main() to repeat the whole thing until the user decides to quit.
Continue and break works in the loop block.. but here you have not added them in loop. Put the whole if block in the while(1) and it will work
#include <stdio.h>
int main()
{
char c;
int d;
while (1) {
printf("say your word to find the vowels\n");
scanf("%c", &c);
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U')
printf("you got a vowel\n");
else
printf("cool no vowel word\n");
printf("To continue enter 1\n");
scanf("%d", &d);
if (d == 1)
continue;
else
break;
}
return 0;
}
Check the continue and break statement syntax in the following link.
https://www.codingunit.com/c-tutorial-for-loop-while-loop-break-and-continue

Shrink my code, Please

I wrote this stupid chunk of code as an answer for a program exercise from the book "c programming a modern approach 2nd E." by K.N.King, namely ch7;ex4.
I couldn't make it does the same thing with less code, so, Can you?
It's just converting alphabetic phone numbers into numeric form, i'm really curious to see a better coding of this..
Thanks..
#include <stdio.h>
void main()
{
char c;
int phone_number;
printf("Enter phone number: ");
while((c = getchar()) != '\n') {
if(c == 'A' || c == 'B' || c == 'C')
printf("2");
if(c == 'D' || c == 'E' || c == 'F')
printf("3");
if(c == 'G' || c == 'H' || c == 'I')
printf("4");
if(c == 'J' || c == 'K' || c == 'L')
printf("5");
if(c == 'M' || c == 'N' || c == 'O')
printf("6");
if(c == 'P' || c == 'R' || c == 'S')
printf("7");
if(c == 'T' || c == 'U' || c == 'V')
printf("8");
if(c == 'W' || c == 'X' || c == 'Y')
printf("9");
if(c == '0')
printf("0");
if(c == '1')
printf("1");
if(c == '2')
printf("2");
if(c == '3')
printf("3");
if(c == '4')
printf("4");
if(c == '5')
printf("5");
if(c == '6')
printf("6");
if(c == '7')
printf("7");
if(c == '8')
printf("8");
if(c == '9')
printf("9");
if(c == '-')
printf("-");
}
printf("\n");
}
We can take advantage of the fact that characters are sequential (not sure if this is standard, but it happens in most representations, anyway)
Let's make a table specifying which number a letter represents. It would look something like
int keynum[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
keynum[0] is the number that A maps to, and so on. If we have a character c and we need to get the proper index in keynum we calculate it by c-'A'
Then our main loop would be:
while((c=getchar()) != '\n') {
if (c>='0' && c<='9')
putchar(c);
else if (c >= 'A' && c <= 'Z')
putchar('0' + keynum[c-'A'])
/* else error? */
}
2 + ((c-'A') / 3) (integral division) is the number you print for A-Y (if 'A'<=c && c<='O'!)

C - Noob error with simple 3-way comparison

Apologies for the dumb question, I'm a bit of a beginner and am having trouble understanding why the following code will not work correctly.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int j = ' ';
int check = 0;
printf("\nPlease enter A, B, or C\n");
j = getch();
while(check == 0)
{
if(j != 'A' || 'B' || 'C')
{
printf("\nInvalid entry, please enter either an A, B, or C\n");
j = getch();
}
else
{
check = 1;
}
}
}
All I want this simple program to do is take in either A, B, or C using getch() (Yes, I need to use getch()) and use my while loop to confirm the entry actually is either an A, a B, or a C. However, I run the program, and even when I enter an A, B, or a C, the program tells me my entry is not valid. Can someone help me here and tell me what I'm doing wrong?
I have a feeling this has to do with the fact that it reads in the character as an ASCII integer, but I'm really not sure how to fix this.
if(j != 'A' || 'B' || 'C')
is equivalent to
if(j != 'A' || 'B' != 0 || 'C' != 0)
Both 'B' and 'C' have non-zero value so the condition will always evaluate true.
I think you want to check that j isn't any of the values listed. If so, the line should be
if(j != 'A' && j != 'B' && j != 'C')
Do the following replacements:
int j = ' '; /* to */ char j = ' ';
if(j != 'A' || 'B' || 'C') /* to */ if(j != 'A' && j != 'B' && j != 'C')
j = getch(); /* to */ j=getchar();
Also, for getchar() to work, include <conio.h> if required.

Resources