I have a few questions about C syntax.
ch = (char *) malloc( sizeof( char ) * strlen(src) );
What do the first brackets mean (char *) ?
Others have already answered yor First two Questions correctly, So I will answer your third Q:
When you enter a character
and hit the ENTER key, two characters are placed in the input buffer, the character
and the newline
character.
You need to account for both of these. So First scanf
consumes the newline and another reads the character.
Step by Step Code Analysis:
printf("enter string \n");
scanf("%s",&str);
With above two statements, You see Enter the string
and program waits for your input.
Let us assume you enter the character C
and pressed Enter once. When you perform this action, the input buffer receives two characters:
C
that you entered & \n
The scanf
statement reads just one character(C
) from the input buffer.Thus, the newline
character remains unread in the Inuput buffer.
printf("enter char \n");
scanf("%c",&ch); //does not scan my char
With above two statements, enter char
gets displayed but the scanf
just skips(not wait for any uer input), this is because the unread newline
character in the Input buffer is read by this scanf
.
So to appropriately receive the next input character you need an additional scanf
.
scanf("%c",&ch); //with this second line do scan my char
(char *)
is a cast; it means "treat the following value as a pointer to char
". In this particular case it's redundant and considered bad practice.
'1' is a character constant; somewhat non-intuitively, it has type int
(this is different from C++, where character constants are type char
). "1" would be a string constant, which is actually an array expression of type char [2]
and has the contents {'1', 0}
.
This is because a newline has been left in the input stream from the previous input operation. Suppose you type "foo" and hit Return; the input stream will then contain the characters 'f', 'o', 'o', '\n'. The first scanf
call reads and assigns "foo" to str
, leaving the trailing '\n' in the input stream. That stray newline is being picked up by the next scanf
call because the %c
conversion specifier doesn't skip whitespace (unlike just about every other conversion specifier).
In simple terms:
1) since malloc returns a pointer of type void, you are casting that pointer to a pointer of type char so that the variable ch will later hold an array of characters (a string )
2) the single quotation marks are mainly used because a switch statement in C always expects an INTEGER and, not a string. having a character surrounded by single quotations would return the integer representation of the character.
3) This is a common problem when using scanf, basically it is caused by by the carriage return that is entered from the previous scanf. I would recommend that you always flush the input before using it.
Here's an example:
#include <stdio.h>
int main (void)
{
char line[200];
while(1)
{
printf("\nenter a string: ");
fflush(stdout); // safety flush since no newline in printf above
scanf(" %[^\n]",line); // note space character
printf("you entered >%s<\n",line);
}
return 0;
}
This is cast; it treats the return value of malloc
(a void*
) as a char*
.
This is an ordinary char
literal.
The (char ) at the beginning of the first statement is a typecast. By default malloc returns a void which should be fine to assign to a char*, but in other cases typecasting is required.
You are correct single quotes should be used around charaters.
Saying scanf sometimes doesn't work is a meaningless statement without example code.
I d like to add to @Alok Save s answer. He has analysed it correctly however, another simple solution exists for that case and that is to use scanf with a whitespace in the string format as follows:
scanf(" %c", &variable);
This is because %c does not absorb the newline character like others(like %d) do. Any whitespace in the format string will cause scan to absorb all consecutive whitespaces thereby obviating the need for another scanf statement.
Hope this helps!