Why does the second "a" have different value than the first "a"? - c

I'm trying to get the difference of two characters.
My problem is when I input the 2nd character it gives a different value even if the character is just the same as the first input:
Example below. I input "a" as first char and then "a" again for the second but it gives different value
int main(){
char* flet;
char* slet;
printf("Input first character:");
scanf("%c", &flet);
fflush(stdin);
printf("Input Second character:");
scanf("%c", &slet);
printf("First Char is \"%c\" and Second Char is \"%c\".", flet,slet);
DiffofChar(flet,slet);
}
void DiffofChar(char* letter1, char* letter2){
int theDiff;
theDiff = letter1 - letter2;
printf("The difference of %c (%d) and %c (%d) is %d.", letter1, letter1, letter2, letter2, theDiff);
}
Output:

This is incorrect:
char* flet;
char* slet;
printf("Input first character:");
scanf("%c", &flet);
At best, the above will cast the character value typed (a) into a pointer and that address (0x61 == 'a') will just be a pointer value for an invalid memory location.
The fix is to declare the variables as just type char. You still pass &flet and &slet to the scanf functions.
char flet;
char slet;
printf("Input first character:");
scanf("%c", &flet);

Related

First %s scanf value get ignore but second %s can store the value

int main()
{
char i;
char s;
printf("Enter first char : ");
scanf("%s", &i);
printf("Enter second char : ");
scanf("%s", &s);
printf("%c", i);
printf("%c", s);
}
The output turn out only print second scanf value while the first scanf value does not print out.
int main()
{
char i;
char s;
printf("Enter first char : ");
scanf("%c", &i);
printf("Enter second char : ");
scanf(" %c", &s);
printf("%c", i);
printf("%c", s);
}
When change to use %c both scanf value can be print out.
Why does the %s only store the last input while the first get ignored?
%s is to scan string :
scanf(" %s",&string);
Or
scanf("%s[^\n]",string);
%c to scan only one caracter :
scanf(" %c",&caracter);
in your first code ,if you want to read two string and print them :use this code
#include <stdio.h>
int main()
{
char i[150];
char s[150];
printf("Enter first char : ");
scanf("%s[^\n]",i);//scan the first string
printf("Enter second char : ");
scanf("%s[^\n]",s);//scan the second string
printf("%s", i);
printf("\n");
printf("%s", s);
printf("\n");
printf("%c",i[1]);//if you want to print the second caracter in the string i
printf("\n");
printf("%c",s[0]);//if you want to print the first caracter in the string s
}

Clearing input buffer/garbage?

In this program, when I enter 8 characters, it screws up the whole program, and I think it has something to do with clearing the input buffer when you enter more than or equal to SIZE characters, how do I make it so that it clears any overflow of characters a user may enter?
Thanks for any help!
#include <stdio.h>
#define SIZE 8
int CharIsAt(char *pStr,char ch,int loc[],int mLoc);
int main(void){
char array[SIZE],search;
int found[SIZE],i,charsFound;
printf("Enter a line of text(empty line to quit): ");
while (fgets(array,SIZE, stdin)!=NULL && array[0]!='\n'){ //Loop until nothing is entered
printf("Enter a character to search: ");
search=getchar();
charsFound=CharIsAt(array,search,found,SIZE);
printf("Entered text: ");
fputs(array,stdout); //Prints text inputted
printf("Character being searched for: %c\n",search); //Prints character being searched for
printf("Character found at %d location(s).\n",charsFound); //Prints # of found characters
//Prints the location of the characters found relative to start of line
for (i=0;i<charsFound;i++)
printf("'%c' was found at %d\n",search,found[i]);
if (fgets(array,SIZE,stdin)==NULL) //Makes loop re-prompt
break;
printf("\nEnter a line of text(empty line to quit): ");
}
return 0;
}
int CharIsAt(char *pStr,char ch,int loc[],int mLoc){
//Searches for ch in *pStr by incrementing a pointer to access
//and compare each character in *pStr to ch.
int i,x;
for (i=0,x=0;i<mLoc;i++){
if (*(pStr+i)==ch){
//Stores index of ch's location to loc
loc[x]=i;
x++; //Increment for each time ch was counted in pStr
}
}
//Returns the number of times ch was found
return x;
}

Why isn't the Isdigit function working properly?

I wrote this short code to test my understanding of the isdigit function:
int inChar;
printf("enter input:");
scanf(" %d", &inChar);
if (isdigit(inChar))
printf("Your input was a number");
else
printf("Your input was not a number.\n");
When I test this program and I enter a number, C returns the else statement (Your input was not a number.). So regardless of if I enter a number or a letter, the program returns the else statement.
Why is this so?
isdigit() checks if a single character that was passed to it by converting the char value an unsigned char.
So, you can't directly pass any int value and expect it to work.
Man isdigit() says:
isdigit()
checks for a digit (0 through 9).
To check single digit, you can modify:
char inChar;
printf("enter input:");
scanf(" %c", &inChar);
if (isdigit((unsigned char)inChar)) {
printf("Your input was a number");
}
else {
printf("Your input was not a number.\n");
}
If you have an array (a string containing a number) then you can use a loop.
The function's purpose is to classify characters (like '3'). Running it on something that's read using %d doesn't make sense.
You should read a single char using %c. Remember to check that reading succeeded.
The C library function void isdigit(int c) checks if the passed character is a decimal digit character.
If you badly wanna try it with an int you can init in this way
int inChar = '2';
The following code gave expected results.
int main()
{
char inChar;
printf("enter input:");
scanf(" %c", &inChar);
if (isdigit(inChar))
printf("Your input was a number. \n");
else
printf("Your input was not a number.\n");
return 0;
}
Output:
vinay-1> ./a.out
enter input:1
Your input was a number

Printing ASCII-code values and their equivalent chars in C

How could I print char for given ASCII code value?..
I saw this question, my problem is similar, just contrary
Below is my program, but it doesn't work.
int main (){
int code; // code that user enters
char ch; //"codes" related ASCII charcter
printf("Enter a ASCII code value: \n");
scanf("%d", &code);
ch=code;
printf(" %c is cherechter that have %d ASCII code\n", &ch ,&code);
system("PAUSE");
return 0;}
In your code, you have to change your printf statement
printf(" %c is cherechter that have %d ASCII code\n", &ch ,&code);
to
printf(" %c is cherechter that have %d ASCII code\n", ch ,code);
because, to print the values, you don't need to supply the address of the variables. The variable name is enough.
Change your code to:
int main (){
char code; //change from int to char
char ch;
printf("Enter a ASCII code value: \n");
scanf("%c", &code);//ASCII is a character not integer
ch=code;
printf(" %c is cherechter that have %x and %d ASCII code\n", ch ,code,code);//don't print the address access the value
system("PAUSE");
return 0;}

How to get first chars of multiple entries in C?

This code works well for words with less than five letters: (but not for higher)
#include <stdio.h>
int main(void)
{
const int size = 5;
char str1[size], str2[size], str3[size];
printf("Type word1: ");
scanf("%s", str1);
printf("Type word2: ");
scanf(" %s", str2);
printf("Type word3: ");
scanf(" %s", str3);
printf("First chars: '%c', '%c' e '%c'.\n", str1[0], str2[0], str3[0]);
return 0;
}
The only way to run correctly would increase the 'size' variable? I wonder if it is possible to work properly with larger words without necessarily increasing the 'size' variable.
This will get you close
Just save 1st char
#include <stdio.h>
int main(void)
{
char str[3];
printf("Type word1: ");
scanf(" %c%*s", &str[0]);
printf("Type word2: ");
scanf(" %c%*s", &str[1]);
printf("Type word3: ");
scanf(" %c%*s", &str[2]);
printf("First chars: '%c', '%c' e '%c'.\n", str[0], str[1], str[2]);
return 0;
}
regarding this kind of line: 'scanf("%s", str1);'
1) the scanf format string needs to limit the number of characters input,
otherwise (in this case)
inputting a word longer than 4 char will result in a buffer overrun
2) always check the returned value from scanf
to assure the input/conversion operation was successful.
3) I would strongly suggest using fgets() and sscanf()
then
--the max number of characters is limited by a fgets() parameter,
--the string is null terminated,
--the newline is part of the string,
so will need to be overlayed with '\0'
4) in the user prompts,
I would use:
printf( "\nUsing %d or less characters, enter a string:", argv[1] );
where argv[1] is a command line parameter that indicates the max string length.
(be sure to allow for the nul terminator byte)

Resources