You have to make four changes:
Change
char * str[25];to
char str[25];as you want an array of 25
chars, not an array of 25 pointers tochar.Change
char car;to
int car;as
getchar()returns anint, not achar.Change
scanf("%[^\n]s", &str);to
scanf( "%24[^\n]", str);which tells
scanfto- Ignore all whitespace characters, if any.
- Scan a maximum of 24 characters (+1 for the Nul-terminator
'\0') or until a\nand store it instr.
Change
printf("\nThe sentence is %s, and the character is %s\n", str, car);to
printf("\nThe sentence is %s, and the character is %c\n", str, car);as the correct format specifier for a
charis%c, not%s.
Videos
You have to make four changes:
Change
char * str[25];to
char str[25];as you want an array of 25
chars, not an array of 25 pointers tochar.Change
char car;to
int car;as
getchar()returns anint, not achar.Change
scanf("%[^\n]s", &str);to
scanf( "%24[^\n]", str);which tells
scanfto- Ignore all whitespace characters, if any.
- Scan a maximum of 24 characters (+1 for the Nul-terminator
'\0') or until a\nand store it instr.
Change
printf("\nThe sentence is %s, and the character is %s\n", str, car);to
printf("\nThe sentence is %s, and the character is %c\n", str, car);as the correct format specifier for a
charis%c, not%s.
str is an array of 25 pointers to char, not an array of char. So change its declaration to
char str[25];
And you cannot use scanf to read sentences--it stops reading at the first whitespace, so use fgets to read the sentence instead.
And in your last printf, you need the %c specifier to print characters, not %s.
You also need to flush the standard input, because there is a '\n' remaining in stdin, so you need to throw those characters out.
The revised program is now
#include <stdio.h>
void flush();
int main()
{
char str[25], car;
printf("Enter a character\n");
car = getchar();
flush();
printf("Enter a sentence\n");
fgets(str, 25, stdin);
printf("\nThe sentence is %s, and the character is %c\n", str, car);
return 0;
}
void flush()
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
;
}
I am trying to develop a code that would delay the printing of letters onto the console so that it looks like its being typed rather than displaying it all at once. To do that I'm trying to scan the number of characters in a string until it reaches a newline character. Then activating the sleep function while it types every letter in a for loop that reads every character and displays it. But I'm having trouble trying to figure out how to scan the letters, any suggestions on how I could accomplish this?
name is a pointer, and &name returns the address of the variable name, so the scanf is putting the name you enter into the pointer itself.
For example, if you enter ABC then the pointer will be 0x00434241 (if the CPU is little-endian) or 0x41434200 (if the CPU is big-endian), where 0x41 is the character code for 'A', 0x42 is the character code for 'B', etc.
You should allocate memory into which the entered name can be stored and then pass a pointer to it to scanf.
Here's an example allocating on the stack:
#include <stdio.h>
#define MAX_NAME_LENGTH 256
int main (void) {
char name[MAX_NAME_LENGTH];
printf("Whats your name? ");
scanf("%s", name);
printf("\nyour name is %s", name);
return 0;
}
Alternatively You can use gets too, to avoid the Standard Input buffer in cases that you have more than 2 sequential inputs.
#include <stdio.h>
#define LENGTH 256
int main (void) {
char name[LENGTH];
printf( "Whats your name? " );
fgets( name, sizeof( name ), stdin );
printf( "\nYour name is %s", name );
return 0;
}
Hi,
So tutorialspoint, geeksforgeek, javatpoint, those websites that claim to have C/C++ tutorials are a bunch of shitty Indian garbage tutorials that are not suited for beginners at all. I'm trying to learn how to declare a string variable with C and read input with scanf() and I get a bunch of different information from all three sites and I'm really frustrated and confused. Do you do?
#include <stdio.h>
int main(void)
{
char name[100000000];
printf("What's your name?\n");
scanf("%c", char);
}Or is it
#inclue <stdio.h>
int main(void)
{
char** x;
printf("What's your name?\n");
scanf("%c", x)
}Or is it
#include <stdio.h>
int main(void)
{
string x;
printf("What's your name?\n");
scacnf("%s", x);
}Or is it
#include <stdio.h>
int main(void)
{
char **name;
printf("What's your name?\n");
scanf("%c", name)What the fuck is it? Is it %c or %s for the format code, and it is char name[] or char **var_name or **char var_name?? Someone please tell me. All these online tutorials are fucking GARBAGE and I can't find a clear answer anywehere