顯示具有 C 標籤的文章。 顯示所有文章
顯示具有 C 標籤的文章。 顯示所有文章

2013/11/20

"%.*s" in printf()

Background

Currently, I am dealing with regex example in C.  There is a abnormal printf command.
......
printf ("'%.*s' (bytes %d:%d)\n", (finish - start),  to_match + start, start, finish);
......
Ouput:
$& is '1 is nice 2' (bytes 5:16)
I am wondering why there are 3 "%s" format options but there are 4 parameters to map those options.

Dig deeper...

To make the story short, "%.*s" is where the amazing happen. This option take 2 arguments: length of the string and the starting point of a string. (XXX: so ?) OK. Take a look the example code below.

#include <stdio.h>

int main(int argc, const char *argv[])
{
        char a[] = "0123456789";

        int len, offset;
        len = 4;

        for (offset = 0; offset < 3; offset++) {
                printf("%.*s\n", len, a + offset);
        }

        return 0;
}

Output:
0123
1234
2345

In other word, "%.*s" allows you to print a subset of a string without doing tedious jobs like following.

char *b = (char *) malloc(4+1);
memcpy(b, a+offset, len);
b[len+1] = '\0';
printf("%s\n",b);



2013/10/18

"va_*()" in c programming

Quick and dirty summery
va_*() are macros helping you to implement functions with dynamic arguments numbers.

Dig deeper
what is va_*()  ?
They are va_start(), va_arg(), va_end() respectively. Those macros will be used if you want to implement a function with vary arguments. For example, printf().

va_start(); /* call before va_arg(), otherwise va_arg() cannot function properly*/
va_arg(); /* return 1st, 2nd, 3rd.... arguments for 1st, 2nd, 3rd ...calls */
va_end(); /* call before returning value */

There are some examples codes on the reference link.

Reference
http://tw.myblog.yahoo.com/jw!kg_rIFWTHgO4kRtDoy15QxVeWQ--/article?mid=21&prev=22&l=f&fid=5

2013/10/16

"extern" in c programming

Quick and dirty summery
1. extends the visibility for a variable or function
2. While using with variable, it declares a variable but not defining it.
3. Special case for (2) is "extern int bar=0;", it treat as definition


Dig deeper
Declaration := A variable or function signature exists somewhere in the program but the memory is not allocated for them.
Definition := Despite the stuff declaration does, it also allocates memory for that variable/function.

PS.
/* function */
int foo (int arg1, int arg2); /* Declaration */
int foo (int arg1, int arg2){
    /* Definition */
     return 0;
}
/* variable */
extern int bar; /* Declaration */
int bar=0; /* Definition */



Reference:
http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

"volatile" in c programming

Quick and dirty summery
"volatile" is a keyword to force compile to fetch a variable's value from memory every-time it reads this variable during the variable's life time.

Dig deeper
Since there maybe optimizations on the C compiler (maybe true or not), compiler will cache the value of a variable if there are no obvious side effects on that variable. "volatile" is a keyword to tell compiler not to do optimization on that variable.



Reference:
http://cboard.cprogramming.com/c-programming/73163-volatile-keyword.html