120 likes | 139 Views
Learn core concepts such as variables, datatypes, arithmetic operations, input/output functions, and more in C programming. Understand how to declare variables, use printf and scanf, and perform basic calculations.
E N D
NOTE: C programs consist of functions one of which must be main. Every C program begins executing at the function main. Comments begin with /* and end with */ The preprocessor directive #include <stdio.h> tells the compiler to include standard input output header file in the program.
Variables and Values • A variable is a location in memory where a value can be stored for use by a program. • Ex : int sum = 5 ; would mean the variable “sum” is an integer and its value is 5 which is stored in memory. sum 5
Datatypes Datatypekeyword • integer int • real/float float • character char All variables in C must be declared before they can be used in the program!
printf • Used to print the string contained in ( “ ”) and to print the value of expressions. • Format when printing 1st argument - format control string 2nd argument - expression whose value will be printed • printf(“format control string”,variable); • printf(“format control string”,list of vars);
Variables in printf Variable 1st 2nd argument argument Integer %d expression Float %f expression Char %c character
/* Program to print integers and floats */ # include <stdio.h> main() { int a = 5; float b = 2.0 ; printf(“The value of a is %d”, a); printf(“The value of b is %f”, b); }
Arithmetic in C C operationArithmetic operator Addition + Subtraction - Multiplication * Division / Modulus %
/* Program to print the sum of two numbers */ # include <stdio.h> main() { int a = 5; float b = 2.0 ; float c; c = a + b; printf(“The sum of %d and %f is %f”, a, b, c); }
scanf • Used to obtain values user enters at the keyboard. • Format 1st argument - format control string 2nd argument - location in memory where the value will be stored. • scanf(“format control string”, &variable);
Example: printf(“Enter an integer \n”); scanf(“%d”, &integer1); %d specifies that data entered should be an integer. Value is stored at location &integer1.
/* To calculate the volume of a cylinder */ #include <stdio.h> #define PI 3.1415926 void main(void) { float radius = 0.0; float height = 0.0; float volume = 0.0; printf(“Enter the radius: \n”);
scanf(“%f”, &radius); printf(“Enter the height: \n”); scanf(“%f”, &height); volume = PI * radius * radius * height; printf(“The volume of the cylinder with height %f and radius %f is: %f”, height, radius, volume); }