SlideShare a Scribd company logo
1 of 35
Download to read offline
{C}
Programming
Fundamentals
of C
Language
2
Features of C Language
 Modularity
 Extensibility
 Elegant syntax
 Case sensitive
 Less memory required
 The standard library concept
 The portability of the compiler
 A powerful and varied range of operators
 Ready access to the hardware when needed
3
Structure of C Program
Documentation section
(Used for comments)
Link section
Definition section
Global declaration section  
(Variables used in more than  
one functions)
void main ()
{
Declaration part  
Executable part
}
Subprogram section
(User defined functions)
Program
1 // Program for addition of 2 nos
2
3
4
5
6 void fun();
7
8 int a=10;
9
10
11 void main( )
12 {
13 printf("Value of a inside main function: %d", a);
14 fun();
15 }
16
14 void fun()
18 {printf("Value of a inside fun function: %d", a);}
4
Comments
 A comment is an explanation or description of the source code of the program
 It helps a programmer to explain logic of the code and improves program
readability.
 At run-time, a comment is ignored by the compiler.
 There are two types of comments in C:
 Single line comment
 Represented as // double forward slash
 It is used to denote a single line comment only.
 Example: // Single line comment
 Multi-line comment
 Represented as /* any_text */ start with forward slash and asterisk (/*) and end with asterisk and
forward slash (*/).
 It is used to denote single as well as multi-linecomment.
 Example: /* multi line comment line ‐1
 multi line comment line ‐2 */
5
Header files
 A header file is a file with extension .h which contains the set of predefined
standard library functions.
 The “#include” preprocessing directive is used to include the header files with
extension in the program.
Header file Description
stdio.h Input/Output functions (printf and scanf)
conio.h Console Input/Output functions (getch and clrscr)
math.h Mathematics functions (pow, exp, sqrt etc…)
string.h String functions (strlen, strcmp, strcat etc…)
6
Data Types
 Data types are defined as the data storage format that a variable can store a data.
 It determines the type and size of data associated with variables.
Data types in C
Primary Data type
(int, float, char)
Secondary Data type
Derived Data type
(array, pointer)
User definer Data type
(structure, union, enum)
7
Primary Data Type
 Primary data types are built in data types which are directly supported by machine.
 They are also known as fundamental data types.
 int:
 int datatype can store integer number which is whole number without fraction part such as 10, 105
etc.
 C language has 3 classes of integer storage namely short int, int and long int. All of these data
types have signed and unsigned forms.
 Example: int a=10;
 float:
 float data type can store floating point number which represents a real number with decimal point
and fractional part such as 10.50, 155.25 etc.
 When the accuracy of the floating point number is insufficient, we can use the double to define the
number. The double is same as float but with longer precision.
 To extend the precision further we can use long double which consumes 80 bits of memory space.
 Example: float a=10.50;
8
Primary Data Type (cont…)
 char:
 Char data type can store single character of alphabet or digit or special symbol such as ‘a’, ‘5’ etc.
 Each character is assigned some integer value which is known as ASCII values.
 Example: char a=‘a’;
 void:
 The void type has no value therefore we cannot declare it as variable as we did in case of int or
float or char.
 The void data type is used to indicate that function is not returning anything.
9
Secondary Data Type
 Secondary data types are not directly supported by the machine.
 It is combination of primary data types to handle real life data in more convenient
way.
 It can be further divided in two categories,
 Derived data types: Derived data type is extension of primary data type. It is built-in system
and its structure cannot be changed. Examples: Array and Pointer.
 Array: An array is a fixed-size sequenced collection of elements of the same data type.
 Pointer: Pointer is a special variable which contains memory address of another variable.
 User defined data types: User defined data type can be created by programmer using
combination of primary data type and/or derived data type. Examples: Structure, Union, Enum.
 Structure: Structure is a collection of logically related data items of different data types grouped
together under a single name.
 Union: Union is like a structure, except that each element shares the common memory.
 Enum: Enum is used to assign names to integral constants, the names make a program easy to read
and maintain.
Fundamentals of C Language 10
Variables and Constants
 Variable is a symbolic name given to some value which can be changed.
 𝑥, 𝑦, 𝑎, 𝑐𝑜𝑢𝑛𝑡, 𝑒𝑡𝑐. can be variable names.
 𝑥=5 𝑎=𝑏+𝑐
 Constant is a fixed value which cannot be changed.
 5, −7.5, 1452, 0, 3.14, 𝑒𝑡𝑐.
Fundamentals of C Language 11
Tokens
 The smallest individual unit of a program is known as token.
 C has the following tokens:
 Keywords
 C reserves a set of 32 words for its own use. These words are called keywords (or reserved words), and
each of these keywords has a special meaning within the C language.
 Identifiers
 Identifiers are names that are given to various user defined program elements, such as variable,
function and arrays.
 Constants
 Constants refer to fixed values that do not change during execution of program.
 Strings
 A string is a sequence of characters terminated with a null character 0.
 Special Symbols
 Symbols such as #, &, =, * are used in C for some specific function are called as special symbols.
 Operators
 An operator is a symbol that tells the compiler to perform certain mathematical or logical operation.
Fundamentals of C Language 12
Operators
 Arithmetic operators (+, - , *, /, %)
 Relational operators (<, <=, >, >=, ==, !=)
 Logical operators (&&, ||, !)
 Assignment operators (+=, -=, *=, /=)
 Increment and decrement operators (++, --)
 Conditional operators (?:)
 Bitwise operators (&, |, ^, <<, >>)
 Special operators ()
Fundamentals of C Language 13
Arithmetic Operators
 Arithmetic operators are used for mathematical calculation.
Operator Meaning Example Description
+ Addition a + b Addition of a and b
- Subtraction a – b Subtraction of b from a
* Multiplication a * b Multiplication of a and b
/ Division a / b Division of a by b
% Modulo division- remainder a % b Modulo of a by b
Fundamentals of C Language 14
Relational Operators
 Relational operators are used to compare two numbers and taking decisions based
on their relation.
 Relational expressions are used in decision statements such as if, for, while, etc…
Operator Meaning Example Description
< Is less than a < b a is less than b
<= Is less than or equal to a <= b a is less than or equal to b
> Is greater than a > b a is greater than b
>= Is greater than or equal to a >= b a is greater than or equal to b
== Is equal to a = b a is equal to b
!= Is not equal to a != b a is not equal to b
Fundamentals of C Language 15
Logical Operators
 Logical operators are used to test more than one condition and make decisions.
Operator Meaning
&& logical AND (Both non zero then true, either is zero then false)
| | logical OR (Both zero then false, either is non zero then true)
! Is greater than
a b a&&b  a||b
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
Fundamentals of C Language 16
Assignment Operators
 Assignment operators (=) is used to assign the result of an expression to a variable.
 Assignment operator stores a value in memory.
 C also supports shorthand assignment operators which simplify operation with
assignment.
Operator Meaning
= Assigns value of right side to left side
+= a += 1 is same as a = a + 1
-= a -= 1 is same as a = a - 1
*= a *= 1 is same as a = a * 1
/= a /= 1 is same as a = a / 1
%= a %= 1 is same as a = a % 1
Fundamentals of C Language 17
Increment and Decrement Operators
 Increment (++) operator used to increase the value of the variable by one.
 Decrement (--) operator used to decrease the value of the variable by one.
x=100;
x++;
Example
After the execution the 
value of x will be 101.
Explanation
x=100;
x‐‐;
Example
After the execution the 
value of x will be 99.
Explanation
Fundamentals of C Language 18
Increment and Decrement Operators (cont…)
Operator Description
Pre increment operator (++x) value of x is incremented before assigning it to the variable
on the left
Operator Description
Post increment operator (x++) value of x is incremented after assigning it to the variable on
the left
x=10;
p=++x;
Example
First increment value of 
x by one then assign.
Explanation
x will be 11
p will be 11
Output
x=10;
p=x++;
Example
First assign value of x 
then increment value.
Explanation
x will be 11
p will be 10
Output
Fundamentals of C Language 19
Conditional Operators
 A ternary operator is known as conditional operator.
 Syntax: exp1 ? exp2 : exp3
exp1 is evaluated first
if exp1 is true(nonzero) then
‐ exp2 is evaluated and its value becomes the value of the expression
If exp1 is false(zero) then
‐ exp3 is evaluated and its value becomes the value of the expression
Working of the ? : Operator
m=2, n=3;
r=(m>n) ? m : n;
Example
m=2, n=3;
r=(m<n) ? m : n;
Example
Value of r will be 3
Explanation
Value of r will be 2
Explanation
Fundamentals of C Language 20
Bitwise Operators
 Bitwise operators are used to perform operation bit by bit.
 Bitwise operators may not be applied to float or double.
Operator Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left (shift left means multiply by 2)
>> shift right (shift right means divide by 2)
Fundamentals of C Language 21
Bitwise Operators
8 = 1000 (In Binary) and 6 = 0110 (In Binary)
int a=8, b=6, c;
c = a & b;
printf("Output = %d", c);
Example: Bitwise & (AND)
int a=8, b=6, c;
c = a | b;
printf("Output = %d", c);
Example: Bitwise | (OR)
0
Output
14
Output
int a=8, b;
b = a << 1;
printf("Output = %d", b);
Example: Bitwise << (Shift Left)
int a=8, b;
b = a >> 1;
printf("Output = %d", b);
Example: Bitwise >> (Shift Right)
16 (multiplying a by a power of two)
Output
4 (dividing a by a power of two)
Output
Fundamentals of C Language 22
Special Operators
Operator Meaning
& Address operator, it is used to determine address of the variable.
* Pointer operator, it is used to declare pointer variable and to get value from it.
, Comma operator. It is used to link the related expressions together.
sizeof It returns the number of bytes the operand occupies.
. member selection operator, used in structure.
-> member selection operator, used in pointer to structure.
Fundamentals of C Language 23
Expressions
 An expression is a combination of operators, constants and variables.
 An expression may consist of one or more operands, and zero or more operators to
produce a value.
answer = a + b * c;
Operand 1
Variable to store 
the expression value 
Operand 2 Operand 3
Operator  1 Operand 2
Fundamentals of C Language 24
Evaluation of Expressions
 An expression is evaluated based on the operator precedence and associativity.
 When there are multiple operators in an expression, they are evaluated according
to their precedence and associativity.
Fundamentals of C Language 25
Operator precedence
 Precedence of an operator is its priority in an expression for evaluation.
 The operator with higher precedence is evaluated first and the operator with the
least precedence is evaluated last.
 Operator precedence is why the expression 5 + 3 * 2 is calculated as 5 + (3 * 2),
giving 11, and not as (5 + 3) * 2, giving 16.
 We say that the multiplication operator (*) has higher "precedence" or "priority"
than the addition operator (+), so the multiplication must be performed first.
Fundamentals of C Language 26
Operator associativity
 Associativity is the left-to-right or right-to-left order for grouping operands to
operators that have the same precedence.
 Operator associativity is why the expression 8 - 3 - 2 is calculated as (8 - 3) - 2,
giving 3, and not as 8 - (3 - 2), giving 7.
 We say that the subtraction operator (-) is "left associative", so the left subtraction
must be performed first.
 When we can't decide by operator precedence alone in which order to calculate an
expression, we must use associativity.
Fundamentals of C Language 27
Type conversion
 Type conversion is converting one type of data to another type.
 It is also known as Type Casting.
 There are two types of type conversion:
 Implicit Type Conversion
 This type of conversion is usually performed by the compiler when necessary without any commands
by the user.
 It is also called Automatic Type Conversion.
 Explicit Type Conversion
 These conversions are done explicitly by users using the pre-defined functions.
int a = 20;
double b = 20.5;
printf("%lf", a + b);
Example: Implicit Type Conversion
double a = 4.5, b = 4.6, c = 4.9;
int result = (int)da + (int)db + (int)dc; 
printf("result = %d", result);
Example: Explicit Type Conversion
40.500000
Output
12
Output
Fundamentals of C Language 28
printf()
 printf() is a function defined in stdio.h file
 It displays output on standard output, mostly monitor
 Message and value of variable can be printed
 Let’s see few examples of printf
 printf(“ ”);
 printf(“Hello World”); // Hello World
 printf(“%d”, c); // 15
 printf(“Sum = %d”, c); // Sum = 15
 printf(“%d+%d=%d”, a, b, c); // 10+5=15
Fundamentals of C Language 29
scanf()
 scanf() is a function defined in stdio.h file
 scanf() function is used to read character, string, numeric data from keyboard
 Syntax of scanf
 scanf("%X", &variable);
 where %X is the format specifier which tells the compiler what type of data is in a variable.
 & refers to address of “variable” which is directing the input value to a address returned by &variable.
Format
specifier
Supported
data types
Example Description
%d Integer scanf(“%d”, &a) Accept integer value such as 1, 5, 25, 105 etc
%f Float scanf(“%f”, &b) Accept floating value such as 1.5, 15.20 etc
%c Character scanf(“%c”, &c) Accept character value such as a, f, j, W, Z etc
%s String scanf(“%s”, &d) Accept string value such as diet, india etc
Fundamentals of C Language 30
getchar and putchar
 getchar function reads a single
character from terminal.
 putchar function displays the
character passed to it on the screen.
#include <stdio.h>
void main( )
{
int c;
printf("Enter a character: ");
/* Take a character as input */
c = getchar();
/* Display the character */
printf("Entered character is: ");
putchar(c);
}
1
2
3
4
5
6
7
8
9
10
11
Program
Enter a character: a
Entered character is: a
Output
Fundamentals of C Language 31
gets and puts
 gets function reads a line from stdin
into the buffer pointed to by s until
either a terminating newline or EOF
(End of File) occurs.
 puts function writes the string 's'
and 'a' trailing newline to stdout.
#include <stdio.h>
void main( )
{
/*Character array of length 100*/
char str[100];
printf("Enter a string: ");
/* Take a string as input */
gets( str );
/* Display the string */
printf("Entered string is: ");
puts( str );
}
1
2
3
4
5
6
7
8
9
10
11
12
Program
Enter a string: india
Entered string is: india
Output
Fundamentals of C Language 32
Preprocessor
 Preprocessors are programs that process our source code before compilation.
 There are a number of steps involved between writing a program and executing a
program in C.
 Let us have a look at these steps before we actually start learning about
Preprocessors.
C Program
Are there
preprocessor
directive
Preprocessor perform action
Compiler Linker
Yes
No
Object
Code
Executable
Code
Fundamentals of C Language 33
Types of Preprocessor
 There are 4 main types of preprocessor directives:
 Macros
 File inclusion
 Conditional compilation
 Other directives
Fundamentals of C Language 34
Macro
 A macro is a fragment of code which has been given a name. Whenever the name
is used in program, it is replaced by the contents of the macro.
 Macro definitions are not variables and cannot be changed by your program code
like variables.
 The ‘#define’ directive is used to define a macro.
 Do not put a semicolon ( ; ) at the end of #define statements.
 There are two types of macros:
 Object-like Macros
 Function-like Macros
Fundamentals of C Language 35
Macro
Description Object-like Macros Function-like Macros
Definition The object-like macro is an identifier that
is replaced by value.
The function-like macro looks like
function call.
Use It is used to represent numeric constants. It is used to represent function.
Syntax #define CNAME value #define CNAME (expression)
Example #define PI 3.14 #define MIN(a,b) ((a)<(b)?(a):(b))
Program #include <stdio.h>
#define PI 3.14
void main()
{ int r=2;
float a;
a=PI*r*r;
printf("%f", a);
}
1
2
3
4
5
6
7
8
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
void main()
{
printf("%d", MIN(2, 5));
}
1
2
3
4
5
6

More Related Content

What's hot (20)

R and Data Science
R and Data ScienceR and Data Science
R and Data Science
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-c
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Unix ppt
Unix pptUnix ppt
Unix ppt
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
 
DBMS Part-4.pdf
DBMS Part-4.pdfDBMS Part-4.pdf
DBMS Part-4.pdf
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell ScriptingLinux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
File handling in C
File handling in CFile handling in C
File handling in C
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
ASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS ANDASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS AND
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operations
 
Phython Programming Language
Phython Programming LanguagePhython Programming Language
Phython Programming Language
 

Similar to Fundamentals of c language

component of c language.pptx
component of c language.pptxcomponent of c language.pptx
component of c language.pptxAnisZahirahAzman
 
Interview Questions For C Language .pptx
Interview Questions For C Language .pptxInterview Questions For C Language .pptx
Interview Questions For C Language .pptxRowank2
 
Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language Rowank2
 
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptxLeenaChaudhari24
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxLikhil181
 
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...Rowank2
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III TermAndrew Raj
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 
Basic Information About C language PDF
Basic Information About C language PDFBasic Information About C language PDF
Basic Information About C language PDFSuraj Das
 

Similar to Fundamentals of c language (20)

component of c language.pptx
component of c language.pptxcomponent of c language.pptx
component of c language.pptx
 
C program
C programC program
C program
 
Interview Questions For C Language .pptx
Interview Questions For C Language .pptxInterview Questions For C Language .pptx
Interview Questions For C Language .pptx
 
Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language
 
Basics of c
Basics of cBasics of c
Basics of c
 
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
C material
C materialC material
C material
 
C basics
C basicsC basics
C basics
 
C basics
C basicsC basics
C basics
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
 
Basic Of C language
Basic Of C languageBasic Of C language
Basic Of C language
 
C intro
C introC intro
C intro
 
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
 
C programming
C programming C programming
C programming
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
Basic Information About C language PDF
Basic Information About C language PDFBasic Information About C language PDF
Basic Information About C language PDF
 

Recently uploaded

MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

Fundamentals of c language

  • 2. 2 Features of C Language  Modularity  Extensibility  Elegant syntax  Case sensitive  Less memory required  The standard library concept  The portability of the compiler  A powerful and varied range of operators  Ready access to the hardware when needed
  • 3. 3 Structure of C Program Documentation section (Used for comments) Link section Definition section Global declaration section   (Variables used in more than   one functions) void main () { Declaration part   Executable part } Subprogram section (User defined functions) Program 1 // Program for addition of 2 nos 2 3 4 5 6 void fun(); 7 8 int a=10; 9 10 11 void main( ) 12 { 13 printf("Value of a inside main function: %d", a); 14 fun(); 15 } 16 14 void fun() 18 {printf("Value of a inside fun function: %d", a);}
  • 4. 4 Comments  A comment is an explanation or description of the source code of the program  It helps a programmer to explain logic of the code and improves program readability.  At run-time, a comment is ignored by the compiler.  There are two types of comments in C:  Single line comment  Represented as // double forward slash  It is used to denote a single line comment only.  Example: // Single line comment  Multi-line comment  Represented as /* any_text */ start with forward slash and asterisk (/*) and end with asterisk and forward slash (*/).  It is used to denote single as well as multi-linecomment.  Example: /* multi line comment line ‐1  multi line comment line ‐2 */
  • 5. 5 Header files  A header file is a file with extension .h which contains the set of predefined standard library functions.  The “#include” preprocessing directive is used to include the header files with extension in the program. Header file Description stdio.h Input/Output functions (printf and scanf) conio.h Console Input/Output functions (getch and clrscr) math.h Mathematics functions (pow, exp, sqrt etc…) string.h String functions (strlen, strcmp, strcat etc…)
  • 6. 6 Data Types  Data types are defined as the data storage format that a variable can store a data.  It determines the type and size of data associated with variables. Data types in C Primary Data type (int, float, char) Secondary Data type Derived Data type (array, pointer) User definer Data type (structure, union, enum)
  • 7. 7 Primary Data Type  Primary data types are built in data types which are directly supported by machine.  They are also known as fundamental data types.  int:  int datatype can store integer number which is whole number without fraction part such as 10, 105 etc.  C language has 3 classes of integer storage namely short int, int and long int. All of these data types have signed and unsigned forms.  Example: int a=10;  float:  float data type can store floating point number which represents a real number with decimal point and fractional part such as 10.50, 155.25 etc.  When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision.  To extend the precision further we can use long double which consumes 80 bits of memory space.  Example: float a=10.50;
  • 8. 8 Primary Data Type (cont…)  char:  Char data type can store single character of alphabet or digit or special symbol such as ‘a’, ‘5’ etc.  Each character is assigned some integer value which is known as ASCII values.  Example: char a=‘a’;  void:  The void type has no value therefore we cannot declare it as variable as we did in case of int or float or char.  The void data type is used to indicate that function is not returning anything.
  • 9. 9 Secondary Data Type  Secondary data types are not directly supported by the machine.  It is combination of primary data types to handle real life data in more convenient way.  It can be further divided in two categories,  Derived data types: Derived data type is extension of primary data type. It is built-in system and its structure cannot be changed. Examples: Array and Pointer.  Array: An array is a fixed-size sequenced collection of elements of the same data type.  Pointer: Pointer is a special variable which contains memory address of another variable.  User defined data types: User defined data type can be created by programmer using combination of primary data type and/or derived data type. Examples: Structure, Union, Enum.  Structure: Structure is a collection of logically related data items of different data types grouped together under a single name.  Union: Union is like a structure, except that each element shares the common memory.  Enum: Enum is used to assign names to integral constants, the names make a program easy to read and maintain.
  • 10. Fundamentals of C Language 10 Variables and Constants  Variable is a symbolic name given to some value which can be changed.  𝑥, 𝑦, 𝑎, 𝑐𝑜𝑢𝑛𝑡, 𝑒𝑡𝑐. can be variable names.  𝑥=5 𝑎=𝑏+𝑐  Constant is a fixed value which cannot be changed.  5, −7.5, 1452, 0, 3.14, 𝑒𝑡𝑐.
  • 11. Fundamentals of C Language 11 Tokens  The smallest individual unit of a program is known as token.  C has the following tokens:  Keywords  C reserves a set of 32 words for its own use. These words are called keywords (or reserved words), and each of these keywords has a special meaning within the C language.  Identifiers  Identifiers are names that are given to various user defined program elements, such as variable, function and arrays.  Constants  Constants refer to fixed values that do not change during execution of program.  Strings  A string is a sequence of characters terminated with a null character 0.  Special Symbols  Symbols such as #, &, =, * are used in C for some specific function are called as special symbols.  Operators  An operator is a symbol that tells the compiler to perform certain mathematical or logical operation.
  • 12. Fundamentals of C Language 12 Operators  Arithmetic operators (+, - , *, /, %)  Relational operators (<, <=, >, >=, ==, !=)  Logical operators (&&, ||, !)  Assignment operators (+=, -=, *=, /=)  Increment and decrement operators (++, --)  Conditional operators (?:)  Bitwise operators (&, |, ^, <<, >>)  Special operators ()
  • 13. Fundamentals of C Language 13 Arithmetic Operators  Arithmetic operators are used for mathematical calculation. Operator Meaning Example Description + Addition a + b Addition of a and b - Subtraction a – b Subtraction of b from a * Multiplication a * b Multiplication of a and b / Division a / b Division of a by b % Modulo division- remainder a % b Modulo of a by b
  • 14. Fundamentals of C Language 14 Relational Operators  Relational operators are used to compare two numbers and taking decisions based on their relation.  Relational expressions are used in decision statements such as if, for, while, etc… Operator Meaning Example Description < Is less than a < b a is less than b <= Is less than or equal to a <= b a is less than or equal to b > Is greater than a > b a is greater than b >= Is greater than or equal to a >= b a is greater than or equal to b == Is equal to a = b a is equal to b != Is not equal to a != b a is not equal to b
  • 15. Fundamentals of C Language 15 Logical Operators  Logical operators are used to test more than one condition and make decisions. Operator Meaning && logical AND (Both non zero then true, either is zero then false) | | logical OR (Both zero then false, either is non zero then true) ! Is greater than a b a&&b  a||b 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1
  • 16. Fundamentals of C Language 16 Assignment Operators  Assignment operators (=) is used to assign the result of an expression to a variable.  Assignment operator stores a value in memory.  C also supports shorthand assignment operators which simplify operation with assignment. Operator Meaning = Assigns value of right side to left side += a += 1 is same as a = a + 1 -= a -= 1 is same as a = a - 1 *= a *= 1 is same as a = a * 1 /= a /= 1 is same as a = a / 1 %= a %= 1 is same as a = a % 1
  • 17. Fundamentals of C Language 17 Increment and Decrement Operators  Increment (++) operator used to increase the value of the variable by one.  Decrement (--) operator used to decrease the value of the variable by one. x=100; x++; Example After the execution the  value of x will be 101. Explanation x=100; x‐‐; Example After the execution the  value of x will be 99. Explanation
  • 18. Fundamentals of C Language 18 Increment and Decrement Operators (cont…) Operator Description Pre increment operator (++x) value of x is incremented before assigning it to the variable on the left Operator Description Post increment operator (x++) value of x is incremented after assigning it to the variable on the left x=10; p=++x; Example First increment value of  x by one then assign. Explanation x will be 11 p will be 11 Output x=10; p=x++; Example First assign value of x  then increment value. Explanation x will be 11 p will be 10 Output
  • 19. Fundamentals of C Language 19 Conditional Operators  A ternary operator is known as conditional operator.  Syntax: exp1 ? exp2 : exp3 exp1 is evaluated first if exp1 is true(nonzero) then ‐ exp2 is evaluated and its value becomes the value of the expression If exp1 is false(zero) then ‐ exp3 is evaluated and its value becomes the value of the expression Working of the ? : Operator m=2, n=3; r=(m>n) ? m : n; Example m=2, n=3; r=(m<n) ? m : n; Example Value of r will be 3 Explanation Value of r will be 2 Explanation
  • 20. Fundamentals of C Language 20 Bitwise Operators  Bitwise operators are used to perform operation bit by bit.  Bitwise operators may not be applied to float or double. Operator Meaning & bitwise AND | bitwise OR ^ bitwise exclusive OR << shift left (shift left means multiply by 2) >> shift right (shift right means divide by 2)
  • 21. Fundamentals of C Language 21 Bitwise Operators 8 = 1000 (In Binary) and 6 = 0110 (In Binary) int a=8, b=6, c; c = a & b; printf("Output = %d", c); Example: Bitwise & (AND) int a=8, b=6, c; c = a | b; printf("Output = %d", c); Example: Bitwise | (OR) 0 Output 14 Output int a=8, b; b = a << 1; printf("Output = %d", b); Example: Bitwise << (Shift Left) int a=8, b; b = a >> 1; printf("Output = %d", b); Example: Bitwise >> (Shift Right) 16 (multiplying a by a power of two) Output 4 (dividing a by a power of two) Output
  • 22. Fundamentals of C Language 22 Special Operators Operator Meaning & Address operator, it is used to determine address of the variable. * Pointer operator, it is used to declare pointer variable and to get value from it. , Comma operator. It is used to link the related expressions together. sizeof It returns the number of bytes the operand occupies. . member selection operator, used in structure. -> member selection operator, used in pointer to structure.
  • 23. Fundamentals of C Language 23 Expressions  An expression is a combination of operators, constants and variables.  An expression may consist of one or more operands, and zero or more operators to produce a value. answer = a + b * c; Operand 1 Variable to store  the expression value  Operand 2 Operand 3 Operator  1 Operand 2
  • 24. Fundamentals of C Language 24 Evaluation of Expressions  An expression is evaluated based on the operator precedence and associativity.  When there are multiple operators in an expression, they are evaluated according to their precedence and associativity.
  • 25. Fundamentals of C Language 25 Operator precedence  Precedence of an operator is its priority in an expression for evaluation.  The operator with higher precedence is evaluated first and the operator with the least precedence is evaluated last.  Operator precedence is why the expression 5 + 3 * 2 is calculated as 5 + (3 * 2), giving 11, and not as (5 + 3) * 2, giving 16.  We say that the multiplication operator (*) has higher "precedence" or "priority" than the addition operator (+), so the multiplication must be performed first.
  • 26. Fundamentals of C Language 26 Operator associativity  Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.  Operator associativity is why the expression 8 - 3 - 2 is calculated as (8 - 3) - 2, giving 3, and not as 8 - (3 - 2), giving 7.  We say that the subtraction operator (-) is "left associative", so the left subtraction must be performed first.  When we can't decide by operator precedence alone in which order to calculate an expression, we must use associativity.
  • 27. Fundamentals of C Language 27 Type conversion  Type conversion is converting one type of data to another type.  It is also known as Type Casting.  There are two types of type conversion:  Implicit Type Conversion  This type of conversion is usually performed by the compiler when necessary without any commands by the user.  It is also called Automatic Type Conversion.  Explicit Type Conversion  These conversions are done explicitly by users using the pre-defined functions. int a = 20; double b = 20.5; printf("%lf", a + b); Example: Implicit Type Conversion double a = 4.5, b = 4.6, c = 4.9; int result = (int)da + (int)db + (int)dc;  printf("result = %d", result); Example: Explicit Type Conversion 40.500000 Output 12 Output
  • 28. Fundamentals of C Language 28 printf()  printf() is a function defined in stdio.h file  It displays output on standard output, mostly monitor  Message and value of variable can be printed  Let’s see few examples of printf  printf(“ ”);  printf(“Hello World”); // Hello World  printf(“%d”, c); // 15  printf(“Sum = %d”, c); // Sum = 15  printf(“%d+%d=%d”, a, b, c); // 10+5=15
  • 29. Fundamentals of C Language 29 scanf()  scanf() is a function defined in stdio.h file  scanf() function is used to read character, string, numeric data from keyboard  Syntax of scanf  scanf("%X", &variable);  where %X is the format specifier which tells the compiler what type of data is in a variable.  & refers to address of “variable” which is directing the input value to a address returned by &variable. Format specifier Supported data types Example Description %d Integer scanf(“%d”, &a) Accept integer value such as 1, 5, 25, 105 etc %f Float scanf(“%f”, &b) Accept floating value such as 1.5, 15.20 etc %c Character scanf(“%c”, &c) Accept character value such as a, f, j, W, Z etc %s String scanf(“%s”, &d) Accept string value such as diet, india etc
  • 30. Fundamentals of C Language 30 getchar and putchar  getchar function reads a single character from terminal.  putchar function displays the character passed to it on the screen. #include <stdio.h> void main( ) { int c; printf("Enter a character: "); /* Take a character as input */ c = getchar(); /* Display the character */ printf("Entered character is: "); putchar(c); } 1 2 3 4 5 6 7 8 9 10 11 Program Enter a character: a Entered character is: a Output
  • 31. Fundamentals of C Language 31 gets and puts  gets function reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF (End of File) occurs.  puts function writes the string 's' and 'a' trailing newline to stdout. #include <stdio.h> void main( ) { /*Character array of length 100*/ char str[100]; printf("Enter a string: "); /* Take a string as input */ gets( str ); /* Display the string */ printf("Entered string is: "); puts( str ); } 1 2 3 4 5 6 7 8 9 10 11 12 Program Enter a string: india Entered string is: india Output
  • 32. Fundamentals of C Language 32 Preprocessor  Preprocessors are programs that process our source code before compilation.  There are a number of steps involved between writing a program and executing a program in C.  Let us have a look at these steps before we actually start learning about Preprocessors. C Program Are there preprocessor directive Preprocessor perform action Compiler Linker Yes No Object Code Executable Code
  • 33. Fundamentals of C Language 33 Types of Preprocessor  There are 4 main types of preprocessor directives:  Macros  File inclusion  Conditional compilation  Other directives
  • 34. Fundamentals of C Language 34 Macro  A macro is a fragment of code which has been given a name. Whenever the name is used in program, it is replaced by the contents of the macro.  Macro definitions are not variables and cannot be changed by your program code like variables.  The ‘#define’ directive is used to define a macro.  Do not put a semicolon ( ; ) at the end of #define statements.  There are two types of macros:  Object-like Macros  Function-like Macros
  • 35. Fundamentals of C Language 35 Macro Description Object-like Macros Function-like Macros Definition The object-like macro is an identifier that is replaced by value. The function-like macro looks like function call. Use It is used to represent numeric constants. It is used to represent function. Syntax #define CNAME value #define CNAME (expression) Example #define PI 3.14 #define MIN(a,b) ((a)<(b)?(a):(b)) Program #include <stdio.h> #define PI 3.14 void main() { int r=2; float a; a=PI*r*r; printf("%f", a); } 1 2 3 4 5 6 7 8 #include <stdio.h> #define MIN(a,b) ((a)<(b)?(a):(b)) void main() { printf("%d", MIN(2, 5)); } 1 2 3 4 5 6