Features and Fundamentals of C Language for Beginners
ChandrakantDivate1
39 views
55 slides
May 10, 2024
Slide 1 of 55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
About This Presentation
Features and Fundamentals of C Language
Size: 8.75 MB
Language: en
Added: May 10, 2024
Slides: 55 pages
Slide Content
Basics of C Programming C. P. Divate
Features of C Language C is the widely used language. It provides many features that are given below. Simple Machine Independent or Portable Mid-level programming language structured programming language Rich Library Memory Management Fast Speed Pointers Recursion Extensible Rich set of controlling statements Rich sent of Operators
Ares of C Language C is the widely used language. It is used to develop sofwares in following areas, Scientific Application Environmental Application Mathematical calculations Embedded Systems Space satellite Application Data Structure Application Computer Graphics Application Computer Network Applications
History of C Language Dennis Ritchie - founder of C language C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. Dennis Ritchie is known as the founder of the c language.
History of C Language Language Year Developed By Algol 1960 International Group BCPL 1967 Martin Richard B 1970 Ken Thompson Traditional C 1972 Dennis Ritchie K & R C 1978 Kernighan & Dennis Ritchie ANSI C 1989 ANSI Committee ANSI/ISO C 1990 ISO Committee C99 1999 Standardization Committee Let's see the programming languages that were developed before C language.
Structure Of a C Program Example Of C Program structure
My First C Program #include < stdio.h > int main() { printf ("Hello C Language"); return 0; } Execution of First C Program
Comments in C Language Comments are non Executable statements in c program. C introduces a new comment symbol // ( double slash). Comments start with a double slash symbol and terminate at the end of line. A comment may start any where in the line and what ever follows till the end of line is ignored. Note that there is no closing symbol. The double slash comment is basically a single line comment. Multi line comments can be written as follows : // this is an example of // c++ program // thank you The c comment symbols /* ….*/ are still valid and more suitable for multi line comments. /* this is an example of c++ program */
Steps in C Language study Let's see the programming languages that were developed before C language.
Steps in C Language study Let's see the programming languages that were developed before C language. Token
1) Character set in C Language
1) Characters Used in C Programs Lowercase letters a b c . . . z Uppercase letters A B C . . . Z Digits 0 1 2 3 4 5 6 7 8 9 Other characters + - * / = ( ) { } [ ] < > ‘ “ ! @ # $ % & _ ^ ~ \ . , ; : ? White space characters blank, newline, tab, etc.
2) The Six Kinds of Tokens / Words in C Keywords Constants / Literals Identifiers - variables / function / structure / array name Statements/ Instructions – e.g. scanf , printf , getch etc. Operators - +, -, * etc. Punctuators - , : “ ‘ . ! ? % etc. The smallest individual units/words in program are known as tokens. C has the following Six different types of tokens.
2) Tokens / words in C Language 2.1) KEYWORDS : Keywords are C tokens that have a strict meaning in c language to perform certain tasks. They are explicitly reserved words and can’t be used as names for the program variables or other user defined program elements. ANSII C has 32 key words . Some implementations such as Borland’s C or Microsoft’s C have additional key words. auto do goto signed unsigned break double if sizeof void case else int static volatile char enum long struct while const extern register switch continue float return typedef default for short union
What is a Constant? C o n s t an t i s a v a l u e (an entity) whose value cannot c h an g e throughout t h e e x e c u t i o n o f a p r o g ra m , is known as constants . Like variables constants have data types . 2) Tokens / words in C Language 2.2) Constants / Literals : 1) Using const keyword const data_type < variable_name >=< val > ; (or) data_type const < variable_name >=< val >; 2) Using Define preprocerssor #define <variable> <Value> Syntax (Grammar) Example const int x = 10; int const x = 10; #define PI 3.14
Types of Constants in C 2) Tokens / words in C Language 2.2) Constants / Literals : Integer constant - (complete / Whole Number) Real constant - (Fractional Number) Character Constant - (Single Letter) String Constant - (multiple letters)
2.2.1) I nteger Constants Decimal Constants Octal Constants Hexa - Decimal Constants 10 012 0xA 1024 02000 0x400 12789845 060624125 0xC32855 2) Tokens / words in C Language 2.2) Constants / Literals : In C language integer constant is a complete / Whole Number. integer constant are represented in one of following formats, Decimal octal (Value starts with 0) or hexa-decimal number (Value starts with 0x). Integer constants are always positive until you specify a negative(-) sign.
Example Program 2) Tokens / words in C Language 2.2) Constants / Literals : #include< stdio.h > void main ( ) { const int a = 10 ; // Decimal int const b = 012; // Octal const int c = 0xA ; // Hexadecimal printf (" %d \t %d \t % d ", a, b, c ); } OUTPUT : 10 10 10
Representation Fractional form Exponential form Type 0.0 0.0e-10 or 0.0E-10 double 6.77 6.77 0.677e1 or 0.677E1 double -6.0f -6.0 -0.6 e1 or - 0.6E1 float 3.1415926536L 3.1415926536 0.31415926536 e1 long double 2.2.2) Real Constants 2) Tokens / words in C Language 2.2) Constants / Literals : In C language real constant is a fractional Number. We can represent the negative numbers in real constants. The default form of real constant is double and it must have a decimal point. It may be in fractional form or exponential form. Ex : 3.45, -2.58, 0.3E-5 (equal to 0.3 x 10-5)
ASCII Character Symbol Alert(bell) '\a' Null character '\0' Backspace '\b' Horizontal Tab '\t' New line '\n' 2.2.3) Character Constants 2) Tokens / words in C Language 2.2) Constants / Literals : Character Constants are in single letter form and must be enclosed with in single quotes. e.g. ‘a’, ‘Z’, ‘1’, ‘4’, ‘&’, ‘%’,’#’ etc. We use escape character along with the character constants, followed by \. The escape character says that it is not a normal character constants as they do some tasks on user(output) screen. ASCII Character Symbol Vertical tab '\v' Form Feed '\f' Carriage Return '\r' Single Quote '\'' Double Quote '\"'
2.2.4 ) String Constants 2) Tokens / words in C Language 2.2) Constants / Literals : A string constant is a sequence of characters enclosed in a double quotes. Examples : “ ” // Null String "programming9" // a full string with 12 characters " wel come" // string with 8 characters including space “Value= %d" // format string with 8 characters including space “a=%d \n b=%d" // format string with 9 characters including space and escape sequence character.
3) IDENTIFIERS : Identifiers refers to the name of variable , functions , array , structure etc. created by programmer. Variable is an entity in c program whose value can changes several time throughout the execution of program. i.e. Variables are entities whose values can not remain constant in C program. Each language has its own rule for naming the identifiers. The following rules for naming the identifiers in C and C ++. Identifier name should start with alphabet character or underscore _. Other characters in identifier can be letters, digits and underscore are permitted. A variable name should not consist of whitespace . 'C ' is a case sensitive language that means a variable named 'age' and 'AGE' are different. A variable name should not consist of a keyword . It can have 8, 32 letter long in size. It must be declared first (locally / Globally) before using in program. 3) Tokens / words in C Language
3) IDENTIFIERS : Following are the examples of valid variable names in a 'C' program: 3) Tokens / words in C Language height or HEIGHT _height _height1 My_name Following are the examples of invalid variable names in a 'C' program: 1height Hei$ght My name case switch
3) IDENTIFIERS : Following are the examples of valid variable names in a 'C' program: 3) Tokens / words in C Language height or HEIGHT _height _height1 My_name Following are the examples of invalid variable names in a 'C' program: 1height Hei$ght My name case switch
3) IDENTIFIERS : 3) Tokens / words in C Language Declaration without initialization: data_type <variable_name1> [,<variable_name2>, …… ] ; Declaration with initialization: data_type <variable_name1>=<val1> [,< variable_name2> =< val2>, …… ] ; Declaration Syntax (Grammar) Declaration without initialization: int x; float x,y,z ; 2) Declaration with initialization: int a=10; float x=20, y=30, z=-40; Declaration example
3) BASIC DATA TYPES IN C 3) Tokens / words in C Language
3) BASIC DATA TYPES IN C 3) Tokens / words in C Language
Tokens in C Language 3) BASIC DATA TYPES IN C
3) BASIC DATA TYPES IN C 3) Tokens / words in C Language
IDENTIFIERS BASIC DATA TYPES / DATA STRUCTURES IN C++ 2.3) IDENTIFIERS: 3) Tokens / words in C Language
4 ) Tokens / Instructions(statements) in C Language Sequential execution means that each command in a program script executes in the order in which it is listed in the program . The first command in the sequence executes first and when it is complete, the second command executes, and so on.
4 ) Tokens / Instructions in C Language
4 ) Tokens / Instructions(statements) in C Language C has following types of statement. Variable Declaration Statements Assignment statements Initialization statement Arithmetical / Mathematical Statements iii) Input Statements iv) Selection (branching) if (expression ) statement if (expression) else statement Nesting of if (expression ) else statement if (expression ) else ladder switch (expression) case statement v) iteration (looping ) statements while (expression ){ block } for(expression ;expression ;expression){ block } do {block } while (expression ); Output statements Normal / Abnormal termination statement; Null Statements
5) Tokens / Operators in C Language An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −
5) Tokens / Operators in C Language Classification of Each Operators Unary Operators Binary Operators The operators which act upon a single operand are called unary operators . The operators which require two operands for their action are called binary operators . Syntax i ) operator <operand> ii) <operand> operator Syntax i ) operand1 operator operand2 Example i ++, j - - ++ c - 46 Example a + b x - 4
5) Tokens / Operators in C Language The following table shows all the arithmetic operators supported by the C language. Assume variable A = 10 and variable B = 20, then 5.1) Arithmetic Operators in c Language Operator Description Example Result + Adds two operands. A + B 30 − Subtracts second operand from the first. A − B -10 * Multiplies both operands. A * B 200 / Divides numerator by de-numerator. B / A 2 % Modulus Operator and remainder of after an integer division. B % A ++ Increment operator increases the integer value by one. A ++ 11 -- Decrement operator decreases the integer value by one. A- - 9
5) Tokens / Operators in C Language 5.1) Classification of Arithmetic Operators in c Language
5) Tokens / Operators in C Language Program for Study of Arithmetic operators 5.1) Arithmetic Operators in c Language #include < stdio.h > main () { int a = 21; int b = 10; int c ; c = a + b; printf ("Line 1 - Value of c is %d\n", c ); c = a - b; printf ("Line 2 - Value of c is %d\n", c ); c = a * b; printf ("Line 3 - Value of c is %d\n", c ); c = a / b; printf ("Line 4 - Value of c is %d\n", c ); c = a % b; printf ("Line 5 - Value of c is %d\n", c ); c = a++; printf ("Line 6 - Value of c is %d\n", c ); c = a--; printf ("Line 7 - Value of c is %d\n", c ); } Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 21 Line 7 - Value of c is 22 Output
5) Tokens / Operators in C Language Priority / Precedence / Hierarchy of Arithmetic Operators in C 5.1) Arithmetic Operators in c Language
5) Tokens / Operators in C Language Priority / Precedence / Hierarchy of Arithmetic Operators in C Example 5.1) Arithmetic Operators in c Language
5) Tokens / Operators in C Language Priority / Precedence / Hierarchy of Arithmetic Operators in C Example 5.1) Arithmetic Operators in c Language
DATA INPUT AND OUTPUT INSTRUCTIONS IN C As we know that any c program is made up of 1 or more then ONE standard function . Likewise it use some functions for input output process . The most common function printf() scanf().
printf() Function printf() function is use d to display something on the console or to display the value of some variable on the console. The general syntax for printf() function is as follows printf(<”format string ”> [ ,< list of variables > ] ); To print some message on the screen printf(“God is great”); This will print message “God is great” on the screen or console . Format specifiers in “format string” must match with respective variable data type. Number of format specifiers must be equal to number of variables in list of variables.
printf() Function To print the value of some variable on the screen Integer Variable : int a=10; print f (“ % d ” ,a); Here %d is format string to print some integer value and a is the integer variable whose value will be printed by printf() function. This will print value of a “10” on the screen.
printf() function To print multiple variable’s value one can use printf() function in following way. int p=1000,n=5 ; float r=10.5; printf(“amount=%d rate=%f year=%d”,p,r,n); This will print “amount=1000 rate=10.5 year=5” on the screen
scanf() Function sc a n f ( ) f unctio n is u s e t o re a d d a t a fr o m keyboard and to store that data in the variables during runtime . Th e ge ne ral synta x f o r sc a n f ( ) f uncti o n i s as follows. scanf (“Format String”, &variable); Here format string is used to define which type of data it is taking as input. T h i s f or mat s t r in g c an b e % c f o r ch ar a c te r, % d for integer variable and %f for float variable . Format specifiers in “format string” must match with respective variable data type. Number of format specifiers must be equal to number of variables in list of variables .
scanf() Function scanf(“Format String ”, &variable 1,… ); Where as variable the name of memory location or name of the variable and & sign is an operator that tells the compiler the address of the variable where we want to store the value.
scanf() Function For Integer Variable : int rollno; printf(“Enter rollno=”); scanf (“%d ”,& r ollno ); Here in scanf() function %d is a format string for integer variable and &rollno will give the address of variable rollno to store the value at variable rollno location. For Float Variable : float per; printf(“Enter Percentage=”); scanf (“%f ”, & per); For Character Variable : char ans; printf(“Enter answer=”); scanf (“%c ”, & ans);
/100
c lrscr () function in C Function " clrscr " (works in Turbo C / C ++ compiler only) clears the screen and moves the cursor to the upper left-hand corner of the screen . It is an output statement in c. Syntax: void clrscr ( ); For Example clrscr ( );
getch () function in C getch () is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS compilers like Turbo C. It is an input statement in c. Syntax: int getch (void ); Parameters: This method does not accept any parameters. Return value: This method returns the ASCII value of the key pressed. For Example int main() { int a; a= getch (); printf ("%c", a); return 0; } Input: g (Without enter key) Output: Program terminates immediately. But when you use DOS shell in Turbo C, it shows a single g, i.e., 'g' Output with explanation
getch () function in C getch () method pauses the Output Console untill a key is pressed. It does not use any buffer to store the input character. The entered character is immediately returned without waiting for the enter key. The entered character does not show up on the console. The getch () method can be used to accept hidden inputs like password, ATM pin numbers, etc.
Single character input – the getchar () function : Single characters can be entered into the computer using the “C” library function getchar. In general terms, a reference to the getchar function is written as. character variable=getchar(); For example char c; c = ge t c h ar (); printf (“Given Character is:%c ”, c);
Single character output – The putchar function Single character can be displayed (i.e. written out of the computer) using the C library function putchar. In general a reference to the putchar function is written as putchar (character variable); For Example char c=’a’; pu t c h a r ( c);
RELATIONAL OPERATORS IN C: Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program . Operator Description Example == Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. (A <= B) is true. Assume variable A holds 10 and variable B holds 20 then