Pikir dan Rasa

cogito ergo sum

Penggunaan define atau const pada bahasa C (perbedaan dengan C++ / CPP)

with one comment

Salah satu kemudahan menggunakan sistem Arduino adalah karena sekalipun terinspirasi dari bahasa Processing, bahasa Arduino yang dipergunakan pada sketch berasal dari C++. Kita bisa mencoba alur logika pemrograman (algoritma) pada banyak kompiler C&C++. Lebih dari itu kita juga bisa memanfaatkan IDE untuk kompiler C/C++, misalnya Codelite, Code::Blocks atau Geany.

Tidak hanya secara offline, kita pun dapat menguji bagian dari kode yang kita buat secara online. Misalnya dengan menggunakan sourceLair. Tetapi tentu saja ada modifikasi yang perlu dilakukan agar, penggalan-penggalan kode itu dapat berhasil dikompilasi.

Salah satu yang kadang luput diperhatikan adalah penggunaan kata kunci const. Jika kita menggunakan kata kunci const pada program yang akan dikompilasi dengan kompiler C maka akan berbeda jika akan dikompilasi dengan kompiler C++. Sebagai contoh dengan menggunakan situs sourceLair, jika kita menggunakan const untuk menyatakan besar ukuran sebuah array maka akan ada pesan kesalahan. Misal pesan itu akan tertulis seperti: 6: error: variably modified ‘array01buffer’ at file scope.


Contoh paling jelas didapat dari situs c-faq.com [1]:

Q: I don’t understand why I can’t use const values in initializers and array dimensions, as in

    const int n = 5;
    int a[n];

A: The const qualifier really means “read-only”; an object so qualified is a run-time object which cannot (normally) be assigned to. The value of a const-qualified object is therefore not a constant expression in the full sense of the term, and cannot be used for array dimensions, case labels, and the like. (C is unlike C++ in this regard.)

When you need a true compile-time constant, use a preprocessor #define (or perhaps an enum).

References: ISO Sec. 6.4
H&S Secs. 7.11.2,7.11.3 pp. 226-7


Pada situs lain, seorang user bernama Seebs memberi penjelasan berikut [2]:

OP: I would understand the error if “const int eodSize = 8192;” was not a constant but since it is a constant why does the compiler not allow it?

Seebs: It is not a constant.
…. You still have to look up the value at runtime (since nothing prevents some other aspect of the system from modifying the “constant” value).


Dari situs learningcppisfun.blogspot.com [3]:

In C, apart from the fact that const are non-modifiable variables, they are the same as any other variables.


Dari situs tutorialspoint.com [4]:

ANSI C allows you to declare constants. When you declare a constant it is a bit like a variable declaration except the value cannot be changed.


Dari irl.eecs.umich.edu [5]:

In ANSI C, it also supports ‘const’, but C++’s ‘const’ is more flexible than C’s. In both C and C++, a value declared as ‘const’ is inviolate; it may not be modified by any part of the program in any way.

The most common use of ‘const’ values in C is to replace ‘#define’ literal constants.

         #define MAX_CUSTOMERS   10
         const int MAX_CUSTOMERS = 10;


         MAX_CUSTOMERS = 10;
         MAX_CUSTOMERS ++;

are both not acceptable.

Note: since you cannot make changes to a ‘const’, each constant must be initialized when declared.

The following is wrong:

         const int invalid;

In C++, you can do something like

         const int ArraySize = 100;
         int Array[ArraySize];

while in ANSI C, this would be flagged as an error.



[1] http://c-faq.com/ansi/constasconst.html

[2] http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2010-06/msg01218.html

[3] http://learningcppisfun.blogspot.com/2008/02/const-in-c-and-c.html

[4] http://www.tutorialspoint.com/ansic/cusing_constants.htm

[5] http://irl.eecs.umich.edu/jamin/pointers/C++NotInC.html

[6] http://www.cs.cf.ac.uk/Dave/C/node4.html#SECTION00450000000000000000

[7] http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/const.html

[8] http://geeksforgeeks.org/forum/topic/const-in-c

[9] http://www.physicsforums.com/showthread.php?t=234518

[10] http://yarchive.net/comp/const.html


Written by sunupradana

January 7, 2012 at 6:53 pm

One Response

Subscribe to comments with RSS.

  1. keren gan… artikelnya bermanfaat…


    May 7, 2017 at 8:52 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: