Unknown errors on multiple lines in C [closed] - c

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 5 years ago.
Improve this question
void functionality()
{
int ll = 5
char x = 'A';
for (int i = 0; i < ll; i++)
{
printf("c ", x);
}
}
I am learning C language and I wrote the above snippet. However, it is not running with loads of errors. I cannot seem to find the problem of what is happening here since I followed the code from a tutorial and I have double checked everything.
int main()
{
printf(functionality);
}

on the first glance of your code I can see 3 problems:
the line int ll = 5 is missing a ;
AND
the line printf("c ", x); should be printf("%c ", x);
AND
a missing } at the end
For next time, try to also provide the error codes, please.
The main function should look like this:
int main(){
functionality();
}
The function is void, hence no need to call it in a print statement. Also, we call a function by first stating the name of the function followed by the curly braces. I suggest that you first familiarize yourself with the basic syntax of the language first.

These errors are not unknown:
There is no main function, so there's nothing to run.
You are missing a closing } at the end of this function.
You are missing a ; at the end of int ll = 5;
Your printf call is malformed, Did you want printf("%c ", x);?
Where is your #include <stdio.h> (or does your compiler bring that in automatically?).

you are missing a ; at the end of the line int ll=5
change the c to %c in the printf() function as follows
printf("%c",x);
also make sure you close all the braces properly at the end of the function.
make sure you have a main function in your program,and also include header files

Related

My C code for 2d array creation and printing doesn't return any value [closed]

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 1 year ago.
Improve this question
I have written a piece of c code which I intend to just print out the value of a 2d array
I'm very new to c so this answer might be really basic sorry.
my code compiles fine (gcc) but then doesn't return what I expect. im using ubuntu 18.04 in WSL with gcc as my compiler.
heres my code
#include <stdio.h>
int main(void) {
int array1[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int i2 = 0;
printf("hello");
for(int i = 0; i > 4; i++){
i2 = i;
printf("hi %d", array1[i][i2]);
}
}
returned value
hello
i would expect it to print all my array elements but i dont understand why it doesnt since i dont get an error in gcc so my syntax seems fine.
Thanks.
There are a couple of things wrong in this piece of code. Firstly, the usage of the column index like that (i2 = i) will make you print elements at the same row and column index, like array1[0][0], array1[1][1], and you'll skip things like array1[0][1]. You'd have to fix this by using a second for loop inside the first one, incrementing a second index (check this for example).
The other wrong thing about this piece of code is the condition in the for loop: you want to keep iterating while i<3, not i>4. In that case, you never enter the for loop, since the condition is not met even at the first iteration.

How to correctly format a "for" loop [closed]

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 2 years ago.
Improve this question
I'm quite new to programming and I'm trying to make a simple program with a loop:
#include <stdio.h>
int a = 5;
int b = 0;
int main(void)
{
for (int i = 0; i < a, i++)
{
b++;
}
printf("%i", b);
}
However, when I try to compile I get the errors: relational comparison result unused [-Werror,-Wunused-comparison] and expected ';' in 'for' statement specifier for line 8. I've tried to look at several different sources on how to construct for loops and I just can't see what I'm doing wrong. Any help would be much appreciated.
Here's an example where you can practice interpreting the error statement. As you'll see, it says expected ';' in 'for' statement specificer. That's saying there's a place where you should have a semi-colon but you don't.
In your casse, there should be a semi-colon after the i < a. Right now, you have a comma.
I think you are using , instead of ; in the for loop after the statement.
for (int i = 0; i < a; i++)

Using printf to print a character leads to nothing showing up [closed]

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 3 years ago.
Improve this question
There is a possibility that this may be a duplicate. However, I am just starting out with C and googling the issue only leads to questions which deal with more complicated situations.
Our class has been given some example code on printing variables:
#include <stdio.h>
int main() {
int i=10;
printf("i = %d\n", i);
return 0;
}
The lecture is over, and now it is time for the students to write code. The 1st exercise involves drawing shapes, so I have some code to draw a shape given a character and dimensions. It is not complete, because going further without pruning all of the errors I already have would be foolish.
#include <stdio.h>
void draw_rectangle(char c, int width, int length){
int i,j;
for(i=0; i<width; i++){
for(j=0; j<length; j++){
printf("%c\n",c);
}
printf("\n");
}
}
int main() {
draw_rectangle('*',4,4);
return 0;
}
Nevermind the fact that it seems like you can't declare and initialize variables in for loops. We have bigger fish to fry. I used printf to print a character much like printf was used in the example to print an integer. When I compile and run the code, nothing happens. This is an improvement over the program just crashing, but only slightly. What is the issue? I expected the following output:
****
****
****
****
Edit: Figured out the issue. I forgot to type a.out to check the output.
For me the program is compiling and running great. But you have an error in your print:
void draw_rectangle(char c, int width, int length){
int i,j;
for(i=0; i<width; i++){
for(j=0; j<length; j++){
printf("%c\n",c); // The error is here must be printf("%c", c);
}
printf("\n");
}
}
Your print("%c\n", c); must be only "%c" because you don't want to print carriage return at each symbols, only at the end of lines.
How are you testing it ? Maybe you are not compiling or running it correctly.
For instance, if your code is in a file called program.c and you are compiling it on linux:
gcc program.c
./a.out

What will be the result of next function and array in main? [closed]

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 5 years ago.
Improve this question
today i tried to do something new,but i didn't do that correct.Would anyone be able to do that and explain why is it so like that? Thank you in advance
#include<stdio.h>
void function(int a[],int n)/*The definition of function with void type,with parameters
int a[],int n */
{
int i;// declared count,type integer//
for(i=0;i<n;i++)//count goes from 0,to <n,and increment all time while goes//
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
printf("\n");// printing the newline //
}
main()
{
int a[]={1,2,3,4,5,6,7}; // declaration of array with 7 elements //
int n=5;// declaration of variable n type integer value of 5 //
function(a,n) // calling the function with parametres a,n//
} // end of sequence //
In my case i got the result of the 1,2,3,4,because i tought that the count goes from 1,to the one number less than n=5,but the IDE show the result of 135 ,i think the problem in my way is with counter...but all advices are welcome,thanks
Please make sure you are posting properly formatted valid C code.
Note that what you get is not one hundred and thirty five, but one, three, and five. You get that because you are incrementing the loop counter twice.
Here's a working, more readable version:
#include <stdio.h>
void function(int a[],int n)
{
int i;
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
int main(void)
{
int a[]={1,2,3,4,5,6,7};
int n=5;
function(a,n);
return 0;
}
replace
printf("%d",a[i++]);// printing on the screen integers (a[i],i=i+1)//
with
printf("%d",a[i]);// printing on the screen integers (a[i],i=i+1)//
in your code you were incrementing i twice. Once in the while and once in the a[i++]

Where did i make a mistake in my program i think its a logical error but i cant quite find it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I have this program that I wrote, it complies but I don't think it outputs correctly. Did I make a mistake here?
Here's my program:
#include <stdio.h>
void main(void)
{
int loop_counter = -8;
int user_input = 9;
char c1 = '9';
char c2 = 43;
while(loop_counter != 21);
{
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
loop_counter = loop_counter + 1;
loop_counter++;
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
getchar();
}
printf("loop exit\n\n");
getchar();
}
The biggest problem is probably that you have not articulated what you are hoping the code will do or what you intended the code to do or what you think it does. I am assuming you just need some help so my attempt to do so is below.
Running this through the compiler and interpreting the error messages helps a little.
Right off the bat, the compiler doesn't like your call to main. The compiler offers a suggestion that will successfully fix this error, so just follow the advice (and don't forget to add a
return 0;
statement before you exit main.
Second warning the compiler generates is that you have a semicolon at the end of your while statement. It also tells you how to fix it. Follow the instructions and you should be able to generate an executable.
You will still, however, have problems at runtime. This gets back to what is your intent.
With errors above corrected, your loop_counter variable enters the while loop initialized to -8, increases by 1 on line 19 and again increases by 1 on line 20. A call is made to getchar() but no input is given. Also lines 7, 8, and 9 are not used by your program.
Hope this gives you some direction. :)
The biggest error is
while(loop_counter != 21);
where the trailing ; will make the loop infinite.

Resources