Nested Functions in C [duplicate] - c

This question already has answers here:
Nested function in C
(9 answers)
Closed 3 years ago.
I read that nested function don't exist in C.
I then successfully executed the following piece of code using the cc command on my linux machine.
#include <stdio.h>
#include <string.h>
int main(){
float dummy(){
printf("hello\n");
}
dummy();
return 0;
}
I got the the output hello.
How does that play out?
Does my compiler support nested functions?

GNU C supports nested functions as an extension. Your cc binary on your Linux machine is almost certainly a symlink to gcc.

Nested functions don't exist in standard C. However, it might be supported as an extension in certain compilers like GNU.

Related

regex.h header not found or breaking the file [duplicate]

This question already has answers here:
Regex.h for windows
(1 answer)
C: Regex library with MinGW
(2 answers)
Closed 4 months ago.
I'm trying to use regular expression for a project in C but it seems like it's not working or I'm maybe missing something.
When I try to #include <regex.h> it says Cannot open source file 'regex.h'.
When I try to #include <regex> it breaks my size_t data type and makes all my function calls not allowed in a constant expressions.
What do I need to make it work using Visual Studio as an environment, maybe there is an easier way to use regular expressions in C

GCC compiler in Ubuntu compiles nested functions [duplicate]

This question already has answers here:
Are nested functions a bad thing in gcc ? [closed]
(11 answers)
Implementation of nested functions
(1 answer)
Nested function in C
(9 answers)
Closed 3 years ago.
I wrote a small program in C on Ubuntu having function definitions within the main function and compiled it using GCC. It compiled and ran without error. However, writing the same program in Codeblocks on Windows doesn't compile, and issues an error that says function-definition is not allowed here.
So, does GCC on Ubuntu allow function definitions within a function? Will it always run perfectly (given that it has compiled and there is no logical error in the program)?

Intercepting Arithmetic Operations in C program [duplicate]

This question already has answers here:
Operator overloading in C
(7 answers)
How do you get assembler output from C/C++ source in GCC?
(17 answers)
Is it possible to overload operators in C?
(4 answers)
Closed 4 years ago.
Is there a way that we can call user defined function when we are calling arithmetic operator in a C program just like operator overloading in C++. using GNU GCC Compiler?
Simply,
I have a function add(), and in my C program I have arithmetic Operation
c = a + b;
when I compile the program, it should call my add() function internally for + operator.
and Is there a way we can see what is the code that gcc compiler is calling when it encounters + operator?
No.
C does not work that way, you cannot overload/override the basic built-in operators.
Seeing the code is of course possible, either by making gcc emit it directly using -S, or by disassembling the resulting binary. The related binutils tool is objdump.
These days much such exploration can also be made "online" using the fantastic Compiler Explorer tools at godbolt.org, of course.

Cross Platform usage of printf_s and scanf_s functions - C (linux/win32) [duplicate]

This question already has answers here:
Disabling Warnings generated via _CRT_SECURE_NO_DEPRECATE
(10 answers)
Closed 5 years ago.
I'm currently trying to get some C code that I originally wrote for linux (gcc) to build on a win32 box.
The MSVC compiler is giving me warnings for all my printf and scanf usage suggesting I should use printf_s and scanf_s instead as a more secure alternative.
Its never nice to ignore 100's of compiler warnings, but should I in this instance?
Is there a simple workaround to fix this? Perhaps encapsulate those functions in a platform specific preprocessor directive?
You can suppress these warning by defining _CRT_SECURE_NO_DEPRECATE before the #include statements. But you should consider to use the new, secure functions.

Problems with header in C [duplicate]

This question already has answers here:
Getting undefined reference to 'clock_gettime' error, for curl program
(2 answers)
Closed 5 years ago.
I have this piece of c code that I am in charge of speeding up. The code is put onto a RasPi and compiled. Two years ago the code was put on and compiled and it works. Now when I try to compile the same file it says there's an undefined reference to 'clock_gettime'. I looked it up and that function is defined in time.h. I thought that maybe that header wasn't installed or called in the code. At the beginning of the code it does say #include so that's not the problem. I checked if the time.h header was installed on the RasPi and it was there with the other headers. I opened it up with nano and the clock_gettime function was defined, so that's not the problem. What should I do? How do I solve this problem?
clock_gettime(2):
#include <time.h>
...
Link with -lrt (only for glibc versions before 2.17).

Resources