Documente Academic
Documente Profesional
Documente Cultură
INTRODUCTION TO C
C was developed by Dennis Ritchie at Bell laboratory in 1972 It is an upgrade version of languages B and BCPL.
Features of C
It is a structured programming language. It is highly portable. It is a middle level language. It is a case sensitive language. It uses Top-Down approach. It is a Free form language.etc,.
Steps in learning C
Character Set
Tokens
Instructions
Programs
C Character Set
C Character Set
Alphabets Digits
Escape Sequence
Special Characters
White Spaces
!@#$%^&*()_-+ = \ | { } [ ] etc,. Blank Space ,Horizontal tab, New line, Vertical tab etc,.
Special characters
Comma Period or dot Semicolon Colon Apostrophe Quotation mark Exclamation mark Vertical bar Back Slash Tilde Underscore Dollar Question mark , . ; : ! | \ ~ $ ?
Ampersand Caret Asterisk Minus Addition Lesser than Greater than Parenthesis Bracket Braces Percentage Hash Equal to At the rate
C Tokens
The smallest element in the C language is the token. It may be a single character or a sequence of characters.
C Tokens (Cont)
C Tokens
Identifiers Keywords Constants Strings operators Eg:main, avg Eg: int, for Eg:17, 15.5 Eg: ab Eg: + -
Executing a C Program
Creating the Program
Compilation
Linking
Execution
Structure of C program
DOCUMENTATION SECTION PREPROCESSOR SECTION DEFINITION SECTION GLOBAL DECLARATION SECTION main() { Declaration part; Executable Part; } sub program section { Body of the subprogram; }
Documentation Section
It contains the comment lines.
Preprocessor Section
It is used to link library files.
Declaration Section
It describes the data to be used within the function.
Executable Part
It contains the valid statements.
C Programs
C program may have many functions.
One and only one of the functions MUST BE named main. main is the starting point for the program. main and other functions in a program are divided into two sections, declaration section and statement section.
Preprocessor Directives
Special instructions to the preprocessor that tells how to prepare the program for compilation E.g: include : tells the processor to include information from selected libraries known as header files e.g. <stdio.h>
The compiler simply ignores comments when it translates the program into executable code. To identify a comments, C uses opening /* and closing */ comment tokens.
Comments (Cont)
Comments can appear anywhere in a program. Comments are also found wherever it is necessary to explain a point about a code. Comments cannot be nested in C i.e. you cannot have comments inside comments.
C program
/* Example program in C*/ Comments # include <stdio.h> Preprocessor Section Global Declaration void main () {
C Tokens
Identifiers Keywords Constants Operators Special symbols
Identifiers
Identifiers are names given to various program elements such as variables, functions and arrays etc,. Eg: #define N 10 #define a 15 Here N and a are user defined identifiers.
Invalid Names
/* $ is illegal */ /* Starts with 2 */
Variables
Variable is an identifier that is used to represent some specified type of information. Eg: x=3 Here x is variable.
Keywords
It is a reserved words. Cannot be used for anything else. Examples:
int while for etc,.
Keywords
Auto Double Int Struct Break Else Long Switch Case Enum Static register typedef Char extern return union Const Float short unsigned While Continue For signed void Default Goto sizeof do If
Constants
It is an entity whose value does not changes during the execution. Eg: x=3 Here 3 is a constant.
Types
Numeric constants Character constant
Constants
Constants
Numeric Constants
Character Constants
Integer Constant
Real Constant
String Constant
Numeric constants
Integer constants It is formed using a sequence of digits.
Decimal - 0 to 9 . Octal - 0 to 7. Hexa - 0 to 9 ,A to F
Numeric constants
Real constants It is formed using a sequence of digits but it contain decimal point. length, height, price distance measured in real number Eg: 2.5, 5.11, etc.
Character constants
Single character constant
A character constant is a single character they also represented with single digit or a single special symbol which is enclosed in single quotes.
Eg: a, 8,_etc.
Character constants
String constants
String constant are sequence of characters enclosed with in double quote. Eg: Hello ,444,a etc,.
Operators
An operator is a symbol that specifies an operation to be performed on the operands. Eg: a + b + is an operator. a,b are operands.
Data Types
A Data type is the type of data that are going to access within the program.
These Standard type can be used to build more complex data types called Derived Types (e.g. pointers, array, union etc.).
Data types
Data type Size(bytes) Range Format string
Char
int
1
2
-128 to 127
-32,768 to 32,767
%c
%d
Float
Double
4
8
%f
%lf
integer
A number without a fraction part : integral number. C supports three different sizes of the integer data type : short int int long int
Floating Point
A floating-point type is a number with a fractional part, e.g. 56.78 Floating point numbers are stored using 4 Byte. Types
character - PLato
Character are generally stored using 8 bits(1 Byte) of the internal storage. Character a x ASCII code value 97(decimal) or 01100001(binary) 120(decimal) or 01111000(binary)
void
The void type has no values and no operations. Both the set of values and the set of operations are empty.
Variables Declaration
To create a variable, you must specify the type and then its identifier :
float price; int a,b; char code;
Types of Operator
Arithmetic operator Relational operator Logical operator Assignment operator Increment or decrement operator(unary) Bitwise operator Conditional operator
Arithmetic operator
It is used to carry out arithmetic operations like addition, subtraction etc, Eg: + , - , * , / etc,
Sample program
#include<stdio.h> // Header File #include <conio.h> int b=10; //Global Declaration void main ( ) /* main is the starting of every c program */ { int a,c; //Local Declaration clrscr( ); scanf(%d,&a); printf( \n The sum of the two values:); c = a+b; printf(%d,c); getch( ); }
Sample program
#include<stdio.h> #include <conio.h> void main ( ) { int a=10,b=4,c; float d=3,e; clrscr( ); c = a/b; printf(" \n value a/b is:%d",c); e = a/d; printf("\n value a/d is:%f",e); getch( ); }
Output
value a/b is:2 value a/d is:3.333333
Relational operator
It is used to compare two or more operands. Eg :< , > , <= , >=, != etc,. 5 < 9 which will return 1
Logical operator
It is used to combine the result of two or more condition. AND(&&) OR (||) NOT (!) are Logical operators. Eg: (i>10)&&(j>5). (i>10)||(j>5) etc,.
Sample program
#include<stdio.h> #include <conio.h> void main ( ) { int a=10,b=3,c=5,e; clrscr( ); if(a>b) // relational operator { printf(" \n a is bigger than b"); } if((a>b)&&(a>c)) //Logical operator { printf(" \n a is biggest"); } getch( ); }
Output
a is bigger than b a is biggest
Assignment operator
It is used to assign a value or expression etc to a variable. Eg: a =10. a=b a = b + c etc,.
Assignment operator(Cont)
Compound operator It is also used to assign a value to a variable. Eg: x + = y means x = x + y Nested operator It is used for multiple assignment. Eg: i = j = k = 0;
Sample program
#include<stdio.h> #include <conio.h> int b=10; void main ( ) { int a=3,b=5; clrscr( ); a+=b; // a= a+b printf(" \n The sum of the two values:%d",a); getch( ); }
Output
The sum of the two values:8
Sample Program
#include<stdio.h> #include <conio.h> void main ( ) { int a=5; clrscr( ); printf(" \n Post increment Value:%d",a++); printf(" \n Pre increment Value:%d",++a); printf(" \n Pre decrement Value:%d",--a); printf(" \n Post decrement Value:%d",a--); getch( );
Output
Post increment Value:5 Pre increment Value:7 Pre decrement Value:6 Post decrement Value:6
Bitwise operator
It is used to manipulate data at bit level. Eg: a=5 i.e 0000 0101 b=4 i.e 0000 0100 Then a & b = 0000 0100 a | b = 0000 0101 etc,.
Sample program
#include<stdio.h> #include <conio.h> void main ( ) { int a=5,b=4,c; //char a=5,b=4,c; clrscr( ); c = a&b; printf(" \n value a&b is:%d",c); getch( ); }
Output
value a&b is:4
Sample Program
#include<stdio.h> #include <conio.h> void main ( ) { int a=5,b=8,c; clrscr( ); c = a>b?a:b; //Conditional operator printf(" \n The Larger Value is%d",c); getch( ); }
Output
The Larger Value is 8
Special Operator
comma operator ( , ) sizeof operator pointer operator (& , *) etc,.
#include<stdio.h> #include <conio.h> void main ( ) { int c; clrscr( ); printf(" \n size of int is:%d",sizeof c); getch( ); }
Output
size of int is: 2
Expression
An expression represent data item such as variable, constant are interconnected using operators. Eg:
Expression a+b+c C Expression a+b+c
a2+b2
a*a + b*b
Example
5 - 20/4 + 3*3 1 =5-5+91 =0+91 =91 =8
Example
5 (20/4) + 3*(3 1) = 5 - 5 + 3*2 =5-5+6 =6
Type Conversion
Converting the type of an expression from one type to another type. Eg: x = (int)10.45
Sample Program
#include<stdio.h> #include <conio.h> void main ( ) { int c; clrscr( ); c=(int)10.45; printf("\nOutput is:%d",c); getch( ); } OUTPUT Output is:10
Input/Output Function
Input/Output Function
Formatted
Unformatted
Input
scanf() fscanf()
Output
printf() fprintf()
Formatted Input/Output
Standard Output
The standard output file is the monitor. Like the keyboard, it is a text file. When you need to display data that is not text, it must be converted into to the text before it is written to the screen.
Decision Making
It is used to change the order of the program based on condition. Categories:
Sequential structure Selection structure Iteration structure Encapsulation structure
Selection structure
In which instruction are executed based on the result of some condition.
Iteration structure
In which instruction are executed repeatedly.
Encapsulation structure
In which some compound structure are used.
SELECTION STRUCTURE
It allows the program to make a choice from alternative paths. C provide the following selection structures
IF statement IF ELSE statement Nested IF ELSE statement IF ELSE ladder
IF Statement
Syntax
IF (condition is true) {
Statements;
Statements False
True
If condition
Example #include<stdio.h>
#include <conio.h> void main ( ) { int a; clrscr( ); printf("\nEnter the number:"); scanf("%d",&a); if(a>10) { printf(" \n a is greater than 10"); } getch( ); }
Output
Enter the number: 12 a is greater than 10
IFELSE Statement
Syntax IF (condition) { If True False True statements; Condition } ELSE True False statements statements { False statements; }
#include<stdio.h> #include <conio.h> void main ( ) { int a; clrscr( ); printf("\nEnter the number:"); scanf("%d",&a); if(a>10) { printf(" \n a is greater than 10"); } else { printf(" \n a is less than 10"); } getch( ); }
NESTED IF ELSE
If Condition 1 True True
If Condition 2
False Statements
False
True statements
False statements
NESTED IF ELSE
Syntax IF (condition1) { IF (condition2) { True statements; } ELSE { False statements; } } ELSE { False statements; }
IFELSE LADDER
TRUE
Condition 1
FALSE
FALSE
2
TRUE Statements Statements
Condition 3
FALSE
Statements
Syntax IF (condition1) { statements; } else if (condition2) { statements; } else if (condition3) { statements; } else { statements; }
IFELSE LADDER
#include<stdio.h> #include<conio.h> void main() { int m1,m2,m3; float avg; printf("\nEnter the marks:"); scanf("%d%d%d",&m1,&m2,&m3); avg=(m1+m2+m3)/3; printf("\n The average is:%f",avg); printf("\n The Grade is:"); if(avg>=60) { printf("First class"); }
Example
else if(avg>=50) { printf("Second class"); } else if(avg>=35) { printf("Thrid class"); } else { printf("Fail"); } getch(); }
Output
Enter the marks:65 75 70 The average is:70.000000 The Grade is: First class
Looping structure
It is used to execute some instructions several time based on some condition.
WHILE DoWHILE For
WHILE Loop
Syntax
.
condition
True Body of The loop
False
Example
#include<stdio.h> #include<conio.h> void main() { int i=1,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); while(i<=n) { fact =fact *i; i++; // i=i+1 } printf("\n The value of %d! is:%d",n,fact); getch(); }
Output
Enter the Number:3 The value of 3! is: 6
DOWHILE Loop
Syntax do { Body of the loop }while (condition);
True
condition
False
for loop
Syntax for (initialization; test condition; Increment/Decrement) { Body of the loop }
for loop
Initialization
Example
#include<stdio.h> #include<conio.h> void main() { int i,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); for(i=1;i<=n;i++) { fact =fact *i; } printf("\n The value of %d! is:%d",n,fact); getch(); }
Output
Enter the Number:3 The value of 3! is: 6
}
}
CASE structure
Switch Case 1
Case 2
Default case
CASE structure
Syntax switch (expression) { case constant 1: block1; break; case constant 2: block2; break; . . default : default block; break; }
Example
#include<stdio.h> #include<conio.h> void main() { int i,n; printf("\nEnter the Number:"); scanf("%d",&n); switch(n) { case 1: { printf("\n Its in case 1"); break; }
Output
Enter the Number:2 Its in case 2
break Statement
It is used to terminate the loop When a break statement is encountered inside a loop, then the loop is terminated.
Continue Statement
When a continue statement is encountered inside a loop, the control is transferred to the beginning.
goto Statement
When a goto statement is encountered inside a loop, the control is transferred to the beginning.
getchar() Example
#include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char x; printf("enter the character:"); x=getchar();
getche() Example
#include <stdio.h> #include <conio.h> void main() { char c ; clrscr(); printf("\nInput a string:"); c = getche();
Getch() Example
#include <stdio.h> #include <conio.h> void main() { char c; clrscr(); printf("\nInput a string:"); c = getch();
getc Example
#include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char x; printf("enter the character:"); x=getc(stdin);
gets() Example
#include <stdio.h> #include<conio.h> void main() { char c[80]; clrscr(); printf("Input a string:"); gets(c);
printf("The string is:"); puts(c); getch(); } Output: Input a string:qwerty The string is:qwerty
Example
#include<stdio.h> #include<conio.h> void main() { int a,b,c,n; clrscr(); printf("\nEnter the value of a,b:"); scanf("%d%d",&a,&b); printf("\nMENU"); printf("\n1.ADD\n2.SUB\n3.MULTIPLY\n0.EXIT"); printf("\nEnter the choice:"); scanf("%d",&n);
switch(n) { case 1:
c=a+b; printf("\nThe result of Addition is:%d",c); break;
case 2:
c=a-b; printf("\nThe result of Subtraction is:%d",c); break;
case 3:
Output
Enter the value of a,b:5 6 MENU 1.ADD 2.SUB 3.MULTIPLY 0.EXIT Enter the choice:1 The result of Addition is:11
Finding Armstrong No
#include<stdio.h> #include<conio.h> void main() { int r=0,sum=0,n,a; printf("\nEnter the number:"); scanf("%d",&n); a=n; while(n>0) { r=n%10; sum=sum+r*r*r; n=n/10; }
if(a==sum) { printf("\nIt is an armstrong number"); } else { printf("\nIt is not an armstrong number"); } getch(); }
Output
Enter the number:153 It is an armstrong number
Output
Enter the no:156 sum of the digits is:12
Reverse of a number
#include<stdio.h> #include<conio.h> void main() { int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0)
Output
Enter the no:567 Reverse of the number is:765
Fibonacci Series
#include<stdio.h> #include<conio.h> void main() { int f=0,f1=-1,f2=1,n,i; printf("\nEnter the number:"); scanf("%d",&n);
Output
Enter the number:5 0 1 1
Swapping
#include<stdio.h> #include <conio.h> void main ( ) { int a,b,c; clrscr( ); printf(" \nEnter the value of a:"); scanf("%d",&a); printf(" \nEnter the value of b:"); scanf("%d",&b); c=a; a=b; b=c;
printf(" \nThe value of a is:%d",a); printf(" \nThe value of b is:%d",b); getch( ); } Output: Enter the value of a:5 Enter the value of b:4 The value of a is:4 The value of b is:5
a=a+b; b=a-b; a=a-b; printf(" \nThe value of a is:%d",a); printf(" \nThe value of b is:%d",b); getch( ); }
Output: Enter the value of a:5 Enter the value of b:6 The value of a is:6 The value of b is:5
Quadratic Equation
#include<stdio.h> #include <conio.h> #include<math.h> void main ( ) { int a,b,c,d,r1,r2; clrscr( ); printf(" \nEnter the value of a:"); scanf("%d",&a); printf(" \nEnter the value of b:"); scanf("%d",&b); printf(" \nEnter the value of c:"); scanf("%d",&c); d=b*b-4*a*c;
if(d>=0) { r1=(-b+sqrt(d))/(2*a); r2=(-b-sqrt(d))/(2*a); printf(" \nThe roots are %d,%d",r1,r2); } else { printf(" \nThe roots are imaginary"); } getch( ); }
Output
Enter the value of a:4 Enter the value of b:5 Enter the value of c:6 The roots are imaginary