0% found this document useful (0 votes)
14 views22 pages

C Language

The document covers fundamental data concepts in C programming, including character sets, tokens, variables, constants, data types, and operators. It explains the importance of constants, types of data types, and various operators used for arithmetic and logical operations. Additionally, it discusses variable declaration, data type conversion, and provides examples to illustrate these concepts.

Uploaded by

hacker8badf00d
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)
14 views22 pages

C Language

The document covers fundamental data concepts in C programming, including character sets, tokens, variables, constants, data types, and operators. It explains the importance of constants, types of data types, and various operators used for arithmetic and logical operations. Additionally, it discusses variable declaration, data type conversion, and provides examples to illustrate these concepts.

Uploaded by

hacker8badf00d
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

DATA CONCEPTS

1. Character set-
A character set is the collection of characters that the language recognizes and
uses to write programs.

Categories of Characters set

Characters divide into letters, digits, special symbols, whitespace, and control
codes.

Category Examples ASCII Range


Uppercase A–Z 65–90
letters
Lowercase a–z 97–122
letters
Digits 0–9 48–57
Special symbols !@#$%^&*()_+-=[]{}|;:'",./?~`<> 33–47, 58–64, 91–96,
123–126
Whitespace space, tab (\t), newline (\n) 9, 10, 32
Control chars \0 (null), \a (bell), \b (backspace), \r (return), \f 0–31, 127
(form feed)
2. Tokens
Tokens are the smallest individual units of a program that the compiler
recognizes.
3. Variable -
A variable is a named memory location whose value can change during program
execution.
4. Constant
A constant in C language is a fixed value that does not change during the
execution of a program. Once a constant is defined, its value remains the same
throughout the program.

Types of Constants in C

1. Integer Constants
Integer constants are whole numbers without any decimal point.

Examples:

10, -25, 0, 100

2. Floating-Point Constants

Floating constants are numbers containing a decimal point.

Examples:

3.14, -0.75, 2.0

3. Character Constants

A character constant is a single character enclosed in single quotes.

Examples:

'A', '9', '#'

4. String Constants

A string constant is a sequence of characters enclosed in double quotes.

Examples:

"Hello"
"C Programming"
5. Symbolic Constants

Symbolic constants are defined using the #define preprocessor directive.

Example:

#define PI 3.14

6. Constant Variables

A constant variable is declared using the const keyword. Its value cannot be
changed after initialization.

Example:

const int MAX = 100;

Advantages of Constants

 Improve program readability


 Prevent accidental modification of values
 Make programs easier to maintain

Example Program

#include <stdio.h>
#define PI 3.14

int main() {
const int r = 5;
float area = PI * r * r;
printf("%f", area);
return 0;
}
Conclusion

Constants are important in C programming because they store fixed values and
help in writing safe, reliable, and well-structured programs.

5. DATA TYPES
Data types in C language specify the type of data a variable can store and
determine the amount of memory allocated to it.

Types of Data Types in C

1. Basic (Primary) Data Types

These are the fundamental data types.

 int → stores integer values


 float → stores decimal values
 char → stores single character
 double → stores large decimal values
Example:

int a;
float b;
char c;

2. Derived Data Types

Derived from basic data types.

 Array
 Pointer
 Function

Example:

int arr[10];
int *p;

3. User-Defined Data Types

Created by the programmer.

 struct
 union
 enum
 typedef

4. Void Data Type

 void represents no value


 Used in functions that return nothing

Example:

void display();
6. Operators in C Language
Definition

Operators in C language are special symbols that are used to perform operations
on variables and constants. They tell the compiler what type of operation should
be performed on the data.

Types of Operators in C

1. Arithmetic Operators

Used to perform mathematical calculations.

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

Example:

int a = 10, b = 3;
int c = a + b;

2. Relational Operators

Used to compare two values. The result is either true (1) or false (0).

Operator Meaning
< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

== Equal to

!= Not equal to

3. Logical Operators

Used to combine or reverse logical expressions.

Operator Meaning
&& Logical AND
`
! Logical NOT

4. Assignment Operators

Used to assign values to variables.

Operator Example
= a = 10
+= a += 5
-= a -= 2
*= a *= 3
/= a /= 2
5. Increment and Decrement Operators

Used to increase or decrease a value by 1.

Operator Meaning
++ Increment

-- Decrement

Example:

a++;
b--;

6. Bitwise Operators

Used to perform operations at the bit level.

Operator Meaning
& Bitwise AND
` `
^ Bitwise XOR
~ Bitwise NOT
<< Left shift
>> Right shift

7. Conditional (Ternary) Operator

Used to make a decision in a single line.

Syntax
(condition) ? value1 : value2;

Example:

max = (a > b) ? a : b;
8. Special Operators

Used for special purposes.

Operator Use
sizeof Finds size of data type
, Comma operator
& Address operator
* Pointer operator
. Structure member access

Example Program

#include <stdio.h>

int main() {
int a = 5, b = 2;
printf("%d", a + b);
return 0;
}

7. Arithmetic Operators in C Language


Definition

Arithmetic operators in C language are operators used to perform basic


mathematical calculations such as addition, subtraction, multiplication, division,
and modulus on variables and constants.

List of Arithmetic Operators in C

Operator Name Function


+ Addition Adds two operands
- Subtraction Subtracts one operand from another
* Multiplication Multiplies two operands
/ Division Divides one operand by another
% Modulus Returns remainder of division

Explanation with Examples

1. Addition Operator (+)

Adds two values.

int c = a + b;
2. Subtraction Operator (-)

Subtracts one value from another.

int c = a - b;
3. Multiplication Operator (*)

Multiplies two values.

int c = a * b;
4. Division Operator (/)

Divides one value by another.

int c = a / b;

If both operands are integers, the result is an integer.

5. Modulus Operator (%)

Gives the remainder after division.

int c = a % b;

Modulus operator works only with integers.


Example Program

#include <stdio.h>

int main() {
int a = 10, b = 3;
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
printf("Division = %d\n", a / b);
printf("Modulus = %d\n", a % b);
return 0;
}

Important Points

 % operator cannot be used with float values


 / operator gives integer result when both operands are integers
 Arithmetic operators are used in arithmetic expressions

8. Arithmetic Expressions in C Language


Definition

An arithmetic expression in C language is a combination of operands (variables or


constants) and arithmetic operators that evaluates to a numeric result.

Components of Arithmetic Expression

1. Operands – variables or constants


2. Arithmetic Operators – + , − , * , / , %

Examples of Arithmetic Expressions

a+b
x*y-5
(10 + 5) / 3

Types of Arithmetic Expressions


1. Simple Arithmetic Expression

Contains only one operator.

Example:

a+b

2. Compound Arithmetic Expression

Contains more than one operator.

Example:

a+b*c

3. Parenthesized Arithmetic Expression

Uses parentheses to change the order of evaluation.

Example:

(a + b) * c

Operator Precedence in Arithmetic Expressions

When an expression contains multiple operators, C follows operator precedence


rules.

Order of Precedence:

1. Parentheses ( )
2. Multiplication *, Division /, Modulus %
3. Addition +, Subtraction -
Example with Explanation

int result = 10 + 5 * 2;

Result = 20, because multiplication is performed before addition.

int result = (10 + 5) * 2;

Result = 30, because parentheses change the order of execution.

Example Program

#include <stdio.h>

int main() {
int a = 10, b = 5, c = 2;
int result = a + b * c;
printf("%d", result);
return 0;
}

Conclusion

Arithmetic expressions are used to perform mathematical calculations in C


programs by combining operands and arithmetic operators following
precedence rules.

9. Declaring Variables in C Language


Definition

Declaring a variable in C language means specifying the data type and name of
the variable so that the compiler can allocate memory and understand how the
variable will be used in the program.
Purpose of Variable Declaration

 To inform the compiler about the type of data


 To allocate memory for the variable
 To ensure type checking during compilation

Syntax of Variable Declaration

data_type variable_name;
Example
int a;
float marks;
char grade;

Declaration with Initialization

A variable can be declared and initialized at the same time.

int x = 10;
float pi = 3.14;

Multiple Variable Declaration

Multiple variables of the same data type can be declared in one statement.

int a, b, c;

Rules for Declaring Variables

 Variable must be declared before use


 Variable name must follow identifier rules
 Keywords cannot be used as variable names
 Variable names are case-sensitive
Scope of Variables

 Variables declared inside a function are local variables


 Variables declared outside all functions are global variables

Example Program

#include <stdio.h>

int main() {
int num;
num = 5;
printf("%d", num);
return 0;
}

10. Data Type Conversion in C Language


Definition

Data type conversion in C language is the process of converting a value from one
data type to another during program execution. This conversion helps in
performing operations between different data types correctly.

Need for Data Type Conversion

 To perform operations between different data types


 To avoid data loss
 To get accurate results in expressions
 To make programs more flexible and reliable
Types of Data Type Conversion in C

1. Implicit Type Conversion (Automatic Conversion)

Definition

Implicit type conversion is performed automatically by the compiler when


different data types are used in an expression.

Features

 No programmer intervention required


 Lower data type is converted into higher data type
 Also known as type promotion

Example
int a = 10;
float b = 5.5;
float result = a + b;

Here, integer a is automatically converted into float.

2. Explicit Type Conversion (Type Casting)

Definition

Explicit type conversion is done manually by the programmer using a type casting
operator.

Syntax
(data_type) expression;
Example
int a = 5, b = 2;
float result = (float)a / b;

Without type casting, result would be 2, but after conversion it becomes 2.5.
Example Program

#include <stdio.h>

int main() {
int x = 7;
int y = 2;
float z;

z = (float)x / y;
printf("%f", z);
return 0;
}

Important Points

 Implicit conversion is handled by the compiler


 Explicit conversion is controlled by the programmer
 Type casting helps prevent integer division problems
 Improper conversion may lead to loss of data

You might also like