Thread: Strange Character in Programs Output:

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Strange Character in Programs Output:

    Okay, I was trying to make a simple program to list all the files with a certain termination in a folder of my system (Slackware Linux 9) following the intructions of the book "Linux Application Development by Michael K. Johnson and Erik W. Troan" I got the following piece of code:

    Code:
    #include<stdlib.h>
    #include<sys/wait.h>
    
    int main(){
    	int result;
    	
    	result = system("exec ls *.c");
    	
    	if (!WIFEXITED(result)){
    		printf("Abnormal termination\n");}
    		else 	
    			printf("%d",result);
    	
    	exit(0);
    }
    Okay result is storing perfectly all the names of *.c files but I'm also recieving a zero in the final output here is a sample of the output:
    imanewbie@blah:~/Projects/Junk Code$ ./listsc
    array.c dinamicstack.c hashtable.c queue.c struct2.c test.c
    child.c doublepointer.c linkedlist.c random.c struct.c tripointer.c
    conta.c hamash.c listsc.c senha.c swapbytes.c
    0


    I would like to know if someone can explain me what is this 0 cause the book doesn't mention is and I don't even know what to search for in the internet. Thanks a lot.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You mean the 0 printed by this

    printf("%d",result);

    Or some other zero?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    You mean the 0 printed by this

    printf("%d",result);

    Or some other zero?
    Yes the 0 from the printf("%d",result)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Oh, you mean why is result 0

    Well that's the return result of the system() call.
    Which MAY in turn be the exit status of the program you tried to run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    Oh, you mean why is result 0

    Well that's the return result of the system() call.
    Which MAY in turn be the exit status of the program you tried to run.
    Hum... I think you are right, system() returns 0 if succesfull, well there would be a way to me remove this 0, well not remove but hide it?

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    What would you like it to print?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by chrismiceli
    What would you like it to print?
    I would like to print all the .c files but without the 0, I'm using it to list mp3's in fact just using .c to make a more simple example.

  8. #8
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    If you are using ls via system() to print the files, then why at all have the printf() statement. You can error check the return value of system if you want, but you don't have to print it. Just get rid of the printf() statement and it won't print the zero. Read this article about using system() and some alternatives: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by chrismiceli
    If you are using ls via system() to print the files, then why at all have the printf() statement. You can error check the return value of system if you want, but you don't have to print it. Just get rid of the printf() statement and it won't print the zero. Read this article about using system() and some alternatives: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Well I don't know if it is the problem but... I ran the program with all options of gcc for warnings and...
    gcc -c -Wall -Werror listsc.c
    listsc.c: In function `main':
    listsc.c:11: warning: implicit declaration of function `printf'


    Well my C isn't great thing so I dont know what exactly the compiler wants me to do... Any sugestions?

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you forgot to include <stdio.h> Don't you love that. I love the -Wall -pedantic options because they tell you that, I always forget stdlib.h with malloc and it reminds me everytime

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by linuxdude
    you forgot to include <stdio.h> Don't you love that. I love the -Wall -pedantic options because they tell you that, I always forget stdlib.h with malloc and it reminds me everytime
    Okay, I included stdlib.h now BUT I still recieving this misterious 0

  12. #12
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    It is printing the zero because your program ran by system() terminated normally. If you don't want it to print 0 when your system() program suceeds, then get rid of the else statement for the Abnormal termination.

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by linuxdude
    It is printing the zero because your program ran by system() terminated normally. If you don't want it to print 0 when your system() program suceeds, then get rid of the else statement for the Abnormal termination.
    I did it and still having the damm 0.

  14. #14
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30
    maragato,

    as linuxdude said, that 0 appear coz your program specifically is asking to it be printed. so, remove the else part of the if.

    jmgk

    ps: pelo teu nick, tu deve ser gaucho... entao falo em portugues: esse zero ai ta aparecendo porque tu ta imprimindo ele com o else printf()... remove essa parte ai do programa e ele vai funcionar beleza.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Lightbulb

    Quote Originally Posted by jmgk
    maragato,

    as linuxdude said, that 0 appear coz your program specifically is asking to it be printed. so, remove the else part of the if.

    jmgk
    ps: pelo teu nick, tu deve ser gaucho... entao falo em portugues: esse zero ai ta aparecendo porque tu ta imprimindo ele com o else printf()... remove essa parte ai do programa e ele vai funcionar beleza.

    Finally I got it! Thx a lot is is working now! Sim eu sou gaścho! Heheeheh. Dudes thanks a lot by the patience. Now I got it, the result = system("exec ls *.c");
    already prints the stuff right so I was with a damm redundant junk piece of code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sizeof" character strings - output issue
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-03-2008, 06:10 PM
  2. Catching another program's output?
    By kenpem in forum C Programming
    Replies: 9
    Last Post: 12-18-2007, 01:43 PM
  3. strange output
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 11:09 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. How do I output one character??
    By kia_1998 in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 10:46 PM