[Courses] C Programming For Absolute Beginners, Lesson 1: Setting Up, First Program

Christopher Howard christopher.howard at frigidcode.com
Mon Feb 6 00:32:04 UTC 2012


On 02/05/2012 02:49 PM, jim wrote:
> 
>     (maybe not in scope for an intro hello world example, 
> but might be interesting for discussion anyway...?) 
> 
> 
>     I took a look at the stdio.h file: 
> ls -l /usr/include/stdio.h  # on my system that's 31KB 
> wc -l /usr/include/stdio.h  # on my system that's 920 lines of text 
> less /usr/include/stdio.h   # that's a lot of C code 
> 
>     Note that the #include preprocessor directive seems to take 
> <stdio.h> 
> as an argument. 
>     The < > angle bracket characters have a special meaning: 
> /usr/include/ 
> 
>     The #include preprocessor directive opens the file and pastes 
> its entire contents into the C program at that location. This 
> means what the compiler compiles is not only the code but all of 
> the code in the stdio.h file, yes? 
>     How does one "look up the spec" for stdio.h? 
> 
>     A look at my working directory: 
> ls -l 
> -rwxr-xr-x 1 jim jim 7180 2012-02-05 15:18 welcome
> -rw-r--r-- 1 jim jim  108 2012-02-05 15:18 welcome.c
> 
> ---------------------^^^^--------------------------- 
> 
>     The welcome.c source code is 108 bytes, but the welcome 
> binary program is 7180 bytes. 
>     Wait a minute. The stdio.h file is 31KB, a lot bigger. 
> What's going on? 
> 
> $ strings welcome 
> strings lchix
> /lib/ld-linux.so.2
> __gmon_start__
> libc.so.6
> _IO_stdin_used
> puts
> getchar
> __libc_start_main
> GLIBC_2.0
> PTRh0
> [^_]
> Hello, and welcome to the Beginning C Course!
> 
>     There are not many strings in the welcome binary file. 
>     How'd the welcome binary file get to be over 7KB but not 
> have hardly any of the stdio.h file contents? 
>     And what's up with the Martian (like [^_] and /lib/ld-linux.so.2 
> and __gmon_start__ and _IO_stdin_used and PTRh0 and so on)? 
> 
>     How come one of strings is 
> puts? 
>     I used the printf() function, not the puts() function. 
> 
>     I'm betting that libc.so.6 stores the binary code for printf 
> and puts and getchar on other stdio.h functions, yes? 
>     What's __libc_start_main about? And GLIBC_2.0 ? 
> 
>     And how come the "Hello, and welcome tothe Beginning C Course!" 
> is on the very bottom? 
> 
> 
> with thanks 
> 
> 
> 
> On Sun, 2012-02-05 at 13:43 -0900, Christopher Howard wrote:
>> On 02/04/2012 11:56 PM, Carla Schroder wrote:
>>
>>>
>>> 2. Look up the spec for stdio.h, and see what the various macros and functions  
>>> in the spec look like in your own stdio.h file.
>>>
>>
>> For those of you who really are beginners, stdio.h is usually located at
>> /usr/include/stdio.h. There is also a stdio.h man page.
>>
>> _______________________________________________
>> Courses mailing list
>> Courses at linuxchix.org
>> http://mailman.linuxchix.org/mailman/listinfo/courses
> 
> 

Running "strings" on a binary is not a very good way to examine its
contents. Run the following command:

$ objdump -d welcome | less

A binary is typically made up of a number of "sections", which have
various purposes, such as allowing the binary to run on your system (see
<https://en.wikipedia.org/wiki/Executable_and_Linkable_Format>),
allowing it to run functions from other libraries, running the actual
program code, and so on.

Most of the source code that you compile will not actually show up in
the executable because it gets converted to a much simpler binary
format: CPU instructions like system calls, register operations, and so
forth. For example, a binary doesn't need to remember the name of
variable, it just needs to remember the location to which it refers.

In fact, the code for the simple "welcome" program is in-and-of itself
not very large -- only a few hundred bytes. However, the executable ends
up being much larger because there is a lot of initial "overhead" code
which is added by the compiler, to make it work properly on your system.

In your binary, the compiler probably replaced the function printf()
with puts() in order to improve performance.  printf() is actually a
slower function because it does a lot of more advanced things that
puts() doesn't do (but which were not used in the "welcome" example).
Smart compilers like gcc will try to use whatever tricks that can to
speed up the final outputted program.

-- 
frigidcode.com
theologia.indicium.us



More information about the Courses mailing list