Tuesday, June 28, 2016

The C Programming Language

Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get. 

Murphy's Law dictates that there is no single correct answer to the very first exercise in the book. Oh well. Here's a "hello world" program:
#include <stdio.h>

int main(void)
{
  printf("hello, world\n");
  return 0;
}

As you can see, I've added a return statement, because main always returns int, and it's good style to show this explicitly.
Here's a list of simple compile lines for a variety of popular compilers:

GNU C 
gcc -W -Wall -ansi -pedantic -o hello hello.c 

Microsoft C, up to version 5.1 
cl -W3 -Zi -Od -AL hello.c 

Microsoft C,version 6, Microsoft Visual C++ 1.0, 1.5 
cl -W4 -Zi -Od -AL hello.c 

Microsoft Visual C++ 2.0 and later 
cl -W4 -Zi -Od hello.c 

Turbo C++ 
tcc -A -ml hello.c (I think) 

Borland C++, 16 bit versions 
bcc -A -ml hello.c 

Borland C++, 32 bit versions 
bcc32 -A hello.c 

Monday, June 27, 2016

Introduction to C Programming Basic Structure and Fundamentals

The basic form of a simple C program is as shown below. ( Line numbers have been added for future reference. )
 1 /* Filename.c
 2
 3    This program was written by Ima Programmer on some date
 4
 5    This program does something useful . . .
 6
 7 */
 8
 9 #include <stdlib.h>       // For accessing the standard library functions
 10 #include <stdio.h>        // For standard input and output functions
 11
 12
 13 int main( void ) {
 14
 15  // Declare variables to be used
 16
 17  // Explain to the user what the program does
 18
 19  // Get user input ( and check it if possible )
 20
 21  // Perform necessary calculations
 22
 23  // Report results ( in a complete and well-formed format. )
 24
 25  system( "pause" ); // Only needed with certain environments, e.g. Dev C++
 26
 27  return 0;
 28
 29 } // main

Explanation of Specific Lines

  1. The symbols /* and */ enclose a block comment, which may extend over any number of lines, or which may enclose only part of a line.
    • EVERY well-written program should begin with a large block comment specifying the name of the file, the author of the program, when it was written, and a brief description of what the program does and why. In large projects the opening comment may include reference to external documents ( software requirements documentation ), and may include a revision history of who modified the program when and for what purposes.
  2. The #include statement informs the compiler of what libraries are needed.
    • stdlib.h is a header file containing necessary definitions for the use of the standard C library functions. Many systems automatically include this one, but there are exceptions, and it never hurts to include it again, just to be sure.
    • stdio.h contains the definitions of standard input and output functions. It is very rare that a C program would not need this header file.
    • More advanced programs may need to include other libraries, such as cmath for using trigonometric functions.
    • The double slash symbol, //, indicates a comment that extends to the end of the current line.
  3. Line 13 says that we are starting a function named main, which is the starting point for all C programs.
    • The keyword int says that main returns an integer value as its return type. ( See line 27. )
    • The open brace indicates the beginning of a block of code, which must be matched by a closing brace ( on line 29. )
    • The keyword void expressly states that main takes no arguments. In other programs you may see the following alternatives:
      • int main ( ) { - Does not expressly say what arguments main takes.
      • int main( int argc, char *argv[ ] ) { - Beyond the scope of this introduction.
      • int main( int argc, char *argv[ ], char ** envp ) { - Beyond the scope of this introduction.
  4. Some IDEs ( Integrated Development Environments ) execute programs in a separate window, and then immediately close the window when the program is completed. In this case, the system( pause )statement holds the window open so that the results can be seen, until the user presses a key to release it. ( Dev C++ is one of those IDEs. ) Note that CodeLab does not permit the use of system( pause ), so make sure to leave it out when submitting CodeLab solutions.
  5. Every function must have a return statement, which causes the function to end and return control to whoever called it.
    • Because main was declared to have an integer return type in line 13, this function must return an integer.
    • The return value from main is typically interpreted as an error code, with a value of zero indicating successful completion.
    • Negative 1 is a commonly used return value used to indicate an unspecified error of some kind.
  6. It is good practice to comment the closing brace of every function, and any other closing brace that is more than a few lines away from its matching opening brace. This makes it much easier to read more complex programs, which may have MANY sets of nested and consecutive braced blocks.

A Simple Sample:

  • This simple program adds three numbers and reports the total.
 1 /* addThree.c
 2
 3    This program was written by John Bell in January 2013 for CS 107
 4
 5    This program asks the user for two floating point numbers and 
 6    an integer, and reports their total.  Note that one of the floating
 7    point numbers is stored as a double precision data type.
 8
 9 */
 10
 11 #include <stdlib.h>  // For accessing the standard library
 12 #include <stdio.h>  // For standard input and output
 13
 14 int main( void ) {
 15
 16  // Declare variables to be used
 17
 18  int number;                             // The integer
 19  float fpNumber = 0.0f;                  // The floating point number
 20  double dpNumber = 0.0, total = 0.0;     // The double and the total
 21
 22  // Explain to the user what the program does
 23
 24  printf( "This program adds together two floating point numbers\n" );
 25  printf( "and an integer.\n" );
 26  printf( "Written January 2009 by John Bell for CS 107.\n\n" );
 27
 28  // Get user input ( and check it if possible )
 29
 30  printf( "Please enter the first floating point number > " );
 31  scanf( "%f", &fpNumber );
 32
 33  printf( "\nPlease enter the second floating point number > " );
 34  scanf( "%lf", &dpNumber );
 35
 36  printf( "\nPlease enter the integer > " );
 37  scanf( "%d", &number );
 38
 39  // Perform necessary calculations
 40 
 41  total = fpNumber + dpNumber + number;
 42
 43  // Report results ( in a complete and well-formed format. )
 44
 45  printf( "\nThe total of %f plus %f plus %d is %f\n", fpNumber,  
 46    dpNumber, number, total );
 47 
 48  system( "pause" ); // Only with certain environments, e.g. Dev C++
 49
 50  return 0;
 51
 52 } // main

Explanation of Specific Lines

  1. Line 18 declares that this program uses a variable named "number" that is of type "int".
    • All variables in C must be declared befrore they can be used.
      • Traditional C compilers requried that all variable declarations be made before any executable statements.
      • Most C programmers continue to follow this convention, even though the restriction has been relaxed in modern compilers, and there are sometimes good reasons for declaring variables later on in the program.
    • In this example, "number" is not assigned any initial value, so it will start out with an unknown random value.
    • Note the semicolon at the end of the line, which is how C knows that a statement has ended.
  2. Line 19 declares a variable named "fpNumber" of type float, the most basic of floating point data types.
    • This variable has been given an inital value of 0.0.
    • The letter "f" following the 0.0 indicates that this number is to be interepreted to be of type "float", as opposed to the default of "double".
  3. Line 20 declares two variables of type "double", a floating-point data type with twice the numerical precision of the data type "float".
    • Scientific and engineering programs typically use doubles instead of floats, unless there is a specific reason to do otherwise.
    • These variables are also both initialized to zero.
  4. "prntf" is the standard library function for formatted printing
    • A simple text string can be enclosed in double quotes, as shown.
    • The \n indicates a new line character. Without this, all the printout would appear on a single line. Multiple \n characters yield blank lines.
    • Every well-written program should begin by explaining to the user what the program does.
  5. Note that because this line has no "\n" at the end, the curser will be left at the end of the line with the question.
  6. scanf is the standard library function for reading data in from the keyboard.
    • The % symbol is a format specifier, and indicates what type of value to read in. %f specifies to read in a float data type.
    • Note that the variable name must have an ampersand in front of it in a scanf statement. The exact reason for this will be explained in the course section on pointers.
  7. The format specifier for a double precison number is %lf", as opposed to %f". ( A double is essentially a "long" float. )
  8. The format specifier for an integer is "%d". ( "d" stands for "decimal" integer, as opposed to octal or hexadecimal. )
  9. The equals symbol, =, as used here is an assignment, which takes the value from the right side and stores it in the left side.
    • The plus sign, +, peforms addition, and yields the sum of its two arguments.
  10. Format specifiers for printing numbers are similar to those used when reading them in.
    • The first argument to printf when printing numbers is a quoted string, with % format specifiers inserted everywhere that a numerical value is desired.
    • The quoted string is followed by a comma-separated list of values ( variables and/or expressions ) to be printed, one per format specifier.
    • Note that both the float and the double type use "%f" as their format specifier when printing, as opposed to the "%f" for floats and "%lf" for doubles when scanning.
    • Note that the output of the results is a complete sentence, and includes an echoing back of the user's input.
    • Note also that a line of code may span over multiple lines in the file. C/C++ only recognizes the semi-colon as the end of a line.

Variables ( Also covered under Data Types )

  • A variable is a named storage location, where data may be stored and later changed.
  • An identifier is a more general term for a named location, which may contain either data or code.
    • Identifiers must begin with a letter or an underscore, preferable letters for user programs.
    • The remaining characters must be either alphanumeric or underscores.
    • Identifiers may be of any length, but only the first 31 characters are examined in most implementations.
    • Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three different identifiers.
    • By convention ordinary variables begin with a lower case letter, globals with a Single Capital, and constants in ALL CAPS.
      • Multi-word variables may use either underscores or "camel case", such as "new_value" or "newValue".
    • Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or N, and floating point variables are usually assigned names beginning with other letters.
    • Identifiers may not be the same as reserved words. ( See below for a full list. )
  • All variables must be declared before they can be used.
    • In K&R C, all variables must be declared before the first executable statement of the program.
    • Modern C allows variables to be declared any time before they are used, but it is still normally good practice to declare all variables at the beginning of the program, unless there is a very good reason to do otherwise.
      • ( Exceptions: Loop counter variables are often declared as part of the loop structure. Occasionally it is beneficial to declare variables within a reduced scope, to be discussed later. )
  • Variables may be given an initial value at the time they are declared. This is called "initialization", or "initializing the variables".
    • Initialization in C is done using an equals sign:
    • Example: double x1 = 0.0, x2 = 0.0;
    • UNINITIALIZED VARIABLES ARE DANGEROUS, AND SHOULD BE CONSIDERED TO HOLD RANDOM VALUES.
  • Variables may be declared "const", meaning that their values cannot be changed.
    • const variables MUST be initialized at the time they are declared.
    • By convention, const variables are named using ALL CAPS.
    • Examples:
      • const double PI = 3.14159;
      • const int MAXROWS = 100;
    • Note: K&R C did not have the const keyword, and so the #define pre-processor macro was used to define constant values. The const qualifier is a better approach when it is available, because it allows the compiler to perform type checking among other reasons. For CS 107 we will defer the discussion of #define until we get to the chapter on the pre-processor.

Keywords

The following words are reserved, and may not be used as identifiers:
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
inline
int
long
register
restrict
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
_Bool
_Complex
_Imaginary


Naming Conventions

In addition to the variable naming rules imposed by the compiler, there are certain conventions that are commonly followed:
  • Ordinary variables normally begin with lower case letters:
int number, nStudents
  • Global variables ( to be covered later ) normally begin with a single capital letter:
double Coordinate, Salary;
  • Defined constants normally are in all upper case:
const double PI = 3.14159;
const int MAXROWS = 100;
  • Variable names consisting of more than one word typically capitalize successive words. This is known as "camel case":
double totalOld, totalNew, sumOfAllDigits;
  • Alternatively, where camel case is awkward or undesired, underscores may be used to indicate the start of words:
double totalOfX_coordinates, total_of_Y_coordinates
  • Integer numbers typically begin with letters I, J, K, L, M, or N. ( Remeber the first two letters in INteger. )
  • Floating point numbers typically begin with other letters:
int nStucents; // Not just students

double average, total, standardDeviation;
  • In any case, variable names should be meaningful and easily understood. ( Think complete words, not just letters, and make sure the words are meaningful and not just temp1, temp2, temp3, etc. )
 

Another Example ( Enhanced version needs to be converted from C++ to C )

Exercises

  • Write a complete C program to convert a temperature from degrees Centigrade to degrees Kelvin. The conversion is that temperatures in degrees Kelvin are exactly 273.15 higher than in degrees Centigrade.
  • Now write another program to convert from Centigrade to Fahrenheit and/or vice-versa.
  • Write a program to evaluate a quadratic or higher-order polynomial, such as Ax^2 + Bx + C. Your program will have to ask the user for each of the coefficients, as well as the value of X at which to evaluate the function.
  • Try more complex calculations, involving square roots, logarithms, trig functions, etc. ( Requires the use of the math library. #include <math.h> )

Sunday, June 26, 2016

Stuctures C

Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds.
Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book −
  • Title
  • Author
  • Subject
  • Book ID

Defining a Structure

To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows −
struct [structure tag] {

   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];  
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. Here is the way you would declare the Book structure −
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;  

Accessing Structure Members

To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type. The following example shows how to use a structure in a program −
#include <stdio.h>
#include <string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info */
   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 book_id : %d\n", Book1.book_id);

   /* print Book2 info */
   printf( "Book 2 title : %s\n", Book2.title);
   printf( "Book 2 author : %s\n", Book2.author);
   printf( "Book 2 subject : %s\n", Book2.subject);
   printf( "Book 2 book_id : %d\n", Book2.book_id);

   return 0;
}
When the above code is compiled and executed, it produces the following result −
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

Structures as Function Arguments

You can pass a structure as a function argument in the same way as you pass any other variable or pointer.
#include <stdio.h>
#include <string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* function declaration */
void printBook( struct Books book );

int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info */
   printBook( Book1 );

   /* Print Book2 info */
   printBook( Book2 );

   return 0;
}

void printBook( struct Books book ) {

   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
}
When the above code is compiled and executed, it produces the following result −
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

Pointers to Structures

You can define pointers to structures in the same way as you define pointer to any other variable −
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the '&'; operator before the structure's name as follows −
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use the → operator as follows −
struct_pointer->title;
Let us re-write the above example using structure pointer.
#include <stdio.h>
#include <string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* function declaration */
void printBook( struct Books *book );
int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info by passing address of Book1 */
   printBook( &Book1 );

   /* print Book2 info by passing address of Book2 */
   printBook( &Book2 );

   return 0;
}

void printBook( struct Books *book ) {

   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);
}
When the above code is compiled and executed, it produces the following result −
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

Bit Fields

Bit Fields allow the packing of data in a structure. This is especially useful when memory or data storage is at a premium. Typical examples include −
  • Packing several objects into a machine word. e.g. 1 bit flags can be compacted.
  • Reading external file formats -- non-standard file formats could be read in, e.g., 9-bit integers.
C allows us to do this in a structure definition by putting :bit length after the variable. For example −
struct packed_struct {
   unsigned int f1:1;
   unsigned int f2:1;
   unsigned int f3:1;
   unsigned int f4:1;
   unsigned int type:4;
   unsigned int my_int:9;
} pack;
Here, the packed_struct contains 6 members: Four 1 bit flags f1..f3, a 4-bit type and a 9-bit my_int.
C automatically packs the above bit fields as compactly as possible, provided that the maximum length of the field is less than or equal to the integer word length of the computer. If this is not the case, then some compilers may allow memory overlap for the fields while others would store the next field in the next word.

Saturday, June 25, 2016

C Oerators

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators
We will, in this chapter, look into the way each operator works.

Arithmetic Operators

The following table shows all the arithmetic operators supported by the C language. Assume variable A holds 10 and variable B holds 20 then −
OperatorDescriptionExample
+Adds two operands.A + B = 30
Subtracts second operand from the first.A − B = -10
*Multiplies both operands.A * B = 200
/Divides numerator by de-numerator.B / A = 2
%Modulus Operator and remainder of after an integer division.B % A = 0
++Increment operator increases the integer value by one.A++ = 11
--Decrement operator decreases the integer value by one.A-- = 9

Relational Operators

The following table shows all the relational operators supported by C. Assume variable A holds 10 and variable B holds 20 then −
OperatorDescriptionExample
==Checks if the values of two operands are equal or not. If yes, then the condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.(A <= B) is true.

Logical Operators

Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then −
OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.(A || B) is true.
!Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.!(A && B) is true.

Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is as follows −
pqp & qp | qp ^ q
00000
01011
11110
10011
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then −
OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands.(A & B) = 12, i.e., 0000 1100
|Binary OR Operator copies a bit if it exists in either operand.(A | B) = 61, i.e., 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) = 49, i.e., 0011 0001
~Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.(~A ) = -61, i.e,. 1100 0011 in 2's complement form.
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 = 240 i.e., 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 = 15 i.e., 0000 1111

Assignment Operators

The following table lists the assignment operators supported by the C language −
OperatorDescriptionExample
=Simple assignment operator. Assigns values from right side operands to left side operandC = A + B will assign the value of A + B to C
+=Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.C += A is equivalent to C = C + A
-=Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.C -= A is equivalent to C = C - A
*=Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.C *= A is equivalent to C = C * A
/=Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.C /= A is equivalent to C = C / A
%=Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.C %= A is equivalent to C = C % A
<<=Left shift AND assignment operator.C <<= 2 is same as C = C << 2
>>=Right shift AND assignment operator.C >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operator.C &= 2 is same as C = C & 2
^=Bitwise exclusive OR and assignment operator.C ^= 2 is same as C = C ^ 2
|=Bitwise inclusive OR and assignment operator.C |= 2 is same as C = C | 2

Misc Operators ↦ sizeof & ternary

Besides the operators discussed above, there are a few other important operators including sizeof and ? : supported by the C Language.
OperatorDescriptionExample
sizeof()Returns the size of a variable.sizeof(a), where a is integer, will return 4.
&Returns the address of a variable.&a; returns the actual address of the variable.
*Pointer to a variable.*a;
? :Conditional Expression.If Condition is true ? then value X : otherwise value Y

Operators Precedence in C

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
CategoryOperatorAssociativity
Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ -Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right