Learn Basics Syntax of C, Data types, Variables, Constants & Operators

Tokens in C

   In a C source program, the basic elements recognized by the compiler is the "Tokens". A token is source program text that the compiler does not break down into component elements.
The Keywords, Identifiers, Constants, String literals and Operators are example of tokens. Punctuation characters such as Brackets([ ]), Braces({ }), Parentheses(( )) and Commas(,) are also tokens. 


The C keywords: -

C keywords are reserved words by the compiler. All the C keywords have been assigned fixed meaning. The keywords cannot used as variable name because they have been assigned fixed job. For utilizing the keywords in a program, no header file is to be included. For example... auto, break, int, float..... etc.



Identifiers:-

A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).
C does not allow punctuation characters such as @, $, and % within identifiers. C is a case sensitiveprogramming language. 

Semicolon:- 


In C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.



Comments:- 
Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminates with the characters */ as shown below:
/* my first program in C */
You can use single line Comments like   // this is single line comments....

Constants:- 
The Constants in C  are applicable to the values, which do not change during the execution of a program. There are several types of constants in C. They are classified as :
  1. Integer Constants
  2. Real Constants
  3. Character Constants


Variables:- 
A variables is a data name used for storing a data value may be changed during the program execution.

Data Types in C

 Data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
S.N.
Types and Description
1Basic Types:
They are arithmetic types and consists of the two types: (a) integer types and (b) floating-point types.
2Enumerated types:
They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.
3The type void:
The type specifier void indicates that no value is available.
4Derived types:
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.

Integer Types

Following table gives you details about standard integer types with its storage sizes and value ranges:
TypeStorage sizeValue range
char1 byte-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-128 to 127
int2 or 4 bytes-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295

Floating-Point Types

Following table gives you details about standard floating-point types with storage sizes and value ranges and their precision:
TypeStorage sizeValue rangePrecision
float4 byte1.2E-38 to 3.4E+386 decimal places
double8 byte2.3E-308 to 1.7E+30815 decimal places
long double10 byte3.4E-4932 to 1.1E+493219 decimal places

The void Type

The void type specifies that no value is available. It is used in three kinds of situations:
S.N.Types and Description
1Function returns as void
There are various functions in C which do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status);
2Function arguments as void
There are various functions in C which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void);
3Pointers to void 
A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.
The void type may not be understood to you at this point, so let us proceed and we will cover these concepts in the upcoming chapters.
Lets see an example...
#include <stdio.h>
int main()
{
 /* variable definition: */
  int a, b;
  int c;
  /* actual initialization */
  a = 10;
  b = 20;
  
  c = a + b;
  printf("value of c : %d \n", c);

   printf("Storage size for int : %d \n", sizeof(int));  // geting size of a data type..
   
   return 0;
}

output:
value of c : 30
Storage size for int : 4

Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. 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

Arithmetic Operators

Following table shows all the arithmetic operators supported by C language. Assume variable A holds 10 and variable B holds 20 then:
OperatorDescriptionExample
+Adds two operandsA + B will give 30
-Subtracts second operand from the firstA - B will give -10
*Multiplies both operandsA * B will give 200
/Divides numerator by de-numeratorB / A will give 2
%Modulus Operator and remainder of after an integer divisionB % A will give 0
++Increments operator increases integer value by oneA++ will give 11
--Decrements operator decreases integer value by oneA-- will give 9

Relational Operators

Following table shows all the relational operators supported by C language. 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 condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not, if values are not equal then 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 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 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 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 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 condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.(A || B) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(A && B) is true.

Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows:
pqp & qp | qp ^ q
00000
01011
11110
10011
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101

No comments:

Post a Comment