Programming Essentials in C - Lecture 4
User Input scanf()
scanf()is the most commonly used function to accept user input from the standard input stream.- The
scanf()function in C reads data from the standard input (usually the keyboard, calledstdin) and stores it in variables.
Syntax of scanf()
c
int x;
scanf("%d", &x); // ("format_specifier", &variable)"format_specifier": Tells the computer what type of input to expect (e.g.,%dfor integers,%ffor floating-point numbers).&variable: The&symbol gives the address of the variable where the input will be stored.
Common Format Specifiers
| Data Type | Format Specifier |
|---|---|
Integer (int) | %d or %i |
Floating Point (float) | %f |
Double (double) | %lf |
Character (char) | %c |
String (char[]) | %s |
Example
c
int num;
float decimal;
printf("Enter an integer and a decimal: ");
scanf("%d %f", &num, &decimal);
printf("Integer: %d, Decimal: %.2f\n", num, decimal);Capturing Strings with scanf
Code Example 1: Using scanf
c
char name[50]; // Empty string with space for 50 characters
printf("Enter your full name (using scanf): ");
scanf("%s", name); // Reads the name you type
printf("Name captured with scanf: %s\n", name);What Happens
- When you run this:
- Type:
John Doeand press Enter. - Output:
Name captured with scanf: John
- Type:
- Why?:
scanfonly grabs the first word (John) and stops at the space. It can't handle full names with spaces.
Code Example 2: Using gets
c
char name[50]; // Empty string with space for 50 characters
printf("Enter your full name (using gets): ");
gets(name); // Reads the name you type
printf("Name captured with gets: %s\n", name);What Happens
- When you run this:
- Type:
John Doeand press Enter. - Output:
Name captured with gets: John Doe
- Type:
- Why?:
getstakes everything you type, including spaces, until you press Enter. It works for full names!
Explanation
scanf: Acts like a tool that cuts your input at spaces and only keeps the first piece.gets: Acts like a tool that grabs your whole sentence, spaces included, until you press Enter.
Conditional Statements
If
- Use the
ifstatement to run a block of code if a condition is true.
Example
c
int num = 4;
if (num % 2 == 0) {
printf("Even\n");
}Else
- Use the
elsestatement to run a block of code if the condition is false.
Example: Check if a Number is Even or Odd
c
int num = 5;
if (num % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}Else If
- Use the
else ifstatement to check a new condition if the first one is false.
Example
c
int number;
printf("Enter a number: ");
scanf("%d", &number); // Reads the number you type
if (number > 0) {
printf("The number is positive!\n");
} else if (number < 0) {
printf("The number is negative!\n");
} else {
printf("The number is zero!\n");
}Short Hand If...Else (Ternary Operator)
- The ternary operator is a short way to write an
if-elsestatement in one line. It uses three parts: a condition, a true result, and a false result.
Example
c
int time = 20;
// Prints "Good evening."
time < 18 ? printf("Good morning.\n") : printf("Good night.\n");Nested If Statements
- A nested
ifstatement is anifinside anotherif. It checks one condition, then another.
Example: Checking if a Number is Positive and Even
c
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num > 0) { // Check if the number is positive
if (num % 2 == 0) { // Check if the number is even
printf("The number %d is positive and even.\n", num);
} else {
printf("The number %d is positive but odd.\n", num);
}
} else if (num < 0) {
printf("The number %d is negative.\n", num);
} else {
printf("The number is zero.\n");
}Switch Statement
- The
switchstatement checks a value and runs code based on which case matches. - It's like a menu: pick an option, and it does that one thing.
How It Works
- The
switchchecks one value. - It compares it to each
case. - If a
casematches, it runs that code. breakstops theswitchfrom continuing.defaultruns if nocasematches (optional).
Rules for Switch Statement
- The value in
switchmust be an integer or character. - Each
casevalue must be a fixed number or character (no variables). caseonly works insideswitch.breakis optional. Without it, all cases after a match will run (called "fall through").
Example
c
int choice;
printf("Enter 1, 2, or 3: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose option 1.\n");
break;
case 2:
printf("You chose option 2.\n");
break;
case 3:
printf("You chose option 3.\n");
break;
default:
printf("Invalid choice.\n");
break;
}