分類彙整: C

Compile C code using gcc Command Line

Adopt from

http://ce.uml.edu/compile.htm


Compile C

gcc yourdotc.c

Compile C++

g++ yourdotcpp.cpp

Error will be shown if there is any error. If there is no error, you will get some .out file. The following command is entered

yourdotout.out

If you want to compiled your C program directly,

gcc –o yourCexec yourdotc.c

The executable file will be named yourCexec.

C++: Conversion between different kinds of string

Adopt from

http://msdn.microsoft.com/en-us/library/ms235631(vs.80).aspx


CString to char*

CString orig("Hello, World!");
wcout << orig << " (CString)" << endl;

// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
strcpy_s(nstring, orig);

char * to CString

char *orig = "Hello, World!";
cout << orig << " (char *)" << endl;

// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;

Convert from 'char *' to 'System::String ^'

Adopt from

http://www.codeguru.com/forum/showthread.php?t=492437


You are writing mixed managed-unmanaged code without any need.
C++/CLR requires good knowledge of both native C++ and .NET. According to your needs, you may decide to go to pure native solution, with using MFC or WTL as UI Framework, or pure .NET solution using C#.
C++/CLI is used only in rare cases, for managed-unmanaged interoperability.
For your specific problem, tale a look at .NET String class and String.Format function, which replaces native sprintf.

By Alex F Senior Member of CodeGuru

main(), wmain() and _tmain()

Reference from

http://thedeveloperonline.wordpress.com/2008/04/10/main-wmain-and-_tmainwhat-is-the-deifference/

http://msdn.microsoft.com/en-us/library/ms683156(VS.85).aspx


main(), wmain() and _tmain() are all the head functions in C++ program. wmain() and main() are different from each other becuase wmain() and _tmain() are used for programmes which needs to support unicode, while main() is for programmes which needs to support ANSI code. The choice our code will affect how you dealing with the input and output characters in command lines.

C: Volatile Variable Basic

Reference from

[1]http://en.wikipedia.org/wiki/Volatile_variable

[2]http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html


Why do we need volatile keyword?

It is because optimizing compiler will read the context of your code and compute some of the values that can have no change so as to optimize the execution. However, it is not always true when the variable would be changed by other elements in the computer system at any time, such as a hardware register of a device connected to the CPU.[1]

There is an example from Wikipedia[1].

static int foo;
 
void bar(void) {
    foo = 0;
 
    while (foo != 255)
         ;
}

The compiler will optimize to something like below

void bar_optimized(void) {
    foo = 0;
 
    while (true)
         ;
}

Using volatile

static volatile int foo;
 
void bar (void) {
    foo = 0;
 
    while (foo != 255)
        ;
}

The compiler will check the condition every time.

volatile keyword affects the compiler's output. The compiler in this case was GCC.

C: Initialize struct

Reference from

http://linuxprograms.wordpress.com/2008/03/07/3/

http://linuxprograms.wordpress.com/2008/03/07/c-structure-initialization-advanced/


Suppose we have a structure

struct BankAccount{

    char name[25];

    int accountNumber;

    double balance;

};

Method I: Initialize line by line

strncpy(bankAcc1.name, "Dick Jobs", 10);

Using this method, you may miss initializing some fields

Method II: Initialize when you declare a variable of a struct (using set notation)

struct BankAccount bankAcc2 = {"Tom Allegro", 135435, 125.4}; //Order is important

Method III: Initialize when you declare a variable of a struct (using set notation)

struct BankAccount bankAcc3 = {.accountNumber = 35484, .name = "Raymond Chu", balance = 125.4}; //Order is NOT important

C: printf for 64 bit integer

Adopt from

http://www.gidforums.com/t-3328.html


#include <stdio.h>
#include <basetsd.h>/* required typedef __int64 for gcc */

typedef char TCHAR;

#define TEXT(x) x
unsigned __int64 atou64(const TCHAR *s) ;
int main()
{
  unsigned __int64 value;
  TCHAR *test = "1234567890123456789";
  printf("in main(): test = <%s>\n", test);
  printf("sizeof(__int64) = %d\n", sizeof(__int64));
  value = atou64(test);
  printf("value using I64d: %I64d\n", value); /* for Borland BCC or Microsoft VC++ */
  printf("value using Ld:   %Ld\n", value); /* for Borland BCC */
  printf("value using lld:  %lld\n", value); /* for gcc */
  printf("value using I64x: %016I64x(hex)\n", value);/* for Borland BCC or Microsoft VC++ */
  printf("value using Lx:   %016Lx(hex)\n", value);/* for Borland BCC */
  printf("value using llx:  %016llx(hex)\n", value); /* for gcc */
  return 0;
}
unsigned __int64 atou64(const TCHAR *s)
{
   const unsigned __int64 max64 = (unsigned __int64)0-1;
   unsigned __int64 v = 0;
   int digit;
   while (*s>=TEXT('0') && *s<=TEXT('9'))
   {
     digit = *s++ - TEXT('0');
     if (v > (max64-digit)/10)
       return 0; /* overflow */
     v = 10*v + digit;
   }
   return v;
}

Function Pointers

Reference

http://www.cprogramming.com/tutorial/function-pointers.html

http://msdn.microsoft.com/en-us/library/zes7xw0h.aspx


Uses

  • Allowing the user to choose how to sort the data is simply to let the user pass in a function to the sort function. This function might take two pieces of data and perform a comparison on them. We'll look at the syntax for this in a bit.
  • Setting up "listener" or "callback" functions that are invoked when a particular event happens. The function is called, and this notifies your code that something of interest has taken place.

A Line of Simple Code

void create_button( int x, int y, const char *text, function callback_func );

Syntax

void (*foo)(int);

void *(*foo)(int *);

To initialize a function pointer, you must give it the address of a function in your program.

foo = &foo_with_1_int_parameter;

foo(1); //Invoke foo_with_1_int_parameter(1);

Sometimes, different return value of the function gives different result.

In the stdlib.h, we have

void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *, const void *) );

For the compare function

Compare function

Return value | Description

< 0 | elem1 less than elem2

0 | elem1 equivalent to elem2

> 0 | elem1 greater than elem2