-4

I encountered a problem while working on a project. I know there are many simillar questions that are answered, but regarding this special one I could not find any help. I am getting the following error:

Compiling main.c
main.c:42:1: error: expected identifier or '(' before '~' token
~
^
Makefile:47: recipe for target 'obj/main.o' failed
make: *** [obj/main.o] Error 1

EDIT: I deleted the last lines of the code, but the error still occures at the line after the last '}'.

The project is about the PageRank Algorithm, using options in the console for choosing what algorithm is wished to be used. I am trying to read or use the options in the command line, but the error stops me from even looking at the semantic of my program.

/*
* main.c
*
*Programmierung 2 - Projekt 2 (PageRank)
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

#include "utils.h" //is existing in the Directory


int main (int argc, char *const *argv) {
    //initialize the random number generator
    rand_init();

    printf("You gave %d command line arguments%c\n", argc-1, argc==1 ? '.' : ':');
    int graph;
    int i = 1;
    char * h = "-h show this help. \n";
    char * p = "...";
    char * m = "...";
    char * r = "...";
    char * s = "...";
    while ((graph = getopt(argc, argv, "hmprs")) != -1) {

        switch (graph) {
            default : printf("make -h | -m | -p | -r | -s "); break;
            case 'h' : printf("%s %s %s %s %s"), h, m, p, r, s); break;
            //this-like outcommended code like the one above
            //and again 
            //and once more
            //and a final one
        }
    printf(" - %s\n", argv[i]);
    i++;
    }
exit(0);
}

One more thing: I encountered a problem regarding the lengh of the case 'h' : printf(), so I outcoded the text in multiple chars. If you need more information about anything, ask me.

17
  • In Line 42 the first letter, wait a sec Jun 4, 2015 at 2:44
  • 2
    Where is you line 42? Your posted code only have 32 lines.
    – Tim3880
    Jun 4, 2015 at 2:44
  • 1
    check this line case 'h' : printf("%s %s %s %s %s"), h, m, p, r, s); break;its having extra bracket
    – aakansha
    Jun 4, 2015 at 2:44
  • I reopened the file and there are now more lines, i added them. Jun 4, 2015 at 2:47
  • 1
    The only problems I see are the extra bracket noticed by @aa1992 and you declaring p twice. But your error looks unusual. Try typing the program once again in a new file or something like that.
    – Spikatrix
    Jun 4, 2015 at 3:07

2 Answers 2

4

The compiler reports an error on line 42, but the source in your question is only 33 lines and it contains no ~ character. You need to show us the entire source you're compiling.

But I have a good guess.

The error message shows a line with a ~ character in column 1 and nothing following it. The vi (or vim) text editor uses ~ to mark lines on the screen that aren't part of the file. If you copy-and-paste a source file from a vi editor session, it's easy to copy too many lines and end up with an extra ~ at the end of your source file.

Edit the file, jump to the end, and delete that line.

5
  • I made that, now it is telling me it's on line 41:1 Jun 4, 2015 at 2:49
  • @SteamyBanana: Your joking, right? Did you even write anything of that code yourself? Just apply the proposesd rule iteratively. Jun 4, 2015 at 2:50
  • @Olaf I am serious. I am trying fixing this for an hour now, in my timezone it's in the middle of the night I am exhausted... By the rule, do you mean the while-loop? Jun 4, 2015 at 2:59
  • @Olaf I wrote everything by myself with the official c reference. Please tell me if it is that trivial: what causes the error? I still have no clue Jun 4, 2015 at 3:26
  • 1
    Then: go to bed!! The problem will be there for you in the morning, after you've had a good night's sleep ... a nice cup of coffee ... maybe a brisk morning walk. (Having done this crazy line of work professionally for more than 32 years now, I can say with certainty that you must know "when to turn the damned computer off!!)" "It's only ones and zeros," after all. Keep that in mind! Jun 4, 2015 at 3:30
1

I got it now. My compiler (vim) added lines out of my sight. I used another editor and could delete the unnessecary code properly. Yes, was my bad all along. I am deeply sorry, it was a long day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.