Maker Pro
Maker Pro

Function and their types

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,743
Joined
Nov 17, 2011
Messages
13,743
The system cannot execute the specified program
What does it mean "cannot execute"? Is there a warning, an error message, ...?

You can replace "count" in your printf with "counter()" and it will work correctly.
Completely correct but not a good example. If you have access to the global variable count, there's no need to evaluate the return value of the function. But it will work and is correct, I repeat.

Calling a subroutine with a return value but no argument is a useful operation e.g. for reading data from a fixed location. For example reading the time from a real time clock could be done by a routine named time(). This routine needs no input parameter (unless you have different clocks or need to tell the routine to return the time in a certain format. The routine time() would read e.g. the current time from a real time clock and return the time e.g. as an unsigned integer meaning seconds from a defined start time (as UNIX does, for example)
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
What does it mean "cannot execute"? Is there a warning, an error message, ...?

I just type in cmd
gcc -o hello hello.c
hello
it show only message "The system cannot execute the specified program"

it deosn't show any error or warnings
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Did your command line(s) work with the previous programs you posted?
Yes I have compiled every program and then I posted result. generally I see this error when something is wrong in program
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
look at following program's. I have not compiled all program I am just showing you my understanding

Function with no return value and no argument
This program doesn't return any value and doesn't pass any argument.
Code:
#include<stdio.h>

void number(void)
{
    unsigned int value = 2;
    printf(" Print number : %d \n ",value);
}  

int main (void)
{
    number();
 
   return 0;
}

Function with no return value and argument

this function doesn't return any value but pass argument 4
Code:
#include<stdio.h>

void number(unsigned int value)
{
    printf(" Print number : %d \n ",value);
}  

int main (void)
{
    number(4);
 
   return 0;
}

Function with return value and no argument
this function return 1 but doesn't pass any argument
Code:
#include<stdio.h>

int number(void)
{
   unsigned int value = 3;
    printf(" Print number : %d \n ",value);
 
   return 1;
}  

int main (void)
{
   number();
 
   return 0;
}

Function with return value and argument
this function return 1 and pass argument 3
Code:
#include<stdio.h>

int number(unsigned int value)
{
    printf(" Print number : %d \n ",value);
 
    return 1;
}  

int main (void)
{
    number(3);
 
   return 0;
}


does it make any sense ?
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
Completely correct but not a good example. If you have access to the global variable count, there's no need to evaluate the return value of the function.
The library function rand() does exactly that. It starts the random number sequence with a seed then, esch time called, computes a new random number and returns it, while keeping the global variable hidden.

Also it is common (and recommended) to use accessor functions in C++ to return the value of private variables without giving full access to those variables.

Bob
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Also it is common (and recommended) to use accessor functions in C++ to return the value of private variables without giving full access to those variables.
Hello bob

what do you think about my last post. does it make any sense for you?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I just type in cmd
gcc -o hello hello.c
hello
it show only message "The system cannot execute the specified program"

Add a "-v" after "gcc" (gcc -v -o hello hello.c). then post the entire contents of what is displayed (including your command line).

You cannot progress simply ignoring something when you have an error. @BobK has given you an excellent example and you need to understand it.

If you decide it's too hard and move on, you will get less and less help...

(I prefer NOT having to read the links given to you. Take this advice and READ what people are telling you. If you don't understand, or can't find any help, ask for clarification. DO NOT JUST IGNORE IT)

edit: I also recommend you call your programs something like "hello.cpp" and use the "g++" compiler instead of "gcc". For 99% of cases things will be exactly the same. Once you start using C++ constructs, things will behave better this way. That is, unless you have some reason to limit yourself to plain vanilla C.
 
Last edited:

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Add a "-v" after "gcc" (gcc -v -o hello hello.c). then post the entire contents of what is displayed (including your command line). .

Please take look at below quote message
C:\Users\Embedded\Desktop>gcc -v -o hello hello.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
COLLECT_GCC_OPTIONS='-v' '-o' 'hello.exe' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/cc1.exe -quiet -v -iprefix c:\mingw\bin\../lib/gcc/mingw32/6.3.0/ hello.c -quiet -dumpbase hello.c -mtune=generic -march=i586 -auxbase hello -version -o C:\Users\MREMBE~1\AppData\Local\Temp\ccOQsVvW.s
GNU C11 (MinGW.org GCC-6.3.0-1) version 6.3.0 (mingw32)
compiled by GNU C version 6.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/include"
ignoring duplicate directory "/mingw/lib/gcc/mingw32/6.3.0/../../../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/include-fixed"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/6.3.0/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/include-fixed
c:\mingw\bin\../lib/gcc/mingw32/6.3.0/../../../../mingw32/include
End of search list.
GNU C11 (MinGW.org GCC-6.3.0-1) version 6.3.0 (mingw32)
compiled by GNU C version 6.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 384cad586f05ed581a9c068b2f18b408
COLLECT_GCC_OPTIONS='-v' '-o' 'hello.exe' '-mtune=generic' '-march=i586'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/as.exe -v -o C:\Users\MREMBE~1\AppData\Local\Temp\ccA8OftL.o C:\Users\MREMBE~1\AppData\Local\Temp\ccOQsVvW.s
GNU assembler version 2.28 (mingw32) using BFD version (GNU Binutils) 2.28
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/6.3.0/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../
COLLECT_GCC_OPTIONS='-v' '-o' 'hello.exe' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/collect2.exe -plugin c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/liblto_plugin-0.dll -plugin-opt=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\MREMBE~1\AppData\Local\Temp\cc2QysNM.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -Bdynamic -o hello.exe c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../.. C:\Users\MREMBE~1\AppData\Local\Temp\ccA8OftL.o -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtend.o
COLLECT_GCC_OPTIONS='-v' '-o' 'hello.exe' '-mtune=generic' '-march=i586'

You cannot progress simply ignoring something when you have an error. @BobK has given you an excellent example and you need to understand it.

If you decide it's too hard and move on, you will get less and less help...
I didn't move on another topic. I took simple example for better understanding because I was trying from long time. so I thought I should take simple example.

It's fine I will be stay with example given by bob


(I prefer NOT having to read the links given to you. Take this advice and READ what people are telling you. If you don't understand, or can't find any help, ask for clarification. DO NOT JUST IGNORE IT).
I gone through that link's and still trying to fix the issues


edit: I also recommend you call your programs something like "hello.cpp" and use the "g++" compiler instead of "gcc". For 99% of cases things will be exactly the same. Once you start using C++ constructs, things will behave better this way. That is, unless you have some reason to limit yourself to plain vanilla C.
I have doubt when I write c program then I run program using gcc -o hello hello.c and when I write c++ program then I run program using g++ -o hello hello.cpp
C:\Users\Embedded\Desktop>g++ -o hello hello.cpp

C:\Users\Embedded\Desktop>exm
The system cannot execute the specified program.

Is it possible to run c program from c++ compiler like you're saying

I am not ignoring any advice. i am just trying my level best
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Ok, I don't see the error message.

What is the size of hello.exe, and what happens if you try to run it?

I'm using my phone now, so it's hard to read through the details of that log. I'll try to do it later today (in maybe 10 to 12 hours time).

If I get there time, I'll try the same example on a Unix machine at work. I'll let you know if I find anything.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Ok, I don't see the error message.

If I get there time, I'll try the same example on a Unix machine at work. I'll let you know if I find anything.

I think I found out the problem it's about the initialization of variable.

bob was assigning count = 0; and I am assigning count = 1;

Code:
#include<stdio.h>

int count = 1;
int counter(void)
{
    count++;
    return count;
}

int main()
{
    while (1)
    {
        printf("%d\n", counter());
    }

return 0;
}

upload_2018-1-23_11-59-40.png

another program
Code:
#include<stdio.h>

int count = 0;
int counter(void)
{
    count++;
    return count;
}

int main()
{
    printf("%d\n", counter());
    
    return 0;
}

function return count number 1
 
Last edited:

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,743
Joined
Nov 17, 2011
Messages
13,743
An interesting and unexpected find.
Would you care to test the program from post #18 with the following modification?
Code:
#include<stdio.h>

volatile int count = 0;
The addition of volatile tells the compiler that this variable may change (unexpectedly in the view of the compiler) and therefore may not be optimized. I don't see a reason why the compiler should optimize your program until it doesn't work, but i'd like to see you give it a try.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
.
Would you care to test the program from post #18 with the following modification?
, but i'd like to see you give it a try.
Hello
I did it. but it give same message " The system cannot execute the specified program."
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
What should I do next, Do I read another topic in programming or try to become good in this topic only. I think now I understood function and their types .

Actually I want start work with LPC arm controller. I have downloaded datasheet but I have some doubt's. should I start with new thread
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
What should I do next,

You could look at how you can pass parameters by value and by reference.

This will require a basic understanding of pointers
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
You could look at how you can pass parameters by value and by reference.
call by value
Code:
#include<stdio.h>
 
void function(int a);
 
int main(void)
{
 int a=10;
 
 printf(" Before calling a = %d \n",a);
 
 function(a);
 
 printf("After calling a=%d \n",a);
 
 return 0;
}
 
void function(int a)
{
 a=15;
}
Before calling a = 10
After calling a=10
I have noticed value of a doesn't change after calling the function. it remain same as previous value.

call by address
Code:
#include<stdio.h>
 
void function(int *a);
 
int main()
{
 int a=10;
 
 printf("Before calling a = %d \n",a);
 
 function(&a);
 
 printf("After calling a = %d \n",a);
 
 return 0;
}
 
void function(int *a)
{
 *a=15;
}

Before calling a = 10
After calling a = 15

I have noticed value of a change after calling the function
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Which ones can you call with a constant value?

Do you understand what you are passing when you say "void function foo(int *a);"?

What does the * mean. Likewise what does the & in &a mean?
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Which ones can you call with a constant value?
This one because it doesn't change the value of actual argument.
Code:
#include<stdio.h>
 
void function(int a); 
int main(void)
{
 int a=10;
 printf(" Before calling a = %d \n",a); 
 function(a);
 printf("After calling a=%d \n",a);
 return 0;
}
void function(int a)
{
 a=15;
}

Do you understand what you are passing when you say "void function (int *a);"?
I am passing address of variable a

What does the * mean. Likewise what does the & in &a mean?

& is a operator means “address of”.

Declaration of Pointer

data_type* pointer_variable_name;

int* pointer;
 
Top