Programs not compiling successfully


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Programs not compiling successfully
# 1  
Old 04-29-2009
Programs not compiling successfully

Preface: this is not a homework question. However, my teacher gave us a review packet with some C coding, and for some reason none of the C programs are compiling. First I compiled them from the shell, then I used a compiler to see if something was wrong with compiling from a shell. Both produced the same results... they'll compile and execure, but there's no out put. Here is an example of some of the code examples:

Code:
   	 	 	 	 	 	  /* Character Arrays
    Read a set of text lines and print the longest
 */
 #include <stdio.h>
 #define MAXLINE 1000
 

 int getline(char [],int);
 void copy(char [], char []);
 

 int getline(char s[], int limit) {
   int c, i;
 

   for (i=0; i<(limit - 1) && (c=getchar())!=EOF  
        && c != '\n'; i++)
     s[i] = c;
   if (c == '\n') {
     s[i] = c;
     i++;
   }
   s[i] = '\0';
   return i;
 }
 

 void copy(char to[], char from[]) {
   int i=0;
 

   while ((to[i] = from[i]) != '\0')
     i++;
 }                 
 

 int main() {
   int len, max;
   char line[MAXLINE], longest[MAXLINE];
   max = 0;
   while ((len = getline(line,MAXLINE)) > 0)
     if (len > max) {
       max = len;
       copy(longest,line);
     }
   if (max > 0)
     printf("%s",longest);
  }

Code:
#include <stdio.h>
  int main () {
    long nc = 0;
    while (getchar() != EOF) 
      nc++;
    printf("%ld\n",nc);
    return 0;
  }

Is there something I'm doing wrong here.... ?

Thanks
# 2  
Old 04-29-2009
Quote:
Originally Posted by lazypeterson
...
Code:
#include <stdio.h>
  int main () {
    long nc = 0;
    while (getchar() != EOF) 
      nc++;
    printf("%ld\n",nc);
    return 0;
  }

Is there something I'm doing wrong here.... ?
Works fine for me:

Code:
$
$ cat textlen.c
#include <stdio.h>
int main () {
  long nc = 0;
  while (getchar() != EOF)
    nc++;
  printf("%ld\n",nc);
  return 0;
}

$
$ gcc -o textlen textlen.c
$
$ cat textlen.c | ./textlen
123
$
$

Since these programs work on input text, are you passing any to them ?

tyler_durden

_________________________________________________________________________________________________
"And the eighth and final rule: if this is your first time at Fight Club, you have to fight."
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Difference between inbuilt suid programs and user defined root suid programs under bash shell?

Hey guys, Suppose i run passwd via bash shell. It is a suid program, which temporarily runs as root(owner) and modifies the user entries. However, when i write a C file and give 4755 permission and root ownership to the 'a.out' file , it doesn't run as root in bash shell. I verified this by... (2 Replies)
Discussion started by: syncmaster
2 Replies

2. Shell Programming and Scripting

Did my script execute successfully ?

Hi, I have two scripts viz and I am running them in background. I wish to know if both the scripts completed execution successfully. So this is what I have done /tmp/commet/bin/connectdb1.sh & condb1=$? /tmp/commet/bin/connectdb2.sh & condb2=$? However, I am getting error... (7 Replies)
Discussion started by: mohtashims
7 Replies

3. UNIX for Dummies Questions & Answers

Cannot successfully execute .sh: su - <name> -c

Hi All, First time poster, I hope I have the basics covered. I am trying to execute a .sh but finding I am getting inconsistent results. A fresh set of eyes would be great. I am using Solaris 10 zones. So I am trying to execute a script but I am getting different results between... (2 Replies)
Discussion started by: att01
2 Replies

4. Shell Programming and Scripting

script has been executed successfully or not??

Guys, How can we know whether a script has been executed successfully or not ? We dont have any log directories, and we are not given a chance to modify the script. Could someone help me out with this Thanks (2 Replies)
Discussion started by: bobby1015
2 Replies

5. AIX

Alert on successfully logins

Hi, How difficult would it be to configure an alert on AIX that will inform me every time someone logs into the system? (1 Reply)
Discussion started by: bbbngowc
1 Replies

6. UNIX for Dummies Questions & Answers

Are programs like sys_open( ) ,sys_read( ) et al examples of system level programs ?

Are the programs written on schedulers ,thread library , process management, memory management, et al called systems programs ? How are they different from the programs that implement functions like open() , printf() , scanf() , read() .. they have a prefix sys_open, sys_close, sys_read etc , right... (1 Reply)
Discussion started by: vishwamitra
1 Replies

7. Solaris

Compiling programs

Hi guys i have posted a thread months ago and a guy called dukenuke or smething like that told me that i have to install Sun Studio if i want to be able to compile programs. I have installed Sun Studio 12 and put it in my PATH but no success compiling anything. when i download some source (tar.gz)... (2 Replies)
Discussion started by: saveka
2 Replies

8. UNIX for Dummies Questions & Answers

Mail delivered successfully?

Hi Is there any way I can know whether my mail has been delivered successfully or not....? I am using a shellscript which configures the header and the message body and also includes configurations for attachments. I use 'sendmail' to send the mails.... I have to resend the failed emails after... (7 Replies)
Discussion started by: Amruta Pitkar
7 Replies

9. UNIX for Dummies Questions & Answers

Successfully Installed Solaris 8 after all.

Hello There, Aftar all i successfully installed Solaris 8 on my Primary Slave 2nd Hard disk. Here what i did. I kept my first hard disk (Windows Me) on primary Master and put my new 2nd hard disk on Primary Slave, CDROM is on Secondary slave. When i install solaris8 it ask me two... (0 Replies)
Discussion started by: abidmalik
0 Replies

10. Programming

Compiling C programs

Hi everyone, If you have a few c programs to compile, from a make file, how can you tell which program is compiled first. Descriptively, you may have: objects = main.o xyz.o 123.o abc.o target : $(objects) $(CC) -o $@ $(objects) Does this mean program xyz is called within main,... (1 Reply)
Discussion started by: rachael
1 Replies
Login or Register to Ask a Question