• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

سينتكس سي

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها
سينتكس هاي رايج سي را هم از سايت:
http://www.cprogramming.com/reference
مي گذارم. براي مراجعه خودم هم هست. چون گاهي كلا سينتكس را يادم ميره.

بعدا شايد مثال هم اضافه كردم. ولي اگر كسي در يكي از اين سينتكس ها مشكل داشت بپرسه. در پيشرفته هاش خودم مشكل دارم كه شايد در تاپيك ((سئوالات كوچك سي)) يا جاي ديگه پرسيدم.

کد:
if(conditional)
{
  //code
}
کد:
if(conditional)
{
  //code
}
else
{
  //code
}
کد:
switch(variable)
{
  case value: 
    //code
  case value:
    //code
  default: 
    //code
}
کد:
goto label;

label:
//code
کد:
loop
{
  break;
  //code...
}
کد:
switch( variable )
{
    case value:
    /* code */
    break;

    case value:
    /* code */
    break;
}
کد:
loop
{
  continue;
  //code
}
کد:
for(variable initialization; conditional; variable increment)
{
  //code
}
کد:
while(conditional)
{
  //code
}
کد:
do
{
  //code
}while(conditional);

بيشتر دستورات اين پست در تاپيك ((آموزش سي پلاس پلاس از پايه)) مثال زده شده و توضيح داده شده.
http://forum.persiantools.com/showthread.php?t=32403
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها

primitive variable declarations
کد:
type identifier;

type >> char, int, float, etc



pointer declaration
کد:
type *identifier;


Pointers and Const-Correctness

Pointers have two modes of const-ness: pointers that do not allow modifications to the data, and pointers that must always point to the same address. The two may be combined. For the full story on const-correctness​
اين قسمت را كه اشاره گر را دو نوع دانسته را نتوانستم بفهمم .ولي دو نوعش را درزير مي آورم.

Pointer to Constant Data

A pointer to const data does not allow modification of the data through the pointer. The declaration of const data merely requires that the const precede the *, so either of the following two declarations are valid.​
کد:
const type* variable;
or​
کد:
type const * variable;
The memory address stored in a pointer to constant data cannot be assigned into regular pointers (that is, pointers to non-const data) without a const cast.

Pointers with Const Memory Address

Pointers with a constant memory address are declared by including the const after the *. Because the address is const, the pointer must be assigned a value immediately.​
کد:
type * const variable = some memory address;

Const Data with a Const Pointer

To combine the two modes of const-ness with pointers, you can simply include const for both data and pointer by putting const both before and after the *:​
کد:
const type * const variable = some memory address;
or​
کد:
type const * const variable = some memory address;
فعلا براي اين كه كامل همه سينتكس ها را بياورم ، اينها را نوشتم ولي بحث انواع اشاره گر را در تاپيكهاي مربوطه خواهيم كرد.
.
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها

getting a memory address - primitive type
کد:
&identifier;
The identifier must be a variable; the & prefix causes the statement to evaluate to the memory address of the identifier, so long as the identifier is a primitive type.

Getting a memory address – arrays
کد:
identifier;
To get the address of an array, you simply use the array name, which stores the memory location of the first value in the array. Note that while using the name of the array causes it to act like a pointer, unlike a regular pointer, it is constant.

getting a memory address – functions

To get the memory address of a function, use the function name without parentheses.

getting a memory address – pointer
کد:
identifier;
A pointer by default returns the memory address of the variable to which it points.

dereferencing a pointer
کد:
*identifier;
The asterisk dereferences the pointer; the value of the expression is the value of the variable whose memory address to which the identifier points.

Pointers and Const-Correctness

Pointers have two modes of const-ness: pointers that do not allow modifications to the data, and pointers that must always point to the same address. The two may be combined. For the full story on const-correctness,

Pointer to Constant Data

A pointer to const data does not allow modification of the data through the pointer. The declaration of const data merely requires that the const precede the *, so either of the following two declarations are valid.​
کد:
const type* variable;
or​
کد:
type const * variable;
The memory address stored in a pointer to constant data cannot be assigned into regular pointers (that is, pointers to non-const data) without a const cast.

Pointers with Const Memory Address

Pointers with a constant memory address are declared by including the const after the *. Because the address is const, the pointer must be assigned a value immediately.​
کد:
type * const variable = some memory address;

Const Data with a Const Pointer

To combine the two modes of const-ness with pointers, you can simply include const for both data and pointer by putting const both before and after the *:​
کد:
const type * const variable = some memory address;
or​
کد:
type const * const variable = some memory address;
.
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها

C-Style Typecast

C-style typecasts are available in both C and C++, but are considered poor C++ style because they are not as noticeable or precise as the C++ casts. C-style casts can be used to convert any type into any other type, potentially with unsafe results (such as casting an integer into a pointer type).​
کد:
(<type>)<value>
This example casts an int to a double for the purpose of avoiding truncation due to integer division:​
کد:
double result = (double)4/5;

Static-cast Typecast

Static casts are only available in C++. Static casts can be used to convert one type into another, but should not be used for to cast away const-ness or to cast between non-pointer and pointer types. Static casts are prefered over C-style casts when they are available because they are both more restrictive (and hence safer) and more noticeable.

Static casts are expressed in terms of a template that takes as a template parameter the type to convert to.​
کد:
static_cast<<type>>(<value>);
This example casts an int to a double for the purpose of avoiding truncation due to integer division:​
کد:
double result = static_cast<double>(4)/5;

Dynamic-cast Typecast

Dynamic casts are only available in C++ and only make sense when applied to members of a class hierarchy ("polymorphic types"). Dynamic casts can be used to safely cast a superclass pointer (or reference) into a pointer (or reference) to a subclass in a class hierarchy. If the cast is invalid because the the real type of the object pointed to is not the type of the desired subclass, the dynamic will fail gracefully.

Pointer dynamic cast

When casting a pointer, if the cast fails, the cast returns NULL. This provides a quick method of determining if a given object of a particular dynamic type.

The syntax for a pointer dynamic cast is​
کد:
<type> *p_sublcass = dynamic_cast<<type> *>( p_obj );

Reference dynamic cast

When casting a reference, it is not possible to return a NULL pointer to indicate failure; a dynamic cast of a reference variable will throw the exception std::bad_cast (from the <typeinfo> header).
<type> sublcass = dynamic_cast<<type> &>( ref_obj );
To be safe, all calls to dynamic_cast must either be unfailable or wrapped in a try/catch block.

Const-cast Typecast

Const casts are only available in C++. Const casts are used to strip the const-ness or volatile-ness from a variable. Const casts should be used sparingly; one example of a valid use of a const-cast is to strip the const-ness of a pointer to pass it into a function when you are certain the function will not modify the variable but the function designer did not specify the input as const.​
کد:
const_case<<type>>(<value>);
This example casts a const pointer to a non-const pointer to pass into a function:​
کد:
void func(char *);

const char *x = "abcd";
func(const_cast<char *>(x));

Reinterpret-cast Typecast

Reinterpret casts are only available in C++ and are the least safe form of cast, allowing the reinterpretation of the underlying bits of a value into another type. It should not be used to cast down a class hierarchy or to remove the const or volatile qualifiers.​
کد:
reinterpret_cast<<type>>( <val> );
To cast an integer to a pointer, you might write​
کد:
reinterpret_cast<int *>(100);
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها

truth
کد:
Everything except 0
Everything is true except what is false, and only 0 is false.

false
کد:
0;

Only 0 and its equivalents are false.

assignment
کد:
identifier = value or identifier;
The assignment operator requires that the identifier on the left hand side be a non-constant variable. (I.e., that its value can be changed by the expression.) This is commonly referred to as an "lvalue".

comparison - equality
کد:
identifier == identifier;
The == sign is used to compare primitive types such as char, int, float, etc. The comparison operator returns true if the two identifiers are equal. == is not a good choice for comparing C-style strings or arrays because it will check whether they occupy the same memory location, not whether they share the same value.

boolean or
کد:
conditional || conditional;
|| returns true if one or both conditions are met, that is, if at least one condition, but possibly both conditions, are true.

boolean and
کد:
 conditional && conditional;
&& returns true if both conditions are met, that is, if both conditions are true.

boolean not
کد:
!identifier;
! returns the opposite truth value of the identifier. For instance, !0 is true.​
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها

Declaring a Struct

The syntax for a struct declaration differs between C and C++. Although the C version is still valid in C++, it is slightly clunkier.


In C++:​
کد:
struct [struct_name]
{
    type attribute;
    // ...
    [struct_name *struct_attribute;]
} [instance1, [instance2, ...]];
A struct declaration requires the keyword struct, optionally the name of the struct (see below), and a body consisting of one or more attributes. It is possible to optionally include a self-referential pointer, but not possible to include a struct of type struct_name (struct_name struct_attribute).

If one or more structs is immediately desired, the entire declaration can be treated as a type, and the name of one or more instances may follow the declaration. In this case, it is possible to omit struct_name entirely, though this is not advisable.

To declare a struct for use at any other point, the name of the struct, struct_name, can be used as though it were any other type.​
کد:
 struct_name struct_instance [, instance2, ...];
In C:​
کد:
 [typedef] struct [struct_name]
{
    type attribute;
    type attribute2;
    // ...
    [struct struct_name *struct_instance;]
} [struct_name_t] [struct_instance];
In this case, there are two options: the first is to omit both typedef and struct_name_t, in which case to declare a struct you will need to actually include the struct keyword:​
کد:
 struct struct_name struct_instance;
Or you can use a typedef to declare a struct_name_t type that you can use:​
کد:
 struct_name_t struct_instance;
In either case, if you wish to declare a pointer to a struct inside of the struct, you must use the first syntax, with the keyword struct:​
کد:
 struct struct_name *struct_instance;


Declaring a Pointer to a Struct


The syntax for declaring struct instances in C and C++ differs. In C, if you do not typedef the struct, you must precede the name of struct type the keyword struct. In C++, this is not required. In either case, to declare a pointer to a struct, you simply precede the name of the instance with a *.

In C++:​
کد:
struct_name *struct_instance;
In C:​
کد:
struct_name_t *struct_instance;  /* struct_name_t must be a typedef */
or​
کد:
struct struct_name *struct_instance;

Accessing Struct Elements

Accessing a struct is as simple as using the "dot operator". If you have a a struct named struct_name and it has an element named struct_element:​
کد:
struct_name.struct_element;
Note that this works only in the case where the struct is not a pointer to a struct.

Accessing Struct Elements Through a Pointer to a Struct

To access an element of a struct when you have a pointer to a struct, instead of using the dot operator, use the arrow operator: ->​
کد:
struct_pointer->element;
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها

Declaring an Array

An array declaration requires the base type (the type that each element of the array will be -- .e.g., char or int), the name of the array, and the size of the array in square braces:​
کد:
  type name[size];
For instance,​
کد:
  int myArray[10];


declares an array of integers of size 10.

It is also possible to declare arrays with more than one dimension by the use of multiple square brackets, one for each dimension. A concrete example is declaring a 2-dimensional array:​
کد:
  int oneHundredElements[10][10];


Accessing an Element of an Array

To access an individual element of an array, use the name of the array name followed by the index of the element in square brackets. Array indices start at 0 and end at size-1:​
کد:
array_name[index];
accesses the index'th element of array_name starting at zero. If index is 0, then the first element is accessed. For instance,​
کد:
int myArray[10];
myArray[0] = 4;
sets the first element of myArray to 4. Note that it is not valid to attempt to access myArray[10] -- this is outside the bounds of the array.

To access an array with multiple dimensions, a similar approach is used, expect that each dimension requires an explicit index:​
کد:
int myArray[10][10];
myArray[3][4] = 45;
sets position 3, 4 in myArray to 45 (why might that be?)​
.
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها

#include
کد:
#include <header name>

The include directive instructs the preprocessor to paste the text of the given file into the current file. Generally, it is necessary to tell the preprocessor where to look for header files if they are not placed in the current directory or a standard system directory. This can be done either at compile time or as part of your compiler's project file. This feature is implementation-specific, so see the compilers page for more information.

If an included file cannot be found, compilation will cease with an error.


#define

The #define directive takes two forms: defining a constant and creating a macro.

Defining a constant
کد:
#define token [value]
When defining a constant, you may optionally elect not to provide a value for that constant. In this case, the token will be replaced with blank text, but will be "defined" for the purposes of #ifdef and ifndef. If a value is provided, the given token will be replaced literally with the remainder of the text on the line. You should be careful when using #define in this way; see this article on the c preprocessor for a list of gotchas.

Defining a parameterized macro
کد:
#define token( [, s ... ]) statement
For instance, here is a standard way of writing a macro to return the max of two values.​
کد:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Note that when writing macros there are a lot of small gotchas; you can read more about it here: the c preprocessor . To define a multiline macro, each line before the last should end with a \, which will result in a line continuation.

#if
کد:
#if <value>
/* code to execute if this value is true */
#elsif lt;value>
/* code to execute if this value is true */
#else
/* code to execut otherwise
#endif
#if checks whether the value is true (in the C and C++ sense of everything but 0) and if so, includes the code until the closing #endif. If not, that code is removed from the copy of the file given to the compiler prior to compilation (but it has no effect on the original source code file). There may be nested #if statements. It is common to comment out a block of code using the following construction because you cannot nest multi-line comments in C or C++.​
کد:
#if 0
/* code */
#endif

#ifdef
کد:
#ifdef <token>
/* code */
#else
/* code to include if the token is not defined */
#endif
ifdef checks whether the given token has been #defined earlier in the file or in an included file. If so, it includes everything between it and the closing #else or, if no #else is present, the closing #endif. #ifdef can be used with built-in token identifiers set by the compiler to indicate that additional functionality is available. For instance, the __cplusplus macro is defined in C++ but not C; you can use this fact to mix C and C++ code using an #ifdef statement:​
کد:
#ifdef __cplusplus
// C++ code
#else
// C code
#endif

#ifndef
کد:
#ifndef <token>
/* code */
#else
/* code to include if the token is defined */
#endif
#ifndef checks whether the given token has been #defined earlier in the file or in an included file; if not, it includes the code between it and the closing #else or, if no #else is present, #endif statement. #ifndef is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.​
کد:
#ifndef _INCL_GUARD
#define _INCL_GUARD
#endif
.
تمام شد. البته اين سايت به طور خلاصه گفته كدها را ولي به عنوان يك هند بوك خوبه.
بعدا اگر توانستم كمي از توضيحات را ترجمه مي كنم.
اميدوارم مفيد واقع بشه.
.
 

saalek

مدیر بازنشسته
تاریخ عضویت
24 می 2005
نوشته‌ها
654
لایک‌ها
53
محل سکونت
در پاي كوهپايه ها

فرهنگ اصطلاحات سي


Identifiers are the names you use to represent variables, constants, types, functions, and labels in your program. You create an identifier by specifying it in the declaration of a variable, type, or function. You can then use the identifier in later program statements to refer to the associated item.


An identifier is a sequence of one or more letters, digits, or underscores that begins with a letter or underscore. Identifiers can contain any number of characters, but only the first 31 are significant to the compiler. (However, other programs that read the compiler output, such as the linker, may recognize even fewer characters.)

از كتاب:
Visual C++ 6: The Complete Reference
لينك در تاپيكهاي مربوطه
 

m3hrz4d

Registered User
تاریخ عضویت
21 سپتامبر 2005
نوشته‌ها
620
لایک‌ها
1
محل سکونت
اصفهان
خیلی عالی بود دستتون درد نکنه.
از اون قضیه Pointers and Const-Correctness منظور اینه که ما 2 نوع حالت const روی یک پوینتر داریم. یه موقع یه پونتر تعریف میکنیم و میگیم محتویات جایی که بهش اشاره میکنه ثابت هست یعنی مثلا نمیشه بوسیله ی این پوینتر محتویاتش رو تغییر داد .برای مثال کد زیر معتبر نیست :
کد:
	int n;
	const int *nPtr = &n;	
	*nPtr = 10;
چون ما میخواهیم محتویات nPtr رو تغییر بدیم. اما این نوع پوینتر میتونه پوینتر دیگه ای بهش اختصاص داده بشه یعنی آدرسی که نگه میداره const نیست :
کد:
	int n, m;
	const int *nPtr = &n;
	const int *mPtr = &m;
	nPtr = mPtr;
و بالعکس پوینتری داریم که متحویاتش const نیست اما آدرسی که نگه میداره const هست.این کد معتبر نیست چون میخواهیم آدرسی که pointer بهش اشاره میکنه رو عوض کنیم :

کد:
int n, m;
	int * const nPtr = &n;

	nPtr = &m;
ضمنا میشه یه پوینتر درست کرد که هم محتویاتش ثابت میشه هم آدرسی که بهش اشاره میکنه :
کد:
int n;
const int * const nPtr = &n;
که البته زیاد به درد نمیخوره.
----
سالک جان شما خودت استادی امیدوارم وسط تاپیک هات پارازیت ننداخته باشم.خلاصه ی اون تاپیک رو اینجا مینویسم:

l-value همونطور که توی اون تاپیک هم بهش اشاره شده میشه گفت چیزی هست که بهش مشه مقداری رو نسبت داد و به نوعی سمت چپ علامت انتساب قرار بگیره (left hand side value) . مثلا 2 یک lvalue نیست چون نمیشه یه چیز دیگه رو بهش نسبت داد اما یه متغیر x یک lvalue هست.یا یک function که خروجی ارجاعی داشته باشه یک lvalue حساب میشه.توی اون تاپیک سوال شده بود که چرا این کد مشکل داره :
کد:
i++ = 10;
(همونطور که اونجا هم نوشته شده ) علت اینه که ++i یک مقدار برمیگردونه نه یک شی و خروجیش یک lvalue نیست و مثل اینه که بنویسیم 2 = 3 که خوب خطای l-value required ایجاد میشه.
 

saalek110

Registered User
تاریخ عضویت
10 آپریل 2007
نوشته‌ها
212
لایک‌ها
1
ممنون plus جان از توضیحات قشنگت. عالی بود.
 
بالا