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

1 (C Basic, Variables and Operators)

Basic C Language .....

Uploaded by

Santosh Amshala
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 views34 pages

1 (C Basic, Variables and Operators)

Basic C Language .....

Uploaded by

Santosh Amshala
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/ 34

Basic C

Topics

➢ Introduction to c
➢ Identifiers
➢ Variables
➢ Constants
➢ Keywords
➢ C Data Types
➢ Hierarchy of operations
➢ Example

https://www.primebitsolution.com/
History

Developed between 1969 and 1973 along with Unix.


Due mostly to Dennis Ritchie.

Designed for systems programming -


--> Operating systems.
--> Mobile devices
--> 3D computer games.
--> Hardware devices.
--> Common consumes devices like microwave ovens,
washing machines and digital cameras.

https://www.primebitsolution.com/
Identifiers

Identifiers or symbols are the names you supply for variables, types, functions, and labels in
your program.
Identifier names must differ in spelling and case from any keywords.

Rules for identifiers


1. Characters :- a-z , A-Z, 0 – 9 and _ .
2. First character must be a letter or an underscore .
3. Case sensitive.
4. Identifiers of length up to 31 characters are acceptable.

https://www.primebitsolution.com/
Identifiers

Some valid identifiers:


_Amount
delete_user( )
priciple012
Rate_of_interest

Some invalid identifiers:


9period
&discount
%rate!interest~
^income_

https://www.primebitsolution.com/
Variables
A variable can be considered as the name given to the location in
memory where this constant is stored the contents of a variable
can change.

Constants
A constant is a quantity that doesn’t change this quantity can be stored
at a location in the memory of the computer.

https://www.primebitsolution.com/
Keywords
Keywords are the words whose meaning has already been explained to the ‘C’
compiler. The keywords are also called as reserved words. There are 32
keywords available in ‘C’.

https://www.primebitsolution.com/
Data Types

https://www.primebitsolution.com/
Data Types and range
• Welcome to C!


Comments
– Text surrounded by /* and */ is ignored by computer
– Used to describe program

https://www.primebitsolution.com/

https://www.primebitsolution.com/

#include <stdio.h>
– Preprocessor directive - tells computer to load contents of a certain file
– <stdio.h> allows standard input/output operation

https://www.primebitsolution.com/

int main()
l
C++ programs contain one or more functions, exactly one of which must be
main
Parenthesis used to indicate a function

int means that main "returns" an integer value
Braces indicate a block
The bodies of all functions must be contained in braces

https://www.primebitsolution.com/

printf( "Welcome to C!\n" );

– Instructs computer to perform an action

– Specifically, prints string of characters within quotes

– Entire line called a statement

– All statements must end with a semicolon

– \ - escape character
– Indicates that printf should do something out of the ordinary
– \n is the newline character

https://www.primebitsolution.com/

return 0;
A way to exit a function

return 0, in this case, means that the program terminated normally
l
Right brace }
Indicates end of main has been reached

https://www.primebitsolution.com/
Input/Output function

To print the value of variable:-


I. To print message:-

printf(“ message ”);


II. To print the value of variable:-

printf(“format specifier”, variable);


To read the value of variable from terminal at runtime
scanf(“format specifier”, &variable);

Structure of a C program:-

Header file(#include<stdio.h>)
main()
{
data-type var1;
statements;
}
https://www.primebitsolution.com/
The printf() function has a special formatting character (%) -- a character
following this defines a certain format for a variable: -
printf(“%format”, variable);

Entering input using scanf():


This function can be used to enter any type of data like numeric,
character and string at runtime. The function returns the number of data
items that have been entered successfully.-
scanf(“format”,&variable);
Ex1: scanf( “%d”,&x );

– “%d” = input is an integer

– &x = store input in memory location


https://www.primebitsolution.com/
– named x
Ex2: scanf(``%c%d%f'',&ch,&i,&x);

https://www.primebitsolution.com/
Opearator and Operands

• Expressions
• Combination of Operators and Operands


Example 2*y+5

• Operands • Operators

https://www.primebitsolution.com/

4 Types

• Logical

• Arithmetic

• Bitwise

• Relational

https://www.primebitsolution.com/
● Mathematical expressions can be expressed in C using arithmetic operators


Examples

• ++i % 7

• 5 + (c = 3 + 8)

• a * (b + c/d)22

https://www.primebitsolution.com/
Relational Operators

▪ Test the relationship between two variables, or between a variable and a


constant


Relational Operators

https://www.primebitsolution.com/
Logical Operators

▪ Logical operators are symbols that are used to combine or negate


expressions containing relational operators

• Example: if (a>10) && (a<20)

https://www.primebitsolution.com/
Expressions that use logical operators return zero for false, and 1 for true

https://www.primebitsolution.com/
True Table

https://www.primebitsolution.com/
Bitwise Operators

• Processes data after converting number to its binary equivalent. (Bit


wise representation)

https://www.primebitsolution.com/
True Table

https://www.primebitsolution.com/
Precedence of operators

Precedence establishes the hierarchy of one set of operators


▪ over another when an arithmetic expression is to be evaluated

▪ It refers to the order in which C evaluates operators


The precedence of the operators can be altered by enclosing
▪ the expressions in parentheses
https://www.primebitsolution.com/
Code example

/* Calculation of simple interest */


#include<stdio.h>
main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n=3;
r = 8.5 ;
/* formula for simple interest */
si = (p * n * r) / 100 ;
printf ( “simple interest = %f" , si ) ;

}
Code example

#include<stdio.h>
Void main()
{
float p,n,r,si;

printf(“Enter values of Principle,NOI,ROI \n”);


scanf(“%f%f%f”,&p,&n,&r);

si=(p*n*r)/100;

printf(“simple interest=%f \n”);

}
Code example

#include<stdio.h>
void main()
{
int x, y;
float sum, difference;
printf("Enter two numbers \n ");
scanf("%d %d", &x, &y);
sum = x + y;
difference = x -
y;
printf("Sum = %f \n ",sum);
printf("Difference = %f \n ",difference);
}
Code example

#include<stdio.h>
void main()
{

int months, days;

printf("Enter number of days : ");


scanf("%d",&days);

months = days / 30;


days = days % 30;
printf("Months = %d \nDays = %d ",months,days);
}
THANK YOU

You might also like