0% found this document useful (0 votes)
35 views38 pages

Introduction C

Uploaded by

punny
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
0% found this document useful (0 votes)
35 views38 pages

Introduction C

Uploaded by

punny
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/ 38

Introduction

C
• C is a procedural programming language.
• It was initially developed by Dennis Ritchie in
the year 1972.
• Many later languages have borrowed
syntax/features directly or indirectly from the
C language. Like syntax of Java, PHP,
JavaScript, and many other languages are
mainly based on the C language. C++ is nearly
a superset of C language
Features
• Mother language
• System programming language
• Procedure-oriented programming language
• Structured programming language
• Mid-level programming language
• Header Files Inclusion: The first and foremost component is the
inclusion of the Header files in a C program.
A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several
source files.
Some of C Header files:
– stddef.h – Defines several useful types and macros.
– stdint.h – Defines exact width integer types.
– stdio.h – Defines core input and output functions
– stdlib.h – Defines numeric conversion functions, pseudo-random
network generator, memory allocation
– string.h – Defines string handling functions
– math.h – Defines common mathematical functions
– Main Method Declaration: The next part of a C
program is to declare the main() function. The
syntax to declare the main function is:
Syntax to Declare the main method:

• int main() {}
• Variable Declaration: It refers to the variables
that are to be used in the function. No
variable can be used without being declared.
Also in a C program, the variables are to be
declared before any operation in the function.
Example:

• int main() { int a; . .


• Body: The body of a function in the C
program, refers to the operations that are
performed in the functions. It can be anything
like manipulations, searching, sorting,
printing, etc.
Example:

• int main() { int a; printf("%d", a); . .


• Return Statement: The last part of any C program is the
return statement. The return statement refers to the
returning of the values from a function. This return
statement and return value depend upon the return type
of the function. For example, if the return type is void,
then there will be no return statement. In any other case,
there will be a return statement and the return value will
be of the type of the specified return type.
Example:

• int main() { int a; printf("%d", a); return 0; }


Output (Print Text)

• To output values or print text in C, we have to use


the printf() function
• It prints the given statement to the console.
• Example
• #include <stdio.h>

int main() {
printf("Hello World!");
return 0;
}
Input (scanf())

• The scanf() function is used for input. It reads the input data from
the console.
• scanf("format string",argument_list);
• Example
• #include<stdio.h>
• int main(){
• int number;
• printf("enter a number:");
• scanf("%d",&number);
• printf("cube of number is:%d ",number*number*number);
• return 0;
• }
Format Specifier
• Format specifiers in C are used to take inputs
and print the output of a type. The symbol we
use in every format specifier is %. Format
specifiers tell the compiler about the type of
data that must be given or input and the
type of data that must be printed on the
screen.
Prints Addition Of 2 Numbers.

• #include<stdio.h>
• int main(){
• int x=0,y=0,result=0;

• printf("enter first number:");
• scanf("%d",&x);
• printf("enter second number:");
• scanf("%d",&y);

• result=x+y;
• printf("sum of 2 numbers:%d ",result);

• return 0;
• }
Variables
• A variable is a name of the memory location.
It is used to store data.
• Its value can be changed, and it can be reused
many times.
Rules for defining variables

• A variable can have alphabets, digits, and


underscore.
• A variable name can start with the alphabet,
and underscore only. It can't start with a digit.
• No whitespace is allowed within the variable
name.
• A variable name must not be any reserved
word or keyword, e.g. int, float, etc.
• Valid variable names:
 int a;
 int _ab;
 int a30;
• Invalid variable names:
 int 2;
 int a b;
 int long;
Types of Variables in C

• There are many types of variables in c:


• local variable
• global variable
• static variable
• automatic variable
• external variable
DataType
• Each variable in C has an associated data type.
Each data type requires different amounts of
memory and has some specific operations which
can be performed over it.
• -It specifies the type of data that the variable can
store like integer, character, floating, double, etc.
• -The data type is a collection of data with values
having fixed values, meaning as well as its
characteristics.
To Do
• 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 - stores single characters, such as 'a' or 'B'.
Char values are surrounded by single quotes
// C program to print Integer data types.

#include <stdio.h>

int main()
{
// Integer value with positive data.
int a = 9;

// integer value with negative data.


int b = -9;

// U or u is Used for Unsigned int in C.


int c = 89U;

// L or l is used for long int in C.


long int d = 99998L;

printf("Integer value with positive data: %d\n", a);


printf("Integer value with negative data: %d\n", b);
printf("Integer value with an unsigned int data: %u\n", c);
printf("Integer value with an long int data: %ld", d);

return 0;
}
Operators

• Operators are used to perform operations on


variables and values.
• operators as symbols that help us to perform
specific mathematical and logical
computations on operands
• c = a + b;
• ‘+’ is the operator known as the addition
operator and ‘a’ and ‘b’ are operands. Thus an
operator operates the operands
• C has many built-in operators and can be classified into
6 types:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Other Operators
// C Program to Demonstrate the working concept of
// Operators
#include <stdio.h>

int main()
{
int a = 10, b = 5;
// Arithmetic operators
printf("Following are the Arithmetic operators in C\n");
printf("The value of a + b is %d\n", a + b);
printf("The value of a - b is %d\n", a - b);
printf("The value of a * b is %d\n", a * b);
printf("The value of a / b is %d\n", a / b);
printf("The value of a % b is %d\n", a % b);
printf("The value of a++ is %d\n",a++); // First print (a) and then increment it
printf("The value of a-- is %d\n",a--); // First print (a) and then decrease it
// by 1
printf("The value of ++a is %d\n",++a); // Increment (a) by (a+1) and then print
printf("The value of --a is %d\n",--a); // Decrement (a+1) by (a) and then print

// Assignment Operators --> used to assign values to


// variables int a =3, b=9; char d='d';

// Comparison operators
// Output of all these comparison operators will be (1)
// if it is true and (0) if it is false
printf("\nFollowing are the comparison operators in C\n");
printf("The value of a == b is %d\n", (a == b));
printf("The value of a != b is %d\n", (a != b));
printf("The value of a >= b is %d\n", (a >= b));
printf("The value of a <= b is %d\n", (a <= b));
printf("The value of a > b is %d\n", (a > b));
printf("The value of a < b is %d\n", (a < b));
// Logical operators
printf("\nFollowing are the logical operators in C\n");
printf("The value of this logical and operator ((a==b) "
"&& (a<b)) is:%d\n",
((a == b) && (a < b)));
printf("The value of this logical or operator ((a==b) "
"|| (a<b)) is:%d\n",
((a == b) || (a < b)));
printf("The value of this logical not operator "
"(!(a==b)) is:%d\n",
(!(a == b)));

return 0;
Precedence
• Operator precedence determines the grouping of
terms in an expression and decides how an
expression is evaluated. Certain operators have
higher precedence than others; for example, the
multiplication operator has a higher precedence
than the addition operator.
• Operator precedence determines which
operation is performed first in an expression with
more than one operators with different
precedence.
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”.
Write a C program to input principle, time and rate (P, T, R) from user and find
Simple Interest. How to calculate simple interest in C programming. Logic to
find simple interest in C program.

• Simple Interest formula


• Simple interest formula is given by.
SI = (P*R*T)/100
• Where,
P is the principle amount
T is the time and
R is the rate
Algorithm
• Step by step descriptive logic to calculate simple
interest.
• Input principle amount in some variable
say principle.
• Input time in some variable say time.
• Input rate in some variable say rate.
• Find simple interest using formula SI = (principle
* time * rate) / 100.
• Finally, print the resultant value of SI.
#include <stdio.h>

int main() {

float principle, time, rate, SI;


/* Input principle, rate and time */
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);

/* Calculate simple interest */


SI = (principle * time * rate) / 100;
/* Print the resultant value of SI */
printf("Simple Interest = %f", SI);

return 0; }
Thanks

You might also like