0% found this document useful (0 votes)
7 views48 pages

Fundamentals in C - Day1 To 3

This document serves as a beginner-friendly guide to C programming, covering fundamental concepts such as data types, variables, operators, control structures, functions, arrays, pointers, and file I/O. It emphasizes the importance of C in modern computing and provides practical examples to help learners build a solid foundation. Additionally, it discusses typecasting, its necessity, and best practices to avoid common pitfalls.
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)
7 views48 pages

Fundamentals in C - Day1 To 3

This document serves as a beginner-friendly guide to C programming, covering fundamental concepts such as data types, variables, operators, control structures, functions, arrays, pointers, and file I/O. It emphasizes the importance of C in modern computing and provides practical examples to help learners build a solid foundation. Additionally, it discusses typecasting, its necessity, and best practices to avoid common pitfalls.
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/ 48

Basic C Programming

Welcome to a beginner-friendly guide to C programming! This presentation will


cover the fundamentals of C, starting with basic concepts and leading to practical
examples. By the end, you'll have a solid foundation to write your own C programs.

By: preencoded.png

Keerthik D U
Introduction to C
What is C? C's Importance

C is a powerful, general-purpose programming language that is C is considered the foundation for many other languages and has
widely used in various fields, from operating systems to embedded played a pivotal role in the development of modern computing. It is
systems. It's known for its efficiency, control over hardware, and widely used in applications such as operating systems, databases, and
portability. compilers.

preencoded.png
Data Types and Variables

int float
Stores whole numbers, both Stores decimal numbers, like 3.14,
positive and negative, like 10, -5, 0. 2.718, -0.5.

char Variable Declaration


Stores single characters, like 'A', 'b', Variables are used to store data.
'!', '?'. Use a data type, followed by the
variable name, like 'int age;'.

preencoded.png
Operators and Expressions

Arithmetic Operators Assignment Operator


+, -, *, /, % for addition, subtraction, = for assigning a value to a variable, like
multiplication, division, and modulo, 'age = 25;'.
respectively.

Comparison Operators Logical Operators


==, !=, <, >, <=, >= for comparing values && (AND), || (OR), ! (NOT) for
and evaluating to true or false. combining conditions.
preencoded.png
Control Structures
1 if/else Statements
Execute different blocks of code based on a condition. For
example, 'if (age >= 18) { ... } else { ... }'.

2 for Loops
Iterate through a sequence of values a predetermined number of
times. Example: 'for (int i = 0; i < 10; i++) { ... }'.

3 while Loops
Repeat a block of code as long as a condition remains true.
Example: 'while (condition) { ... }'.

preencoded.png
Functions and Scope
Function Definition
A block of code designed to perform a specific task, taking inputs
and potentially returning a value. Example: 'int add(int a, int b) {
return a + b; }'.

Function Call
Executing a function by its name and passing arguments. Example:
'int sum = add(5, 3);'.

Scope
The region of a program where a variable is accessible. Variables
declared within a function have local scope, while variables
declared outside have global scope.

preencoded.png
Arrays and Pointers
Arrays
Store collections of elements of the same data type, accessed by an index. Example: 'int
1
numbers[5] = {1, 2, 3, 4, 5};'.

Pointers
2 Variables that store memory addresses. Useful for directly manipulating data in
memory and working with arrays efficiently.

Pointers and Arrays


3 Pointers can be used to access elements within arrays and perform
operations directly on the array's memory location.

preencoded.png
File I/O
File Opening
1
Use fopen() function to open a file for reading, writing, or appending. Example: 'FILE *fp = fopen("data.txt", "r");'.

File Reading
2
Use fscanf() to read data from a file into variables. Example: 'fscanf(fp, "%d", &age);'.

File Writing
3
Use fprintf() to write data to a file. Example: 'fprintf(fp, "The age is %d\n", age);'.

File Closing
4 Always close the file after you're done using fclose() function. Example:
'fclose(fp);'.

preencoded.png
Conclusion and Next Steps

1 2
Start Practicing Explore Resources
Experiment with C code, write small Find online tutorials, books, and coding
programs, and explore different aspects of communities to learn more about C.
the language.

3
Build Projects
Apply your knowledge by building simple
projects, such as games or data processing
tools.

preencoded.png
By:
Keerthik D U
Task
1)
#include <stdio.h>
int main() {
float pi = 3.14;
printf("Value of pi: %d\n", pi);
return 0;
}

2)
#include <stdio.h>
int main() {
int x = 5
printf("Value of x: %d\n", x);
return 0;
}

3)
#include <stdio.h>
int main() {
x = 10;
printf("Value of x: %d\n", x);
return 0;
}
4)
#include <math.h>
int main() {
printf("Hello, World!\n");
int x = 5
printf("Value of x: %d\n", x);
return 0;
}

5)
#include <stdio.h>
int main() {
int num1 = 5;
int num2 = 10;
printf("Sum: %d\n", Num1 + num2);
return 0;
}

6)
#include <stdio.h>
int main() {
int x;
x = "Hello";
printf("Value of x: %d\n", x);
return 0; }
Operators in C Programming

Operators are the foundation of C programming, allowing us to manipulate and


combine data in powerful ways.

By: preencoded.png

Keerthik D U
Introduction to Operators
Building Blocks Types of Operators

Operators are special symbols that perform specific operations on C supports a wide variety of operators, including arithmetic,
operands (values or variables). They act as verbs in the C relational, logical, assignment, increment/decrement, bitwise, and
programming language, dictating the actions taken on data. conditional. Each type fulfills a different purpose, allowing for
diverse and complex operations.

preencoded.png
Arithmetic Operators
Addition (+) Subtraction (-)
Adds two operands together. Subtracts the second operand from
the first operand.

Multiplication (*) Division (/)


Multiplies two operands together. Divides the first operand by the
second operand.

preencoded.png
Relational Operators

Greater Than ( > ) Less Than ( < )


Returns true if the first operand is Returns true if the first operand is less
greater than the second operand. than the second operand.

Equal To ( == ) Not Equal To ( != )


Returns true if both operands are equal. Returns true if both operands are not
equal.

preencoded.png
Logical Operators
1 Logical AND ( && ) 2 Logical OR ( || ) 3 Logical NOT ( ! )
Returns true if both operands are true. Returns true if at least one operand is Inverts the logical state of the
true. operand.

preencoded.png
Assignment Operators
Simple Assignment ( = ) Compound Assignment ( +=, -=, *=, /= )

Assigns the value of the right operand to the left operand. Combines an arithmetic operation with assignment, simplifying
common operations.

preencoded.png
Increment and Decrement Operators
Increment ( ++ )
Increases the value of the operand by one.

1 2

Decrement ( -- )
Decreases the value of the operand by one.
preencoded.png
Bitwise Operators
Bitwise AND ( & )
Performs a bitwise AND operation on each bit of the operands.

Bitwise OR ( | )
Performs a bitwise OR operation on each bit of the operands.

Bitwise XOR ( ^ )
Performs a bitwise XOR operation on each bit of the operands.

Bitwise NOT ( ~ )
Flips each bit of the operand (0 to 1 and 1 to 0).

Left Shift ( << )


Shifts the bits of the operand to the left, filling empty bits with zeros.

Right Shift ( >> )


Shifts the bits of the operand to the right, filling empty bits with zeros or ones, depending on the sign.
preencoded.png
Conditional (Ternary) Operator

Ternary Operator
1 Provides a concise way to express conditional logic.

Syntax
2
condition ? expression1 : expression2

Evaluation
3 If the condition is true, expression1 is evaluated. Otherwise,
expression2 is evaluated.

preencoded.png
Typecasting in C Programming

Typecasting is a powerful feature in C, allowing you to control how data


is interpreted and manipulated.

By: preencoded.png

Keerthik D U
What is Typecasting?
Typecasting, also known as type conversion, is the process of For instance, you can convert an integer variable to a floating-point
converting a variable of one data type to another. It involves changing variable, allowing you to perform calculations with decimal values.
the way the computer interprets the data's representation in memory.
Syntax:
int x;
float y;
y = (float) x;

preencoded.png
Need for Typecasting
Data Compatibility Precision Control
Typecasting is crucial when you It allows you to increase or
need to perform operations on decrease the precision of a value by
variables of different data types, converting it to a data type that can
ensuring compatibility and accommodate the desired level of
preventing errors. detail.

Flexibility and Efficiency


Typecasting enables you to use variables in ways that might not be explicitly
supported by their original data types, optimizing code logic.

preencoded.png
Implicit Typecasting
Implicit typecasting, also known as automatic type conversion, occurs when the
compiler automatically converts a variable from one data type to another. This
happens based on the rules of the C language.

preencoded.png
Explicit Typecasting

Manual Conversion Syntax


Explicit typecasting is when you You use the typecast operator, which is
explicitly tell the compiler to convert a a parenthesis with the target data type
variable from one data type to another inside, followed by the variable to be
using a typecast operator. converted.

preencoded.png
Dangers of Improper Typecasting

Data Loss
Converting a larger data type to a smaller one can result in data loss, as the smaller type may
1 not be able to hold all the information.

Overflow
If the target data type can't accommodate the value after conversion, overflow can
2
occur, leading to unexpected results or even program crashes.

Undefined Behavior
In some cases, improper typecasting can lead to undefined behavior,
3
making the program's output unpredictable and unreliable.

preencoded.png
Integer Promotions
Char to Int
1
A char variable is automatically promoted to an int type when used in an expression with integers.

Short to Int
2 A short int variable is also promoted to an int type in most arithmetic operations involving
integers.

Why Promotions?
These promotions ensure that the result of calculations involving
3
smaller integers is always stored in a sufficiently large integer type.

preencoded.png
Signed vs Unsigned Integers

1 2
Signed Unsigned
Used for both positive and negative integers. Used for only non-negative integers.

3
Typecasting Impact
Typecasting between signed and unsigned
integers can result in unexpected values due
to different interpretations of the bits.

preencoded.png
Best Practices for Typecasting
Use Explicitly
Always use explicit typecasting whenever possible, making your code
more readable and less prone to errors.

Consider Data Loss


Be mindful of potential data loss when converting between different
data types, especially from larger to smaller types.

Document Clearly
If you perform typecasting, document the reason for the conversion
and its intended outcome in comments.

Test Thoroughly
After using typecasting, test your code thoroughly to ensure it behaves
as expected and handles potential edge cases.

preencoded.png
Task
1)
#include <stdio.h>
int main() {
int num = 5;
if (num & 1 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}

2)
#include <stdio.h>
int main() {
int a = 5, b = 2;
float result = a / b;
printf("Result: %.2f\n", result);
return 0;
}
3)
#include <stdio.h>

int main() {
int a = 10;
float b = 3.5;
int result = a + b;
printf("Result: %d\n", result);
return 0;
}

4)
#include <stdio.h>
int main() {
int a = 5;
if (a = 0) {
printf("a is zero.\n");
} else {
printf("a is not zero.\n");
}
return 0;
}
5)
#include <stdio.h>
int main()
{
char a = 30, b = 40, c = 10;
char d = (a * b) / c;
printf ("%d ", d);
return 0;
}

You might also like