When i compile and run this program my input string is not same as output.
#include<stdio.h>
int main()
{
int n; char ch[100];
scanf("%d : %5s", &n, ch);
printf("%d : %s", n, ch); // there is some problem with output of the string
return 0;
}
input:
45
asdf
output:
45 : ∟sëuⁿ■a
The scanf part will read until it hits the colon, but it will not read the colon itself, which means the following integer will not be read correctly and the rest of the string will be parsed incorrectly (if at all).
Try removing the colon (:) from the scanf
#include <stdio.h>
int main()
{
int n; char ch[100];
scanf("%d %5s", &n, ch);
printf("%d : %s", n, ch);
return 0;
}
get rid of the ":" in scanf(), nothing is read after the "%d" input
Related
Given task is:
Enter 10 characters. For each character entered the corresponding function prints whether it is a digit 0-9 or not.(Also I use older compiler if anyone concerns about my "gets()" and goal is to do this without pointers.)
So far I tried something like this, but for some reason it does not work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1];
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
}
for(i=0;i<10;i++){
if(strcmp(character[i],zero)>=0 && strcmp(character[i],nine)<=0){
printf("%d. character '%c' is a digit.", i, character[i]);
}
else{
printf("%d. character '%c' is not a digit.", i, character[i]);
}
putchar('\n');
}
return 0;
}
Also I tried this, but it can not output correctly characters:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1], pom[10][1];
int num_character[10];
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
strcpy(pom[i],character[i]);
num_character[i]=atoi(character[i]);
}
for(i=0;i<10;i++){
if(num_character[i]!=0){
printf("Character '%c' is digit.", pom[i]);
}
else{
printf("Character '%c' is not digit.", pom[i]);
}
putchar('\n');
}
return 0;
}
isdigit() also does not work after I include ctype header.
You are doing several things wrong here, gets is never recommended and fgets will put new line character in the end.
The syntax for strcmp is:
int strcmp(const char *s1, const char *s2);
You need two strings as input in strcmp but you are using a string and a character.
Here, it is better to use a character array than a 2D array.
#include <stdio.h>
int main(void)
{
char character[10]; //a simple character array
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
scanf(" %c", &character[i]); //scan characters
}
for(i=0;i<10;i++)
{
if(character[i] >= zero && character[i] <= nine) //print
printf("%d. %c is a digit \n", i+1, character[i]);
else
printf("%d. %c is not a digit \n", i+1, character[i]);
}
}
#include <stdio.h>
int main() {
char str[11];
int num;
scanf("%s %i", &str, &num);
printf("You typed %s and %i\n", str, num);
if (num == 0)
{
printf (str);
}
else
{
printf (str[num]);
}
return 0;
}
The input is a phone number and a digit. If the digit is not 0, then print number[digit].
It the last second line, I tried both str[num] and &str[num]. The first case will cause Segmentation fault (core dumped). The second one will return a string but not a char.
6479378812 1
You typed 6479378812 and 1
479378812
But what I want is the second digit, which is 4. How can I fix it?
You need to specify the formatting string for your printfs. The first needs a string format "%s", the second a single character "%c". You can find all the details about the format used in the formatting string of printf() here.
#include <stdio.h>
int main() {
char str[11];
int num;
scanf("%s %i", &str, &num);
printf("You typed %s and %i\n", str, num);
if (num == 0)
{
printf ("%s", str);
}
else
{
printf ("%c", str[num]);
}
return 0;
}
You have to specify what do you want to print with the printf function.
printf("%c", str[num]);
With %c you tell to the function that you want to print a character.
If you want to print a whole string, you shall use %s.
By the way, you shall always specify the formatting string, because it is unsafe if you do not specify it. https://en.wikipedia.org/wiki/Uncontrolled_format_string
Im doing some work for Uni and wrote a programm which stores Integers in an char Array and converts them to their ASCII Values and prints them at the end. My code did not work before and only started working when i changed "%c" to "%i" in my scanf line. My question: Why does it have to be "%i" when i wanna store those Numbers in an char Array and not an Int Array. Thanks!
My code:
#include <stdio.h>
int main()
{
int i; /counter
char numbers[12];
printf("Please enter 12 Numbers\n");
for(i = 0; i < 12; i++){
printf("please enter the %i. Number\n", i+1);
scanf("%i", &numbers[i]);// <-- changed "%c" to "%i" and it worked.why?
}
for(i = 0; i < 12;i++){
printf("The %i.ASCII value is %i and has the Char %c\n", i+1, numbers[i], numbers[i]);
}
return 0;
}
%c is for reading a single character. So for example if you type in "123", scanf will read '1' into the char variable and leaves the rest in the buffer.
On the other side %i is the specifier for int and will therefore lead to undefined behavior when trying to read in a char.
I think what you are looking for is the %hhi specifier, which reads a number into a char variable.
#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
I have to take input in the following format
S1 S2 S3
where S1 is a character and S2,S3 are integers
for example
3
A 123 452
D 450 53
B 330 672
(where the '3' represents the number of queries)
Now I've written the following code for it :
while(i<=Q){
scanf("%c %d %d",&ch,&index,&num);
printf("%c %d %d\n",ch,index,num);
i++;
}
However, for the above shown three values I am getting the following output
0 755130840
A 123 452
123 452
with an extra line at the top and that large value (here 755130840) changing every time.
Where am I going wrong?? I even tried scanning the 3 values individually and flushing the input stream before each scan statement. However, It doesn't help either.
Given the two blank lines, I believe the newline ('\n') is being stored in some variable.How do I handle it?
Add a space before %c in scanf. This will allow scanf to skip any number of white spaces before reading ch.
scanf with blank skips white space (including newlines) and reads the next character that is not white space.
Here is a code , this would be working fine.
#include <stdio.h>
int main(void) {
// your code goes here
int i =0;
int Q = 2;
char ch;
int index;
int num;
while(i<=Q){
scanf(" %c %d %d",&ch,&index,&num);
printf("%c %d %d\n",ch,index,num);
i++;
}
return 0;
}
do you want something like this?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, count, numone, numtwo;
char charip;
printf("Enter numbr of elem:\t");
scanf("%d", &num);
if (num < 0)
{
printf("Enter positive value!!!!!\n");
exit (-1);
}
count = 0;
while (count < num)
{
getchar();
scanf ("%c %d %d", &charip, &numone, &numtwo) ;
printf("%c %d %d\n", charip, numone, numtwo);
count++;
}
return 0;
}