Tuesday, 30 August 2016

C Language at Raw Softwares

 

     C Programming language

RAW SOFTWARES


C is a high-level and general purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System (OS) in the early 1970s.

C belongs to the structured, procedural paradigms of languages. It is proven, flexible and powerful and may be used for a variety of different applications. Although high-level, C and assembly language share many of the same attributes.
Following are C programming language features:
  • Fixed number of keywords, including a set of control primitives, such as if, for, while, switch and do while.
  • Multiple logical and mathematical operators, including bit manipulators.
  • Multiple assignments may be applied in a single statement.
  • Function return values are not always required and may be ignored if unneeded.
  • Typing is static. All data has type but may be implicitly converted.
  • Basic form of modularity, as files may be separately compiled and linked.
  • Control of function and object visibility to other files via extern and static attributes.
Getting start with c Language:
In order to run a C program, you need a compiler. Compiler change source code(code written by programmer) to object code(code that computer understands) and creates executable file. There are many free and professional compilers available. For the sake of this course, GNU GCC compiler is used. All the examples in this course are tested and verified in GNU GCC compiler.
we are starting with basic terminology of c language
Tokens:
c tokens are the basic buildings blocks in c language which are constructed together to write a c program. each and every smalllest individual units in a c program are known as c tokens. 
c tokens are of six types. they are,
  1. keywords
  2. identifiers
  3. constants 
  4. strings
  5. special symbols
  6. operators
1. Keywords:
    Keywords are predefined, reserved words used in programming that have special meaning. Keywords are part of the syntax and they cannot be used as an identifier. For example:
int money;
Here, int is a keyword that indicates 'money' is a variable of type integer. 
Keywords in C Language
autodoubleintstruct
breakelselongswitch
caseenumregister typedef
charexternreturnunion
continueforsignedvoid
doifstatic while
defaultgotosizeofvolatile
constfloatshortunsigned
Along with these keywords, C supports other numerous keywords depending upon the compiler.
2. Identifiers:
Identifiers are the names you can give to entities such as variables, functions, structures etc.
Identifier names must be unique. They are created to give unique name to a C entity to identify it during the execution of a program. For example:
int money;
double accountBalance;
Here, money and accountBalance are identifiers.
Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.
Rules for writing an identifier:
  1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscore only.
  2. The first letter of an identifier should be either a letter or an underscore. However, it is discouraged to start an identifier name with an underscore. It is because identifier that starts with an underscore can conflict with system names.
    In such cases, compiler will complain about it. Some system names that start with underscore are _fileno_iob_wfopen etc.
  3. There is no rule on the length of an identifier. However, the first 31 characters of identifiers are discriminated by the compiler. So, the first 31 letters of two identifiers in a program should be different.
  3. Constants:       
A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy" etc.
As mentioned, an identifier also can be defined as a constant.
const double PI = 3.14
Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.
Integer constants
A integer constant is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming:
  • decimal constant(base 10)
  • octal constant(base 8)
  • hexadecimal constant(base 16)
For example:
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.

Floating-point constants

A floating point constant is a numeric constant that has either a fractional form or an exponent form. For example:
-2.0
0.0000234
-0.22E-5
Note: E-5 = 10-5

Character constants

A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F'

Escape Sequences

Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequence is used.
For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.
Escape Sequences
Escape SequencesCharacter
\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null character
String constants
String constants are the constants which are enclosed in a pair of double-quote marks. For example:
"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having single character.
"Earth is round\n"         //prints string with newline
Enumeration constants
Keyword enum is used to define enumeration types. For example:
enum color {yellow, green, black, white};
Here, color is a variable and yellowgreenblack and white are the enumeration constants having value 0, 1, 2 and 3 respectively.

4.String:
A string in C is merely an array of characters. The length of a string is determined by a terminating null character: '\0' . So, a string with the contents, say, "abc" has four characters: 'a' , 'b' , 'c' , and the terminating null character. The terminating null character has the value zero.
we will learn string in letter blogs.rawsoftwares.blogspot.com

5. Special Symbols
c language contains the following special character in association with the letters and digits.
 Symbol
 Meaning
 ~  Tilde
 ! Exclamation mark 
 # Number sign 
 $ Dollar sign 
Percent sign  
 ^ Caret
 & Ampersand 
  * Asterisk 
(  Lest parenthesis 
 ) Right parenthesis 
Underscore  
 + Plus sign 
 |  Vertical bar
  \  Backslash
 ` Apostrophe
 -  Minus sign
 =  Equal to sign
  {  Left brace
  }  Right brace
 [  Left bracket
 Right bracket
 :  Colon
 "  Quotation mark
 ;  Semicolon
 <  Opening angle bracket
 >  Closing angle bracket
  ?  Question mark
 ,  Comma
 . Period
  /  Slash

6. Operators

An operator is a symbol which operates on a value or a variable. For example:+ is an operator to perform addition.
C programming has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as:
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators
Arithmetic Operators
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


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


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


pqp & qp | qp ^ q
00000
01011
11110
10011

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

 size of operator and conditional operator( ? : )
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

For Next topic visit next blog. thank you. rawsoftwares.blogspot.com

No comments:

Post a Comment