C Programming · Unit 1
Fundamentals of C Programming
Exam-focused notes for Fundamentals of C Programming (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
Fundamentals of C Programming in short
This chapter introduces the building blocks of every C program. You learn how a program is laid out, how it becomes a running file, and which words and names C allows.
What this unit covers
- Basic structure of C program
- Compilation and execution process
- Comments and escape sequences
- Tokens and statements
- Identifiers and keywords
- Rules for naming valid identifiers
Rules for naming valid identifiers
2078 · 10 marks
What is an Identifier and Keyword? Explain the rules for naming valid Identifiers in C with example.Write a program to convert a given string to lowercase without using any library functions.[5+5]
Show solved answerHide answer
Identifiers and Keywords in C
Identifiers
An identifier is a name given to various program elements such as variables, functions, arrays, structures, and user-defined types. Identifiers are used to uniquely identify a program element within its scope.
Example:
int age; // 'age' is an identifier
float salary; // 'salary' is an identifier
void display(); // 'display' is an identifier
Keywords
A keyword is a reserved word that has a predefined meaning in the C language. Keywords cannot be used as identifiers. C has 32 keywords.
Examples of Keywords:
int | float | char | if |
|---|---|---|---|
else | while | for | return |
void | struct | switch | break |
Rules for Naming Valid Identifiers in C
-
Letters, digits, and underscore only: An identifier can only contain alphabets (A-Z, a-z), digits (0-9), and underscore ( _ ).
-
Must begin with a letter or underscore: An identifier cannot start with a digit.
-
No spaces allowed: Spaces are not permitted inside an identifier.
-
Keywords not allowed: Reserved keywords cannot be used as identifiers.
-
Case sensitive:
Ageandageare treated as different identifiers. -
No special characters: Characters like
@,#,$,!, etc. are not allowed. -
Length: Typically, the first 31 characters are significant (as per ANSI C standard).
Examples:
| Identifier | Valid/Invalid | Reason |
|---|---|---|
age | Valid | Starts with letter |
_salary | Valid | Starts with underscore |
total_marks | Valid | Letters and underscore |
var1 | Valid | Letter followed by digit |
1var | Invalid | Starts with digit |
my var | Invalid | Contains space |
int | Invalid | Reserved keyword |
my@name | Invalid | Special character @ |
Program to Convert a String to Lowercase Without Library Functions
#include <stdio.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
gets(str); /* read the string */
/* Convert each character to lowercase */
for (i = 0; str[i] != '\0'; i++) {
/* Check if character is uppercase (A=65, Z=90 in ASCII) */
if (str[i] >= 'A' && str[i] <= 'Z') {
/* Add 32 to convert uppercase to lowercase */
str[i] = str[i] + 32;
}
}
printf("Lowercase string: %s\n", str);
return 0;
}
Sample Output:
Enter a string: Hello WORLD
Lowercase string: hello world
Logic Explanation:
- In ASCII, uppercase letters range from 65 ('A') to 90 ('Z').
- Lowercase letters range from 97 ('a') to 122 ('z').
- The difference between any uppercase letter and its lowercase equivalent is 32.
- So, adding 32 to an uppercase character converts it to lowercase.
- For example:
'A'(65) + 32 = 97 ='a' - Characters that are already lowercase or non-alphabetic are left unchanged.
Basic structure of C program
2078 · 5 marks
What is the basic structure of a C program? Explain each part. [5]
Show solved answerHide answer
Basic Structure of a C Program
Basic Structure of a C Program
A C program is organized into several distinct sections. Below is the general template:
/* Documentation Section */
// Program description, author, date
/* Link Section (Header Files) */
#include <stdio.h>
#include <conio.h>
/* Definition Section (Macros/Constants) */
#define PI 3.14159
/* Global Declaration Section */
int globalVar;
/* Main Function */
int main()
{
/* Local Declaration */
int a, b;
/* Executable Statements */
printf("Hello, World!\n");
return 0;
}
/* User-Defined Functions */
void myFunction()
{
// function body
}
Explanation of Each Part
1. Documentation Section
- Contains comments about the program such as program name, author, date, and purpose.
- Written using
/* ... */(multi-line) or//(single-line). - Ignored by the compiler; meant for human readability.
2. Link Section (Header Files)
- Contains preprocessor directives that include standard library header files.
- Syntax:
#include <filename.h> - Example:
#include <stdio.h>includes standard input/output functions likeprintf()andscanf(). - These are processed before compilation.
3. Definition Section
- Used to define symbolic constants or macros using
#define. - Example:
#define PI 3.14159 - The preprocessor replaces every occurrence of
PIwith3.14159before compilation.
4. Global Declaration Section
- Variables declared here are global variables, accessible throughout the entire program.
- Declared outside all functions.
- They retain their value for the entire lifetime of the program.
5. Main Function main()
- Every C program must have exactly one
main()function -- it is the entry point of execution. - Contains two parts:
- Local Declaration: Variables declared inside
main(), accessible only within it. - Executable Statements: The actual logic -- assignments, function calls, loops, etc.
- Local Declaration: Variables declared inside
- Returns an integer value to the operating system (
return 0indicates successful execution).
6. User-Defined Functions
- Functions written by the programmer to perform specific tasks.
- Defined after (or before)
main(). - Promotes modularity and code reusability.
Summary Table
| Section | Purpose |
|---|---|
| Documentation | Comments and program description |
| Link Section | Includes header/library files |
| Definition Section | Defines constants and macros |
| Global Declaration | Declares global variables |
main() Function | Entry point; contains program logic |
| User-Defined Functions | Custom functions for specific tasks |
2077 · 5 marks
What is the basic structure of a C program? Explain each part. [5]
Show solved answerHide answer
Basic Structure of a C Program
Basic Structure of a C Program
A C program is organized into several distinct sections. Below is the general template:
/* Documentation Section */
// Program description, author, date
/* Link Section */
#include <stdio.h>
#include <stdlib.h>
/* Definition Section */
#define PI 3.14159
/* Global Declaration Section */
int globalVar = 10;
/* Main Function */
int main()
{
/* Local Declaration */
int a, b;
/* Executable Statements */
printf("Hello, World!\n");
return 0;
}
/* User-Defined Functions */
void myFunction()
{
// function body
}
Explanation of Each Part
1. Documentation Section
- Contains comments about the program such as program name, author, date, and purpose.
- Written using
/* ... */(multi-line) or//(single-line). - Ignored by the compiler; meant for human readability.
2. Link Section (Preprocessor Directives)
- Contains header file inclusions using
#include. - Tells the compiler to include standard library files before compilation.
- Example:
#include <stdio.h>includes standard input/output functions likeprintf()andscanf().
3. Definition Section
- Contains macro definitions using
#define. - Defines symbolic constants or macros used throughout the program.
- Example:
#define PI 3.14159
4. Global Declaration Section
- Declares global variables and function prototypes.
- Global variables are accessible by all functions in the program.
- Example:
int count = 0;
5. Main Function (main())
- The entry point of every C program; execution always begins here.
- Contains two parts:
- Local Declaration: Variables declared inside
main(). - Executable Statements: The actual logic, calculations, input/output operations.
- Local Declaration: Variables declared inside
- Returns an integer value (typically
0to indicate successful execution).
6. User-Defined Functions
- Functions written by the programmer to perform specific tasks.
- Defined after
main()(or declared before using prototypes). - Promotes modularity and code reusability.
Summary Table
| Section | Purpose |
|---|---|
| Documentation | Comments and description |
| Link | Include header files |
| Definition | Define constants/macros |
| Global Declaration | Declare global variables |
main() | Program execution starts here |
| User-Defined Functions | Custom reusable functions |
0 · 5 marks
Define the basic structure of C program. Explain the Compilation and Execution of a C program? [5]
Comments and escape sequences
2078 · 5 marks
Write Short Notes on: a) Escape Sequence Write Short Notes on: b) Null Pointer [2.5+2.5]
0 · 5 marks
What are the uses of comments and escape sequences in a C program, explain with example? [5]
Identifiers and keywords
2077 · 10 marks
What is an Identifier and keyword? Explain the rules for naming valid identifiers in C with example.Write a C program to find sum of digits of a 'n' digit number.[5+5]
Compilation and execution process
2077 · 5 marks
Find errors (If any) in the following program and rewrite it.
#define (studio,h)
float main < >
int m [ ] [ ] = 1,2,3,4;
for (i = 4, I>=0 ; i--)
for (j=0, j <4; j++);
m[i] [j] = a[i][j] + b[i][j];
return 0. 0;
}
[5]
Tokens and statements
2081.2 · 5 marks
Define token. Describe any three types of statements. [5]
Make Unit 1 stick
Practice BIT102 with flashcards & quizzes