Variables in C BY CHAITANYA SINGH | FILED UNDER: C-PROGRAMMING A variable represents a memory location that stores the data. For example: an int variable num has a value 10 (int num = 10), here the variable name is "num" that represents the location in the memory where this value 10 is stored. As the name suggests, the value of a variable can be changed any number of times . Syntax – Declaring a variable data_type variable name ; For example: //a variable num of int type int num ; //two variable ch1 and ch2 of char type char ch1 , ch2 ; //three variable x, y and z of float type where y has been // initialized with a value and other variables x & z are // un-initialized. Float x , y = 10.5 , z ; Example: Variables in C #include <stdio.h> int main () { int num1 = 20 , num2 = 50 ; char ch = 'A' ; float x = 10.5 , y = 13.5 ; printf ( "Variable 'ch' value: %c\n" , ch ); ...
#include <stdio.h> int main () { int num1 , num2 ; float avg ; printf ( "Enter first number: " ); scanf ( "%d" ,& num1 ); printf ( "Enter second number: " ); scanf ( "%d" ,& num2 ); avg = ( float )( num1 + num2 )/ 2 ; //%.2f is used for displaying output upto two decimal places printf ( "Average of %d and %d is: %.2f" , num1 , num2 , avg ); return 0 ; } Output: Enter first number : 12 Enter second number : 13 Average of 12 and 13 is : 12.50
#include <stdio.h> int main () { int num1 , num2 , sum ; printf ( "Enter two integers: " ); //Storing user input into variable num1 & num2 scanf ( "%d %d" , & num1 , & num2 ); // Adding two input numbers sum = num1 + num2 ; printf ( "Sum of %d and %d is: %d" , num1 , num2 , sum ); return 0 ; } Output: User enters the numbers 25 and 35 and program prints sum of these two numbers 60 as an output.
Comments
Post a Comment