Permission denied when trying to run C program in terminal [closed] - c

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 years ago.
Improve this question
I'm completely new to programming. I'm actually learning from a Harvard class on iTunes U. When trying to code along side the instructor I've ran into a problem. I can't run my .c program in terminal. I've tried the sudo commands, and I've searched with Google and I can't seem to find an answer, probably because I'm so new to programming. It's probably something I've overlooked or I just don't understand yet.
#include <stdio.h>
int
main(void)
{
printf("temperature in f: ");
float f = GetFloat();
float c=f / 9.0 * (f-32);
Printf("%. if F = %. if c\n", f, c)
I'm using Sublime text editor on a MacBook with Mac OS X Yosemite (10.10.x).

Your compiler should have warn you about some errors:
// string.c
#include <stdio.h>
int main(void) // There must be a return statement
{
printf("temperature in f: ");
float f = GetFloat();
float c = f / 9.0 * (f-32);
printf("%f if F = %f if c\n", f, c); // Missing ';' is the most common syntax error
return 0; // Here is the return
} // Do not forget to close your brackets
When you do:
gcc string.c -o myprogram
It will tell you what is wrong in your program. Once you have fixed all the errors you can run the program with:
./myprogram
Understand that you cannot run a C-file: the .c contains human-readable instructions for the machine. You have to compile it, i.e. translate it into a language that your computer will understand : it is roughly your compiled myprogram (and you do not want to open it to look what it contains, it will burn your eyes :p).

Just adding to the below answer your compiler should throw the below error also:
undefined reference to `Printf'
check case of your printf()

Related

What is wrong with using %var after format specifier in a scanf call? [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
main ()
{
int a,b,toplam;
float ort;
printif("iki sayi girin :");
scanf("%d,%d",%a,%b);
toplam=a+b;
ort=toplam/2;
printif("Ortalama= %f olarak hesaplandı.",ort);
}
error: expected expression before '%' token scanf("%d,%d",%a,%b);
What was my mistake?
Replace scanf("%d,%d",%a,%b); with scanf("%d,%d",&a,&b);
Check this answer for more information.
You need to replace the '%' before a and b with '&'.
Also you need to use printf not printif.
int a,b,toplam;
float ort;
printf("iki sayi girin :");
scanf("%d,%d",&a,&b);
toplam=a+b;
ort=toplam/2;
printf("Ortalama= %f olarak hesaplandı.",ort);
return 0;
Read Modern C and see this C reference
Your scanf statement should be (using the & prefix "address-of" operator):
scanf("%d,%d",&a,&b);
and your code is incomplete, since scanf can fail. You should read its documentation.
Consider coding a test against such failure using:
if (scanf("%d,%d", &a, &b) < 2) {
perror("scanf failure");
exit(EXIT_FAILURE);
}
Read also the documentation of your C compiler, e.g. GCC, and of your debugger, e.g. GDB. You could compile your code with gcc -Wall -Wextra -g to get all warnings and debug information, and later use gdb to debug your executable and understand its runtime behavior.
Your code is missing a #include <stdio.h> before your main.
The printif is wrong. You want to use printf (and read its documentation).
Consider studying for inspiration the source code of some existing free software, such as GNU make or GNU bash.

C ignores the return value [closed]

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 6 years ago.
Improve this question
I am working with a big MPI project. In one of the files (let's call it as one.c) I have a function like this one:
#include two.h
void foo(){
printf("the value is %f", bar(1));
}
Where in file two.h I have (apart from other function def):
double bar(int);
Finally, in file two.c I have (apart from other stuff):
static double a[2] = {0.0,4.0};
double bar(int temp){
return a[temp];
}
From the above code I should obtain 4.0 but I obtain always 0.0, even if I just put:
return 3.0;
I cannot understand the problem because even if in two.c I do:
double bar(int temp){
printf("temp is %d", temp);
return a[temp];
}
I obtain 1 for temp and a[1] returns also 4.0. So the problem is that is not connecting fine the value between files. Any idea?
NOTE: As I said, it is a BIG MPI project. I am not going to post here all the code because is not reasonable. I just add what I though it was necessary. Of course all is linked, otherwise I would not ask it. I am asking this because it is a very strange thing that I though some strange thing could scape from my knowledge. At the end, I have realised that if I call another double function in two.c, then, if I call again my foo function I obtain the good value but still I do not know why because as I said, bar was being recognised, temp was recognised, and a[temp] had the correct value.
NOTE2: I think that my problem was when doing make mpi. Maybe when saving the files or when I was doing the make, the date was not updated and at the end, files where not built again.
Use #include "two.h".
Otherwise it works for me.
If I make files one.c two.c, and two.h to your specification, add
main.c:
void foo();
imt main(){ foo(); }
compile them:
for c in *.c; do gcc $c -c; done
link them
gcc *.o
and run the result
./a.out
I get:
the value is 4.000000

C program sqrt not working [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I also entered include<math.h> but it still doesnt work. People are saying to enter -Im but im new to this where do I put -Im and how do I fix this.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
float a=0, b=0, c=0, root1=0, root2=0;
printf("Enter the value of a,b and c to determine the roots\n");
scanf("%f%f%f",&a,&b,&c);
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("The first roots of the quadratic equation are\nFirst root=%.1f\nSecond root=%.1f",root1,root2);
return 0;
}
You have a copy-paste bug here:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
should be:
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root2=(-b-sqrt(b*b-4*a*c))/(2*a);
Also you may need to link with the math library, e.g.
$ gcc -Wall foo.c -o foo -lm
Two things: first you copy pasted "root1" twice so you will lose the "plus" value and root2 will be zero. Second, for the benefits of others, the problem is most probably at compile time and the googled answer is there:
http://www.cs.cf.ac.uk/Dave/C/node17.html
And you should test for imaginary values:
if(b*b-4*a*c < 0){
printf("error: complex solution unsupported, see http://en.wikipedia.org/wiki/Square_root\n");
exit(1);
}

system command in C forced to run in serial on OSX? [closed]

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 8 years ago.
Improve this question
I am using Mac OSX 10.8.4 and programming in C. I trying to use openmp and I am compiling with gcc-mp-4.7. I am working in bash. Currently I have an executable (I will call executable1 in the program) which I am trying to run in parallel by using a system call inside of an openmp parallel for loop. The example code is as follows:
my_omp.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
void main() {
int n = 100;
double var1 = 65.4;
char place[100] = "/under/a/rock";
double var2 = 4.5e4;
double var3;
char program[200];
int i;
#pragma omp parallel for private(program,var3)
for (i=0; i<=n; i++) {
var3 = var1*pow(var2,i);
sprintf(program,"./executable1 %.15e %s %.15e %d", var1, place, var2, var3, i);
printf("%s \n", program);
system(program);
}
}
I compile the program using gcc-mp-4.7 -fopenmp my_omp.c, then run the newly compiled executable, (differently named than exectuable1).
What seems to happen is that 8 (which I believe is the number of "cpus" openmp thinks I have) of the print statements will appear in the stdout (terminal) and then it will run only a single call of the executable1, then when it finishes it prints out another of the printf program lines, then runs another executable1 until it finishes the for loop (I know this because executable1 is extremely verbose, and it would be obvious is two where running as numbers printed to stdout would be out of synch and appearing in at multiples).
So it seems maybe that the printf is running in parallel, but for some reason the system() command is not? Does anyone have any ideas?
Thanks for any help you can offer.
UPDATE:
I have gotten this exact code to run properly on a lunix distribution with a different compiler, I will look into finding a better compiler to use in Mac OSX and see if that works.
The system(3) library call in OS X is implemented using a global mutex lock - see the system.c file in the source code of OS X's C library:
#if __DARWIN_UNIX03
pthread_mutex_lock(&__systemfn_mutex);
#endif /* __DARWIN_UNIX03 */
...
#if __DARWIN_UNIX03
pthread_mutex_unlock(&__systemfn_mutex);
#endif /* __DARWIN_UNIX03 */
Therefore when one thread calls into system(3), all other threads have to wait for the first call to finish, resulting in serialised execution.

libyahoo Segmentation fault [closed]

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 8 years ago.
Improve this question
I wrote this code:
#include <libyahoo2/yahoo2.h>
#include <libyahoo2/yahoo2_callbacks.h>
int main() {
int id ;
char username[255] = "slam";
char password[255] = "ss" ;
id = yahoo_init(username, password);
enum yahoo_status mYahoo ;
mYahoo = YAHOO_STATUS_AVAILABLE ;
yahoo_login(id , mYahoo );
return 0;
}
Compile it, gcc -l yahoo2 y.c -o yahoo and run it with ./yahoo gives me a error: Segmentation fault
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x001379b1 in yahoo_login (id=1, initial=0) at libyahoo2.c:1735
line 1735 code is:
tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->client_id,
host, yss->pager_port, yahoo_connected, ccd, 0);
and see this :
(gdb) list YAHOO_CALLBACK
Function "YAHOO_CALLBACK" not defined.
How do I debug this?
How do I debug this?
Execute these commands:
(gdb) print yd->client_id
(gdb) print yss->pager_port
My guess would be that one or both of the above commands will fail, because yd or yss is NULL.
If so, the problem is earlier in libyahoo2.c, and it (apparently) doesn't check for errors properly.
The reason you can't list YAHOO_CALLBACK is most likely that it is a macro. Look in libyahoo2/yahoo2_callbacks.h -- it's very likely to be defined there.
Also, your link command line:
gcc -l yahoo2 y.c -o yahoo
is totally bogus. Correct command line should be:
gcc y.c -o yahoo -lyahoo2
You may want to read this explanation to understand why order of sources and libraries on command line matters.

Resources