A few questions about C syntax

I have a few questions about C syntax.

  • ch = (char *) malloc( sizeof( char ) * strlen(src) ); What do the first brackets (char *) mean?

  • c=getch();

    switch(c) {

    case '1' :{

    My teacher asked why quotes of this type are used, not double quotes. I said this is C syntax if char variable is used. But he said NO! Why are single quotes used and not duplicated?

  • Sometimes when using scanf nothing happens and it is used twice to get something scanned. What is the cause of this problem? For instance:

     printf("enter string \n"); scanf("%s",&str); printf("enter char \n"); scanf("%c",&ch); //does not scan my char scanf("%c",&ch); //with this second line do scan my char 
0
source share
8 answers

Others have already answered the first two questions correctly, so I will answer your third question:

When you type character and press ENTER, two characters are placed in the input buffer, character character and newline .

You need to consider both of these options. So, the first scanf consumes a new line, and the other reads a character.

Step by Step code analysis:

 printf("enter string \n"); scanf("%s",&str); 

Over two statements, you see Enter the string , and the program is waiting for your input. Suppose you enter the character C and press Enter once. When you perform this action, the input buffer takes two characters:

  • The character C you entered and
  • Newline \n

The scanf statement reads only 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 

Enter enter char displayed over two statements, but scanf simply skips (do not wait for uer to enter), this is because this unread newline character in the input buffer is read by this scanf .

To get the next input character correctly, you need an additional scanf .

 scanf("%c",&ch); //with this second line do scan my char 
+8
source
 ch = (char *) malloc( sizeof( char ) * strlen(src) ); 

(char*) is a cast. It states to treat the return value of malloc as a pointer to a char . However, this is not necessary since void* , the return type of malloc is an assignment compatible with all pointer variables in C.

The larger sizeof(char) is false, since sizeof(char) == 1 by definition. Finally, this distribution almost certainly makes one element too small. There must be space for the null terminator.

So this should probably be written:

 ch = malloc(strlen(src)+1); 

As for '1' , it is a char literal. It has type int .

This should not be confused with "1" , which is a string literal of type char* , a pointer to a memory block containing two characters, '1' , followed by \0 .


Regarding question 3, it is not clear to me what you mean, and in any case I will refer to one question in the rule of time in order to justify its non-appeal! Others answered for you.

+5
source

In simple words:

1), since malloc returns a pointer of type void, you set this pointer to a pointer of type char so that the variable ch later contains an array of characters (a string)

2) single quotes are mostly used because the switch statement in C always expects INTEGER, not a string. with a character surrounded by single quotes will return an integer representation of the character.

3) This is a common problem when using scanf, it is mainly caused by the carriage return, which is entered from the previous scanf. I would recommend that you always clear the input before using it.

Here is 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; } 
+4
source
  • This is a cast; it treats the return value of malloc (a void* ) as char* .

  • This is a regular char literal.

+2
source

IN:

 char *ch = (char *) malloc( sizeof( char ) * strlen(src) ); 

The first (char *) returns the return value in char * . In C, this is completely unnecessary, and can mask the failure of #include <stdlib.h> .

In addition, sizeof(char) always 1 , so it is never required.

The literal character '1' is of type int in C.

Most likely, getch returns int . String literal "1" consists of two characters. The number 1 and the end of line NUL . If you used case "1": return value of getch compared with the value of a pointer to "1" (after implicit conversion to int ).

As for scanf , the input buffer may contain input that was not processed by your program.

See also. Why does everyone say not to use scanf? What should i use instead? .

+2
source
  • (char) at the beginning of the first statement is a type. By default, malloc returns a void that must be valid for char * assignment, but in other cases type casting is required.

  • You must correctly use single quotes around charaters.

  • Saying scanf sometimes does not work - it is a meaningless statement without sample code.

+1
source
  • (char *) is cast; this means "treat the next value as a pointer to a char ". In this particular case, it is redundant and is considered bad practice.

  • '1' is a character constant; somewhat unintuitive, it is of type int (this is different from C ++, where character constants are of type char ). "1" will be a string constant, which is actually an expression of an array of type char [2] and has the contents of {'1', 0} .

  • This is due to the fact that in the original stream from the previous input operation, a newline remained. Suppose you type "foo" and press Return; then the input stream will contain the characters 'f', 'o', 'o', '\ n'. The first call to scanf reads and assigns "foo" str , leaving the trailing "\ n" in the input stream. This modified new line is matched by the next call to scanf , because the %c conversion specifier does not skip spaces (unlike any other conversion specifier).

+1
source

I would like to add @Alok Save to the answer. He analyzed it correctly, but for this case there is another simple solution, namely using scanf with a space in the string format as follows:

 scanf(" %c", &variable); 

This is because% c does not absorb newlines like others (e.g.% d). Any spaces in the format string will cause the scan to absorb all consecutive spaces, thereby eliminating the need for another scanf command.

Hope this helps!

0
source

Source: https://habr.com/ru/post/1385750/


All Articles