Declaring, reading and printing of different data types

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);

Example 1: Displaying Different Data Types

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

Example 2: Size of Different Data Types

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

Example 3: Constants in C

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

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer