Basic Syntax and Data Types in C
C programming provides a simple but powerful syntax to declare variables, store data, and
perform input/output operations. Let’s go step by step:
1. Variables and Constants
Variable: A named storage location in memory whose value can change during
program execution.
Constant: A fixed value that does not change once defined.
Example:
#include <stdio.h>
int main() {
int age = 20; // variable: value can change
const float PI = 3.14; // constant: value cannot change
printf("Age = %d\n", age);
printf("Value of PI = %.2f\n", PI);
age = 25; // we can update a variable
printf("Updated Age = %d\n", age);
// PI = 3.14159; // ERROR: cannot modify a constant
return 0;
}
Comments:
int age = 20; → variable declaration with an initial value.
const float PI = 3.14; → constant, cannot be reassigned.
%d → format specifier for integers, %.2f for float with 2 decimals.
2. Data Types
C provides several built-in data types for different kinds of values.
int → integers (e.g., 10, -5, 1000)
char → single characters (e.g., 'A', 'z')
float → decimal numbers (single precision, e.g., 3.14)
double → decimal numbers (double precision, e.g., 3.14159265)
1
Example:
#include <stdio.h>
int main() {
int number = 100; // integer
char grade = 'A'; // character
float price = 45.75; // single precision float
double pi = 3.14159265; // double precision float
printf("Number = %d\n", number);
printf("Grade = %c\n", grade);
printf("Price = %.2f\n", price);
printf("PI = %.8lf\n", pi); // %lf for double, prints 8 decimals
return 0;
}
Comments:
%d prints integer, %c prints character.
%f or %.2f prints float with decimals.
%lf prints double with higher precision.
3. Input and Output Functions
printf() → used to display output on the screen.
scanf() → used to take input from the user.
Example:
#include <stdio.h>
int main() {
int age;
char initial;
printf("Enter your age: ");
scanf("%d", &age); // %d expects integer, & stores input in variable
printf("Enter your first initial: ");
scanf(" %c", &initial); // %c reads a character (note space before %c to avoid
newline issues)
printf("You entered Age = %d and Initial = %c\n", age, initial);
return 0;
}
2
Comments:
scanf("%d", &age); → %d for integer, & means "store value in this variable".
scanf(" %c", &initial); → space before %c ignores leftover newline character.
4. Type Conversion and Casting
Sometimes we need to convert between data types.
Implicit Conversion (Type Promotion) → C automatically converts smaller type to
larger type.
Explicit Conversion (Casting) → Programmer manually converts data type.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 2;
float result;
// Implicit conversion (int → float automatically)
result = a / b;
printf("Integer division: %f\n", result); // Output: 2.000000 (not 2.5!)
// Explicit conversion (Casting one operand to float)
result = (float)a / b;
printf("With casting: %f\n", result); // Output: 2.500000
return 0;
}
Comments:
a / b → since both are integers, result is integer division (2).
(float)a / b → converts a to float before division, result is accurate (2.5).
Note:
Variables hold data that can change, constants hold fixed values.
Data types define the kind of values stored (int, char, float, double).
printf() shows output, scanf() takes input.
3
Type conversion ensures correct arithmetic and data handling.
More examples
Case Study 1: Temperature Converter (Celsius to Fahrenheit)
Problem:
A weather station wants to convert temperature from Celsius to Fahrenheit. The formula is:
F=95×C+32F = \frac{9}{5} \times C + 32F=59×C+32
Solution:
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
return 0;
}
Explanation:
Step 1: #include <stdio.h>
This is a preprocessor directive.
It tells the compiler to include the Standard Input Output library so we can use
printf() and scanf() functions.
Step 2: int main()
This is the main function where the program starts executing.
int means the function returns an integer value, typically 0 to indicate successful
execution.
Step 3: Variable Declaration
float celsius, fahrenheit;
float → data type used for decimal numbers.
celsius → will store the temperature entered by the user.
fahrenheit → will store the converted temperature.
Step 4: Prompting the User
4
printf("Enter temperature in Celsius: ");
printf() displays the message on the screen asking the user to enter a temperature.
Step 5: Reading Input
scanf("%f", &celsius);
scanf() reads input from the user.
%f → format specifier for float values.
&celsius → stores the input in the variable celsius.
Example: if user types 25.5, it will be stored in celsius.
Step 6: Converting Celsius to Fahrenheit
fahrenheit = (celsius * 9 / 5) + 32;
Formula used: F = (C × 9/5) + 32
First, celsius * 9 / 5 calculates the proportional conversion.
Then, + 32 adjusts it to the Fahrenheit scale.
The result is stored in fahrenheit.
Step 7: Displaying the Result
printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
%.2f → prints float values with 2 decimal places.
Outputs something like:
25.50 Celsius = 77.90 Fahrenheit
Shows both the input and the converted temperature clearly.
Step 8: Ending the Program
return 0;
Ends the program and returns 0 to the operating system, meaning the program
executed successfully.
Case Study 2: Bank Account Balance Calculator
Problem:
A bank teller wants to compute the new account balance after a deposit.
Solution:
5
#include <stdio.h>
int main() {
double balance, deposit, new_balance;
printf("Enter current balance: ");
scanf("%lf", &balance);
printf("Enter deposit amount: ");
scanf("%lf", &deposit);
new_balance = balance + deposit;
printf("New balance = %.2lf\n", new_balance);
return 0;
}
Explanation:
double → high-precision decimal for money calculations.
Addition operation demonstrates arithmetic using variables.
%.2lf → prints balance with 2 decimal places.
Case Study 3: Character Input for Initials
Problem:
A school wants a program to read a student’s first initial and print a welcome message.
Solution:
#include <stdio.h>
int main() {
char initial;
printf("Enter your first initial: ");
scanf(" %c", &initial); // space avoids newline issues
printf("Welcome, student %c!\n", initial);
return 0;
}
Explanation:
char → stores a single character.
6
Input and output demonstrate basic I/O.
Case Study 4: Average of Three Numbers
Problem:
A teacher wants a program to compute the average of three students’ marks.
Solution:
#include <stdio.h>
int main() {
int mark1, mark2, mark3;
float average;
printf("Enter three marks: ");
scanf("%d %d %d", &mark1, &mark2, &mark3);
average = (float)(mark1 + mark2 + mark3) / 3; // explicit casting
printf("Average marks = %.2f\n", average);
return 0;
}
Explanation:
int → stores whole number marks.
(float) → converts sum to float to avoid integer division.
Demonstrates type casting and arithmetic.