Paramclasses Academy - Blog

Tutorials Details

←
Post 2 of 15
→

What is Structure of a C Program in C Language

What is Structure of a C Program in C Language

October 12, 2026

1. Introduction

In Module 1, we learned the basics of C programming and wrote our first program using printf().

Now, we will learn how to store and manage data in a C program.

Every program works with data. For example, a student management system stores student names, roll numbers, and marks. A billing system stores product prices and quantities.

In C, variables, constants, and data types help us represent and manage this information.

By the end of this module, you will understand how to declare variables, choose suitable data types, use constants, and display values using printf().

2. What Is a Variable?

A variable is a named object that stores a value that can be accessed or modified while a program runs.

Syntax

data_type variable_name;

Example

int age;
age = 20;

Here:

  • int specifies the data type.

  • age is the variable name.

  • 20 is the value assigned to the variable.

You can also declare and initialize a variable in one statement:

int age = 20;

Complete program

#include <stdio.h>

int main(void)
{
    int age = 20;

    printf("Age: %d\n", age);

    return 0;
}

Output:

Age: 20

3. Rules for Naming Variables

C has specific rules for naming variables.

  1. A variable name may contain letters, digits, and underscores.

  2. It cannot begin with a digit.

  3. Spaces are not allowed.

  4. C keywords cannot be used as variable names.

  5. Variable names are case-sensitive.

  6. Choose meaningful names that describe the stored data.

Valid names

Invalid names

age

2age

student_name

student name

totalMarks

float

price1

total-price

_count

my.name

Best practice: Prefer descriptive names such as studentMarks or totalPrice instead of vague names such as x or a, except in short mathematical examples where single-letter names are conventional.

4. What Are Data Types?

A data type specifies the kind of value an object can store and determines the operations that can be performed on it.

For example, a person's age can usually be represented by an integer, while a product's price may require a floating-point value.

Common data types in C

Data type

Typical purpose

Example

char

Character or small integer value

'A'

int

Integer values

100

float

Single-precision floating-point values

12.5f

double

Double-precision floating-point values

123.456

void

Represents no value or no specified type

void

The exact sizes of numeric types depend on the C implementation. Use sizeof when you need to determine the size on a particular system.

5. The int Data Type

The int data type is used for integer values without a fractional part.

Example

#include <stdio.h>

int main(void)
{
    int age = 25;
    int marks = 85;

    printf("Age: %d\n", age);
    printf("Marks: %d\n", marks);

    return 0;
}

Output:

Age: 25
Marks: 85

The %d format specifier is used to display an int value with printf().

6. The float Data Type

The float data type represents single-precision floating-point values.

Example

#include <stdio.h>

int main(void)
{
    float price = 99.5f;

    printf("Price: %.2f\n", price);

    return 0;
}

Output:

Price: 99.50

Here, %.2f displays two digits after the decimal point.

Remember that floating-point values are approximations for many decimal numbers. They are not ideal for every financial calculation where exact decimal arithmetic is required.

7. The double Data Type

The double data type represents double-precision floating-point values.

Example

#include <stdio.h>

int main(void)
{
    double pi = 3.1415926535;

    printf("Pi: %.6f\n", pi);

    return 0;
}

Output:

Pi: 3.141593

The double type typically provides greater precision than float.

For printf(), %f is used to display a double value.

8. The char Data Type

The char type is used to represent a character-sized value, commonly a character encoded as a byte.

Example

#include <stdio.h>

int main(void)
{
    char grade = 'A';

    printf("Grade: %c\n", grade);

    return 0;
}

Output:

Grade: A

Important points:

  • Character constants use single quotation marks, such as 'A'.

  • String literals use double quotation marks, such as "Hello".

  • %c displays a character value.

9. Constants in C

A constant is a value that should not be changed during a particular part of a program.

C supports several ways to represent constant values. Two common approaches are const objects and preprocessor macros.

Using const

#include <stdio.h>

int main(void)
{
    const float PI = 3.14159f;

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

    return 0;
}

Output:

PI: 3.14159

The const qualifier prevents modification of the object through that identifier.

For example, this assignment is invalid:

const int MAX = 100;
MAX = 200;  // Constraint violation

Using #define

#include <stdio.h>

#define MAX_MARKS 100

int main(void)
{
    printf("Maximum marks: %d\n", MAX_MARKS);

    return 0;
}

Output:

Maximum marks: 100

#define creates a preprocessor macro. Unlike a const object, a macro does not declare a typed variable.

10. The sizeof Operator

The sizeof operator tells us how many bytes an object or type occupies on the current implementation.

Example

#include <stdio.h>

int main(void)
{
    printf("Size of char: %zu bytes\n", sizeof(char));
    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of float: %zu bytes\n", sizeof(float));
    printf("Size of double: %zu bytes\n", sizeof(double));

    return 0;
}

Output on a common modern system might be:

Size of char: 1 bytes
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes

These sizes are implementation-dependent, except that sizeof(char) is always 1 by definition. Also, %zu is the appropriate printf() format specifier for a value of type size_t.

11. Taking Input from the User

The scanf() function can read formatted input from the standard input stream.

Example

#include <stdio.h>

int main(void)
{
    int age;

    printf("Enter your age: ");

    if (scanf("%d", &age) != 1)
    {
        printf("Invalid input.\n");
        return 1;
    }

    printf("Your age is %d\n", age);

    return 0;
}

Sample input:

Enter your age: 21

Output:

Your age is 21

Explanation

  • scanf("%d", &age) attempts to read an integer.

  • %d specifies the expected input format.

  • &age provides the address where the input value should be stored.

  • Checking the return value ensures the program does not use an uninitialized value when input fails.

12. Multiple Variables in One Program

You can declare multiple variables of different data types in the same program.

#include <stdio.h>

int main(void)
{
    int rollNumber = 101;
    char grade = 'A';
    float marks = 89.5f;

    printf("Roll Number: %d\n", rollNumber);
    printf("Grade: %c\n", grade);
    printf("Marks: %.1f\n", marks);

    return 0;
}

Output:

Roll Number: 101
Grade: A
Marks: 89.5

This example demonstrates how different types represent different kinds of information.

13. Common Beginner Mistakes

  • Using a variable before assigning it a valid value.

  • Choosing the wrong format specifier for printf() or scanf().

  • Forgetting & when using scanf() with ordinary scalar variables.

  • Trying to assign a new value to an object declared const.

  • Assuming every computer uses the same size for int.

  • Using single quotation marks for strings instead of double quotation marks.

  • Ignoring the return value of scanf().

14. Practical Exercises

Try these programs yourself:

  1. Declare an integer variable named age and display it.

  2. Store and display a student's name initial, roll number, and marks.

  3. Create two integer variables and display their sum.

  4. Declare a const value for the number of days in a week.

  5. Use sizeof to display the sizes of char, int, float, and double.

  6. Ask the user to enter two numbers and display both values.

  7. Create a simple product record using variables for product ID, price, and quantity.

15. Interview Questions

Q1. What is a variable in C?

A named object used to store a value that a program can access and, when permitted, modify.

Q2. What is a data type?

A type specifies the kind of data an object represents and the operations that can be performed on it.

Q3. What is the difference between float and double?

Both represent floating-point values, but double typically provides greater precision.

Q4. What is a constant?

A value or named object intended to remain unchanged in the relevant context.

Q5. What is the difference between const and #define?

const qualifies a declared object, while #define creates a preprocessor macro.

Q6. What does sizeof do?

It determines the size in bytes of a type or object.

Q7. Why is & used with scanf()?

It supplies the address where the input value should be stored.

16. Quick Revision

  • Variables store values.

  • Data types define the kinds of values objects represent.

  • int represents integer values.

  • float and double represent floating-point values.

  • char represents a character-sized value.

  • const helps prevent modification of an object through its identifier.

  • #define creates a preprocessor macro.

  • sizeof determines an object's or type's size in bytes.

  • printf() displays formatted output.

  • scanf() reads formatted input and its return value should be checked.

17. Homework

Create a Student Information Program with the following requirements:

  1. Declare variables for a student's roll number, initial, age, and marks.

  2. Ask the user to enter each value.

  3. Validate the return values from scanf().

  4. Display the entered information with suitable labels.

  5. Use appropriate format specifiers.

  6. Declare a constant for the maximum marks and display it.

Expected output format

Enter roll number: 101
Enter initial: R
Enter age: 20
Enter marks: 88.5

--- Student Information ---
Roll Number: 101
Initial: R
Age: 20
Marks: 88.5
Maximum Marks: 100

Conclusion

In Module 2, you learned how to declare variables, use common C data types, define constants, inspect object sizes, and accept user input.

←
Post 2 of 15
→