1. Comments
Comments are notes ignored by the compiler, used to explain code.
Single-line:
// like thisMulti-line:
/* for longer notes */
On-Board Activity
Write a C code snippet that prints your name but includes at least one comment explaining what the code does.
(Ask: What happens if you remove the comment markers?)
2. Constants and Variables
Constant: Value cannot change after definition, e.g.
#define PI 3.14Variable: Value can change, must be declared with a data type first, e.g.
int age = 16;
On-Board Activity
Declare a constant for your school’s year and a variable for the number of students. Print both values using printf().
3. Data Types
Common types:
int(integer),float(decimal),char(character)Example:
float price = 99.50;
On-Board Activity
Declare three variables (integer, float, character). Assign them values and display all three with an appropriate output function.
4. Format Specifiers
Used with
printf()andscanf()%dforint,%fforfloat,%cforchar,%sfor strings
On-Board Activity
Given the variable declarations int a = 5; float b = 3.2; char c = 'A';, write a printf statement to display all three values on different lines.
5. Output Functions
putchar(c): prints a single characterputs(str): prints a string with a new lineprintf(): prints formatted output
On-Board Activity
a) Use putchar() to print the first letter of your name.
b) Use puts() to print a greeting.
6. Input Functions
scanf(): gets formatted inputgetchar(): gets one charactergetch(),getche(): get characters (difference—getche()echoes,getch()does not)gets(): gets string input (but not recommended for new code)
On-Board Activity
Write code to input a character using getchar(), and a number using scanf(), then print both values.
7. Assignment and Compound Assignment Operators
=assigns a value:x = 5;Compound:
x += 3;(meansx = x + 3;), similar for-=,*=,/=
On-Board Activity
If x starts at 5, write code that increases x by 10 using a compound assignment. Show the value before and after.
1. Comments
Code Example
c// This code prints my name printf("Ali\n");
Removing "//" makes it regular code, not a comment.
2. Constants and Variables
Code Example
c#define SCHOOL_YEAR 2025 // constant int students = 40; // variable printf("Year: %d\n", SCHOOL_YEAR); printf("Number of students: %d\n", students);
3. Data Types
Code Example
cint rollNo = 12; float percentage = 87.5; char grade = 'A'; printf("Roll: %d\n", rollNo); printf("Percentage: %f\n", percentage); printf("Grade: %c\n", grade);
4. Format Specifiers
Code Example
cint a = 5; float b = 3.2; char c = 'A'; printf("%d\n", a); printf("%f\n", b); printf("%c\n", c);
5. Output Functions
Code Example
cputchar('A'); // prints: A puts("Hello Class 10!"); // prints: Hello Class 10! // and adds a new line
6. Input Functions
Code Example
cchar ch; int num; ch = getchar(); // input one character scanf("%d", &num); // input one integer printf("Character: %c\n", ch); printf("Number: %d\n", num);
(Note: For getch() / getche() and gets(), you generally need to include conio.h for older compilers. Modern C discourages gets() and recommends fgets().)
7. Assignment and Compound Assignment Operators
Code Example
cint x = 5; printf("Before: %d\n", x); x += 10; // same as x = x + 10; printf("After: %d\n", x);
You can ask any student to come up and write any of these on the board. If you want full explanations or variations, just ask!
No comments:
Post a Comment