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/ 10
{
printf( "%c\n", (char)65 );
getchar();
or Type Casting - C Programming
Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float. Casting allows you to make this type conversion explicit, or to force it when it wouldn‟t normally happen.
Type conversion in c can be classified into the following two types:
1. Implicit Type Conversion
When the type conversion is performed automatically by the compiler without programmers intervention, such type of conversion is known as implicit type conversion or type promotion.
int x;
for(x=97; x<=122; x++)
printf("%c", x); /*Implicit casting from int to char thanks to %c*/
2. Explicit Type Conversion
The type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion. The explicit type conversion is also known as type casting.
C PROGRAMMING Page 51 Type casting in c is done in the following form:
(data_type)expression;
where, data_type is any valid c data type, and expression may be constant, variable or expression.
For example,
int x;
for(x=97; x<=122; x++)
printf("%c", (char)x); /*Explicit casting from int to char*/
The following rules have to be followed while converting the expression from one type to another to avoid the loss of information:
All integer types to be converted to float.
All float types to be converted to double.
All character types to be converted to integer.
Example
Consider the following code:
int x=7, y=5 ;
float z;
z=x/y; /*Here the value of z is 1*/
If we want to get the exact value of 7/5 then we need explicit casting from int to float:
C PROGRAMMING Page 52 int x=7, y=5;
float z;
z = (float)x/(float)y; /*Here the value of z is 1.4*/
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. Consider an example of adding a character with an integer −
#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 );
When the above code is compiled and executed, it produces the following result −
Value of sum : 116
Here, the value of sum is 116 because the compiler is doing integer promotion and converting the value of 'c' to ASCII before performing the actual addition operation.
Usual Arithmetic Conversion
C PROGRAMMING Page 53 The usual arithmetic conversions are implicitly performed to cast their values to a common type. The compiler first performs integer promotion; if the operands still have different types, then they are converted to the type that appears highest in the following hierarchy –
UNIT II
STATEMENTS
A statement causes the computer to carry out some definite action. There are three different classes of statements in C:
Expression statements, Compound statements, and Control statements.
C PROGRAMMING Page 54 Null statement
A null statement consisting of only a semicolon and performs no operations. It can appear wherever a statement is expected. Nothing happens when a null statement is executed.
Syntax: - ;
Statements such as do, for, if, and while require that an executable statement appear as the statement body. The null statement satisfies the syntax requirement in cases that do not need a substantive statement body.
The Null statement is nothing but, there is no body within loop or any other statements in C.
Example illustrates the null statement:
for ( i = 0; i < 10; i++) ;
or
for (i=0;i<10;i++)
C PROGRAMMING Page 55 //empty body
Expression
Most of the statements in a C program are expression statements. An expression statement is
simply an expression followed by a semicolon. The lines
i = 0;
i = i + 1;
and printf("Hello, world!\n");
are all expression statements. In C, however, the semicolon is a statement terminator. Expression statements do all of the real work in a C program. Whenever you need to compute new values for variables, you'll typically use expression statements (and they'll typically contain assignment operators). Whenever you want your program to do something visible, in the real world, you'll typically call a function (as part of an expression statement). We've already seen the most basic example: calling the function printf to print text to the screen.
Note -If no expression is present, the statement is often called the null statement.
Return
The return statement terminates execution of a function and returns control to the calling function, with or without a return value. A function may contain any number of return statements. The return statement has
syntax: return expression(opt);
If present, the expression is evaluated and its value is returned to the calling function. If necessary, its value is converted to the declared type of the containing function's return value.
A return statement with an expression cannot appear in a function whose return type is void . If there is no expression and the function is not defined as void , the return value is undefined. For example, the following main function returns an unpredictable value to the operating system:
main ( )
C PROGRAMMING Page 56 return;
Compound statements
A compound statement (also called a "block") typically appears as the body of another statement, such as the if statement, for statement, while statement, etc
A Compound statement consists of several individual statements enclosed within a pair of
braces { }. The individual statements may themselves be expression statements, compound statements or control statements. Unlike expression statements, a compound statement does not end with a semicolon. A typical Compound statement is given below.
pi=3.14;
area=pi*radius*radius;
The particular compound statement consists of two assignment-type expression
statements.
Example:
C PROGRAMMING Page 57 Selection Statement/Conditional Statements/Decision Making Statements
A selection statement selects among a set of statements depending on the value of a controlling expression. Or
Moving execution control from one place/line to another line based on condition
Or
Conditional statements control the sequence of statement execution, depending on the value of a integer expression
C‟ language supports two conditional statements.
1: if
2: switch.
1: if Statement: The if Statement may be implemented in different forms.
1: simple if statement.
2: if –else statement
3: nested if-else statement.
4: else if ladder.
if statement.
The if statement controls conditional branching. The body of an if statement is executed if the value of the expression is nonzero. Or if statement is used to execute the code if condition is true. If the expression/condition is evaluated to false (0), statements inside the body of if is skipped from execution.
Syntax : if(condition/expression)
true statement;
C PROGRAMMING Page 58 }
statement-x;
If the condition/expression is true, then the true statement will be executed otherwise the true statement block will be skipped and the execution will jump to the statement-x. The „true statement‟ may be a single statement or group of statement.
If there is only one statement in the if block, then the braces are optional. But if there is more than one statement the braces are compulsory
Flowchart
Example:
#include<stdio.h>
main()
int a=15,b=20;
C PROGRAMMING Page 59 if(b>a)
printf("b is greater");
Output
b is greater
#include <stdio.h> int main() { int number;
printf("Enter an integer: ");
scanf("%d", &number);
// Test expression is true if number is less than 0
if (number < 0) { printf("You entered %d.\n", number); }
printf("The if statement is easy.");
return 0; } Output 1 Enter an integer: -2 You entered -2. The if statement is easy.
Output 2 Enter an integer: 5 The if statement in C programming is easy.
If-else statement : The if-else statement is an extension of the simple if statement. The general form is. The if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).