C language training

Page 1

C Language WELCOME IN THE REATIVE WORLD OF C LANGUAGE


Introduction to C Language Denis Ritchie modified B language & implement at bell laboratories in 1972. The new language thus formed is named C. C was developed along with UNIX operating system so it is strongly associated with UNIX. These days C is running under a number of operating system including MS-DOS.


Importance of C •C has many set of built in functions and operators can be used to write any complex program. •C is suitable for writing both system software and business packages because C compiler combines the capabilities of an assembly language with the features of a high level language. •C is highly portable language.


• C program is basically a collection of functions that are supported by the C library. The programming task become simple with the availability of a large number of functions. • C language is well suited for structured programming. The structure of function modules or blocks makes program debugging, testing & maintenance easier.


Format of C Program main( ) { ---------------------------------------}

Function Name Starting of Program Program Statement End of Program


Basic Structure of C Program Documentation Section Link Section Definition Section Global Declaration Section Main ( ) function Section { Declaration Part Execution Part } Sub Program Section Function 1 Function 2 …. … Function m

User Defined Function


Executing a ‘C’ Program A series of Steps will be involved to execute a C program. 1. Creating the Program 2. Compiling the Program 3. Linking the Program 4. Executing the Program


Creating the Program The program must be entered into a file. The file is created with the help of a text editor. Any corrections in the program are done under the editor. When the editing is over, the file is saved on disk. The file is saved on disk. The file name can consist of letters, digits and special characters, followed by a dot and a letter c. The program that is entered into the file is known as the source program, since it represent the original form of the program.


Compiling the Program The source program instructions are translated into a form that is suitable for execution by the computer. After examining each instruction about its correctness the translation is done. It every thing is alright, the compilation proceeds silently and the translated program is stored on another file with the name with extension of o i.e. (.o) which is known as object code.


Linking the Program Linking is the process of putting together other program files and functions that are required by the program. The programs will be linked with functions that are needed from the C library. Compilation process list out the mistakes in the syntax and semantics of the language.


Executing the Program While executing a program the instructions will be executed. The program may request for some data to be entered through the keyboard while executing. For the wrong program logic or data the program may not produce the desired result for which source program or data should be corrected. In case the source program is modified, the entire process of compiling, linking and executing the program should be repeated.


Constants, Variables and Data Types Constant The value that do not change during the execution of the program. 1. Numeric constant: i. Integer constant ii. Real constant 2. Character constant: i. Single Character constant ii. String constant


Integer constant refers to a sequence of digits (0-9) also – or + sign Real Constant refers to a number containing fractional parts. (floating point like 0.009) Character constant contains a single character enclosed in a pair or single quote marks. ‘5’, ‘x’, ‘;’ A String constant may be letter, numbers, special characters and blank space enclosed in double quotes. “Name!”, “Well done”, “5+2”, “X”, “44”


Variables A variable is a data name that may be used to store a data value. Like constant this also remain unchanged during the execution of a program but a variable may take different values a different times during execution. Keyword, Special character and blank spaces are not valid variable name but under score ( _ ) is valid.


Data types C is rich in data type. The variety of data types available allow the programmer to select the type appropriate to the needs of the application as well as machine. C support 4 class of data type. 1.Primary or fundamental data type 2.User defined data type 3.Derived data type 4.Empty data set


Primary Data Type 1. Integer Signed Type Int (16 bit) Short int (8 bit) Long int (32 bit) 2. Character Signed char (8 bit) Unsigned char (8 bit)

Unsigned Type Unsigned int (16 bit) Unsigned short int (8 bit) Unsigned long int (32 bit) 3. Floating Point Value Float (32 bit) double (64 bit) long double (80 bit)


Data Type Char Int Float Double Unsigned char Unsigned int Short int Unsigned Short int Long int Unsigned int Float Double Long Double

Range -128 to 127 -32,768 to 32767 3.4 e-38 to 3.4e +38 1.7 e-308 to 1.7 e+308 0 to 255 0 to 65535 -128 to 127 0 to 255 -2,147,483,648 to 2,147,483,647 0 to 4,294,967,295 3.4E-38 to 3.4E+38 1.7E-308 to 1.7E+308 3.4E-4932 to 1.1E+4932


Data Types: In a program we use the following basic data types: a) Integers: An integer is a basic data type which contains discrete numbers like 1,2,4, 17, etc. An integer data can not be in fraction or exponent. Normally 2 byte is assigned for integer. b) Characters: Character data type contain printable alpha-numeric characters as well as special characters as #, @, %, etc. Normally character is assigned by 1 byte.


c) Real: Real data type can contain any numeric value that may be signed or unsigned, fractions, exponential. Eg: 7.0, 4.9986, 3.0E-4 etc. The number of digits before and after the decimal is fixed in fixed point representation of Real number. There is no fixed number of digits before and after the decimal point in floating point representation of real number. Here the decimal point can float. The amount of storage for double precision numbers are twice than the single precision floating point numbers.


Logical or Boolean Data Type Logical data type is useful to check the conditions inside the program. This data type contain only 2 type of value i.e. True or False. But this is not supported by all programming language.


Data Structures A Data structures is an organized grouping of data items treated as unit. They can be also called as complex data type: 1) Array: An array is a collection of same type of elements. The individual data items in an array are called as its element. Elements may be basic data types like integer, float, character or other data structures too.


Each space in array is given an index number which is used to access each individual value, which is stored in each array element. Eg: An array is of 1- dimensional or 2 dimensional even more as well.


String String is the one-dimensional array of characters. Part of string is called sub string. If the string is of fixed length then for data storage fixed number of character places will be available. The data will be provided just the needed number of spaces in variable - length strings.


Character Set The characters that can be used to form words, number and expression depend upon the computer on the program is run. The characters in c are grouped into 1. letter- A-Z, a-z 2. digits- 0-9 3. special character -, ; : ? \ / !~# $ % ^ & * ( ) - + | { } [ ] 4. white space - Blank space, new line horizontal tab.


Trigraph Characters If same characters are not available in keyboard they are supplied in the program by combining other characters ?? = # number sign ?? ( [ ?? ) ] ??< { ?? > } ?? ! ! veatical bar ?? / \ ?? ’ ^ ?? ~


C Tokens In C, smallest individual units are known as c token 6 types Keyword Identifier Constant

float while string “ABC” “Year” main amount special symbol [ ] { } -15.5,100 Operators + - , * ,


Keyword and Identifier Identifier refer to the name of variables, functions & array. These are user defined names and consist of a sequence of letters and digits, with a letter as a first character. Both upper & lower case permitted. All keywords have fixed meanings & they cannot be changes. They are basic building block of a program statement. Written in lower case. Eg :- auto, break, case, char, const, continue, default, do, double, else enum – extern, float, foe, goto, if, int, long, return, short signed size of, static, struct, switch, typedef, union, unsigned, void volatile, while. Constant < Num < int / real < char < single character / string


Declaration of variable Declaration must be done before their use in program. Declaration of variable does two things. 1. It tells the complier what the variable name is. 2. It specifies what type of data the variable will hold.

Primary type declaration Variable can be used to store a value of any data. Data_type var1, var2, var3, ........., varN; In place of data_type particular keywords is used. Variables are separately by comma and ended with semicolon. Eg;float x, y; long int amount ; unsigned n; int roll; double deviation; Short clas; short int Roll; char C ; long students; If the specifier is without basic data type then it will be treated as int.


typedef “type identifier� allows user to define an identifier that would represent an existing data type. Eg:- typedef type identifier; typedef int units; Later on it can be used as. units batch1, batch2;


enum The identifier is a user defined enumerated data type which can be used to declare variables that can have one of the values enclosed within the brace ( enumeration constant) enum identifier v1, v2, …. Vn ; then v1….vn will have of the values Value 1 value 2….. value n. v, = value 3 ; v7 = value 2 Eg ;- enum day {Monday, Tuesday, ….., Sunday}; enum day week_st , week_end; week_st = Monday; week_end = Friday;


Declaring a variable as constant const int class_size = 40; Declaring a variable as volatile volatile int data; volatile int location = 100; Reading data form keyboard scanf(“control string", &variable1, &variable2,‌);


Operator and expression An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. They are the part of mathematical or logical expression. They are: 1. Arithmetic operator - +, - , * , / , % (modulo division)

2. Relational operator :- used for the comparison < , <= , >= ,= = , != 3. Logical operator :- used to test more then one condition & make decisions. && - logical and if ( age > 25 && salary < 5000) , || - logical or if ( num <0 || num > 100)


4. Assignment operator :- used to assign the result of an expression to a variable. variable operator = expression x+ = 1 x=x+1 x-=1 x=x–1 x* = n + 1 x = x * (n +1) x/= n +1 x = x/( n +1) x%=b x=x%b (right to left)


5. Increment and decrement operators ++ m or m++ - - m or m - ++ m is equivalent to m = m+1 ; (or m+ = 1;) If m = 5; and y = ++m; then value of y and m would be 6 Prefix operator first adds 1 to the operator and then the result is assigned to the variable on left. If m = 5; and y = m++; then value of y would be 5 & m would be 6 Postfix operator first assigns the value of the variable on the left and then increments the operand.


6. Conditional operator Exp1? Exp2: Exp 3; First exp1 is evaluated, if exp1 is non zero exp2 will be evaluated. If exp1 is false exp3 will be evaluated. If a =10; b = 15 ; then for x = ( a > b ) ?a :b; if ( a > b) x=a; else x =b;


Automatic type conversion If the operands are of different types, the ‘lower’ type is automatically converted to the ‘higher’ type before operation proceeds. The result is of higher type. Order – long double, double, float, unsigned long int, unsigned int.


Casting a value If we want to force a type conversion in a way that is different form the automatic conversion. ( type_name ) expression Here the type_name can be standard c data type and expression can be constant, variable or an expression. eg:- x=(int)7.5 , b=(double)x/y z=(int)(x+y) w=(int)p+q


Some useful math functions To use math functions a header file math.h should be included. Function tan(x) sinh(x) ceil(x) exp(x) fabs(x) floor(x) fmod(x,y) log(x) log10(x) pow (x,y) sqrt(x)

-

Meaning tangent of x hyperbolic sin of x x rounded up to the nearest integer e to the power x (ex) absolute value of x x rounded down to the nearest integer remainder of x/y natural log of x, x>0 base 10 log of x, x>0 x to the power y (xy) square root of x , x >=0


Backslash Character Constants Backslash Character constants are used in output functions. ‘\a’ Audible Alert ‘\’’ Single Quote ‘\b’ Back Space ‘\”’ Double Quote ‘\n’ New Line ‘\?’ Question Mark ‘\t’ Horizontal Tab ‘\\’ Back Slash ‘\v’ Vertical Tab ‘\0’ Null


CETPA Noida D-58, Sector-2, Red FM Lane, Noida -201301, Uttar Pradesh Contact Us: 0120-3839555, +919212172602

CETPA Roorkee #200, Purvawali, 2nd Floor (Opp. Railway Ticket Agency) Railway Road, Ganeshpur, Roorkee 247667 Contact Us: +91-9219602769, 01332270218 Fax - 1332 - 274960 CETPA Lucknow #401 A, 4th Floor, Lekhraj Khazana, Faizabad Road , Indira Nagar, Lucknow - 226016 Uttar Pradesh Contact: +91-9258017974, 0522-6590802

CETPA Dehradun 105, Mohit Vihar, Near Kamla Palace, GMS Road, Dehradun-248001,UK Contact: +91-9219602771, 0135-6006070


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.