What is C Programming Language?
● C is a general-purpose programming language used to develop software like operating
systems, databases, and applications.
● It's beginner-friendly and helps you understand the basics of coding.
Printing Output in C (printf)
● To display messages or values, use the printf function.
Syntax:
printf("Your message here");
● Example:
printf("Hello, World!\n"); // Prints "Hello, World!" and moves
to the next line. \n - used for moving to next line
Placeholders: You can use format specifiers to print variables.
Example:
int num = 10;
printf("The value is %d", num);
Declaration
● Telling the program what kind of data a variable will hold.
● Brief: Specifies the data type and name of a variable.
Syntax:
data_type variable_name;
● Example:
int age; // Declares an integer variable
Initialization
● Assigning a value to a variable when declaring it.
● Brief: Combines declaration and value assignment.
Syntax:
data_type variable_name = value;
● Example:
int age = 20;
Case-Sensitive
● C distinguishes between uppercase and lowercase letters.
● Brief: Name and name are treated as different identifiers.
Example:
int Age = 25; // Not the same as 'age'
int age = 30;
Variables
● Storage locations in memory with a name and a type.
● Brief: Used to hold data values like numbers or characters.
Example:
int number = 10; // 'number' stores the value 10
Compiling
● The process of converting your code into a machine-readable
format.
● Brief: You write code, the compiler checks for errors, and if
correct, it creates an executable file.
Common Data Types
scanf
● A function used to take input from the user.
● Brief: It reads the value entered and stores it in a variable.
Syntax:
scanf("%d", &variable); // %d is the format specifier for
integers
Example:
int age;
scanf("%d", & age);
Format specifiers
Format specifiers are placeholders used with functions like printf and scanf to specify the type of
data you want to print or read. They ensure the correct format for the input or output.
In other words, Format specifiers are like labels that tell the computer what kind of data you're
working with when you print something (printf) or take input (scanf). Think of them as a way to
say, "Hey, this is a number" or "This is a letter."
Common Format Specifiers:
● %d - Integer
● %f - Float
● %c - Character
● %s - String
Think of It Like This:
● %d = "Whole number"
● %f = "Decimal number"
● %c = "One letter"
● %s = "A word or sentence"
● %% = "Print the % symbol"
Using Format Specifiers in printf
● To print values, match the variable type with the correct format
specifier.
Example
int num = 10;
float pi = 3.14;
printf("Integer: %d\n", num); // Prints 10, %d\n - Integer, displays
whole numbers
printf("Float: %.2f\n", pi); // Prints 3.14 (2 decimal
places), %f - Float, displays decimal numbers
A simple program that shows how to declare, initialize, and use int,
float, double, and string with comment
#include <stdio.h> // Include standard input-output header
int main() {
// Declare and initialize an integer
int age = 18; // Integer (whole number) to store age
// Declare and initialize a float
float height = 5.9; // Float (decimal number) to store height in
feet
// Declare and initialize a double
double pi = 3.1415926535; // Double (more precise decimal number)
// Declare a string (array of characters)
char name[50]; // A string to store a name (up to 49 characters +
null character)
// Print values of the variables
printf("Age: %d\n", age); // Use %d for integers
printf("Height: %.1f feet\n", height); // Use %f for float (1
decimal place shown)
printf("Value of Pi: %.10lf\n", pi); // Use %lf for double (show
10 decimal places)
// Ask the user to input a name
printf("Enter your name: ");
scanf("%s", name); // %s reads a string (no need for & with
strings)
// Print the name entered by the user
printf("Your name is: %s\n", name); // Use %s to print a string
return 0; // End of program