The document provides an introduction to the C programming language, including its history, features, character sets, tokens, data types, operators, and the basic structure of a C program. It discusses key concepts such as variables, constants, comments, functions, preprocessing directives, and how to compile and execute a C program.
Overview of C, developed by Dennis Ritchie at Bell Labs in 1972 as an upgrade from B and BCPL.
C is a structured, portable, case-sensitive, middle-level language that follows a Top-Down approach.
Key steps to learning C include understanding the Character Set, Programs, Instructions, and Tokens.
C character sets include execution, source, special characters, digits, alphabets, and escape sequences.
Tokens are the smallest elements in C, including identifiers, keywords, operators, strings, and constants.
Steps: Create, compile, link, and execute a C program, with examples of using C editor and UNIX commands.
A C program consists of various sections including documentation, preprocessor, global declarations, and the main function.
Preprocessor directives include commands for including library files; comments help document code but are ignored during compilation.
Explanation of identifiers, keywords, and constants used in C programming.
Naming rules, including character limitations and reserved words for identifiers.
Declaration and usage of variables, types of keywords and their significance in C.
Characteristics of constants, including numeric and character constants, with examples.
Integer constants and rules for their definition; includes examples of decimal, octal, and hexadecimal values.
Focus on real constants containing decimal points, along with definitions of character and string constants.
Explanation of operators including arithmetic, relational, logical, and examples of their usage.Detailed insight into decision-making (if, else), looping (while, do while, for), and case structures. Testing characters and examples of input functions like getchar(), gets(), and getc().
C program examples including basic operations and advanced functions demonstrating loops and conditions.
INTRODUCTION TO CC was developed by Dennis Ritchie at Bell laboratory in 1972 It is an upgrade version of languages B and BCPL.
3.
Features of CIt 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,.
C Character SetC Character Set Execution Character Set Source Character Set Special Characters Digits Alphabets Escape Sequence White Spaces
6.
C Character Set(Cont) Source Character Set It is used to construct the statements in the program. Executable Character Set These characters are employed at the time of execution i.e. they have effects only when the program is being executed.
7.
Source Character SetLetters a to z ,A to Z Digits 0 to 9 Special Characters ! @ # $ % ^ & * ( ) _ - + = \ | { } [ ] etc,. White Spaces Blank Space ,Horizontal tab, New line, Vertical tab etc,.
8.
Special characters Comma , Period or dot . Semicolon ; Colon : Apostrophe ‘ Quotation mark “ Exclamation mark ! Vertical bar | Back Slash \ Tilde ~ Underscore - Dollar $ Question mark ?
9.
Ampersand &Caret ^ Asterisk * Minus - Addition + Lesser than < Greater than > Parenthesis () Bracket [] Braces {} Percentage % Hash # Equal to = At the rate @
10.
Executable Character SetCharacters Escape Sequence Back Space \b Horizontal Space \t Vertical Space \v Newline \n
11.
C Tokens Thesmallest element in the C language is the token. It may be a single character or a sequence of characters.
12.
C Tokens (Cont)C Tokens Identifiers Eg:main, avg Keywords Eg: int, for operators Eg: + - Strings Eg: “ab” spI symbol Eg: # $ % Constants Eg:17, 15.5
13.
Executing a CProgram Creating the Program Compilation Linking Execution
14.
Executing a CProgram (Cont) Enter the program in a C editor. Save the program (File Save) or F2. Use the extension .c for saving the file. Eg: sample .c Compile the program(Compile Compile) or Alt+F9. Run the program(Run Run) or Ctrl+F9.
15.
Executing C programusing UNIX Enter the program in vi editor. Save the file using :wq Use the extension .c for saving the file. Eg: sample .c Compile the program. Eg: cc sample.c (or) gcc sample.c Run the program using a.out .
16.
Structure of Cprogram DOCUMENTATION SECTION PREPROCESSOR SECTION DEFINITION SECTION GLOBAL DECLARATION SECTION main() { Declaration part; Executable Part; } sub program section { Body of the subprogram; }
17.
Documentation Section Itcontains the comment lines. Preprocessor Section It is used to link library files. Global Declaration Section The Global declaration section comes at the beginning of the program and they are visible to all parts of the program. Declaration Section It describes the data to be used within the function. Executable Part It contains the valid statements.
18.
C Programs Cprogram 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.
19.
Preprocessor Directives Specialinstructions 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>
20.
Comments (Program documentation)The compiler simply ignores comments when it translates the program into executable code. To identify a comments, C uses opening /* and closing */ comment tokens.
21.
Comments (Cont) Commentscan 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.
22.
C program /*Example program in C*/ Comments # include <stdio.h> P reprocessor Section Global Declaration void main () { Local declaration printf ( “ Hello World! \n ” ); Statements } Output : Hello World
Identifiers Identifiers arenames 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.
25.
Rules for namingidentifier First character must be alphabetic or underscore. Must consist only of alphabetic characters, digits, or underscores. Only the first 31 characters of an identifier are significant and are recognized by the compiler. Cannot use a keywords or reserved word (e.g. main, include, printf & scanf etc.). No space are allowed between the identifiers etc,. C is case sensitive, e.g. My_name my_name.
Variables Variable isan identifier that is used to represent some specified type of information. Eg: x=3 Here x is variable.
28.
Keywords It isa reserved words. Cannot be used for anything else. Examples: int while for etc,.
29.
Keywords Auto register Continue Double typedef For Int Char signed Struct extern void Break return Default Else union Goto Long Const sizeof Switch Float do Case short If Enum unsigned Static While
30.
Constants It isan entity whose value does not changes during the execution. Eg: x=3 Here 3 is a constant.
Constants Constants CharacterConstants Numeric Constants Real Constant Integer Constant String Constant Single Character Constant
33.
Numeric constants Integerconstants It is formed using a sequence of digits. Decimal - 0 to 9 . Octal - 0 to 7. Hexa - 0 to 9 ,A to F Eg: 10,75 etc.
34.
Rules for definingInteger Constant It must have atleast one digit. Decimal point are not allowed. No blank space or commas are allowed. It can be either positive or negative. Etc,.
35.
Numeric constants Realconstants 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.
36.
Character constants Singlecharacter 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.
37.
Character constants Stringconstants String constant are sequence of characters enclosed with in double quote. Eg: “Hello” ,”444”,”a” etc,.
38.
Operators An operatoris a symbol that specifies an operation to be performed on the operands. Eg: a + b + is an operator. a,b are operands.
39.
Data Types A Data type is the type of data that are going to access within the program.
40.
Standard Data Types These Standard type can be used to build more complex data types called Derived Types (e.g. pointers, array, union etc.).
41.
Data types Datatype Size(bytes) Range Format string Char 1 -128 to 127 %c int 2 -32,768 to 32,767 %d Float 4 3.4 e-38 to 3.4 e+38 %f Double 8 1.7 e-308 to 1.7 e+308 %lf
42.
integer A numberwithout a fraction part : integral number. C supports three different sizes of the integer data type : short int int long int
43.
Floating Point Afloating-point type is a number with a fractional part, e.g. 56.78 Floating point numbers are stored using 4 Byte. Types Float Double long double
44.
character Characterare generally stored using 8 bits(1 Byte) of the internal storage. Character ASCII code value a 97(decimal) or 01100001(binary) x 120(decimal) or 01111000(binary)
45.
void The voidtype has no values and no operations. Both the set of values and the set of operations are empty.
46.
Variable ’ sDeclaration To create a variable, you must specify the type and then its identifier : float price; int a,b; char code;
47.
Entire Data typesin c: Data type Size(bytes) Range Format string Char 1 128 to 127 %c Unsigned char 1 0 to 255 %c Short or int 2 -32,768 to 32,767 %i or %d Unsigned int 2 0 to 65535 %u Long 4 -2147483648 to 2147483647 %ld Unsigned long 4 0 to 4294967295 %lu Float 4 3.4 e-38 to 3.4 e+38 %f or %g Double 8 1.7 e-308 to 1.7 e+308 %lf Long Double 10 3.4 e-4932 to 1.1 e+4932 %lf
48.
Types of OperatorArithmetic operator Relational operator Logical operator Assignment operator Increment or decrement operator(unary) Bitwise operator Conditional operator
49.
Arithmetic operator Itis used to carry out arithmetic operations like addition, subtraction etc, Eg: + , - , * , / etc,
50.
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( ); }
51.
Division operator onDifferent Data Type Operation Result Example int/int int 5/2 = 2 int/real real 5/2.0 = 2.5 real/int real 5.0/2 = 2.5 real/real real 5.0/2.0 = 2.5
52.
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( ); }
Relational operator Itis used to compare two or more operands. Eg :< , > , <= , >=, != etc,. 5 < 9 which will return 1
55.
Logical operator Itis 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,.
56.
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( ); }
Assignment operator Itis used to assign a value or expression etc to a variable. Eg: a =10. a = b a = b + c etc,.
59.
Assignment operator(Cont) Compoundoperator 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;
60.
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( ); }
Increment or decrementoperator(Unary) It is used to Increment or decrement an operand. Eg: ++x (Pre Increment), x++ (Post Increment), --x (Pre Decrement), x-- (Post Decrement).
63.
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( ); }
64.
Output Post incrementValue:5 Pre increment Value:7 Pre decrement Value:6 Post decrement Value:6
65.
Bitwise operator Itis 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,.
66.
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( ); }
Conditional Operator (or)Ternary Operator It is used to checks the condition and execute the statement depending on the condition. Eg: C = a > b ? a:b
69.
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( ); }
Expression An expressionrepresent data item such as variable, constant are interconnected using operators. Eg: Expression C Expression a + b + c a + b + c a 2 +b 2 a*a + b*b
75.
Operator Precedence &Associativity The arithmetic expressions evaluation are carried out based on the precedence and associativity. The evaluation are carried in two phases. First Phase: High Priority operators are evaluated. Second Phase: Low Priority operators are evaluated.
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
81.
Input/Output Function Input/Output Function Unformatted Formatted Output printf() fprintf() Input scanf() fscanf() Input getc() gets() getchar() Output putc() puts() putchar()
82.
Formatted Input/Output Cuses two functions for formatted input and output. Formatted input : reads formatted data from the keyboard. Formatted output : writes formatted data to the monitor.
Standard Output Thestandard 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.
Formatted Input (scanf ) The standard formatted input function in C is scanf (scan formatted). scanf consists of : a format string . an address list that identifies where data are to be placed in memory. scanf ( format string, address list ); ( “ %c … .%d … ..%f … .. ” , &a, … .&i, … ..,&x … ..)
Character Test FunctionIt is used to test the character taken from the input. isalpha(ch) isdigit(ch) islower(ch) isupper(ch) tolower(ch) toupper(ch) etc,.
89.
90.
Decision Making Itis used to change the order of the program based on condition. Categories: Sequential structure Selection structure Iteration structure Encapsulation structure
91.
Decision Making (cont)Sequential structure In which instructions are executed in sequence. 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.
92.
SELECTION STRUCTURE Itallows 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
93.
IF Statement SyntaxIF (condition is true) { Statements; } If condition False True Statements
94.
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( ); }
#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( ); }
98.
NESTED IF…ELSE If Condition 1 False Statements True If Condition 2 True False True statements False statements
IF…ELSE LADDERSyntax IF (condition1) { statements; } else if (condition2) { statements; } else if (condition3) { statements; } else { statements; }
102.
Example #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"); }
Output Enter themarks:65 75 70 The average is:70.000000 The Grade is: First class
105.
Looping structure Itis used to execute some instructions several time based on some condition. WHILE Do…WHILE For
106.
WHILE Loop Syntax . WHILE (condition) { . Body of the loop; . } Body of The loop condition False True
107.
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(); }
DO…WHILE Loop Syntax do { Body of the loop }while (condition); Body of The loop condition False True
110.
for loop Syntax for (initialization; test condition; Increment/Decrement) { Body of the loop }
111.
for loop Initialization condition False Body of the loop Inc / Decrement
112.
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(); }
CASE structure Syntaxswitch (expression) { case constant 1: block1; break; case constant 2: block2; break; . . default : default block; break; }
117.
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; }
118.
case 2: {printf("\n Its in case 2"); break; } default: { printf("\n Its in default"); break; } } getch(); }
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);
143.
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;
144.
case 3: c=a*b;printf("\nThe result of Multiplication is:%d",c); break; case 0: exit(0); break; } getch(); }
145.
Output Enter thevalue of a,b:5 6 MENU 1.ADD 2.SUB 3.MULTIPLY 0.EXIT Enter the choice:1 The result of Addition is:11
146.
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; }
147.
if(a==sum) { printf("\nIt is an armstrong number"); } else { printf("\nIt is not an armstrong number"); } getch(); }
Sum of theDigits #include<stdio.h> #include<conio.h> void main() { int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0) { r=n%10;
Reverse of anumber #include<stdio.h> #include<conio.h> void main() { int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0)
153.
{ r=n%10; sum=sum*10+r;n=n/10; } printf("Reverse of the number is:%d",sum); getch(); }
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);
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;
159.
printf(" \nThe valueof 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
160.
Swapping without usingthird variable #include<stdio.h> #include <conio.h> void main ( ) { int a,b; clrscr( ); printf(" \nEnter the value of a:"); scanf("%d",&a); printf(" \nEnter the value of b:"); scanf("%d",&b);
161.
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
162.
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;
163.
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( ); }
164.
Output Enter thevalue of a:4 Enter the value of b:5 Enter the value of c:6 The roots are imaginary