Arithmetic Operators in C
Most C programs perform calculations using the C arithmetic operators. Note the use of various special symbols not used in algebra. The asterisk(*) indicates multiplication and the percent sign(%) denotes the remainder operator, which is introduced below.
C-operation Arithmetic_operator
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
Example:
C-prointf() and scanf()
printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default. These functions are declared and related macros are defined in “stdio.h” which is a header file in c language.
We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions in C language.
1. PRINTF() FUNCTION IN C LANGUAGE:
- In C programming language, printf() function is used to print the (“character, string, float, integer, octal and hexadecimal values”) onto the output screen.
- We use printf() function with %d format specifier to display the value of an integer variable.
- Similarly %c is used to display character
- %f for float variable
- %s for string variable
- %lf for double
- %x for hexadecimal variable.
- To generate a newline, we use “\n” in C printf() statement.
EXAMPLE PROGRAM FOR C PRINTF() FUNCTION:
#include <stdio.h>
int main()
{
char ch = ‘A’;
char str[20] = “fresh2refresh.com”;
float f = 10.234;
int no = 150;
double d = 20.123456;
printf(“Character is %c \n”, ch);
printf(“String is %s \n” , str);
printf(“Float value is %f \n”, f);
printf(“Integer value is %d\n” , no);
printf(“Double value is %lf \n”, d);
printf(“Octal value is %o \n”, no);
printf(“Hexadecimal value is %x \n”, no);
return 0;
}
OUTPUT:
Character is A
String is fresh2refresh.com
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96
2. SCANF FUNCTION IN C LANGUAGE:
- In C programming language, scanf() function is used to read character, string, numeric data from keyboard
- Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.
- Then, user enters a string and this value is assigned to the variable “str” and then displayed.
EXAMPLE PROGRAM FOR C PRINTF AND SCANF FUNCTIONS IN C PROGRAMMING LANGUAGE:
#include <stdio.h>
int main()
{
char ch;
char str[100];
printf(“Enter any character \n”);
scanf(“%c”, &ch);
printf(“Entered character is %c \n”, ch);
printf(“Enter any string ( upto 100 character ) \n”);
scanf(“%s”, &str);
printf(“Entered string is %s \n”, str);
}
OUTPUT:
Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
hello
Entered string is hello