Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
What will be output if you will compile and execute the following c statement?
if(printf("This is"))
printf(" tricky question");
the output is
This is tricky question
The docs for printf explain why this happens
Return value
Upon successful return, these functions return the number of
characters printed (excluding the null byte used to end output to
strings).
printf("This is") returns 7 so your if condition succeeds.
Return type of printf function is integer which returns number of character it prints including blank spaces.
So printf function inside if condition will return 7. And print massage This is
In if condition any non- zero number means true so else part will not execute. And print massage tricky question
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
int n;
scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 5.
n=scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 1.
Need help understanding the second one.
scanf returns the number of fields it assigns. So, first, it assigns n as 5. Then it returns 1 as it has assigned to only 1 variable. Then, n takes that value.
That's not the correct way to use scanf(). In C standards scanf() always returns the number of conversion it did from stdin, in your case only 1 conversion is done, hence 1 is assigned to n.
In C, if you want to modify a parameter's value inside a function, you need to pass the address of the variable (&var) to that function, and the parameter of the function should be a pointer (type *). Then, you need to de-reference the pointer like (*ptr).
Now, we need to change the value of n inside scanf() function, that's why we need to give the address of n.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Im trying to write a program that takes array inputs from the user and assigns the the odd elements to the first index until there are no more odd elements and the even elements are assigned to the end of the array until they're done so for example
Assuming this a size 10 array,The user enteres 1 for the first element and 2 for the second element and 3 for the third element ,so the final array would have indices 0 and 1 to have the values of 1 and 3 and indice 10 to have the value of 2 and so on and so forth,and here's my code
int main() {
int array1[31];
int array2[31];
for(int i=0;i<31;i++) {
if(scanf("%d",&array1[i])%2==0) {
array2[31-i]=array1[i];
}
else {
array2[i]=array1[i]
}
}
for (int i=0;i<31;i++) {
printf ("%d\t",array2[i]);
}
return 0;
}
But this code is only printing exactly what the user has entered in the exact same order,it's like my if condition doesn't execute and im not sure why,I'm still a beginner in C so I apologize if this problem is too trivial,but yeah any help is appreciated
First of all, learn that scanf() does not return the matched value, it returns the number of items matched. You should
Check the scanf() return value against the number of expected matches (1 in this case)
If scanning is success, use the supplied argument to check the scanned value.
If scanning is failure, clean up the input buffer and ask for input again.
That said, in the code, when you start the loop from i value of 0, you cannot use
array2[31-i]=array1[i];
as, for i value of 0, it will be like
array2[31]=array1[i];
which is off-by-one, you need to use
array2[31-i-1]=array1[i];
So that your array indexes are [0,31).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
This is input: Francetic, Petra#13/12/1930 Trg zrtava Uskoka 156 (Skopje) 800893452/2008
It wont fscanf properly it show's m=0 and that's how I know that while loop was not successful.
while(fscanf(mrtvaciTxt, "%[^,],%[^#]#%d/%d/%d %[^0-9]%d (%[^)]) %[^/]/%[^\n] ",
&pomrli[m].prezime, &pomrli[m].ime,
&pomrli[m].dan,&pomrli[m].mjesec,&pomrli[m].godina, &pomrli[m].adresa,&pomrli[m].brUlice,
&pomrli[m].brOsobne, &pomrli[m].godSmrti )== 9)
{
m++;
}
printf("%d\n\n", m);
How can I fscanf this and is there any tutorial how can i be better at this because it takes me so much time.
Francetic
Petra
13
12
1930
Trg zrtava Uskoka 156 (Skopje)
800893452
2008, I want fscanf to look like this
There are multiple errors in the scanf format.
Here is the correct scanf format string:
" %[^,], %[^#]#%d/%d/%d %[^(](%[^)]) %[^/]/%d"
Here is the list of errors:
there are 11 % while you expect only 9 fields
lacking a space in front of the format string to consume the newline
there is a / in front of the first %d which shouldn’t be there
you are using a [^\n] where you should use [^(]( for the adresa
you are using %*[\n] for unknown reason
you should use [^)] to get the grad
etc.
you modified you question, so I don’t know what else you did.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
For example it prints '(night' despite tokenizing (), why does this happen?
char* word = strtok(&c, ",.;()");
while(word!= NULL)
{
word = strtok(NULL, ",.;()");
printf("%s ", &c);
}
Your code just prints &c on every iteration (whatever that is). You never print word, which is your next token. That's why you never see the results of your tokenization. If you want to see the tokens, you have to print word, not c.
On top of that it is completely unclear why you are applying & operator to your c. If c is a string pointer or a char array, that & there makes no sense at all.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My C code is :
void main()
{
int a=5,b=6,c=11;
clrscr();
printf("%d %d %d");
getch();
}
When i compiled it , it is giving Compile time error but the answer to this question is : 11 6 5
I'm unable to understand how the output is 11 6 5
Please somebody tell the correct output with proper explanation .
Thanks
its behavior is undefined. if you want to show the values of a, b and c you should have coded as below:
printf("%d%d%d",a,b,c);
now the output is:
5 6 11
The program has undefined behaviour, since the printf format string requires that you pass three additional int arguments, which you aren't doing. Anything could happen. Printing certain output is one form of "anything".
Program's behavior is undefined. You well get anything.
You are passing no arguments to the printf function while it is expecting three int type arguments. The statement
printf("%d %d %d",c, b, a);
will give you desired output.
11 6 5