Showing posts with label gcc. Show all posts
Showing posts with label gcc. Show all posts

Tuesday, January 8, 2013

How to Print to Console in ANSI C

The ANSI C standard I/O library (stdio.h) provides us with 3 powerful functions for outputting characters to STDOUT (the stream used for the console):
-Printing a character:  putchar
-Printing a string : puts
-Printing a formatted string : printf

A formatted string is a dynamical string that is built on the go. Here's an example:
#include<stdio.h>

int main(void)
{
    int myInt = 42;
    unsigned int myUint = 0xFFU;
    char myChar = 'a';
    double myDouble = 3.42;
    char myString[20] = "myString";

    printf("Formatted string with the values: %d, %x, %c, %lf and %s.",
            myInt, myUint, myChar, myDouble, myString);

    return 0;
}
/*Output
Formatted string with the values:: 42, ff, a, 3.420000 and myString.
 */
You can read more about formatted strings in this article.

There is also vprintf, which allows to print arguments received as a va_list, be we shall not go into details since is not very used.

Here's a short example on how to use these three functions to print a string:
#include<stdio.h>
#include<string.h>

int main(void)
{
    char myString[21] = "The bird is the word.";
    //Computing the length of the string for putchar
    int length = strlen(myString);
    //Iterator used for putchar
    int i = 0;

    //Outputting the string using printf
    printf("%s\n",myString);
    //Outputting the string using puts
    puts(myString);
    //Outputting the string using putchar
    for(i = 0; i<length; i++)
    {
        putchar(myString[i]);
    }
    putchar('\n');

    return 0;
}
/*Output
The bird is the word.
The bird is the word.
The bird is the word.
 */
The program above prints the string myString. As you can see you can print it as a formatted string using printf, as a normal string using puts and character by character using putchar.

TIP 1: puts automatically adds a newline character to the string. If you use printf or putchar you will have to specify the newline yourself.
TIP 2: To print a string using puts, you will probably need to know the length of the string. This can be achieved by using strlen from string.h.
TIP 3: printf returns the number of characters that were printed if successful. Otherwise returns a negative value.
TIP 4: puts returns a non-negative value if it was successful. Otherwise it returns EOF.
TIP 5: putchar returns the written character if it was successful. Otherwise it returns EOF.
TIP 6: You can also use putchar in the following fashion:
#include<stdio.h>

int main(void)
{
    //Outputs a character specified by character
    putchar('w');
    //Outputs a character specified by its ASCII code in decimal
    putchar(119);
    //Outputs a character specified by its ASCII code in hexadecimal
    putchar(0x77);
    //Outputs a character specified by its ASCII code in octal
    putchar(0167);

    return 0;
}
/*Output
wwww
 */

A little bit about performance:

Speaking in terms of performance, printing is fastest when using puts.
Number of charactersputs (sec)printf (sec)putchar (sec)
100.010.0580.017
1000.0540.2710.073
10000.1050.7820.164
100000.5041.2640.961
1000005.1265.7099.428
The table above represents a benchmark I took using gcc on Linux. By no means, the data is 100% percent accurate since it depends on the hardware configuration and the other processes running at that time, but it can offer a little overview.

If you want to see my test, click here.

Question: In which situations do you use printf, in which putchar and in which puts?

References:
http://www.songho.ca/misc/timer/timer.html
http://www.cplusplus.com/reference/cstdio/
http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdio.h.html

Sunday, April 15, 2012

Compiling and Linking using gcc in Command Line under Linux

Let us suppose that we have a simple program "hello world" program: place in the source file helloWorld.c.

Compiling the source file
gcc -c helloWorld.c
  • gcc - invokes the compiler
  • -c - the compiler flag that specifies that only compilation will be done (no linking)
  • helloWorld.c - the path to the C source file
After running this command you will observe that new file helloWorld.o has appeared. This file is the object file resulted after the compilation. In order to create an executable you will need to call the gcc again to do the linking.

Creating the executable
gcc -o helloWorld helloworld.o
  • gcc - invokes the compiler
  • -o - places the output in a specified file (in our case helloWorld), which will represent the executable
  • helloWorld - the executable who resulted
  • helloworld.o - the path to the object file
Running the executable
./helloWorld

Now, let's say that you have 2 source files, main.c and interface.c and main.c uses some functions defined in interface.c.

First, we will need to compile the source files in order to obtain the object files:
gcc -c main.c
gcc -c interface.c
By invoking the gcc, you will obtain 2 object files: main.o and interface.o.

In order to create the executable, you will need to invoke gcc again, but this time both object files will need to be specified.
gcc -o main main.o interface.

Now you will have in your folder the newly created executable main.

Let's say now that the source file interface.c contains a call to the double sqrt(double number) function from math.h. In order to obtain the executable, you will have to manually link to the m (mathematical) library:
gcc -o main main.o interface.-lm

If your program, would have been linked to a user library who we shall call myLib.a placed in /usr/lib/myLib.a, the gcc invocation would had looked like:
gcc -o main main.o interface.o /usr/lib/myLib.a

If you would need to compile and link multiple files (for a larger project), the method presented above will prove to be cumbersome and time consuming. A better method is the use of a makefile, but this will be discussed in another article.

Monday, October 31, 2011

Solving Gcc Math.h Linking Problem in Eclipse

If you use Eclipse/gcc and have functions that need the math.h library, you may find out that the linker can't find functions like sqrt or pow and signal it like this:

undefined reference to `pow'    Matrix.c    /TzUtils/Sources    line 152    C/C++ Problem
undefined reference to `pow'    Matrix.c    /TzUtils/Sources    line 204    C/C++ Problem

This thing happens because the library which contains math.h is not linked by default. To link it you will need to add the extra flag -Im.

In Eclipse this can be done by:
  1. Right click on your project in Project Explorer and select Properties.
  2. Go to C\C++ Build -> Settings -> Tool Settings -> Gcc Linker -> Libraries and click on green plus button to add a new library. When the dialog pops up, write m, and Eclipse will automatically add the -Im flag.



Related Posts Plugin for WordPress, Blogger...