1 (C Basic, Variables and Operators)
1 (C Basic, Variables and Operators)
Topics
➢ Introduction to c
➢ Identifiers
➢ Variables
➢ Constants
➢ Keywords
➢ C Data Types
➢ Hierarchy of operations
➢ Example
https://www.primebitsolution.com/
History
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.
https://www.primebitsolution.com/
Identifiers
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" );
– \ - 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
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);
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
●
Relational Operators
https://www.primebitsolution.com/
Logical Operators
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
https://www.primebitsolution.com/
True Table
https://www.primebitsolution.com/
Precedence of operators
}
Code example
#include<stdio.h>
Void main()
{
float p,n,r,si;
si=(p*n*r)/100;
}
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()
{