LinuxQuestions.org
Visit Jeremy's Blog.
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 01-27-2017, 06:35 PM   #1
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 203

Rep: Reputation: 20
C, gcc, printf Not Printing


I'm using the native gcc in Mac OS X.

gcc -v returns:
Code:
couldn't understand kern.osversion `14.5.0'
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~123/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
I'm working my way through Head First C. (I know. This is like book two or three.)

Anyway, on page 94, it has the code for a small program that will take input and search an array of strings for the input, returning the strings that contain the input. The code compiles, but after entering the search string, the program just ends. The same thing happens if I copy and paste the code from the book. Let me know what I'm missing.

Code:
#include <stdio.h>
#include <string.h>
char tracks[][80] = {
    "I left my heart in Harvard Med School",
    "Newark, Newark - a wonderful town",
    "Dancing with a Dork",
    "From here to maternity",
    "The girl from Iwo Jima",
    };
void find_track(char search_for[])
{
    int i;
    for (i = 0; i < 5; i++) {
    if (strstr(tracks[i], search_for))
    printf("Track %i: '%s'\n", i, tracks[i]);
    }
}
int main()
{
    char search_for[80];
    printf("Search for: ");
    fgets(search_for, 80, stdin);
    find_track(search_for);
    return 0;
}
 
Old 01-27-2017, 07:43 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Does the book teach how to use a debugger?

Set a break point on line 14 and print out the variable search_for:
Code:
(gdb) b 14
Breakpoint 1 at 0x795: file main.c, line 14.
(gdb) run
Starting program: a.out 
Search for: left

Breakpoint 1, find_track (search_for=0x7fffffffdc00 "left\n") at main.c:14
14          if (strstr(tracks[i], search_for))
(gdb) print search_for
$1 = 0x7fffffffdc00 "left\n"
See the problem?
 
Old 01-28-2017, 10:51 AM   #3
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 203

Original Poster
Rep: Reputation: 20
norobro, no, it doesn't, or it hasn't covered it yet. To be honest, I'm having an issue puzzling out your output from gdb.

I guess I'll spend a couple of my personal CPU cycles today reading about gdb.

Thank you,
Doug


Quote:
Originally Posted by norobro View Post
Does the book teach how to use a debugger?

Set a break point on line 14 and print out the variable search_for:
Code:
(gdb) b 14
Breakpoint 1 at 0x795: file main.c, line 14.
(gdb) run
Starting program: a.out 
Search for: left

Breakpoint 1, find_track (search_for=0x7fffffffdc00 "left\n") at main.c:14
14          if (strstr(tracks[i], search_for))
(gdb) print search_for
$1 = 0x7fffffffdc00 "left\n"
See the problem?
 
Old 01-28-2017, 11:13 AM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
gdb is a handy tool, well worth spending some personal cpu cycles on.

As to your problem, from the fgets() man page:
Quote:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer.
There are no newline characters in your char array so strstr() returns a null pointer and the if statement fails. You need to get rid of the linefeed. There are several ways to do that. I'll leave finding them up to you.

Last edited by norobro; 01-28-2017 at 11:27 AM.
 
Old 01-28-2017, 04:37 PM   #5
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 203

Original Poster
Rep: Reputation: 20
norobro,
An aside. Do you have any recommendations for books. I get a little ticked when a book leaves issues like this in there.

Note, I apparently need to do a new install of gdb. I get a lot of these lines. I'm assuming this is indicating missing files:

Code:
warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(tls_hashhmac.o)" - no debug information available for "tls_hashhmac.c".

warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_record.a(tls_hmac.o)" - no debug information available for "tls_hmac.c".

warning: Could not find object file "/BinaryCache/coreTLS/coreTLS-35.40.1~1/Symbols/BuiltProducts/libcoretls_stream_parser.a(tls_stream_parser.o)" - no debug information available for "tls_stream_parser.c".
 
Old 01-28-2017, 04:57 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,590

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
Quote:
Originally Posted by killingthemonkey View Post
Note, I apparently need to do a new install of gdb. I get a lot of these lines. I'm assuming this is indicating missing files:
That suggests missing debug info for your libraries, not a problem with gdb. I don't know how it works on OSX, but Debian, for instance, puts the debug info in separate pacakges from the main package to save space for those who don't need the debug info (https://packages.debian.org/stable/debug/).

Since you probably won't be debugging the TLS library, you don't need to worry about such messages anyway.
 
Old 01-28-2017, 10:34 PM   #7
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by killingthemonkey
An aside. Do you have any recommendations for books.
Sorry, I have been using C++ for quite some time so I really don't know of any good C books. I started learning C with the Kerrigan & Ritchie book long ago.

If you are interested in learning C++ here are a couple of good websites:
http://www.learncpp.com
http://www.cplusplus.com/doc/tutorial/ - good reference site too with an active forum
 
Old 01-28-2017, 11:20 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,573
Blog Entries: 11

Rep: Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588Reputation: 3588
The GDB Manual from GNU.org is an excellent reference, and includes a very useful example session in the first chapter which will get you started with the basics very quickly. The first 3-4 chapters will have you using it with your C programs in a very short time.
 
Old 01-29-2017, 08:42 AM   #9
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 203

Original Poster
Rep: Reputation: 20
norobro,
Thanks for your suggestions.

astrogeek,
Thank you. Headed that way now.
 
Old 02-06-2017, 10:38 PM   #10
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
@killingthemonkey - Regarding C books: http://www.linuxquestions.org/questi...p?t=4175599117

Last edited by norobro; 02-06-2017 at 10:44 PM. Reason: link didn't work
 
Old 02-07-2017, 06:21 AM   #11
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 203

Original Poster
Rep: Reputation: 20
@norobro - Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: GCC 4.9 vs. GCC 5.1 vs. GCC 6.0 SVN Compiler Benchmarks LXer Syndicated Linux News 0 06-08-2015 01:00 PM
printing float using printf shows 0.00 the program answerme Linux - Software 1 08-23-2013 07:36 AM
printf is printing to the file MPRT Programming 2 05-10-2011 05:34 PM
[SOLVED] SEGMENTATION FAULT using gcc 4.4.4 -O2 , works with gcc 4.1.0 -O2 or gcc 4.4.4 -O1 amir1981 Programming 36 07-26-2010 06:07 PM
BASH - printf printing escape codes even though I have %0b defined DevonB Linux - Newbie 5 12-23-2009 01:42 PM

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

All times are GMT -5. The time now is 06:13 AM.

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