0% found this document useful (0 votes)
48 views

3-Elements of C-31-07-2023

Elements of C expressions can include operators, constants, and variables. There are different types of expressions such as constant expressions containing only constants, floating point expressions that produce floating point results, and relational expressions that yield boolean results. Operator precedence and associativity determine the order of operations in an expression. Type conversion, either implicit or explicit, may occur when different data types are combined in an expression. Common I/O statements in C include printf() for output and scanf() for input, along with unformatted functions like getchar(), putchar(), gets(), and puts().

Uploaded by

Dhruv Gupta
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)
48 views

3-Elements of C-31-07-2023

Elements of C expressions can include operators, constants, and variables. There are different types of expressions such as constant expressions containing only constants, floating point expressions that produce floating point results, and relational expressions that yield boolean results. Operator precedence and associativity determine the order of operations in an expression. Type conversion, either implicit or explicit, may occur when different data types are combined in an expression. Common I/O statements in C include printf() for output and scanf() for input, along with unformatted functions like getchar(), putchar(), gets(), and puts().

Uploaded by

Dhruv Gupta
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/ 31

Elements of C

1
7. EXPRESSION
(VII) Expression
 It is a combination of operators, constants and variables.
Types of expression
ExpressionType Description with Example
Constant expression Expression with only constant values.
Ex: 10 + 5 / 6.0
Floating expression Produce floating point results
Ex: x + y, 10.75
NOTE: x, y are the floating point variables
Relational expression Yield results of type boolean type (0 or 1)
Ex: x <= y, x + y > 2
Logical expression Combine two or more relational expressions and produces
boolean type results.
Ex: x > y && x == 10, x == 10 || y == 5
Bitwise expression Manipulate data at bit level.
Ex: x << 3, y >> 1
OPERATOR
PRECEDENCE
Operator Precedence
 Tells the order of evaluating an expression.
 Priority (precedence) & associativity.

Note: operators with the highest priority (precedence) appear at the top
of the table & lowest priority appear at the bottom.
Operator Precedence
Example - 1
int var = 15 - 4 * 3;
Example - 2
Quiz - ?
 Evaluate the following:
 (10 - 4) + (20 / (2 * 5)) * 3
 7 + 2 * 4 - 3;
 1 == 3 != 5;
 ++a * (3 + 8) % 35 - 28 / 7, assume a = 5
 3<<1+2*3/7+x++,assume x=2
Example
// OperPrecAsso.c
#include<stdio.h> OUTPUT:
void main() x=12
{ y=12
z=1
int x=(10 - 4) + (20 / (2 * 5)) * 3; b=27
int y=7 + 2 * 4 - 3;
int z=1 == 3 != 5;
int a=5;
int b = ++a * (3 + 8) % 35 - 28 / 7;
printf("x=%d\n",x);
printf("y=%d\n",y);
printf("z=%d\n",z);
printf("b=%d\n",b);
}
TYPE CONVERSION
Type Conversion
 Takes place when two or more data type present in an
expression.
 Process of converting one data type to another.
 Performed by a compiler.
 NOTE:The destination data type can‟t be smaller than the
source data type.
 Types:
 Implicit type conversion
 Explicit type conversion
(a) Implicit Type conversion
(or) automatic type conversion
 Done by the compiler on its own, without any external
trigger from the user.
 All the data types of the variables are upgraded to the data
type of the variable with the largest data type.

bool  char  short int  int  unsigned int  float 


double  long double
Variable implicit type conversion in
expressions
Example
// Imp_TypeConversion.c
#include <stdio.h> Output:
void main() X = 107
Z=108.000000
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII value of 'a' is 97
x = x + y;
printf("x = %d, z = %f", x, z);
// x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
}
(b) Explicit type conversion (or)
Type casting
 User-defined.
 The user can perform the type casting of the result.
 Syntax:
(type) Expression;
 Example:
(int) x;
Type casting: Explicit
 In implicit type conversion, the data type is converted
automatically.
int result, var1=10, var2=3;
result=var1/var2;
In this case, after the division performed on variables var1 and var2
the result stored in the variable “result” will be in an integer format.
The value stored in the variable “result” loses its meaning because it
does not consider the fraction part which is normally obtained in the
division of two numbers.
To force the type conversion in such situations, we use explicit type
casting.
(type-name) expression
Example
// ExplicitTypeConv.c
OUTPUT:
#include<stdio.h> sum = 2
void main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
}
Quiz?
Quiz?
 From the given statements find the implicit and explicit type
conversions:
 double var1 = 4+75.12;
 b=145.33; x=(int) b;
 int a = (int) c % (int) b;
 (char) c
IO STATEMENTS
I/O Statements
 The I/O library functions are listed in the “header” file
<stdio.h> .
 Take the data through input functions.
 Sends results through output functions.
Formatted Input/Output Functions
 Formatted I/O functions are used to take various inputs
from the user and display multiple outputs to the user.
 These types of I/O functions can help to display the
output to the user in different formats using the format
specifiers. These I/O supports all data types like int,
float, char, and many more.
 printf() - output function
 scanf() - input function
scanf()
 Predefined input function, reads all types of data values given
by the user.
 It requires the format specifiers such as %s and %d to
identify the data type to be read during the program
execution.
 It is required to pass the address of the variable using „&‟ so
that read values can be assigned to the correct destination.
 Syntax:
scanf("format string",&argument_list);
printf()
 Predefined output function, prints all types of data value to
the standard output.
 Syntax:
printf("format string",argument_list);
Example
// find cube of the given number
#include<stdio.h>
void main()
{
int number;
printf("enter a number:");
scanf("%d", &number);
printf("cube of number is:%d ",number*number*number);
getch();
}
Unformatted
IO Description
IO functions
Syntax Program
&Example
getchar() Reads a single character from variable-name = #include<stdio.h>
the input, but does not return getchar(); void main()
the character to the program {
until the enter key is pressed. Example:- char c;
char c; printf("Enter a character");
c = getchar(); c=getchar();
printf("c = %c ",c);
}
putchar() Writes a single character on putchar( variable- #include<stdio.h>
the output. name); void main()
Example: {
char ch = 'C'; char ch;
putchar(ch); printf("Enter a character: ");
scanf("%c", &ch);
putchar(ch);
}
Unformatted IO functions
IO Description Syntax Program
&Example
gets() • Read the string from input gets(variable) char ch[20];
and places it into some gets(ch);
buffer which the
programmer must provide.
• It stops reading when an
enter key is pressed or EOF
is reached.
puts() • Send the string to the puts(variable) char ch[20];
output, until it finds a NULL puts(ch);
end of string marker
Example
#include<stdio.h>
void main()
{
char ch[20];
char a;
a = getchar();
putchar(a);
printf("Enter the text: ");
gets(ch);
puts(ch);
}
Delimiters

You might also like