calling of function ?
-
Quote:
#include
#include
#include
// main.c#include "Utility.c"
#include
//#include "sample.c"
//#include "noise.c"float* random1(int SizeOfData);
int main(void)
{
//===================random number sequences generationint i,j;
int SizeOfData=63530;
float* squaring;
float total_sum,summation_noise;
float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));random\_seq=random1(SizeOfData); for(i=0;i
// utility.c
Quote:
float* random1(int SizeOfData)
{
srand(12345); // generate same random number anytime it run
//srand((unsigned int)time NULL)
int i,j;
float sum=0.0;
float a=0.2;
float summation_noise=0.0;float \*y;
y=(float *)malloc(SizeOfData* sizeof(float ));
for(i=0;iwhile running expected output has to be random float number from 0-63530 but it is displaying only one number .
-
Quote:
#include
#include
#include
// main.c#include "Utility.c"
#include
//#include "sample.c"
//#include "noise.c"float* random1(int SizeOfData);
int main(void)
{
//===================random number sequences generationint i,j;
int SizeOfData=63530;
float* squaring;
float total_sum,summation_noise;
float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));random\_seq=random1(SizeOfData); for(i=0;i
// utility.c
Quote:
float* random1(int SizeOfData)
{
srand(12345); // generate same random number anytime it run
//srand((unsigned int)time NULL)
int i,j;
float sum=0.0;
float a=0.2;
float summation_noise=0.0;float \*y;
y=(float *)malloc(SizeOfData* sizeof(float ));
for(i=0;iwhile running expected output has to be random float number from 0-63530 but it is displaying only one number .
Quote:
#include "Utility.c"
What's its purpose (usually you include just header files)? Your program (without such line) runs producing the expected output. Please note:
-
Quote:
float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));
is useless, since memory is allocated inside the
random1
function. -
You never free the allocated memory (you have to :) ).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
-
Quote:
#include "Utility.c"
What's its purpose (usually you include just header files)? Your program (without such line) runs producing the expected output. Please note:
-
Quote:
float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));
is useless, since memory is allocated inside the
random1
function. -
You never free the allocated memory (you have to :) ).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
-
utility.c is the program file where the function is declared from there I was aiming to call to main.c that y I include it.
maibam debina wrote:
utility.c is the program file where the function is declared
What function? You know, functon declarations should be inside header files and you should include just header files (as rule of thumb for beginners, at least).
Quote:
rom there I was aiming to call to main.c that y I include it
You cannot call a file (
main.c
is a file).THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
maibam debina wrote:
utility.c is the program file where the function is declared
What function? You know, functon declarations should be inside header files and you should include just header files (as rule of thumb for beginners, at least).
Quote:
rom there I was aiming to call to main.c that y I include it
You cannot call a file (
main.c
is a file).THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
In Utility.c its consist of already one created header file Utility.h which consist of struct for waveread do i need to add on this and if needed which way i have to?
#define _USE_MATH_DEFINES
#define PI M_PI /* pi to machine precision, defined in math.h */
#define TWOPI (2.0*PI)#ifndef UTILITY_H_
#define UTILITY_H_
struct WavHeader {
char chunkid[4];
int chunksize;
char format[4];
char subchunk1id[4];
int subchunk1size;
short int audioformat;
short int numchannels;
int samplerate;
int byterate;
short int blockalign;
short int bitspersample;
char subchunk2id[4];
int subchunk2size;};
float * wavRead(char*, int*);
#endif /* UTILITY_H_ */
-
maibam debina wrote:
utility.c is the program file where the function is declared
What function? You know, functon declarations should be inside header files and you should include just header files (as rule of thumb for beginners, at least).
Quote:
rom there I was aiming to call to main.c that y I include it
You cannot call a file (
main.c
is a file).THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
In Utility.c its consist of already one created header file Utility.h which consist of struct for waveread do i need to add on this and if needed which way i have to?
#define _USE_MATH_DEFINES
#define PI M_PI /* pi to machine precision, defined in math.h */
#define TWOPI (2.0*PI)#ifndef UTILITY_H_
#define UTILITY_H_
struct WavHeader {
char chunkid[4];
int chunksize;
char format[4];
char subchunk1id[4];
int subchunk1size;
short int audioformat;
short int numchannels;
int samplerate;
int byterate;
short int blockalign;
short int bitspersample;
char subchunk2id[4];
int subchunk2size;};
float * wavRead(char*, int*);
#endif /* UTILITY_H_ */
You should include
Utility.h
if you need eitherWavHeader
orwavRead
(or both).Utility.c
should contain thewavRead
function definition (its implementation). You should NOT includeUtility.c
inmain.c
.THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
Is it always neccessary to create header file,if i want to call a function which is in another file?
It is the good practice (the bad practice is copying the function declarations in the source code that needs them).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
Is it always neccessary to create header file,if i want to call a function which is in another file?
It is almost always necessary, and there is no reason not to use header files. Therefore you should never include a .c file. It all comes down to the question where the actual implementation of the function can be found within the final executable program. The compiler will read your .c files one by one and generate the machine code for all functions implemented therein. All include statements will be resolved before the compiler even sees those files, therefore including another .c file is equivalent to copying the entire code of that .c file! This may result in multiple copies of the same function, and, as a result, the linker may be unable to generate a program. Technically you can avoid these issues without the help of header files: all you need are declarations of the data types and functions declared somewhere, that you need in your current .c file. The header files are used as a means to assemble this information in just one place, rather than copy the declarations to every .c file that needs them. This way you can ensure that all files are properly updated with the relevant information, whenever the data type or function declarations change!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Quote:
#include
#include
#include
// main.c#include "Utility.c"
#include
//#include "sample.c"
//#include "noise.c"float* random1(int SizeOfData);
int main(void)
{
//===================random number sequences generationint i,j;
int SizeOfData=63530;
float* squaring;
float total_sum,summation_noise;
float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));random\_seq=random1(SizeOfData); for(i=0;i
// utility.c
Quote:
float* random1(int SizeOfData)
{
srand(12345); // generate same random number anytime it run
//srand((unsigned int)time NULL)
int i,j;
float sum=0.0;
float a=0.2;
float summation_noise=0.0;float \*y;
y=(float *)malloc(SizeOfData* sizeof(float ));
for(i=0;iwhile running expected output has to be random float number from 0-63530 but it is displaying only one number .
hi, Your code is working fine & generating random sequence of 65350 times.
-
hi, Your code is working fine & generating random sequence of 65350 times.