C/C++ IDE
- Turbo C,C++ IDE for Windows XP
- GCC 4.5 for any Linux OS
- Dev C,C++ for any Windows OS
- C,C++ Emulator for Windows 7
What is a computer?
A computer is a machine that manipulates data according to a set of instructionsWhat is Language ?
Set of codes used to write a program (i.e)code- collection of program statementThere are 3 level in Language
- High level
- Low level
- Machine language
High level language
- Consists of statements
- Using English words & mathematical notations
- Used in any computer
- Easy to understand
- Eamples: BASIC,PASCAL,FORTRAN,ADA,ALGO,C++, C#,Java,HTML,etc
- C is Mid level language
Low level language
- Consists of mnemonics
- Mnemonics-shortform of word
- Vary from platform to platform
- ASSEMBLY LANGUAGE is the best example of the low level language
Machine language
- Consists of detailed cryptic instructions
- Using only 0’s and 1’s
- It can be understand by only computer
- We can’t understand
What is a Program ?
- Set of instructions
- Stored - Processor
- Executed sequencely - Computer
- Carryout - Specific task
- Represented - in Binary codes
- Composed – in Alphabets, Digits, Special symbols
Evolution of Languages
C FEATURES
- Structured Programming (top down processor)
- Flexible & Efficient
- It includes features of low level language
- Portable ( If same platform of computer)
- Faster (1:50 => C:BASIC)
- Ability to extend itself, Case sensitive
- No. of. Build-in functions
- Simple & Small
- Compiler / Interpreter
- Robust (Build-in functions & Operators)
- 32 keywords only & uses American Standard Code for Information Interchange( so available only in English )
Compiler and Interpreter
To change source to object file, compile using compiler or interpret using interpreter (c has both compiler as well as interpreter)- Interpreter:convert program line by line to machine code
- Compiler: Convert Whole program to machine code at one
- Linker: convert object code into executable machine language
Internal operations
American Standard Code for Information Interchange
Character set used in CShort Cut Keys In Turbo C Editor
- F1= Help (to call help of the compiler)
- F2= Save (to save the program)
- F3= Open (to open already created program)
- F5= Zooming window
- F6= Next window
- ALT+X= Exit
- ALT+#= Go to n'th line
- ALT+F3= Close window
- ALT+F5= View last output
- ALT+F7= Interpret
- ALT+F9= Compile
- CTRL+F9= Execute
- SHIFT+DEL= Cut
- CTRL+INS= Copy
- SHIFT+INS= Paste
- CTRL+DEL= Clear/Delete
- ALT+BACKSPACE= Undo
- ALT+first leter of the menu= to open that menu
How to write your Hello World Program in C
Programming Structure Of C
Syntax
// Documentation text #includereturn-type main(argument) { //Body of the main function }
Where
- return-types can be void, int, float, char, double, etc
- We generally use void as return-type when there is no returning values
FIRST PROGRAM
// This is our first example program #includevoid main() { printf("Hello World"); }
Output:
Welcome to C
Where
- //-command line
- #-Preprocessor
- Include-Directives of C Compiler
- <>-Tag
- stdio-Standard input output
- conio-Console input output
- .h-Header files
- main-program where to start execution
- {}-block
Terminologies
- Algorithm:
- Steps writing sequencely in simplest way in which program solved
- Flowchart
- Graphical representation of algorithm where all activities are depicted using symbols as flows
- Statement
- Single line of the program(made of operator,operands,etc)
- Syntax
- Refers to the grammer of a programming language
Main Program
- It is special function
- CALLED by C system
- Show starting of the program
- So every program must have exactly one main function
- If we have more than one, compiler will confuse
COMMENTS
- Are non-executable statements
- Are used for better understanding
Two types of comments in C
- Single line using //
- Multi line using /* */
//first program
/*first program */
Header Files
- Special source files
- Contains library functions
- Library functions are pre-written
- Used to carry out commonly used routines
- To use the library function ,we should include the header file where the function
#include<headerfile.h>Where
- Filename - header file name
Commonly used Header files
- stdio- Standard I/o library functions
- conio - DOS console I/o routines
- string- String manipulation functions
- math- Mathematical functions
- stdlib- utility function(conversion routines, search/sort routines, and other miscellany.)
- ctype-Character testing & conversion functions
C Tokens
The following are 6 types of tokens in C- Keywords
- Constants
- Strings
- Operators
- Identifiers
- Special symbols
Keywords
- They have fixed predefined meaning
- We can't change
- 32 key words in ANSI C
- Keywords should be in lowercase
- Another name reserved words
List of Keywords
Identifiers
- Used defined name- variable, function, array.
- Consists - sequence of letters,digits,underscore
- First character - alphabet/ underscore
- Length- max. 31 characters
- Can't use Keywords & White spaces
- Case sensitive
- Valid Example: AO, disks, I, i
- Invalid Example: AO/, auto, 56
Constants
Fixed value, do not change even during executionTypes of Constants
Numeric Constant
- Must have atleast one digit
- May be +ve / -ve
- decimal point is allowed only for real constants
- Can't use White spaces & Commas
Character Constant:
- Must have atleast one character
- Can be single Alphabet/Digit/Special symbol
- Enclosed within ' ' for single character constants
- Enclosed within " " for string constants
Variables
- Simply Changeable values
- It can take different values at different times during execution
Operators
- Symbols that perform operation on data
- C supports rich set of built-in operators
Special Symbols
- They are such as [ ] { } < > ( ) / \ . , ? " ' ; : ! @ # $ % ^ & * _ - = + | ~ `
String
- Sequence of character
- Enclosed within double quotes (“”)
- Character - letters, numbers, special characters, blank spaces
- Case sensitive: 'A' ≠ 'a', 'A' ≠ "A"
Escape Characters
- These are used in output functions
- They are not two characters, It is single
- They are known as escape sequence, backslash characters
List of Escape Characters
Data Types
Used to specify the type of the date so that we can easily classify our operationsPrimitive Data Types: char, int, float, double
Derived Data Types: Array, Pointer, Function
User Defined Data Types: enum, struct, union
Where: signed, unsigned, short, long are Data type modifierDeclaration
All variables must be declared before it is used to reserve memory storage area for that variable. Syntax:Data-type Variable-list;Where:
- Data Type:int,char,float,double,etc
- Variable-List:list of variable separated by commas
int a; int harish; float b,chitra;
Initialization and Value Assignment
Initialization: First assignment of value to the variable is called initializing- To store int numbers dont use decimal point
- To store float number can use decimal points(7 digits after precision)
- To store double numbers can use decimal point (14 digits after precision)
- To store single character
- To store sequence of character(String)
int a; a=5;//Declare and assign by two steps (or) int a=5;//Declare and assign by single step
float a; a=5.6549870;//Declare and assign by two steps (or) float a=5.6549870;//Declare and assign by single step<
double rs; rs=5.01234567890123;// Declare and assign by two steps(or) double rs=5.01234567890123;//Declare and assign by single step
char a; a=‘n’;//(or) char a=‘n’;
char a[10]; a=“harish”;//(or) char a[10]=“harish”;
Input & Output Functions
- To interface the user and computer I/O functions are used
- To display message, output, warning
- To get input from the user
- These are available in appropriate header files such as stdio.h, conio.h, etc
List of Type Specifiers
printf() from stdio.h
Syntaxprintf(“control string”,arg-list);
Displaying text message
Example:printf("What is this");Output:
What is this
Displaying variable values using type specifier
Example:int a=5; printf("result: %d",a);Output:
result: 5Note: %- shows the content of the variable
Displaying escape characters
Example:printf("well \ncome");Output:
well come
scanf() from stdio.h
Syntax:scanf(“control string”,&arg-list);
Reading a value
Example:int a; scanf("%d",&a);
Reading no.of values
Example:int a; char b; scanf("%d %c",&a,&b);Note: &- shows address of the variable
Unformatted I/O
Reading a character
To get a single character from the userstdio.h
- getchar()-display the character while typing the character
conio.h
- getch() - won’t display the character while typing the character
- getche() - display the character while typing the character and echo it again
Writing a character
To display a single character to user- putchar(), putch() - both are used to display the character which is passed through it.
- fflush() - method is used to clear the unwanted dummy values in the variable
- clrscr() - method is used to clear the last out in the output screen
Example 1
#include<stdio.h> void main() { float a; printf("Enter the value for A:"); scanf("%f",&a); printf("Enterd value: %f",a); }Output:
Enter the value for A:4.52 Entered value:4.520000Example 2
#include<conio.h> void main() { char a; Printf("Enter a character: "); a=getche(); }Output:
Enter a character: j jExample 3
#include<stdio.h> void main() { char a; printf("Enter A value:"); a=getchar(); printf("Entered value: "); putchar(a); }Output:
Enter A value: j Entered value: jExample 4
#include<conio.h> void main() { char a; printf("Enter A value:"); a=getch(); printf("Entered value:"); putch(a); }Output:
Enter the value: Entered value:j
8 TYPES OF OPERATORS
- Arithmetic
- Relational
- Logical
- Assignment (short hand)
- Increment decrement
- Conditional
- Bitwise
- special
Arithmetic Operator
To perform arithmetic operation such as addition, subtraction, multiplication, division and modulo operation on numeric//Example Program #include<stdio.h> void main() { int a=10,b=5,c; c=a+b; printf("ADD: %d",c); c=a-b; printf("SUB: %d",c); c=a*b; printf("MUL: %d",c); c=a/b; printf("DIV: %d",c); c=a%b; printf("MOD: %d",c); }Output:
ADD:15 SUB:5 MUL:50 DIV:2 MOD:0
Relational Operator
To find relationship between two operands and its result will either -true(1) / false(0)20>30 false 20<30 true 25>=30 false 25<=25 true 25==30 false 25!=30 true
Logical Operator
To find the relation ship between two or more relational expressions and its result will either true(1) / false(0)((20>30)&&(20<30)) False false true ((20>30)||(20<30)) True false true !(20>30) True false
Shorthand Assignment Operator
To simplify certain statement (operator =) Syntax:variable-name arithmentic = expressionExample:
int a=5,b=6; a+=b; printf("a: %d",a);Output:
a=11;
Increment and Decrement Operator
Increment Operator(++)
Syntax:variable++ (or) ++variablePre Increment Example:
int a=5,b; b=++a; printf("a=%d, b=%d",a , b);Output:
a=6, b=6Post Increment Example:
int a=5,b; b=a++; printf("a=%d, b=%d",a , b);Output:
a=5, b=6Note: we may use only one operand int a=5; a++; printf("a=%d", a); Output:
a=6
Decrement Operator(--)
Syntax:variable-- (or) --variablePre Decrement Example:
int a=5,b; b=--a; printf("a=%d, b=%d",a , b);Output:
a=4, b=4Post Decrement Example:
int a=5,b; b=a--; printf("a=%d, b=%d",a , b);Output:
a=4, b=5Note: we may use only one operand
int a=5; a--; printf("a=%d", a);Output:
a=4
Conditional Operator
Builds simple conditional expression Syntax:Conditional-Expresion ? True-Statement: False-StatementExample:
5>6 ? printf("true") : printf("false");Output:
false
Special Operators
Comma (,)
- To link releated Expression
- Evaluated from left to right
int a=5,b=6;
Sizeof
- To get the size of the operand
- Used to dynamic memory allocation of like array and structures
int a=5,b; b=sizeof(a); printf("Size of int variable a:%d",b);Output:
Size of int variable a:2
Bitwise Operators
- Bit by bit manipulation
- Can’t be applied on float or double
- Require two integer type operands
- Refer table of logical operator
a=5,b=6,c c=a&b; //a=0000 0000 0011 0101 //b=0000 0000 0011 0110 //c=0000 0000 0011 0100 printf("c=%d",c);Output:
c=4Example OR
a=5,b=6,c; c=a|b; //a=0000 0000 0011 0101 //b=0000 0000 0011 0110 //c=0000 0000 0011 0111 printf("c=%d",c);Output:
c=7Example XOR
a=5,b=6,c; c=a^b; //a=0000 0000 0011 0101 //b=0000 0000 0011 0110 //c=0000 0000 0000 0011 printf("c=%d",c);Output:
c=3Example NOT
a=5; c=^a; //a=0000 0000 0011 0101 //c=1111 1111 1100 1010 overflow printf("c=%d",c);Output:
c=-6(over flow)Example Right shift
a=5,b; b=a>>2; //a= 0000 0000 0011 0101 //b= 00 0000 0000 0011 01-- printf("b=%d",b);Output:
b=1Example Left shift
a=5,b; b=a<<2; //a=0000 0000 0011 0101 //b=---00 0000 0011 0101 00 printf("b=%d",b);Output:
b=20
Operator Presedence
How to evaluate the equation
Example 1A=9-12/3+3*2-1Steps:
A=9-4+3*2-1 A=9-4+6-1 A=5+6-1 A=11-1 A=10Example 2
A=9-12/(3+3)*(2-1)Steps:
A=9-12/6*(2-1) A=9-12/6*1 A=9-2*1 A=9-2 A=7
Expressions
- Arithmetic
- Relational
- Logical
Variable=Expression;
Arithmetic Expressions
It contains collection of constants, variables and arithmetic operator and results a numeric valuex=5*6-6+4/3;
Relational Expresssions
It contains collection of constants, variables and relational operator and results either true(1) or false(0) valuex=(5==6);
Logical Expression
It contains collection of relational expressions and logical operators and results either true(1) or false(0)x=(a<5)&&(b==2)||(c!=1);
Type Casting
- Converting form one type to another type
- Small in Big, not Big in Small (memory space)
Types of Type Casting:
- Implicit Type Casting Example:
- Explicit Type Casting Syntax:
int x=5; float y=6.2,z; z=x+y;//z as floating
Type (var) (or) (Type) varExample:
x=(int)y+.5;//x as floating
Comments
Post a Comment