ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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", °_lat, &min_lat, &sec_lat, s2);
printf("Please enter in the longitudinal co-ordinates for start point:\n");
scanf("%d%d%d%s", °_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*/
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", °_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", °_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?
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.
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.
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.
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.
#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", °_lat, &min_lat, &sec_lat, s1);
scanf("%d%d%d%s", °_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
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);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.