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