100% found this document useful (1 vote)
417 views47 pages

C Programming

This presentation introduces the fundamentals of C programming with practical examples and easy explanations. Overview of C language, history, and features. Structure of a C program with sample codes. C tokens: keywords, identifiers, constants, and variables. Data types and declaration rules. Managing input/output functions and operators. Operators and expressions in C. Decision-making and branching: IF statements, Switch, and GOTO. Example programs and quizzes for practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
417 views47 pages

C Programming

This presentation introduces the fundamentals of C programming with practical examples and easy explanations. Overview of C language, history, and features. Structure of a C program with sample codes. C tokens: keywords, identifiers, constants, and variables. Data types and declaration rules. Managing input/output functions and operators. Operators and expressions in C. Decision-making and branching: IF statements, Switch, and GOTO. Example programs and quizzes for practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

C Programming

UNIT I
Presented By
Mrs.S.Geethamani
Assistant Professor
Dept. of Computer Application
Sri Ramakrishna College of Arts & Science for
Women,
CBE
Overview

1. Introduction
Basic Structure of C
C Tokens
Keywords & Identifiers
Constants & Variables
Data Types & Declarations
2. Managing Input & Output
Input / Output Functions
Operators
3. Operators & Expressions
4. Decision Making & Branching
IF Statements (Types)
Switch Statement
GOTO Statement
what is
programming
The process of creating a set of instructions
that tell a computer how to perform a specific
task or solve a problem
Introduction

C Programming
• C is a middle level procedural programming language.
• developed by Dennis Ritchie in 1972 at Bell Laboratories of
AT&T Labs to build the UNIX operating system.
• It provides low-level memory access, high performance, and
portability.
Our Mission
• C has evolved through standards like ANSI C, C99, C11, and
C23.
• it remains widely used in operating systems, embedded
systems, compilers, databases, networking, game engines,
and real-time systems for its efficiency and hardware-level
control.
Structure of the
C program
#include <stdio.h>
int main()
{
// Our first basic program in C
printf("Hello World!\n\n");
return 0;
}
C program to
Right-Angled Triangle Square
display your name
of * of *
and address

#include <stdio.h>
#include <stdio.h> #include <stdio.h>

int main() {
int main() { int main() {
// Display Name and
Address printf("*\n"); printf("* * * * *\
printf("Name : John printf("* *\n"); n");
Doe\n"); printf("* * *\n"); printf("* * * * *\
printf("Address: 123, printf("* * * *\n"); n");
Main Street, Cityville\n"); printf("* * * * *\ printf("* * * * *\
n"); n");
return 0;
return 0; printf("* * * * *\
} n");
}
printf("* * * * *\
Keywords in C
• Keywords are reserved words in C with predefined
meanings.
• You cannot use them as variable names or identifiers.
Identifiers in
C
• Identifiers are the names you give to variables,
functions, arrays, etc.
• You define them.

Example

int age; // 'age' is an identifier


float salary; // 'salary' is an identifier

Rules for Identifiers

• Must begin with letter (A-Z or a-z) or


underscore (_)
• Can contain letters, digits (0-9), underscore
• Cannot be a keyword
• Case-sensitive (Age and age are different)
Types of Constants
Constants in C:
Integer Constants

in C • Whole numbers without a fractional part


• Examples: 10, -25, 0

Constants are fixed values Floating-point


that do not change during Constants
• Numbers with a decimal point or in exponential
form
the execution of a C • Examples: 3.14, -0.001, 2.5e3
program. They are also Character Constants
called literals. • Single character enclosed in single quotes
• Examples: 'A', '9', '#'

String Constants
• Sequence of characters enclosed in double
quotes
• Examples: "Hello", "123", "C Programming"

Enumeration
Constants
• Named integer constants declared using enum
Data types in C

Data types in C define


the type of values a
variable can store and
the amount of memory it
occupies.
Declaration of data types
VARIABLES IN
C
A variable is a named memory location used to store a
value of a specific data type.
Rules for using Variables
• Names can contain letters,
digits and underscores

• Names must begin with a letter


or an underscore (_)

• Names are case-sensitive (myVar and


myvar are different variables)

• Names cannot contain whitespaces


or special characters like !, #, %, etc

• Reserved words (such as int) cannot


be used as names
Input and
Output
Operators
Example Program

#include <stdio.h>

int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n",
age);
return 0;
}
Operators and
AGENDA
Expressions

Operators in C are symbols that instruct the compiler to perform


specific mathematical, relational, logical, or bitwise operations on
operands. They are fundamental building blocks for creating
expressions and controlling program flow.
OPERATORS IN C
IMPLEMENTATION
PLAN
Arithmetic
Operators
Relational
Operators
Logical
Operator
Bitwise
Operators
The conditional operator in C is also called
the ternary operator because it takes three
operands.
Conditional Operator
SYNTAX
in C
condition ? expression_if_true : expression_if_false;

#include <stdio.h>

int main() {
int a, b, max;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

// Using conditional operator


max = (a > b) ? a : b;

printf("The greater number is: %d\n",


max);

return 0;
Decision Making and
Branching
What is Decision Making
Decision making and branching in programming
allow a program to execute different blocks of
code based on specific conditions.
Decision Making and
Branching
Types of IF
statements
IF statements
in C

• if statements are used


for decision-making and
controlling the flow of SYNTAX:
execution based on if (condition)
conditions. {
• It allows the computer
// Code to be
to evaluate the
executed if condition
expression first and then
depending on whether is true
the value of expression }
or condition is true or
false, it transfers the
control to particular
Example 1:

#include <stdio.h>
#include<conio.h>
void main()
{
int age = 18; OUTPUT
if (age >= 18)
printf("You are You are eligible to
eligible to vote."); vote.
getch();
}
IF-ELSE
Statement
It provides an alternative block of code to
execute when the if condition is false.
Example 1:

#include <stdio.h>
#include<conio.h>
void main()
{
int age = 15;
if (age >= 18)
{
printf("You are eligible to OUTPUT
vote.");
}
You are not eligible to
else
printf("You are not eligible to vote.
vote.");
}
Else-if ladder
or SYNTAX:

if-else-if ladder if (condition1) {


// statements to execute if condition1 is
true }
• This is used to test a series of conditions sequentially.
else if (condition2)
The code block corresponding to the first true condition
is executed, and the rest of the ladder is skipped. {
// statements to execute if condition2 is
true
}
else if (condition3)
{
// statements to execute if condition3 is
true
}
else
{
// statements to execute if none of the
Program to relate two integers using =, > or <
symbol

#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

//checks if the two integers are equal. Enter two integers: 12


if(number1 == number2) {
printf("Result: %d = %d",number1,number2); 23
}

//checks if number1 is greater than number2. Result: 12 < 23


else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}

//checks if both test expressions are false


else {
printf("Result: %d < %d",number1, number2);
}

return 0;
Nested if-else
Statement
This involves placing an if or if-else statement inside
another if or else block. This allows for more complex
decision-making based on multiple levels of conditions.
Syntax
if (outer_condition)
{
if (inner_condition)
{
// Code for both conditions true
}
else
{
// Code for outer_condition true,
inner_condition false
}
}
else
{
// Code for outer_condition false
}
#include <stdio.h> Example Program
int main() {
int marks;

printf("Enter your marks: ");


scanf("%d", &marks);

// Outer if-else: Check if the student passed or failed


if (marks >= 50) { Enter your marks:
printf("You passed! ");
// Nested if-else: Determine the grade for passing 95
students
if (marks >= 90) {
You passed!
printf("Grade: A\n"); Grade: A
} else if (marks >= 70) {
printf("Grade: B\n"); or
} else {
printf("Grade: C\n"); Enter your marks:
}
} else { 60
// Else block for the outer if: Student failed
printf("You failed! Grade: F\n");
You passed!
} Grade: C
return 0;
switch Statement in C

• The switch statement is an alternative to the if-else-if ladder.


• It is used to perform different actions based on the value of a single
variable or expression.
• The value in the switch expression is compared with each case label.
• When a match is found, the corresponding block of code is executed.
• The break statement is used to exit the switch block after executing a
case.
• The default case is optional and runs if no case matches the expression.
• switch is generally used with int, char, or enumerated types in C (not with
float or string).
Syntax

switch (expression)
{
case value1:
// code to execute if expression == value1
break;
case value2:
// code to execute if expression == value2
break;
...
default:
// code to execute if no case matches
EXAMPLE PROGRAM
#include <stdio.h>

int main() {

// variable to be used in switch statement


int var = 18;

Output // declaring switch cases


switch (var) {
Eligible for vote case 15:
printf("You are a kid");
break;
case 18:
printf("Eligible for vote");
break;
default:
printf("Default Case is executed");
break;
}

return 0;
QUIZ TIME

For Your Attention

You might also like