0

i have this simple code that accepts numbers from the standard input and print them , i wrote this code on code blocks and it works .. now i want to run the same code on eclipse and i don't know how it's supposed to work ? also after that i run it on eclipse i need to run it on bash where i have a directory that includes tests and i nee to check my code with these tests but i can't figure how to compile this c program there ! this is this is the simple code :

#include <stdio.h>
#include <stdlib.h>


int main()
{
      int x;
    int i;
    int k;
    int a;
    printf("Enter size of input:\n");
    scanf("%d",&x);
    if (x<0){
      printf("Invalid size\n");
      return 0;
    }
    int *numbers=malloc(sizeof(int)*x);
    printf("Enter numbers:\n");
    for(i=0;i<x;++i){
        scanf("%d",&numbers[i]);
    }
    for(k=0;k<x;++k)
    {
        a=numbers[k];
             printf("The number %d is a power of 2 \n",a);
        }

  return 0;

}

also i am tried to compile this code on bash with this line :

-std=c99 -Wall -pedantic-errors -Werror -DNDEBUG main.c compiled.o 

what am i doing wrong ?

  • anyhelp anyone ? a hint maybe :| – newprogrammerha Oct 29 '17 at 23:09
  • What is the output of your compiler? What compiler are you using? Btw, you didn't tell the compiler what to do with compiled.o, add the `-o` option before the `compiled.o` part. – redxef Oct 29 '17 at 23:21
  • yes i forgot the -o !! but that didn't help :\ i am using now eclipse .. i don't know if this answer you question but i am using minGW GCC toolchain .. and i am working with gdb' – newprogrammerha Oct 29 '17 at 23:28
  • i just want to know how to accept parameter in eclipse ! should i give it the input from bash line command ? and in this case how i am i supposed to check the code in eclipse? – newprogrammerha Oct 29 '17 at 23:30
  • Your program behaves the same, no matter where you start it from. Try creating a new C project within eclipse and then choosing the compiler. After that hitting run should be enough. The console of eclipse will then run the program in the same way if you had launched it in a shell. – redxef Oct 29 '17 at 23:32
  • you didn't understand my problem ! i am just confuse of how can my program run on elipse ? because the code i wrote supposed to exept input from the keyboard but when i run it on eclipse it doesn't allow me to input anything – newprogrammerha Oct 29 '17 at 23:40

3 Answers3

0

Use the following command. Works like a charm in ubuntu bash. You can just input the values in the terminal, after running the program.

gcc main.c -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG -o main

Above a command generates a binary with the name main, Run the main file using following command. ./main Then enter the your values.

yadhu
  • 1,253
  • 14
  • 25
  • Post the output of the compiler, otherwise we can't help you. – redxef Oct 29 '17 at 23:38
  • i get so many errors for example : main.c:1: error: stray â\1â in program main.c:1: error: stray â\4â in program main.c:1:4: error: null character(s) ignored – newprogrammerha Oct 29 '17 at 23:38
  • Then your file contains some hidden characters, Looks like you copied source code from html file or something, Try to type the code manually or enable viewing hidden characters in the editor. – yadhu Oct 30 '17 at 16:08
0
  1. To compile gcc main.c -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG -o main
  2. to run from bash and accept arguments from a file named 'testcasefile' ;type following main < (path to file)/testcasefile. 3 as for howv to run from eclipse refer to
    https://stackoverflow.com/a/16921891/6721448
mage
  • 3
  • 5
  • i wrote whis line in bash gcc main.c -std=c99 -Wall -pedantic-errors -Werror -DNDEBUG -o compiled.o and i got so many errors that repeated themself the first 5 lines in the error i get : (i got many lines like this when i write this command line ) .. – newprogrammerha Oct 30 '17 at 00:04
  • main.c:1: error: stray â\1â in program main.c:1: error: stray â\4â in program main.c:1: error: stray â\332â in program main.c:1: error: stray â\3â in program main.c:1:11: error: null character(s) ignored main.c:1:14: error: null character(s) ignored main.c:1: error: stray â\4â in program main.c:1: error: stray â\1â in program main.c:1: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before â.â token main.c:1:26: error: null character(s) ignored – newprogrammerha Oct 30 '17 at 00:08
0
  • Let's start anew. Make a new directory for your project main inside project directory a directory for test case testcase

    mkdir -p main main/testcase

  • now make test cases

Test case 1 2 3 4

  • compile main.c as follows

    gcc - std=c99 -Wall -o main main.c

  • execute out put main with test case

    ./ main < testcase/testcase1

mage
  • 3
  • 5