Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
C newbie here. Was wondering why doesn't this code throw and error. It has missing parentheses and comparison between characters.
#include <stdio.h>
int main(){
while ('a'<'b')
printf("-");
return 0;
}
'a' and 'b' are character integer constants (6.4.4.4) and comparable like any other pair of integers. That comparison is probably evaluated at compile-time leaving you with an infinite loop:
while(1) print("-");
The answer is simple, if you are not using the curly brackets to create a block of code you if's while's etc... will execute the first line of code after the condition\loop etc...
while( condition )
func1();
But this is bad practice, you can fool yourself thinking that the example below will execute in a loop the 3 lines of code, in reality it will execute inside the while just the first one:
while( condition )
func1(); //<- just this line will be executed inside the while loop.
func2();
func3();
Correct way:
while( condition )
{
func1();
func2();
func3();
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so I've written some codes to add two numbers in C, but I can't figure out how to add three instead without spilling the results into another variable or clobbering the caller's version of the variables?
int add(a,b,c)
int a, b, c;
{ int tempr;
for(;b--;++a); // danger
/*tempr = c+a;*/
tempr = a+c;
return (tempr);
}
Here's how, if you must use a function for some reason.
int add(int a,int b,int c) {
return a+b+c;
}
When you define a function in C, you have to define the type of the parameters as well.
Please note the following:
It is an error to define a variable in a function that has the same name as a function parameter
Although not an error, there's no need for parentheses when calling return (tempr);.
return tempr; is perfectly fine
if you use a for loop, ending it with ; immediately after the for statement will result in the following statement not being a part of the loop. This might not be what you had in mind.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Only been coding in C for about a month, but wondered why the while loop wasn't used more often for FIZZBUZZ. Is it not as clean or some other reason? Here's what I came up with:
int main()
{
int num=1;
while(num<=100)
{
if (num%3==0 && num%5==0)
{
printf("FIZZBUZZ!!!\n");
}
else if (num%3==0)
{
printf("FIZZ!!!\n");
}
else if (num%5==0)
{
printf("BUZZ!!!\n");
}
else
{
printf("%d\n", num);
}
num++;
}
return 0;
}
Your loop can be neatly folded into a for loop:
for(int num = 1; num <= 100; ++num)
There are two advantages here:
num is scoped inside the loop, when before it was bleeding into whatever scope followed the while. Restricting variables to the minimum possible scope is a good rule of thumb, because it minimizes the amount of variables to think about at any given point.
The range over which your program will operate is now summarized in a single place: you can see the bounds (1 to 100) and the step (1) inside the for. That kind of ranges is pretty well balanced to be read and memorized quickly before reading the body of the loop. For example, if you now wanted to only check odd numbers, it would be immediately clear from reading the num += 2 in the for header, rather than stumbling upon it at the very end of the loop's body.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to C. I was just interested in making a program which takes two integer input from the user and add it but the condition is that i have to use only one variable. I came up with this code:
#include <stdio.h>
int main()
{
int *a;
scanf("%d%d",a,(a+1));
printf("\nSum=%d",*a+*(a+1));
return 0;
}
scanf() function takes an valid address as an argument and i am passing the value in a(which is not initialised yet). So, how this code worked in Turbo C++?
You are trying to access an area that is not within the scope of the program. Luckily TCC gave it, but I believe if you go on experimenting, results will be undefined.
You can do something like this to solve your problem of adding using 1 variable.
int main()
{
int a;
scanf("%d",&a); //scan the first number
getchar();
a += getchar()-'0'; // get the second number (restricted to 0-9)
printf("%d",a);
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What are the cases in which a loop cannot be unrolled? I've been reading a paper which shows a loop that it says is not able to be unrolled. I cannot actually post the specific code as it is private, however, I am wondering if there is something obvious I am missing in regards to not being able to unroll.
Thanks in advance. If there's any other info that I can try to provide, let me know.
well you can't unroll a loop with any type of recursion in it because it could be infinitely long, also you cant unroll an infinite loop or one with some kind of method for breaking out that isn't incremental
recursion:
method(int x){
if(x > 0)
return 0;
else
return method(x-1);
infinite loop:
while(true){
...
if(some condition)
break;
}
last one:
boolean somevar = true;
while(somevar){
...
if(some condition)
somevar = false;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Ruby's for/in loop can have many statement:
for a in 1..2 do
expression 1
expression 2
..
end
But it seems for loop in C can only have one:
for (a = 0; a < 10; a ++) expression 1;
Is there any way to make multiply statement in the for loop in C?
Yes, formally speaking all loop statements in C take only one statement as the loop body. The same is true for branching statements (like if) and virtually all other statements in C.
However, that one statement can be a compound one. A compound statement begins with {, ends with } and contains an arbitrary number of nested statements inside. (Note that there's no ; at the end of compound statement.)
Use braces for the body of the loop:
for (a = 0; a < 10; a++)
{
doSomething();
doSomethingElse();
}
This concept extends to other things, like if, as well. This should be mentioned right alongside the if and for themselves in any book, etc.
You need to learn C syntax. You put them in a block
for (...) {
expression 1;
expression 2;
}