0% found this document useful (0 votes)
71 views20 pages

C Constants and I/O Operations Guide

Constants in C are values that cannot be changed, like numbers, characters, and strings. There are two ways to define constants in C - using the #define preprocessor or the const keyword. Format specifiers are used in input/output functions to determine the format of data being input or output, such as %d for integers, %f for floats, and %c for characters.
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)
71 views20 pages

C Constants and I/O Operations Guide

Constants in C are values that cannot be changed, like numbers, characters, and strings. There are two ways to define constants in C - using the #define preprocessor or the const keyword. Format specifiers are used in input/output functions to determine the format of data being input or output, such as %d for integers, %f for floats, and %c for characters.
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

Constants in C

A constant is a value or variable that can't be changed in the

program, for example: 10, 20, 'a', 3.4, "c programming" etc.

There are two simple ways in C to define constants


Using #define preprocessor.

Syntax:

#define identifier value

Example

#define LENGTH 10

#define WIDTH 5
Using const keyword.

Syntax:
const type var
const int LENGTH = 10;
const int WIDTH = 5;
Format specifier
• The Format specifier is a string used in
the formatted input and output
functions.
• The format string determines the format
of the input and output.
• The format string always starts with a
'%' character.
EXAMPLE:
%d - int
%f - float
%c - char
EXAMP
LE
#include <stdio.h>
void main()
{
int sample = 5;
printf("Number =
%d",sample);
}

• Output:Numbe
r=5
• We use %d
format specifier
to print int
types.
• Here, the %d
Example 3: float and double
Output
#include <stdio.h>
void main()
{
float number1 = 13.5;
double number2 = 12.4;
printf("number1 = %f\
n", number1);
printf("number2 = %lf",
number2);
}
Print Characters
#include <stdio.h>
void main()
{
char chr = 'a';
printf("character = %c", chr);
}
Output
character = a
To print char, we use %c
format specifier
INPUT OUTPUT (I/O) STATEMENTS
 scanf() function - to take input from the user,
Syntax: scanf(“%X”, &variableOfXType);
where %X is the format specifier in c
& is the address operator in C.

EXAMPLE:
#include <stdio.h>
void main()
{
int a;
// Displays the string inside quotations
printf("C Programming");
//getting input from the user
scanf(“%d”,&a);
printf(“Given data is %d”,a);
}
C Input
Example 6: Float and Double Input/Output
#include <stdio.h>
int main()
{
float num1;
double num2;
printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another
number: ");
scanf("%lf", &num2);
printf("num1 = %f\n", numprintf("num2 = %lf",
num2);
return 0;}
C Input
C Character I/O
#include <stdio.h>
void main()
{
char chr;
printf("Enter a
character: ");
scanf("%c",&chr);
printf("You
entered %c", chr);
}
Output
Enter a character: g
You entered g.
ASCII
Value
#include <stdio.h>
int main()
{
char chr;
printf("Enter a
character: ");
scanf("%c", &chr);
// When %c is used, a character is displayed
printf("You entered %c.\n",chr);
/*When %d is used, ASCII value is displayed */
printf("ASCII value is % d.", chr);
return 0;
}
Output
Enter a character: g
You entered g.
ASCII value is 103.
I/O Multiple
Values
Here's how you can take multiple inputs from the user and displaythem.
#include <stdio.h>
int main()
{
int a;
float b;
printf("
Enter
integer
and
then a
float:
");
// Taking multiple inputs Page13
scanf("%d%f", &a, &b);
printf("You entered %d and %f",
a, b);
return 0;
}
Output
Type Conversion in
C
• The type conversion process in C is basically
converting one type of data type to other to
perform some operation.

• The conversion is done only between those


datatypes wherein the conversion is possible

• EX – char to int and vice versa.

• There are two types of type conversion


1) Implicit Type Conversion
2) Explicit Type Conversion
1.Implicit Type Conversion
• This type of conversion is usually performed by the
compiler when necessary without any commands
by the user. Thus it is also called "Automatic
Type Conversion".

• The compiler usually performs this type of


conversion when a particular expression contains
more than one data type.

• In such cases either type promotion or demotion


takes place.
Whenever the compiler deals with different data types in an
expression, the operand which is present at the lower rank
will be converted to the corresponding datatype of the
operand with the higher rank.
Exampl
e
int a = 20;
double b = 20.5;
a + b;

 Here, first operand is int type and other is of


type double.

 So, as per rule 2, the variable a will be converted


to double.

 Therefore, the final answer is double a + b =

40.500000.
2) Explicit Type
Conversion
• Explicit type conversion rules out
the use of compiler for converting
one data type to another instead
the user explicitly defines within the
program the datatype of the
operands in the expression.
2) Explicit Type
Conversion

Output:
1. Thus, in the above example we find that the output result is
12 because in the result expression the user has explicitly
defined the operands (variables) as integer data type.
2. Hence, there is no implicit conversion of data type by the
compiler.If in case implicit conversion was used the result
would be 13.
C - Type Casting

• Type casting is a way to convert a variable


from one data type to another data type.

• For example, if you want to store a 'long'


value into a simple integer then you can type
cast 'long' to 'int'.

• You can convert the values from one type to


another explicitly using the cast operator as
follows −

• (type_name) expression
Example:

#include <stdio.h>
main() {
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );
}
• When the above code is compiled and
executed, it produces the following result
• Value of mean : 3.400000
Integer Promotion
Integer promotion is the process by which values of integer
type "smaller" than int or unsigned int are converted either
to int or unsigned int.

#include <stdio.h>
main() {
int i = 17;
char c = 'c'; /*
ascii value is 99 */
int sum;
sum = i + c;
printf("Value of
sum : %d\n",
sum );
}

Value of sum : 116

You might also like