0% found this document useful (0 votes)
6 views6 pages

C Programming Constants and Structures

The document provides a C programming example demonstrating various types of constants, including integer, floating-point, character, string, and symbolic constants. It also explains the use of enums, structs, storage classes, and control flow statements such as loops and switch-case. Additionally, it includes sample output from the program, showcasing how to print values and use defined constants.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

C Programming Constants and Structures

The document provides a C programming example demonstrating various types of constants, including integer, floating-point, character, string, and symbolic constants. It also explains the use of enums, structs, storage classes, and control flow statements such as loops and switch-case. Additionally, it includes sample output from the program, showcasing how to print values and use defined constants.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <stdio.

h>

// Symbolic constant using #define

#define PI 3.14159

// Enum constants

enum Week { MON = 1, TUE, WED, THU, FRI, SAT, SUN };

int main() {

// 1. Integer Constants

int decimal = 10; // Decimal

int octal = 045; // Octal (base 8)

int hex = 0x1A; // Hexadecimal (base 16)

// 2. Floating-point Constants

float pi_value = 3.14; // Decimal form

double exp_value = 2.5e3; // Exponential form

// 3. Character Constants

char ch1 = 'A';

char ch2 = '9';

char ch3 = '\n'; // newline character

// 4. String Constants

char str1[] = "Hello, World!";

char str2[] = "C Programming";

// 5. Const Qualifier

const int DAYS_IN_WEEK = 7;

// 6. Enum Constants usage

enum Week today = WED;

// ----------- OUTPUT --------------

printf("Integer Constants:\n");

printf("Decimal = %d, Octal = %d, Hex = %d\n\n", decimal, octal, hex);


printf("Floating-point Constants:\n");

printf("Pi = %.2f, Exponential = %.2lf\n\n", pi_value, exp_value);

printf("Character Constants:\n");

printf("ch1 = %c, ch2 = %c, newline char prints a new line%c\n\n", ch1, ch2, ch3);

printf("String Constants:\n");

printf("str1 = %s\n", str1);

printf("str2 = %s\n\n", str2);

printf("Symbolic Constant (using #define):\n");

printf("PI = %.5f\n\n", PI);

printf("Const Qualifier:\n");

printf("DAYS_IN_WEEK = %d\n\n", DAYS_IN_WEEK);

printf("Enum Constants:\n");

printf("Today is day number %d of the week\n", today);

return 0;

}
Program Explanation for Naive Students

1. Header Files

#include <stdio.h>

#include <string.h>

• stdio.h → lets us use printf() for printing.

• string.h → lets us use functions like strcpy() to copy strings.

2. typedef keyword

typedef unsigned int uint;

• typedef creates a new name for an existing type.

• Here, instead of writing unsigned int, we can simply write uint.

Example:

uint a = 5; // same as unsigned int a = 5;

3. struct keyword

struct Student {

char name[20];

int age;

};

• struct lets us group related data.

• A Student has two things:

o name (characters array/string)

o age (integer)

4. main function starts

int main() {

This is where program execution begins.


5. Storage Classes & Constants

auto int x = 10; // auto storage class (default, local variable)

const float pi = 3.14; // const = value cannot change

register int i; // register = stored in CPU register (fast access)

static int count = 0; // static = remembers value between calls

Key points:

• auto is the default (local variable).

• const means fixed value.

• register hints compiler to keep it fast.

• static keeps value alive even after function ends.

6. enum keyword

enum Days { MON, TUE, WED, THU, FRI, SAT, SUN };

enum Days today = WED;

• enum = set of named constants.

• Here, days of the week are named.

• today = WED → stores Wednesday.

7. Using struct

struct Student s1;

strcpy([Link], "Alice");

[Link] = 20;

• Create a student s1.

• Copy "Alice" into name.

• Assign 20 as age.

8. Loop with control flow


for (i = 0; i < 3; i++) {

if (i == 1)

continue; // skips this loop

else if (i == 2)

break; // exits loop

else

printf("Loop i = %d\n", i);

• i=0 → prints Loop i = 0

• i=1 → continue (skip printing)

• i=2 → break (exit loop)

Output:

Loop i = 0

9. switch-case

switch (today) {

case MON: printf("It's Monday\n"); break;

case WED: printf("It's Wednesday\n"); break;

default: printf("Some other day\n");

• Checks value of today.

• If today = WED, prints:

It's Wednesday

10. Printing values

printf("Name: %s, Age: %d\n", [Link], [Link]);

printf("Const pi = %.2f\n", pi);

printf("Count = %d\n", count);


Output will be:

Name: Alice, Age: 20

Const pi = 3.14

Count = 0

Final Program Output

Loop i = 0

It's Wednesday

Name: Alice, Age: 20

Const pi = 3.14

Count = 0

You might also like