What’s the best way to check if a file exists in C?

Look up the access() function, found in unistd.h. You can replace your function with You can also use R_OK, W_OK, and X_OK in place of F_OK to check for read permission, write permission, and execute permission (respectively) rather than existence, and you can OR any of them together (i.e. check for both read and write permission using R_OK|W_OK) Update: Note that on Windows, you can’t use W_OK to reliably … Read more

What does ‘const static’ mean in C and C++?

It has uses in both C and C++. As you guessed, the static part limits its scope to that compilation unit. It also provides for static initialization. const just tells the compiler to not let anybody modify it. This variable is either put in the data or bss segment depending on the architecture, and might be in memory marked read-only. … Read more

Valgrind: Invalid read of size 1

How to read this: Invalid read of size 1 Your program is trying to read one byte from somewhere that Valgrind doesn’t like. at 0x401569: main (:395) Where in the code this happens (clearly strcmp has been inlined) Address 0x0 is not stack’d malloc’d or (recently) free’d What the address it was reading – 0x0 … Read more

Expected declaration specifier error in function

You defined is_Prime inside the body of main. This is not possible in C. Or alternately, you are missing a closing curly brace at the end of main‘s body. The other problem was already mentioned in shf301’s answer.

Categories C Tags

gcc/g++: “No such file or directory”

Your compiler just tried to compile the file named foo.cc. Upon hitting line number line, the compiler finds: or The compiler then tries to find that file. For this, it uses a set of directories to look into, but within this set, there is no file bar. For an explanation of the difference between the versions of the … Read more

Array definition – Expression must have a constant value

In C language keyword const has nothing to do with constants. In C language, by definition the term “constant” refers to literal values and enum constants. This is what you have to use if you really need a constant: either use a literal value (define a macro to give your constant a name), or use a enum constant. (Read … Read more

Categories C Tags

Return char[]/string from a function

Notice you’re not dynamically allocating the variable, which pretty much means the data inside str, in your function, will be lost by the end of the function. You should have: Then, when you call the function, the type of the variable that will receive the data must match that of the function return. So, you should … Read more

What can cause a “Resource temporarily unavailable” on sock send() command

“Resource temporarily unavailable” is the error message corresponding to EAGAIN, which means that the operation would have blocked but nonblocking operation was requested. For send(), that could be due to any of: explicitly marking the file descriptor as nonblocking with fcntl(); or passing the MSG_DONTWAIT flag to send(); or setting a send timeout with the SO_SNDTIMEO socket option.

What’s wrong with my code? What is argv[1]?

I’m trying to ask the user to type in a string so I will print the length of the string. My code is built succeeded. However, when I entered a word and pressed ‘enter’, the program keeps running. I had to enter a second word, then the length of the first string displays. I’m confused … Read more