my c notes

Share Embed Donate

Short Description

it is my B.P.Sharma C notes...


13.09.2008 Introduction to C BCPL – Basic Combined Programming Language – Martin Richard B Language – Ken Thompson C Language – Denis Ritchie C++  Bjarne Stroustrup Java  James Gosling C#  from Microsoft Features of C       

 

 

C is a high level language with the capabilities of middle level language. C is a format free language C is portable C is a structured or modular programming language Every C program must have .c extension Every executable C program must have a main() function followed by pair of braces main() function can have any of six syntaxes o void main() o int main() o void main(int argc, char *argv[]) o int main(int argc, char *argv[]) o void main(int argc, char *argv[], **ppenv) o int main(int argc, char *argv[], **ppenv) Every statement must terminate by a semicolon ; C provides a big set of library functions under special files called as header files with .h extension o stdio.h  printf(),fprintf(), scanf(), gets(), fflush(), NULL, stdin, stdout, strprn etc. o conio.h  clrscr(), gotoxy(), getch(), getche() etc. o math.h  pow(), log(), cos(), atoi(), atof() etc. o ctype.h  toupper(), tolower(), isdigit(), isalpha(), isupper(), islower() etc. o string.h  strlen(), strcpy(), strrev(), strcat(), strcmp() etc. To include a header file use any of two methods o #include o #include “header file” #include is called as pre-processor directive Add some remarks or comment inside the programs for future use o Use /* and */ delimiters to use the comments

Software Requirements TC 3.0 TC 4.5 Borland C++ 5.0 Installing TC 3.0 TURBOC –d c:\ Creates a TC folder. Double click on TC.EXE file under TC\BIN folder Turbo C is an IDE (Integrated Development Environment) from Turbo company overtaken by Borland. Key combinations used with TC        

Alt+X  Exit TC Ctrl+F1  Context Sensitive Help F2  Save F3  Open Alt+F3  Close Window Alt+Window number – to switch between the windows F5 -> Full Screen or Restore window Alt+F5  To see output window

14.09.2008 Block Operations Selection  Shift+Arrow keys Copy  Ctrl+KC Move  Ctrl+KV Delete  Ctrl+KY De-select  Ctrl+KH Delete a line -> Ctrl+Y Undo Alt+Backspace Basic functions printf(“message”);  to print on screen in black/white cprintf(“message”);  to print on screen in defined color textcolor(colorname);  to define a text color textbackground(colorname);  to define a text background gotoxy(column,row);  to send cursor to given position clrscr();  clear the screen

Compilations options Alt+F9  Compile F9  Build Ctrl+F9  Build and Run .c  tcc  .obj (Pass 1) + .LIB Tlink  exe (Pass2) Files in C .c  Source File .obj  Object File .exe  Executable file .bak  Backup file Data Types - Special keywords that define the type of data and amount of data Types of data types  Primary Data Types o char – 1 byte o int – 2 or 4 bytes o float – 4 bytes – 6 dec.places o double – 8 bytes- 14 dec. places o void - nothing  Modified Data Types o Data types created by modifying the primary types using some special keywords called modifiers  short  long  signed  unsigned o char  signed char (char)  unsigned char o int  short int (short)– 2 bytes  unsigned short int – 2 byte  long int (long) – 4 byte  unsigned long int  unsigned int o double  long double – 10 bytes  Derived data types

o Created using primary and modified types o Also called as custom types o Arrays, Pointers, Structures, Unions, Bitfilds etc. Communicating data with outside world and memory - Use the special codes called format specifiers %d - decimal %o - Octal %x or %X – Hexa Decimal %i - integer %c - Characters %s - String without space %[^\n] – String with space %ld - long %u - unsigned int %lud - unsigned long int %f - fixed float %e - scientific float %g - general float %lf - double and long double Memory Variables - A name given to some memory location to store and retrieve the data - A variable name must associate a data type ; Example int empid; char gender; float basic; char name[51]; Variable Naming Rules - A variable name can have alphabets (a-z, A-Z), digits (0-9) or underscore (_) only - Can never start with digit - Keywords cannot be used a variable names - A variable can be upto any length but first 32 characters are significant Literals - The values that we use from our side in variable assignments, expression etc.

Types of Literals 1. Integral Literals a. Default is int b. int n=56; c. long k=45L; d. usigned int p=45U; e. Use l or L for long and u or U for unsigned 2. Floating literals a. Default is double b. Use f or F for floats c. double k=1.1; d. float p=1.1F; 3. Character Literals a. Characters are enclosed in single quotes b. Each character takes one byte c. Characters and numbers are interchangeable char ch=’A’; or char ch=65; d. C provides some special characters made of two or more characters but represent only one character. Used with \ i. \\ - print the \ ii. \’ – single quotes iii. \” – double quotes iv. \0 – Null Character v. \t – Horizontal Tab vi. \n – New Line vii. \r – Carriage Return viii. \a or \007 – alert or beep sound Example Print the following line He said, “\n” is made for new line. 4. Sting literals a. Enclosed in double quotes b. Each string is terminated by null character \0 char name[]=”Vikas”; //6 char name[40]=”Vikas”;//40 char name[40]; name=”Vikas”; //error Note: use sizeof() operator to see the size of a variable or data type Next Topics

Input Methods scanf() getch() getche() getchar() gets() fflush() Operators Conditional Statements Looping Statements 20.09.2008 Input Methods scanf() – to read any kind of data using specifier along with address of variable - Use & operator to get address of some variable Syntax scanf(“specififer”,address of variable); %d or %i decimal %c  character %s  string without space %[^\n]  string with space %f  float %lf  double and double %u  unsigned %lud  unsigned long int For Character Input getch() – Get a character but don’t echo it. Un-buffered mode getche() – Get a character and echo it. Un-buffered Mode getchar() – Get a character. Buffered Mode For String input gets(stringvariable) – to get a string including space

Operators Arithmetic operators + * 5/2 2 5.0/2  2.5 5.0/2.0  2.5 5/2.0  2.5



Cast Operator - To covert one kind of data to another kind using casting int a=5,b=2; float c=a/b; 2.0 float c=(float)a/b; 2.5  cast operator Relational or conditional or comparison operators - Used to compare two values - There are six operators o == Equals to operator o != Not equals to operator o > o >= o < o 0; int k=5 Right Shift int n=6 & 9; //0 00110 01001 ------00000 ------int k=67 | 34; //99 64 32 16 8 4 2 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 -------------------------------------------------------1 1 0 0 0 0 1 ------------------------------------------------------int k=67 ^ 34; //97 int k=5b? (a>c?a:c) : (b>c?b:c); Switch Statement -

Used only for equality comparisons

switch(variable) { case value1: statements; break; case value2: statements;break; … default:statements; } Example 1. Get a digit and print into words. 2. Get a character and check it to be vowel or consonant Looping Statements - Repetition of same statements again and again till the condition is true - C provides thee kinds of loops o while o do-while o for - while is called as pre-check loop o It first checks the conditions, if the condition is true then statements get executed


initilization; while(condition) { Statements; Updation; } for loop is very similar to while loop for(initialization;condition;updation) statement;

or for(initialization;condition;updation)

{ statements; } Do-while loop - A post-check loop that executes at least once irrespective of condition do { Statements; Updation; }while(condition); 28.09.2008 1. View the ASCII characters and their codes. 2. Print on the screen a. α + β = δ (224, 225, 235) b. printf(“%c + %c = %c”,224, 225, 235); 3. Print heart symbols in third line 4. Print the whole screen with hearts 5. Print the following for given number of lines 1 22 333 4444 1 12 123 1234 A BB CCC DDDD A AB ABC ABCD Explanation Total lines n Line no. from 1 to n  l (dependency on n) Characters per lines 1 to l  i (dependency on l) 6. Print the following based on user input

* *** ***** 1 121 12321 121 1 Class assignment Write a program to get a number and print it into words 678  Six Seven Eight 10 Minutes  Reverse the number  Crack the digits of the reversed number  Print the word corresponding to digit Get a number and check to be palindrome. Get a number and check it to be prime. Get two numbers from keyboards and print all prime numbers in range (inclusive) 04.10.2008 Working with Arrays - Array is a variable that can hold multiple data elements of similar type - Each element is differentiated by a number called Index Number - Index number starts with 0 - Types of arrays o Single Dimension Array  Having only one row o Two Dimensional Array  Having multiple rows o Multi-Dimensional  Having more than 2 dimensions Creating Single Dimensional Array Syntax [size]; Example int n[5]; //n[0] to n[4] double num[4]; //num[0] to num[3]

Double Dimensional Array - Having rows and columns o int n[3][4]; // 3 rows and 4 columns  12 elements Array Initialization - Provide values while creating an array int n[]={4,6,1,9};  4 elements int n[10]={3,4,6,2,4}; Sample Case - Get 5 numbers and print average of it - Get 5 numbers and print greatest of those numbers - Get any number of numbers and print greatest of it - Create an array of 3 X 4 and input the values in that array. Show the values of array in proper matrix format 05.10.2008 Matrix Multiplication -

Columns in first matrix must be equal to rows in second column

A[i][j] X B[j][k]  A[i][k] for(i for(k { C[i][k]=0 for(j C[i][k]+=A[i][j]*B[j][k]; } String Operations 1. Get a string and print the length of the string 2. Reverse print a string 3. Swapping or interchanging of numbers a. Method 1 i. Using Third variable 1. int a=5,b=10,temp; 2. temp=a; 3. a=b; 4. b=temp; b. Method 2 i. Without using third variable 1. int a=5,b=10;

2. a=a+b; 3. b=a-b; 4. a=a-b; c. Using XOR operator ^ i. a^=b; ii. b^=a; iii. a^=b; d. Using XOR in one line i. a^=b^=a^=b; 11.10.2008 Number Sorting -

Get a numbers, compare and swap them for sorting

Functions -


A block of statements given a name to do some specific repetitive task Functions are of two types o Library Functions  Provided under header files o User Defined Functions (UDF)  Created by programmers for their needs Library functions o math.h  pow(n,p)  abs(n)  log()  cos()  tan()  atoi() – String to integer conversion  atof() – String to float conversion • char data[]=”345.44”; • float x=atof(data); o ctype.h  Characters related operations • toupper() • tolower() • isupper() • islower() • isalpha() • isdigit()  Get a string from print in toggle case • Amit aMIT  Get a string and count lower, upper and special characters o string.h  For string operations

• • • • •

• •

strupr(str) – To upper case string strlwr(str) – To lower case string strrev(str) – To reverse the string strlen(str) – String length strcmp(s1,s2) To compare two strings o Returns 0 if s1==s2 o Returns >0 if s1>s2 o Returns
View more...


Copyright ©2017 KUPDF Inc.