0
0
Cprogramming~20 mins

Constants and literals - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constants and Literals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of integer and floating-point constants
What is the output of this C program?
#include <stdio.h>
int main() {
    const int a = 5;
    const float b = 3.0f;
    printf("%d %.1f\n", a, b);
    return 0;
}
C
#include <stdio.h>
int main() {
    const int a = 5;
    const float b = 3.0f;
    printf("%d %.1f\n", a, b);
    return 0;
}
A5 3.0
B5 3
C5.0 3.0
D3 5.0
Attempts:
2 left
💡 Hint
Look at the format specifiers used in printf.
Predict Output
intermediate
2:00remaining
Output of character and string literals
What will this C program print?
#include <stdio.h>
int main() {
    const char c = 'A';
    const char *s = "Hello";
    printf("%c %s\n", c, s);
    return 0;
}
C
#include <stdio.h>
int main() {
    const char c = 'A';
    const char *s = "Hello";
    printf("%c %s\n", c, s);
    return 0;
}
AHello A
B65 Hello
CA 'Hello'
DA Hello
Attempts:
2 left
💡 Hint
Remember that '%c' prints a single character and '%s' prints a string.
Predict Output
advanced
2:00remaining
Output of octal and hexadecimal integer constants
What is the output of this C program?
#include <stdio.h>
int main() {
    int x = 010;  // octal 10
    int y = 0x10; // hex 10
    printf("%d %d\n", x, y);
    return 0;
}
C
#include <stdio.h>
int main() {
    int x = 010;  // octal 10
    int y = 0x10; // hex 10
    printf("%d %d\n", x, y);
    return 0;
}
A8 16
B10 10
C16 8
D010 0x10
Attempts:
2 left
💡 Hint
Octal 010 is 8 in decimal, hex 0x10 is 16 in decimal.
Predict Output
advanced
2:00remaining
Output of escape sequences in character constants
What does this C program print?
#include <stdio.h>
int main() {
    char c1 = '\n';
    char c2 = '\t';
    printf("%d %d\n", c1, c2);
    return 0;
}
C
#include <stdio.h>
int main() {
    char c1 = '\n';
    char c2 = '\t';
    printf("%d %d\n", c1, c2);
    return 0;
}
A92 116
B10 9
Cn t
D0 0
Attempts:
2 left
💡 Hint
Escape sequences represent special ASCII codes.
Predict Output
expert
2:00remaining
Value of a floating-point constant with suffix
What is the output of this C program?
#include <stdio.h>
int main() {
    double d = 1.5;
    float f = 1.5f;
    printf("%.1f %.1f\n", d, f);
    return 0;
}
C
#include <stdio.h>
int main() {
    double d = 1.5;
    float f = 1.5f;
    printf("%.1f %.1f\n", d, f);
    return 0;
}
A1.0 1.5
B1.5 0.0
C1.5 1.5
D1.5 1.0
Attempts:
2 left
💡 Hint
The suffix 'f' makes the constant a float, but printing with %.1f shows the value.