LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2003, 10:43 AM   #1
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Rep: Reputation: 15
global declarations


Hi all,
i am writing a C program, and i don't know quite how i did it but i declared a printf statement to be global. I made a completely new set of code and new programfile name but the printf statement still popped up even though those lines weren't in the program code.

i had some character arrays
Code:
  
           ........... /* the previous lines were the usual program header files  and integer declarations*/             
            char s1[10],
                    s2[10],
                    s3[10],
                    s4[10];
these are the same character arrays i used in the last program, it was only when i removed them from the program was i able to print the printf i wanted to and not the previous one from the old program.

Is there a way i can get rid of that global declaration? i deleted the old file and that doesn't work.
 
Old 03-12-2003, 10:54 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Are these programs long? Post them so we can look at them. I can't quite make heads or tails based on the symptoms you've described... need a little more info
 
Old 03-12-2003, 05:59 PM   #3
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
i had only completed part of the program when this error had come up, you'll have to forgive the messy code, i was just getting the program to take form.

Code:
       #include <stdio.h>
       #include <math.h>
       #include <string.h>

       int main(void)
       {
  
              int deg_lat, min_lat, sec_lat;
              int deg_long, min_long, sec_long;

              char s1[]= "North";
              char s2[10];
              char s3[10];
              char s4[10];
              char s5[10];
              char s6[] = "East";

/*The following line never gets printed because it conflicts with the global variable and i never set one!*/

              printf("Please enter in co-ordinates in the format of 123 45 30         North where 123 = degrees 45 = mins 30 = secs and North = direction");

 /*this one*/  printf("Please enter lattitudinal co-ordinates for starting point: \n");
            
           scanf("%d%d%d%s", &deg_lat, &min_lat, &sec_lat, s2);
            printf("Please enter in the longitudinal co-ordinates for start point:\n");
              scanf("%d%d%d%s", &deg_long, &min_long, &sec_long, s3);

/* i know there are some unused variables those were for the second set of longitude and lattitude and  i didn't declare the integer variable for the second set of values to be scanned*/
this is the initial part of the program that cause the problem. i created a completely different program called test.c and i had just one printf() statement and the statement labelled /*this one gets printed*/
 
Old 03-12-2003, 06:25 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Hmmmm.... I don't see anything immediately wrong with the program. In fact, I tried this:
Code:
#include <stdio.h>
#include <math.h>
#include <string.h>

int main(void)
{
  int deg_lat, min_lat, sec_lat;
  int deg_long, min_long, sec_long;

  char s1[] = "North";
  char s2[10];
  char s3[10];
  char s4[10];
  char s5[10];
  char s6[] = "East";

  printf( "Please enter in co-ordinates in the format of 123 45 30 North\n");
  printf( "  where 123 = degrees 45 = mins 30 = secs and North = direction\n");

  printf( "Please enter lattitudinal coordinates for starting point: \n");
  scanf( "%d%d%d%s", &deg_lat, &min_lat, &sec_lat, s2);
  printf( " Degrees: %d\n Minutes: %d\n Seconds: %d\n Direction: %s\n",
          deg_lat, min_lat, sec_lat, s2 );

  printf( "Please enter in the longitudinal coordinates for start point: \n");
  scanf( "%d%d%d%s", &deg_long, &min_long, &sec_long, s3);
  printf( " Degrees: %d\n Minutes: %d\n Seconds: %d\n Direction: %s\n",
          deg_long, min_long, sec_long, s3 );

  return 0;
}
That compiled and ran perfectly for me, and gave this output:
Code:
Please enter in co-ordinates in the format of 123 45 30 North
  where 123 = degrees 45 = mins 30 = secs and North = direction
Please enter lattitudinal coordinates for starting point: 
2 4 8 South
 Degrees: 2
 Minutes: 4
 Seconds: 8
 Direction: South
Please enter in the longitudinal coordinates for start point: 
16 32 64 North
 Degrees: 16
 Minutes: 32
 Seconds: 64
 Direction: North
So that leads me to ask:

What was the command line you used to compile the code?
What was the output it gave you when you tried to compile (copy and paste it here exactly as it came out)?
Did you do anything to the header files you included?
 
Old 03-12-2003, 06:43 PM   #5
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
i used gcc -Wall test.c
and i didn't alter any of the header files

but you wouldn't believe this, it doesn't give that error anymore.
how odd!

just on another note
you would't happen to have an easy way of knowing or telling what arguments to put into a function prototype.

in other words how would i know that it is an argument in the function so i'd declare it as a funcion prototype parameter.

i know you can declare some variable within the function, so i'm a bit confused about that.
 
Old 03-12-2003, 06:51 PM   #6
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
to clarify what i said further, i want to roll all of those printf() and scanf() ffunctions into one function that i can call in main . but i'll need to acces the numbers scanned in by that function later on to do some calculations.

i hope that makes sense, if more clarification is needed then i'll do my best to oblige.
 
Old 03-12-2003, 06:56 PM   #7
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
Your arguements in a prototype are the data types you plan on passing to the function. So, if your function is going to work on three ints and a char, you should put three ints and a char in the prototype. Since it's your function you can pretty much do what you want.

Remember the order you pass things in or the compiler will yell at you.

Good Luck
 
Old 03-12-2003, 07:06 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
If the function needs to know the variable to do its job, it must be passed as a parameter. If you need a variable to help the function do its job, but don't care about its value later (like a loop counter), then you can declare it in the function.

You won't gain much by putting those printf and scanf statements into a separate function. I would suggest not doing so.
 
Old 03-12-2003, 07:09 PM   #9
rmartine
Member
 
Registered: Dec 2002
Location: San Luis Obispo, CA
Distribution: Fedora Core 3
Posts: 618

Rep: Reputation: 30
Oops... I think we were posting at the same time. Sure if you want to make a function in main that does all that it could look something like this. This isn't exact... so you can't copy/paste

// functinon proto
void prompt(int x, int y, int z, char q, int a, int b, int c, char w);

// your function
void prompt(int x, int y, int z, char q, int a, int b, int c, char w)
{
printf( "Please enter in co-ordinates in the format of 123 45 30 North\n");
printf( " where 123 = degrees 45 = mins 30 = secs and North = direction\n");

printf( "Please enter lattitudinal coordinates for starting point: \n");
scanf( "%d%d%d%s", &x, &y, &z, q);
printf( " Degrees: %d\n Minutes: %d\n Seconds: %d\n Direction: %s\n",
x, y, z, q);

printf( "Please enter in the longitudinal coordinates for start point: \n");
scanf( "%d%d%d%s", &a, &b, &c, w);
printf( " Degrees: %d\n Minutes: %d\n Seconds: %d\n Direction: %s\n",
a, b, c, w );

return;
}

Just remember that when prompt returns to main, the values you assigned in there won't be preserved. You'll need to use pointers.

Good Luck
 
Old 03-12-2003, 07:46 PM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Hehehe... yeah, I really ought to get in the habit of previewing the post before submitting... it would cut down on the overlapped responses...

Having a function would work, but I just see it as unnecessary. Personal preference is all.
 
Old 03-12-2003, 07:52 PM   #11
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
here's what i came up with so far:
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>

void read_GPS(int,int,int,char[10],int,int,int,char[10]);

int main(void)
{
	int deg_lat, min_lat, sec_lat, deg_long, min_long, sec_long;
	char s1[10], s2[10];
	printf("Please enter co-ordinates in format of 123 45 30 North \n" );
	printf("where 123 = Degrees 45 = minutes 30 = seconds and North = direction.\n");
        printf("Please enter in lattitudinal co-ordinates for start point:\n ");
        read_GPS(deg_lat,min_lat,sec_lat,s1);
	printf("Please enter in longitudinal co-ordinates for start point:\n ");
	read_GPS(deg_long,min_long,sec_long,s2);        

        printf("Degrees:%d \n Minutes: %d \n Seconds:%d \n Direction:%s\n", deg_lat, min_lat,
			sec_lat, s1);
	printf("%c", getchar());
	return(0);
}


void read_GPS(int,int,int,char[10],int,int,int,char[10])
{
	int deg_lat, min_lat, sec_lat, deg_long, min_long, sec_long;
	char s1[10], s2[10];
		scanf("%d%d%d%s", &deg_lat, &min_lat, &sec_lat, s1);
	        scanf("%d%d%d%s", &deg_long, &min_long, &sec_long, s2);
}
and that gives me the error of too few arguments in read_GPS. and parameter names omitted, i was under the impression tha in ANSI C you could omit the parameter names. does the function and prototype look correct to you guys??

thanks for all the help so far! i REALLY appreciate it
 
Old 03-12-2003, 08:05 PM   #12
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
When you call a function, you must supply an argument for each parameter listed in the function declaration. This is what you want to do:

Here's the function prototype:
Code:
void read_GPS(int *, int *, int *, char *);
Here's the function definition:
Code:
void read_GPS(int *degrees, int *minutes, int *seconds, char *direction)
{
  scanf("%d%d%d%s", degrees, minutes, seconds, direction);

  return;
}
Replace your two calls to read_GPS with this:
Code:
  ...
  read_GPS(&deg_lat, &min_lat, &sec_lat, s1);
  ...
  read_GPS(&deg_long, &min_long, &sec_long, s2);
  ...

Last edited by Dark_Helmet; 03-12-2003 at 08:06 PM.
 
Old 03-12-2003, 08:08 PM   #13
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Just out of curiosity, what are you writing this for?
 
Old 03-12-2003, 08:17 PM   #14
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
hey,
would you believe i got it down to something similar to what you just posted but i didn't use any pointers. I'm doing as an assignment for school, and i find the explanations here and the suggestions are much better than the one's the Teaching Assistants give.

this is what i had:
[code]
include <stdio.h>
#include <string.h>
#include <math.h>

void read_GPS(int,int,int,char[10]);

int main(void)
{
int deg_lat, min_lat, sec_lat, deg_long, min_long, sec_long;
char s1[10], s2[10];
printf("Please enter co-ordinates in format of 123 45 30 North \n" );
printf("where 123 = Degrees 45 = minutes 30 = seconds and North = direction.\n");
printf("Please enter in lattitudinal co-ordinates for start point:\n ");
read_GPS(deg_lat,min_lat,sec_lat,s1);
printf("Please enter in longitudinal co-ordinates for start point:\n ");
read_GPS(deg_long,min_long,sec_long,s2);

printf("Degrees:%d \n Minutes: %d \n Seconds:%d \n Direction:%s\n", deg_lat, min_lat,
sec_lat, s1);
printf("%c", getchar());
return(0);
}


void read_GPS(int,int,int,char[10])
{
int deg_lat, min_lat, sec_lat, deg_long, min_long, sec_long;
char s1[10], s2[10];
scanf("%d%d%d%s", &deg_lat, &min_lat, &sec_lat, s1);

}
 
Old 03-12-2003, 08:28 PM   #15
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
i made those changes and it works wonderfully now!

Thanks to everyone for all the help

now i am going to try to proceed with the rest of the program.

Thanks again!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about C - 'restrict' in function declarations? lowpro2k3 Programming 1 09-11-2005 12:50 AM
C++ function declarations and definitions... AM1SHFURN1TURE Programming 2 08-29-2005 06:57 PM
FIO declarations missing from bits/ioctls.h srdennis Programming 0 04-22-2004 05:07 AM
dhcpd.conf shared network declarations adamantium Linux - Networking 0 02-24-2004 02:24 PM
Forward declarations somnium Programming 3 09-16-2003 05:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration