Declaration | Reading scanf() | Printing output printf() |
---|---|---|
char c; | scanf("%c",&c); | printf("%c",c); |
unsigned char uc; | scanf("%c",&uc); | printf("%c",uc); |
int i; | scanf("%d",&i); | printf("%d",i); |
unsigned int i; | scanf("%u",&i); | printf("%u",i); |
short int i; | scanf("%d",&i); | printf("%d",i); |
unsigned short int i; | scanf("%d",&i); | printf("%d",i); |
long int i; | scanf("%ld",&i); | printf("%ld",i); |
unsigned long int i; | scanf("%lu",&i); | printf("%lu",i); |
float x; | scanf("%f",&x); | printf("%f",x); |
double y; | scanf("%lf",&y); | printf("%lf",y); |
long double z; | scanf("%Lf",&z); | printf("%Lf",z); |
This example demonstrates how to declare and print different data types in C, including int, float, double, and char.
#include <stdio.h>
int main() {
int a = 10;
float b = 5.25;
double c = 10.123456;
char d = 'A';
printf("Integer: %d\n", a);
printf("Float: %.2f\n", b);
printf("Double: %.6lf\n", c);
printf("Character: %c\n", d);
return 0;
}
Output:
Integer: 10
Float: 5.25
Double: 10.123456
Character: A
This example shows how to find the size of each data type using the sizeof() function.
#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of char: %lu bytes\n", sizeof(char));
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
This example demonstrates how to define and use constants in C using the const keyword.
#include <stdio.h>
int main() {
const int a = 10;
const float b = 3.14;
printf("Constant Integer: %d\n", a);
printf("Constant Float: %.2f\n", b);
return 0;
}
Output:
Constant Integer: 10
Constant Float: 3.14
Constants and Variables in C