I am currently taking a basic C course and I was wondering why my code below doesn't run.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char string[4];
printf("Enter some text\n");
scanf(" %c %c %c", &string[0], &string[1], &string[2]);
printf("You Entered ");
int i;
for (i = 0; i < 4; i++){
printf("%c",string[i]);
}
return 0;
}
Xcode said there is an errr with my scanf line.
I was hoping to type in "a b c d" and expect
a "You entered abcd";
This code should run (albeit with a bug). I suspect you need to configure the Xcode build options correctly.
As for the bug, you have an array of four chars, but you are only scanning for three. Add another %c and &string[3] to your scanf line.
Here's an ideone snippet showing the modified code in action
#include <stdio.h>
int main()
{
char string[4];
int i;
printf("Enter some text\n");
scanf("%c %c %c %c", &string[0], &string[1], &string[2], &string[3]);
printf("You Entered ");
for (i = 0; i < 4; i++){
printf("%c", string[i]);
}
return 0;
}
This compiles just fine on the Mac command line (assuming the source is in "test.c")
$ cc -g -Wall -o test test.c
./test
Enter some text
a b c d
You Entered abcd
Also note that this particular snippet requires only stdio.h (man scanf and man printf will tell you which header to use).
How about executing this code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char strin[10];
printf("Enter some text\n");
scanf("%s", strin);
printf("You Entered %s",strin);
return 0;
}
The following code gives you :
Enter some text
abcd
You Entered abcd
Related
Consider:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf("%[^\n]s", &name);
}
}
Whenever I am entering the value of n, it just prints (Enter n values) and exits the program. The for loop never runs. It ran successfully for the first time, but after that it just exits the program.
There were some answers that said it will not print anything. I don’t want it to print just to take input n times. It is not doing that.
My aim is to take n as input and then take strings of names (like harry, robin, etc.) n number of times as input.
Your code is a little incomplete. And there are a few errors here: scanf ("%[^\n]s", &name)
Do this and everything will be fine:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf(" %d", &n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf(" %99[^\n]", name);
printf("%s\n", name);
}
return 0;
}
scanf is particularly unsuited for user input.
You probably want this:
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("Enter %d values\n", n);
for (int i = 0; i < n; i++)
{
// the space at the beginning of "%[^\n]"
// gets rid of the \n which stays in the input buffer
scanf(" %[^\n]", name); // also there sis no 's' at the end of the "%[^\n]" specifier
printf("name = %s\n", name); // for testing purposes
}
}
But this doesn't actually make much sense because the program is asking for n names, but at each run of the for loop the previous name will be overwritten with the new name.
Also be aware that scanf("%[^\n]", name); is problematic because if the user types more than 99 characters you'll get a buffer overflow.
I started learning C language yesterday and made a simple binary coverter. It works fine on my PC, however, it doesn't work on most online code runners(compilers) except for could9. It looks like the scanf function are not compatible with online IDE in general?
Here's the code.
#include <stdio.h>
#include <string.h>
int main(){
int a;
int b;
int c[255];
char opt[100];
printf("10進数の値を入力してください > ");
scanf("%d", &a);
printf("計算式を表示しますか? y(yes) or n(no) > ");
scanf("%s", opt);
if(strcmp(opt, "y") == 0){
printf("\n計算式: \n");
}
int i = 0;
while(a > 0){
b = a / 2;
c[i] = a % 2;
if(strcmp(opt, "y") == 0){
printf("%d ÷ 2 = %d 余り %d\n", a, b, c[i]);
}
a = b;
i++;
}
printf("\n2進数: ");
int j;
for(j = i-1; j >= 0; j--){
printf("%d", c[j]);
}
printf("\n");
return 0;
}
Any advice will be much appreciated.
My psychic powers tell me you are expecting these online compilers to prompt you for input (like running the program in a terminal window). Unfortunately, this is not the case for most online compilers. Instead, there is a text box somewhere where you type in all of the input that you want in your program's standard input.
For example, take this small program:
#include <stdio.h>
int main()
{
char c;
printf("Gimme a character! ");
fflush(stdout);
scanf(" %c", &c);
printf("You typed in '%c'! Yay! :)\n", c);
}
Compiling and running this in a terminal could produce the following in the window (input is in bold):
Gimme a character! f
You typed in 'f'! Yay! :)
But running this in an online compiler that doesn't prompt you for input could look like this:
Gimme a character! You typed in ' '! Yay! :)
By typing input into the text box provided (where it is depends on the online compiler) you can give input to the program that way.
STDIN:
f
Output:
Gimme a character! You typed in 'f'! Yay! :)
I'm writing a program that will sort words you input alphabetically, but I found it impossible to progress because the loop doesn't work as intended.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
char string[50][50];
int i, n;
printf("Insert the number of strings: ");
scanf("%d ", &n);
for(i=0; i < n; i++)
{
printf("Insert %d. string: ", i+1);
fgets(string[i],50,stdin);
}
return 0;
}
I tried using gets() and tried to use fgets(), but the result is the same. It prints:
Insert 1. string: Insert 2. string:
Then you can insert strings, but 1 less than specified.
you have semicolon after for loop !!!
I'm a newbie in programming. It is confusing me when I taking input of a char array after scanning an integer. It is not working properly.
The code is following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
Output is following:
The problem is due to the white spaces.After scanf("%d",&x); the last entered '\n' character is taken and saved the string a of scanf("%[^\n]",a).
To avoid this give space in scanf() statement
scanf(" %[^\n]",a);//give a space
Why to give a space?
By giving a space,the compiler consumes the '\n' character or any
other white space ('\0','\t' or ' ' ) from the previous scanf()
your code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf(" %[^\n]",a);//give a space
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
fflush(stdin);
scanf("%[^\n]",a);
fflush(stdin);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
This also works. The same goes here, /0 at the end adds up to the character scan and interferes. Using fflush(stdin) will discard any unnecessary input data including the /0.
Correct me if I am wrong as I too am a newbie at coding. :p
Replace scanf("%[^\n]",a); with scanf(" %99[^\n]", a);
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf("%s",a); // get char array without inputing space
scanf(" %99[^\n]", a); // get char array, allowing inputing space
scanf("%d",&y);
printf("%d\n%s\n%d\n",x,a,y);
return 0;
}
Instead of %d use %d\n to consume the newline, so that the following command will not just read nothing:
scanf("%d\n",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
#include <stdio.h>
#include <string.h>
void main()
{
int N,i,s;
char op;
scanf("%d",&N);
int a[N];
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
scanf("%c %d",&op,&s);
printf("%c %d",op,s);
}
If i include this for loop in my code and then execute this code, scanf doesn't read my input.
If i insert value of opand s is R and 5 then the output is garbage.
And if remove the for loop, then i printf print the correct answer.
You need to consume the trailing \n character. The easiest way to do it is to write:
scanf(" %c %d",&op,&s); //notice the space before %c