0% found this document useful (0 votes)
31 views39 pages

Chapter 2

Uploaded by

patel.ved98795
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views39 pages

Chapter 2

Uploaded by

patel.ved98795
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

CHARUTAR VIDYA MANDAL UNIVERSITY

A D PATEL I NSTITUTE O F T ECHNOLOGY

FUNDAMENTALS OF C

P REPARED BY:
P ROF. A XIT K ACHHIA
CP D EPARTMENT
Outline
✓Features of C Language
✓Structure of C Program
✓Comments, Header files, data types
✓Constant & variables
✓Operators
✓Expression & Evaluation of Expressions
✓Type Conversion
✓Precedence & Associativity
✓I/O Function
2
Features of C Language

3
Features of C Language
1) Simple
C is a simple language in the sense that it provides a structured approach (to
break the problem into parts), the rich set of library functions, data types,
etc.
2) Machine Independent or Portable
Unlike assembly language, c programs can be executed on different
machines with some machine specific changes. Therefore, C is a machine
independent language.
3) Mid-level programming language
Although, C is intended to do low-level programming. It is used to develop
system applications such as kernel, driver, etc. It also supports the features
of a high-level language. That is why it is known as mid-level language.
4
Features of C Language
4) Structured programming language
C is a structured programming language in the sense that we can break the
program into parts using functions. So, it is easy to understand and modify.
Functions also provide code reusability.
5) Rich Library
C provides a lot of inbuilt functions that make the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can
free the allocated memory at any time by calling the free() function.

5
Features of C Language
7) Speed
The compilation and execution time of C language is fast since there are
lesser inbuilt functions and hence the lesser overhead.
8) Extensible
C language is extensible because it can easily adopt new features.

6
Structure of C Program

7
Structure of C Program

8
Structure of C Program
➢Documentation section
It includes the statement specified at the beginning of a program, such as a
program's name, date, description, and title. It is represented as:
//name of a program
➢Preprocessor section
The preprocessor section contains all the header files used in a program. It
informs the system to link the header files to the system libraries. It is given
by:
1.#include<stdio.h>
2.#include<conio.h>

9
Structure of C Program
➢User defined functions
Return function is generally the last section of a code. But, it is not necessary
to include. It is used when we want to return a value. The return function
returns a value when the return type other than the void is specified with the
function.
Return type ends the execution of the function. It further returns control to
the specified calling function. It is given by:
return;
return expression ;
For example,
return 0;
1
0
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 improve program
readability. At run-time, a comment is ignored by the compiler.
There are two types of comments in C:
Single line comment
Multi-Line Comment

1
1
Comments
➢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-line comment.
◦ Example: /* multi line comment line -1
multi line comment line -2 */
1
2
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.
1. #include<stdio.h> (Standard input-output header)
Used to perform input and output operations in C like scanf() and printf().
2. #include<string.h> (String header)
Perform string manipulation operations like strlen and strcpy.

1
3
Header Files
3. #include<conio.h> (Console input-output header)
Perform console input and console output operations like clrscr() to clear the
screen and getch() to get the character from the keyboard.
4. #include<math.h> (Math header )
Perform mathematical operations like sqrt() and pow(). To obtain the square
root and the power of a number respectively.
5. #include<time.h>(Time header)
Perform functions related to date and time like setdate() and getdate(). To
modify the system date and get the CPU time respectively.

1
4
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.

1
5
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.
➢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;

1
6
Primary Data Type
➢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.

1
7
Primary Data Type
➢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.

18
Primary Data Type
Keyword Used Data Type

int Integer

float Floating-point

void Void

char Character

double Double

19
Derived Data Type
➢Derived data types are data types which are formed by combining one or
more primitive data types or basic data types in C.
➢For example, there might be some cases where primitive data types are not
sufficient for us. Like if we want to store a phone number we can use an
integer but what if we want to store phone numbers of all the students in a
college.
➢Making variables for each of them is not the optimal way.

20
Derived Data Type
➢To deal with such situations optimally, C has some derived data types, which
we can use as per our convenience .
1.ARRAY
2.STRING
3.POINTER
4.FUNCTIONS

21
Derived Data Type
1.ARRAY:
➢Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
➢To create an array, define the data type (like int), and specify the name of
the array followed by square brackets [].
➢To insert values to it, use a comma-separated list, inside curly braces:
int myNumbers[] = {25, 50, 75, 100};

➢Example:
➢int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);

22
Derived Data Type
1.ARRAY:
➢int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

printf("%d", myNumbers[0]);

// Now outputs 33 instead of 25

23
Derived Data Type
2. STRING:
➢Strings are used for storing text/characters.
➢For example, "Hello World" is a string of characters.
➢Unlike many other programming languages, C does not have a String type to
easily create string variables. However, you can use the char type and create
an array of characters to make a string in C:

➢char greetings[] = "Hello World!";

24
Derived Data Type
2. STRING:
➢Example:
➢char greetings[] = "Hello World!";
printf("%s", greetings);

➢Modify Strings:
➢char greetings[] = "Hello World!";
greetings[0] = 'J';
printf("%s", greetings);

// Outputs Jello World! instead of Hello World!


25
Constants
➢Constant is the most fundamental and essential part of the C programming
language. Constants in C are the fixed values used in a program, and their
value remains the same during the entire program execution.
•Constants are also called literals.
•Constants can be any of the data types.
•It is considered best practice to define constants using only upper-
case names.

26
Constants
➢Types :
1.Numeric Constants
1.Integer Constants
2.Real Constants
2.Character Constants
1.Single Character Constants
2.String Constants

27
Variables
➢Variables are containers for storing data values.
In C, there are different types of variables (defined with different keywords),
for example:
➢Int- stores integers (whole numbers), without decimals, such as 123 or -123
➢float- stores floating point numbers, with decimals, such as 19.99 or -19.99
➢char- tores single characters, such as 'a' or 'B'. Char values are surrounded
by single quotes

28
Difference
Constant Variables
A constant does not change its value over A variable, on the other hand, changes its
time. value dependent on the equation.

Constants are usually written in numbers. Variables are specially written in letters or
symbols.

Constants usually represent the known Variables, on the other hand, represent the
values in an equation, expression or in line of unknown values.
programming.

Constants are used in computer Variables also have its uses in computer
programming. programming and applications.

29
Operators
➢Operators are used to perform operations on variables and values.
➢An operator is a symbol to tell the computer to perform certain
mathematical or logical manipulation.
➢Operators are used in program to manipulate data and variables.
➢They usually form a part of the mathematical or logical expression.
➢C divides the operators into the following groups:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Bitwise operators
30
Expressions
➢An expressions in C is a combination of operands and operators – it
computes a single value stored in a variable.
➢The operator denotes the action or operation to be performed.
The operands are the items to which we apply the operation.

31
Expressions
➢An expressions can be defined depending on the position and number of its
operator and operands:

➢Infix Expression (operator is used between the operands): a = x + y


➢Postfix Expression (operator is used after the operands): xy+
➢Prefix Expression (operator is used before the operands): +xy
➢Unary Expression (one operator and one operand): x++

32
Types of Expressions

33
Types of Expressions
1. Arithmetic Expression:
➢It consists of arithmetic operators ( + , - , * , and / ) and computes values of
int, float or double type.
2. Relational Expression:
➢It usually consists of comparison operators (> , < , >= , <= , === , and !== )
and computes the answer in the bool type, i.e., true (1) or false (0).
3. Logical Expression:
➢It consists of logical operators (&&, ||, and !) and combines relational
expressions to compute answers in the bool type.
4. Conditional Expression:
➢It consists of conditional statements that return true if the condition is met
and false otherwise. 34
Operator Precedence and Associativity
➢Operator precedence determines which operation is performed first in an
expression with more than one operators with different precedence.
➢The precedence is used to determine how an expression involving more
than one operator is evaluated.
➢For example:
➢10 + 20 * 30

➢10 + 20 * 30 is calculated as 10 + (20 * 30) not as (10 + 20) * 30

35
Operator Precedence and Associativity

36
Operator Precedence and Associativity
➢Operators Associativity is used when two operators of same precedence
appear in an expression. Associativity can be either Left to Right
or Right to Left.
➢For example: ‘*’ and ‘/’ have same precedence and their associativity
is Left to Right, so the expression “100 / 10 * 10” is treated as
“(100 / 10) * 10”.

➢Operators Precedence and Associativity are two characteristics of


operators that determine the evaluation order of sub-expressions in
absence of brackets

37
Operator Precedence and Associativity
➢For Example:
➢100 + 200 / 10 – 3 * 10

➢Associativity is only used when there are two or more operators of same
precedence.
➢All operators with the same precedence have same associativity
➢Precedence and associativity of postfix ++ and prefix ++ are different

38
Operator Precedence and Associativity
➢For Example:
➢100 + 200 / 10 – 3 * 10

39

You might also like