Computer - Hardware: Arithmetic and Logic Unit Micro Processor
Computer - Hardware: Arithmetic and Logic Unit Micro Processor
Monitor Printer
Output Devices
Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical representation of an algorithm. Algorithm to find whether a number even or odd: Step1: Begin Step1: START Step2: Take a number Step2: Read num Step3: if the number is divisible by2 then Step3: if(num%2=0) then print that number is even print num is even otherwise print that number is odd otherwise print num is odd Step4: End Step4: STOP (Algorithm in natural language) (Algorithm by using pseudo code)
#include<stdio.h> #include<conio.h> main() { int num; printf(Enter any number); scanf(%d,&num); if(num%2==0) printf(%d is even,num); else printf(%d is odd,num); } (Program in C language)
start
Flow chart:
read num
Yes If num%2=0 No
Hexagon
Trapezoid Cylinder
Iteration
Manual Operation Magnetic Disk Storage
1 2 3 4 5 6 7 8 9 1 0
#include<stdio.h> int main(void) { int n1, n2,product; printf(Enter two numbers : ); scanf(%d %d,&n1,&n2); product = n1 * n2; printf(%d,product); return 0; }
The only language the computer can understand is machine language (binary language).
A high level language is an English like language where one instruction typically translates into a series of machinelanguage instructions.
A low level language corresponds closely to machine code so that a single low-level language instruction translates to a single machine language instruction.
Structure of C program
/*Program to find area and perimeter of Circle */ #include<stdio.h> #define PI 3.1415 float radius; float area(); float perimeter(); int main() { float a, p; printf(Enter radius : ); scanf(%f,&radius); a = area(); p = perimeter(); printf(Area of Circle : %f,a); printf(Perimeter : %f,p); } float area() { return (PI * radius * radius); } float perimeter() { return (2 * PI * radius); } Documentation Section
Linkage Section Definition Section Global Declaration Section
Executing a C program
Text Editor
C-compiler
010110 100 . 01011 101 #include<stdio.h> int main() { .
prog1.c
compiles
Syntax Errors? No Object machine code Yes
Linker
00101010 . 01010101
adds
prog1.obj
Executes Feeds
C-Runtime Input
Runtime or Logic Errors ?
Yes
Output
Translators are system software used to convert high-level language program into machine-language code. Compiler : Coverts the entire source program at a time into object code file, and saves it in secondary storage permanently. The same object machine code file will be executed several times, whenever needed. Interpreter : Each statement of source program is translated into machine code and executed immediately. Translation and execution of each and every statement is repeated till the end of the program. No object code is saved. Translation is repeated for every execution of the source program.
Character Set of C-Language Alphabets : A-Z and a-z Digits : 0-9 Special Symbols : ~ ! @ # $ % ^ & ( ) _ - + = | \ { } [ ] : ; <>,.?/ White Spaces : space , Horizontal tab, Vertical tab, New Line Form Feed. C-Language Keywords(C99)
auto break case char const continue default do _Bool inline double else enum extern float for goto if _Imaginary int long register return short signed sizeof static restrict struct switch typedef union unsigned void volatile while _Complex
C-Tokens
Tokens : The smallest individual units of a C- program are called Tokens. Key words, Identifiers, Constants, Operators, Delimiters. Key words : have a predefined meaning and these meanings cannot be changed. All keywords must be written in small letters (except additional c99 keywords). Identifiers : names of variables, functions, structures, unions, macros, labels, arrays etc., Rules for define identifiers : a) First character must be alphabetic character or under score b) Second character onwards alphabetic character of digit or under score. c) First 63 characters of an identifier are significant. d) Cannot duplicate a key word. e) May not have a space or any other special symbol except under score. f) C language is Case-sensitive.
C-Tokens
Constants : fixed values that do not change during execution of a program. Boolean constants : 0 ( false) and 1 (true) Character constants : only one character enclosed between two single quotes ( except escape characters ). wide character type - wchar_t - for Unicode characters. Integer constants : +123, -3454 , 0235 (octal value), 0x43d98 ( hexa - decimal value) 54764U, 124356578L, 124567856UL Float constants : 0.2 , 876.345, .345623 , 23.4E+8, 47.45e+6 String Constants : Hello world , Have a nice day! Complex Constants : real part + imaginary part * I ex : 12.3 + 3.45 * I Operators : a symbol, which indicates an operation to be performed. Operators are used to manipulate data in program. Delimiters : Language Pattern of c-language uses special kind of symbols : (colon, used for labels) ; (semicolon terminates statement ) ( ) parameter list [ ] ( array declaration and subscript ), { } ( block statement ) # ( hash for preprocessor directive ) , (comma variable separator )
Conversion Specifiers
Code %a %s %c %d %f %e %g %u %o %x %i %p %n %hd %ld %lf %% Format Hexa decimal output in the form of 0xh.hhhhp+d(C99 only) String of characters (until null zero is reached ) Character Decimal integer Floating-point numbers Exponential notation floating-point numbers Use the shorter of %f or %e Unsigned integer Octal integer Hexadecimal integer Signed decimal integer Display a pointer The associated argument must be a pointer to integer, This sepecifier causes the number of characters written in to be stored in that integer. short integer long integer long double Prints a percent sign (%)
Code
\b \f \n \r \t \" \' \\ \v \a \? \N \xN
Meaning
Backspace Form feed New line Carriage return Horizontal tab Double quote Single quote Backslash Vertical tab Alert Question mark Octal constant (N is an octal constant) Hexadecimal constant (N is a hexadecimal constant)
Executes all Prefix Operations Evaluation of expression Executes all Postfix Operations
/* prefix operators */ #include<stdio.h> int main() { int a = 7, b = 12, c; c = b * (++a) + 5 * (++a); printf( a = %d, a); printf(\n b = %d,b); printf(\n c = %d,c); } Output: a=9 b = 12 c = 153 ( 12 * 9 + 5 * 9)
/* prefix and postfix operators */ #include<stdio.h> int main() { int a = 7, b = 12, c; c = b * (a++) + 5 * (++a); printf( a = %d, a); printf(\n b = %d,b); printf(\n c = %d,c); } Output: a=9 b = 12 c = 136 ( 12 * 8 + 5 * 8)
/* postfix operators */ #include<stdio.h> int main() { int a = 7, b = 12, c; c = b * (a++) + 5 * (a++); printf( a = %d, a); printf(\n b = %d,b); printf(\n c = %d,c); } Output: a=9 b = 12 c = 119 ( 12 * 7 + 5 * 7)
Bitwise OR A (42) : 00000000 00101010 B (15) : 00000000 00001111 ----------------------------------| (47) : 00000000 00101111 ----------------------------------Bitwise NOT A (42) : 00000000 00101010 ----------------------------------~ (-43) : 11111111 11010101 -----------------------------------
TYPE CONVERSION
1.bool
2.char 3.short int 4.int 5.long int 6.long long int 7.float 8.double 9.long double
(forcible)
. . .
(natural) Gravity
Operators
( ) [ ] >. - + ! ~ ++ (type) * & sizeof */% + << >> < <= > >= = = != & ^ | && || ?: = += = *= /= %= &= ^= |= <<= >>= ,
Associativity
LR RL LR LR LR LR LR LR LR LR LR LR RL RL LR
simple if:
Entry
Test Expression ?
if-else:
True True
Entry
Test Expression ?
False
False
True Statement-block
True-block Statements
Next statement
/* check a citizen is eligible for voting */ #include<stdio.h> int main() { int age; printf(Enter the age : ); scanf(%d,&age); if(age >= 18) printf(Eligible for voting); getch(); }
/* print a number is even or odd */ #include<stdio.h> int main() { int number; printf(Enter a number : ); scanf(%d, &number); if((number %2) == 0) printf(%d is even number.,number); else printf(%d is odd number.,number); }
nested ifelse:
Entry False
Test condition1 ?
False
Statement-3
Statement-2
Next statement
/* check whether a year is leap year or not */ #include<stdio.h> int main() { int year; printf("Enter the year ?"); True scanf("%d",&year); if((year %100) == 0) { Test True if((year % 400) == 0) condition2 printf("%d is leap year.",year); ? else printf("%d is not leap year.",year); Statement-1 } else { if((year % 4) == 0) printf("%d is leap year.",year); else printf("%d is not leap year.",year); } getch(); }
ifelseif :
Entry Test condition1 ?
True Statement-1
False
Test condition2 ?
True Statement-2
False
Test conditionN ?
/* program to print the grade of student */ #include<stdio.h> int main() { int marks; printf("Enter marks ? "); scanf("%d", &marks); if(marks >= 75) printf("Distinction"); else if(marks >= 60) printf("First class"); else if(marks >= 50) printf("Second class"); else if(marks >= 35) printf("Third class"); else printf("Failed"); }
switch statement :
Entry
switch expression ?
value1
value2 ...
valueN
associate statement
associate statement
associate statement
Exit
Next statement
/* program to simulate a simple calculator */ #include<stdio.h> int main() { float a,b; char opr; printf("Enter number1 operator number2 : "); scanf("%f %c %f",&a,&opr,&b); switch(opr) default { case '+': printf("Sum : %f",(a + b)); associate break; statement case '-': printf("Difference : %f",(a - b)); break; case '*': printf("Product : %f",(a * b)); break; case '/': printf("Quotient : %f",(a / b)); break; default: printf("Invalid Operation!"); } }
Loop Statements
False
Following Statement /* sum of 1 to 10 numbers */ #include<stdio.h> int main() { int i = 1,sum = 0; while(i<=10){ sum = sum + i; i = i + 1; } printf(Total : %d ,sum); }
/* average of 5 numbers */ #include<stdio.h> int main() { int count = 1; float x, sum = 0; do { printf(x = ); scanf(%f,&x); sum += x; ++ count; } while(count <= 5); printf(Average = %f , (sum/5)) }
for -- Statement
Initialization Statement
False
Following Statement
/* check whether a number is prime or not */ #include<stdio.h> int main() { int n,i,factors = 0; printf("Enter a number : "); scanf("%d",&n); for(i = 1; i <= n; i++) { if((n % i)==0) ++factors; } if (factors == 2) printf("%d is prime number.",n); else printf("%d is not prime number.",n); }
log(x) pow(x,y) sqrt(x) exp(x) sin(x) cos(x) tan(x) fmod(x,y) hypot(x,y) log10(x)
www.jntuworld.com
For More Materials, Text Books, Previous Papers & Mobile updates of B.TECH, B.PHARMACY, MBA, MCA of JNTUHYD,JNTU-KAKINADA & JNTUANANTAPUR visit www.jntuworld.com