c programming tokens bhi program ko banane ke liye tokens ki jarurat hoti hai isliye tokens ko tik...

35
C Programming Tokens Basic Concept of C Programming: Tokens: Tokens ko building block of c programming kaha jata hai. Yane iska matlab hai tokens ki help se hi pura program banata hai. Program me use hone wale every small part ko token kaha jata hai. Aasan bhasha me hum yesa bhi kaha sakate hai ki program ek collection hai different tokens ka. Kisi bhi program ko banane ke liye tokens ki jarurat hoti hai isliye tokens ko tik tarah se samaj lena jaruri hai. Types Of Tokens: 1. Keyword 2. Identifiers 3. Constants 4. String 5. Operators 6. Special Symbols

Upload: hakhue

Post on 09-Jul-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • C Programming Tokens

    Basic Concept of C Programming: Tokens:

    Tokens ko building block of c programming kaha jata hai. Yane iska matlab hai tokens ki help se hi pura program banata hai. Program me use hone wale every small part ko token kaha jata hai. Aasan bhasha me hum yesa bhi kaha sakate hai ki program ek collection hai different tokens ka. Kisi bhi program ko banane ke liye tokens ki jarurat hoti hai isliye tokens ko tik tarah se samaj lena jaruri hai.

    Types Of Tokens:

    1. Keyword 2. Identifiers 3. Constants 4. String 5. Operators 6. Special Symbols

  • 1. Keywords:

    Keyword in predefined meaning in c programming.

    Keyword yane ek word hota hai jiska meaning pahale hi define kiya hota hai. Jab bhi hum koi keyword ka use program me karate hai to compiler ko pahale se uska meaning malum hota hai.

    C programming me total 32 keywords hai. Jiski list maine niche di hai. sabhi keywords ko small case me wirte kiya jata hai. Keywords ko hum variable, function name ki tarah use nahi kar sakate.

    Auto break case char const

    continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

    2. Identifiers: Definition: Identifiers is name given to the variable, function, structure, union etc.

    Identifier ko user define karata hai.

    Identifiers ko define karate samay kuch rules ko follow karana padata hai:

    1. Identifier ka name alphabet se start hona chahiye na ki kisi digit (number) se.

    2. Identifier ki length kitni bhi lambhi ho sakate hai kyon ye user defined hai.

    3. Identifiers me uppercase aur lower case ye dono alag alga hote hai.

    4. Identifiers name keyword nahi ho sakata.

    5. Blank spaces, white spaces allowed nahi hai.

  • 3. Constants:

    Constant is value which doesnt change in program execution.

    Constant yane ek value hotii hai jo program execution me change nahi hota. Constant ki value fixed hoti hai. Constant ke bhi different types hai to hum aage details me dekhenge.

    e.g. x=10,

    Age=15

    4. String:

    It is sequence of character.

    String ek sequence hota hai kisi bhe character ka.

    Example:

    Total,

    Name=Deepak

    5. Operators:

    Operators is special symbol which is used to perform the mathematical operation.

    Operator is symbol hota hai jiska usse programming me addition, subtraction, multiplication, division is tarah ke mathematical operation karane ke liye hota hai.

    C programming me kayi tarah ke operators available hai. Jo hum aage details me dekhange.

    Example:

    +,-, /, % etc.

  • 6. Special Symbols: C programming me kuch special symbols ka use bhi hota hai. Symbols yane alphabet, digit ko chodkar kuch symbols ka use programming me hota hai. Example: *, @, $ etc.

    Niche Ke Program me tokens ka use kiya hai /* Program for demonstration of tokens */ # include # include Void main ( ) { Int a, b, c; a=10; b=20 c=a+b printf(\n Addition=%d , c); getch(); } Upar diye gaye examples me total identifiers ka use karke program banaya gaya hai. Jisme void keyword a, b constants =, +, % - Operators (),{} special symbols

  • Constants Constant value fixed |

    Constant data type |

    Constant Literals |

    Constant Pointer | Syntax : const data_type variable_name = value (optional) ; Types of Constant :

    Integer Constant Decimal Constant Octal Constant Hexadecimal Constant Floating-point / Real Constant Character Constant String Constant Preprocessor

    With Examples

    Constant Types with Examples

    Integer eg. 2, 10, -2

    Decimal (Integer) eg. 2, 10, -2

    Octal (Integer) eg. 02, 010

    Hexadecimal (Integer) eg. 0x12, 0x1f

    Floating-point/Real eg. -2.4, 4.8

    Character eg. 'i', 'j'

  • String eg. "Hello", "Hi"

    Preprocessor eg. #define a 5

    Integer Constant Types:

    1. Decimal Integer Constant 2. Octal Integer Constant 3. Hexadecimal Integer Constant

    Integer Constants Integer Constant normal Variable |

    Integer Constant positive (+) negative (-) |

    Integer Constant Range -32768 32767 | for eg. Source Code :

    12345678910#include int main(){ const int num = 5; // integer Constant printf("integer constant value : %d", num); return 0; }

    Output

    integer constant value : 5

    Character Constants

    Character Constant normal Variable |

    Character Constant single character | |

  • Escape Sequences character constant |

    Escape Sequences Explaination

    \' Single Quotation Mark

    \" Double Quotation Mark

    \\ Backslash

    \? Question Mark

    \a Audible Bell

    \b Backspace

    \f Form Feed

    \n New line

    \r Carriage Return

    \h Horizontal Tab

    \v Vertical Tab

    for eg. Source Code :

    1234567891011#include int main(){ const char ch = 'H'; // character constant const char escape[]= "Hello\tWorld"; // Escape sequence - Horizontal Tab and string constant printf("Character constant : %c\n", ch); printf("String constant : %s\n", escape);

  • return 0; }

    Output

    Character constant : H String constant : Hello World

    Floating-point Constant Floating-point Constant normal float variable |

    Floating-point Constant Decimal point (.) |

    Floatint-point Constant value integer type Decimal

    point (.) | for eg. Source Code :

    1234567891011#include int main(){ const float num1 = 5; // floating-point constant with integer value const float num2 = 3.525984; // Floating-point constant printf("Floating-point constant : %f\n", num1); printf("Floating-point constant : %f\n", num2); return 0; }

    Output

    Floating-point constant : 5.000000 Floating-point constant : 3.525984

    String Constant

    String Constant value Double Quotation Mark (" ")

    |

  • String Constant Single Multiple characters |

    String Constant Escape Sequences | for eg. Source Code :

    12345678910111213#include int main(){ const char str1 []= "H"; // String Constant with single character const char str2 []= "Hello World"; // Normal String constant const char str3 [] = "Hello\nWorld"; // String Constant with Escape Sequence printf("String Constant with single character : %s\n", str1); printf("Normal String constant : %s\n", str2); printf("String Constant with Escape Sequence : %s\n", str3); return 0; }

    Output

    String Constant with single character : H Normal String constant : Hello World String Constant with Escape Sequence : Hello World

    Preprocessor Constant

    Preprocessor Constant value | for eg. Source Code :

    1234567891011#include #define a 5 // Constant value with preprocessor #define b 10 // Constant value with preprocessor

  • int main(){ printf("Value of a : %d\n", a); printf("Value of b : %d", b); return 0; }

    Output

    Value of a : 5 Value of b : 10

    Operators in C

    Types of Operators Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators

  • Increment / Decrement Operators Conditional / Ternary Operator

    1. Arithmetic Operators

    Operators Explaination

    + (Addition) Operands add |

    - (Subtraction) right operand left operand |

    *(Multiplication) Operands multiply |

    / (Division) right operand left operand divide |

    % (Modulus) right operand left operand divide

    remainder |

    For eg. Source Code :

    1234567891011121314151617181920212223242526#include int main() { int a,b,c;

  • printf("Enter two numbers "); scanf("%d%d",&a,&b); c=a+b; printf("Addition of a and b is %d\n",c); c=a-b; printf("Subtraction of a and b is %d\n",c); c=a*b; printf("Multiplication of a and b is %d\n",c); c=a/b; printf("Division of a and b is %d\n",c); c=a%b; printf("Remainder of a and b is %d\n",c); return 0; }

    Output

    Enter two numbers 5 4 Addition of a and b is 9 Subtraction of a and b is 1 Multiplication of a and b is 20 Division of a and b is 1 Remainder of a and b is 1

  • 2. Relational Operators

    Operators Explaination

    < (less than)

    Operand value Operand true

    return | for eg. num1=5; num2=6; num1 < num2

    > (greater than)

    Operand value Operand true

    return | for eg. num1=6; num2=5; num1 > num2

    = num2

    == (equal to) Operands )equal) , true return

    |

    != (not equal to) Operands - , true return

    |

  • For eg. Source Code :

    123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#include int main(){ int a=6, b=5; if(a=b){ printf(" a is greater than b\n"); }

  • else{ printf(" a is less than b\n"); } if(a==b){ printf(" a is equal to b\n"); } else{ printf(" a is not equal to b\n"); } return 0; }

    Output

    a is greater than b a is greater than b a is greater than b a is greater than b a is not equal to b

    3. Logical Operators

    Operators Explaination

    && (logical AND) conditions true true return | for eg. (55)

    || (logical OR) true , true return | for eg. (55)

    ! (logical NOT) condition true false |

  • for eg. !((55)) !((55))

    For eg. Source Code :

    12345678910111213141516171819202122232425#include int main(){ if((55)){ printf("Condition is true.\n"); } else{ printf("Condition is false.\n"); } if((55)){ printf("Condition is true.\n"); } else{ printf("Condition is false.\n"); } if(!((56))){ printf("Condition is true.\n"); } else{ printf("Condition is false.\n"); } return 0; }

    Output

    Condition is true. Condition is true. Condition is true.

  • 4. Bitwise Operators Truth Table for &, |, ^

    a b a & b a | b a ^ b

    0 0 0 0 0

    0 1 0 1 1

    1 1 1 1 0

    1 0 0 1 1

    Operation on AND(a&b)

    Decimal Value Binary Value

    a = 20, b = 12 ,

    20 0 0 0 1 0 1 0 0

    12 0 0 0 0 1 1 0 0

    4 0 0 0 0 0 1 0 0

    Operation on OR(a|b)

    Decimal Value Binary Value

    a = 20, b = 12 ,

    20 0 0 0 1 0 1 0 0

  • 12 0 0 0 0 1 1 0 0

    28 0 0 0 1 1 1 0 0

    Operation on XOR(a^b)

    Decimal Value Binary Value

    a = 20, b = 12 ,

    20 0 0 0 1 0 1 0 0

    12 0 0 0 0 1 1 0 0

    24 0 0 0 1 1 0 0 0 Binary Left Shift( > )

    Left Shift(> 2 numeric value binary value binary

    number 2 binary numbers right side shift | for e.g.a=20; /*

    0001 0100 */ 0000 0101 5 | Complement Operator (~)

    Operator bit reverse |

  • Operator 0 1 1 0 |

    Operation on Complement( ~ )

    Decimal Value Binary Value

    ~12 0 0 0 0 1 1 0 0

    243 1 1 1 1 0 0 1 1

    Output -13 243 ? 2's Complement of 243 -(reverse of 243 in binary + 1)

    Operation on 2's Complement( ~ )

    Decimal Value Binary Value 2's Complement

    243 1111 0011 -(0000 1100+1) = -(0000 1101) = -13(output)

    For eg. Source Code :

    12345678910111213141516171819202122232425262728#include int main() { int a=20; /* 0001 0100 */ int b=12; /* 0000 1100 */ int c; c=a&b; printf("value of c is %d",c); /* 4 = 0000 0100 */ c=a|b; printf("\nvalue of c is %d",c); /* 28 = 0001 1100 */ c=a^b;

  • printf("\nvalue of c is %d",c); /* 24 = 0001 1000 */ c=a2; printf("\nvalue of c is %d",c); /* 5 = 0000 0101 */ printf("\nvalue of b is %d",~b); /* -13 = 1111 0011 */ return 0; }

    Output

    value of c is 4 value of c is 28 value of c is 24 value of c is 80 value of c is 5 value of b is -13

    5. Assignment Operators Assignment Operators |

    1. Assignment Operator (=) 2. Add Assignment Operator (+=) 3. Subtract Assignment Operator (-=) 4. Multiply Assignment Operator (*=) 5. Divide Assignment Operator (/=) 6. Modulus Assignment Operator (%=) 7. Bitwise AND Assignment Operator (&=) 8. Bitwise OR Assignment Operator (|=) 9. Bitwise XOR Assignment Operator (^=)

  • 10. Left Shift Assignment Operator (=)

    Operators Examples

    = (assignment) c = a + b

    += (add assignment) c += a same as c = c + a

    -= (subtract assignment) c -= a same as c = c - a

    *= (multiply assignment) c *= a same as c = c * a

    /= (divide assignment) c /= a same as c = c / a

    %= (modulus assignment) c %= a same as c = c % a

    &= (AND assignment) c &= a same as c = c & a

    |= (OR assignment) c |= a same as c = c | a

    ^= (XOR assignment) c ^= a same as c = c ^ a

    > a

    For eg. Source Code :

    1234567891011121314151617181920212223242526272829303132333435363738394041#include int main() { int a=20,b=12; b = a + b; printf("value of b is %d\n",b);

  • b += a; printf("value of b is %d\n",b); b -= a; printf("value of b is %d\n",b); b *= a; printf("value of b is %d\n",b); b /= a; printf("value of b is %d\n",b); b %= a; printf("value of b is %d\n",b); b &= 2; printf("value of b is %d\n",b); b |= 2; printf("value of b is %d\n",b); b ^= 2; printf("value of b is %d\n",b); b = 2; printf("value of b is %d\n",b); return 0; }

    Output

    value of b is 32 value of b is 52 value of b is 32

  • value of b is 640 value of b is 32 value of b is 12 value of b is 0 value of b is 2 value of b is 0 value of b is 0 value of b is 0

    6. Increment (++) and Decrement (--) with Prefix and Postfix

    Increment Operator (++) variable value 1 |

    Decrement Operator (--) variable value 1 |

    Operators Same as

    ++a (Increment Prefix) a = a + 1

    --a (Decrement Prefix) a = a - 1

    a++ (Increment Postfix)

    a-- (Decrement Postfix)

    for eg. Source Code :

    1234567891011121314151617#include int main() { int a=20;

  • printf("Print Value with prefix : %d\n", ++a); // increase value with increment prefix printf("Value of a : %d\n", a); printf("Print Value with prefix : %d\n", --a); // decrease value with decrement prefix printf("Value of a : %d\n", a); printf("Print Value with postfix : %d\n", a++); // increase value with increment postfix printf("Value of a : %d\n", a); printf("Print Value with postfix : %d\n", a--); // decrease value with decrement postfix printf("Value of a : %d\n", a); return 0; }

    Print Value with prefix : 21 Value of a : 21 Print Value with prefix : 20 Value of a : 20 Print Value with postfix : 20 Value of a : 21 Print Value with postfix : 21 Value of a : 20

    6. Conditional / Ternary Operator (?:)

    Conditional Operator Expressions |

    Conditional Operator Ternary Operator |

    Conditional Operator expression true ,

    expression output print |

  • Conditional Operator expression false ,

    expression output print | Syntax for Conditional / Ternary Operator expression1 ? expression 2 : expression 3 for eg. Source Code :

    1234567891011#include int main() { int a = 100, b ; b = ( a == 100 ? 2 : 0 ) ; printf("Value of a is %d\n", a); printf("Value of b is %d\n", b); return 0; }

    Output :

    Value of a is 100 Value of b is 2

  • Variables What is a Variable ?

    data types values store | Variable memory location |

    Variable case-sensetive | for eg int a int A - variables |

    Variable alphabet(a-z, A-Z) underscore( _ ) |

    Variables alphanumeric | For eg. a1 = 5, var1, var2

    Variable space allow | Variable name C Keywords |

  • Variable Declaration Variable declare variable data type

    , Memory allocate | Variable Declare Garbage

    Value | Garbage Value :Garbage Value Variable Compiler

    | Syntax for Single Variable Declaration data_type single_variable_name; for eg.

    int a; Source Code : 12345678910#include int main(){ int a; printf("Value of a : %d", a); return 0; } Output : Value of a : 27

    Variable_name a

    Variable_value 27 Garbage Value

    Address 5454

  • Syntax for Multiple Variable Declaration data_type multiple_variable_name; for eg. int a, b, c; Source Code : 123456789101112#include int main(){ int a, b, c; printf("Value of a : %d\n", a); printf("Value of b : %d\n", b); printf("Value of c : %d", c); return 0; } Output : Value of a : 27

    Value of b : 8

    Value of c : 35

    Variable_name a b c Variable_value 27 8 35 Garbage Value

    Address 5454 5458 5462 4bytes/Variable

  • Variable Initialization Variable initialize variable data type ,

    Memory allocate |for eg. int for 2bytes(16-bit) | 4bytes(32-bit) | 8bytes(64-bit), char

    Variable intialization Variable normal value | Variable intialization variable value for eg. int

    a = 5, 6 ; int a = 5; Syntax for Single Variable Initialization data_type single_variable_name = value;

    for eg. int a=5; Source Code : 12345678910#include int main(){ int a = 5; printf("Value of a : %d", a); return 0; } Output : Value of a : 5

  • Variable Scopes Variable Scope | 1. Local Variable 2. Global Variable Local Variable Local Variables function | Local Variables function

    visible | Local Variables default value 'garbage

    value' | Source Code : 123456789101112#include int main(){ int a = 5, b = 6, c; // Local Variable printf("Value of a : %d", a); printf("Value of b : %d", b); printf("Default Value of c : %d", c); return 0; } Output : Value of a : 5 Value of b : 6 Default Value of c : 6 // garbage value

  • Global Variable Global Variables function | Global Variables visibility program

    | Global Variables default value '0'

    | Source Code : 1234567891011#include int a = 5, b = 6, c; // Global Variable int main(){ printf("Value of a : %d", a); printf("Value of b : %d", b); printf("Default Value of c : %d", c); return 0; } Output : Value of a : 5 Value of b : 6 Default Value of c : 0

  • Input and Output functions Input and Output in-built functions | scanf (Input) printf (Output) 1. scanf (Input Function) scanf standard library function | scanf numeric, characters string value input | scanf program stdio.h header file include | scanf Keyboard data read | Syntax for scanf() function scanf("format_specifier(s)", &variable_list(s) ); example format specifiers variables & (address operator or ampersand) | data types - format specifiers | %d integer data type format specifier | data Keyboard read Variable '&' | input Compiler variable address | for eg.

    scanf( "%d %d", &a, &b );

  • 2. printf (Output Function) printf standard library function | printf data screen write | printf program stdio.h header file include | Syntax for scanf() function printf("String / format_specifier(s) / escape sequence(s)", variable_list(s) ); for string string | String Double quotes(" ") semi-colon |

    printf("Hello World !"); for format specifier and variable scanf input numeric (%d), character(%c), float(%f) string(%s) output print | variable address(&) |

    printf("%d", a) for escape sequence printf \n(newline) escape sequence |

    printf("\n");

  • Input/Output Library Functions getchar() and putchar() (for single character) gets() and puts() (for string) 1. getchar(Input) and putchar(Output) (for single character) getchar putchar standard input/output (stdio.h) header file library functions , #include preprocessor | getchar user input character | user character input putchar character output print |

    For Ex. Source Code : 1234567891011121314#include int main( ) { int i; printf( "Enter a character :"); i = getchar(); printf( "Entered character is : "); putchar(i); return 0; } Output : Enter a character :hello Entered character is : h

  • 2. gets() and puts() (for string) gets puts standard input/output (stdio.h) header file library functions , #include preprocessor | gets user input string | puts input string output print |

    For Ex. Source Code : 123456789101112131415#include int main(){ char name[20]; printf( "Enter your name : \n"); gets(name); printf( "You name is : "); puts(name); return 0; } Output : Enter your name : Deepak Your name is : Deepak