CSE 101: Fundamental C Programming
Concepts
A Comprehensive Guide Based on Your PPT Lecture
1. The C Character Set
The C Character Set defines all the legal characters used to write a C program. Any character
outside this set causes a compilation error.
Uppercase letters: A to Z
Lowercase letters: a to z
Digits: 0 to 9
Special characters: +, -, /, *, %, =, &, |, !, <, >, (, ), {, }, [, ], ;, :, ?, ,, ., _, ", ', \, #
Escape Sequences: \n (newline), \t (tab), \b (backspace), etc.
Whitespace characters: space, tab (\t), newline (\n), carriage return (\r), form feed (\f)
2. Identifier
An identifier is a name given to programming elements such as variables, functions, arrays, etc.
Rules for Creating Identifiers:
First character must be a letter or underscore (_).
Subsequent characters can be letters, digits, or underscores.
Special characters (e.g., $, @, -) are not allowed.
Cannot be a keyword.
C is case-sensitive (myVar, MyVar, myvar are different).
Keep them concise but meaningful (older compilers use only first 31 characters).
Examples of Valid Identifiers:
X
y12
sum_1
area
_sum
record1
Name_address
Name
File_3
total_students
gpaCalculator
Examples of Invalid Identifiers:
4X (Starts with digit)
"y12" (Quotes used)
sum-1 (Hyphen not allowed)
ar ea (Contains space)
$tax (Special character)
return (Keyword)
123_25 (Starts with digits)
my@variable (Contains @)
3. Keyword
Keywords are reserved words in C with predefined meanings. They cannot be used as identifiers.
Rules for Keywords:
Always written in lowercase.
Cannot be redefined or used for other purposes.
Common Keywords:
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while
4. Variables & Constants
4.1 Variable
A variable is a named memory location to store data. The value of a variable can be changed
during program execution.
Examples:
int age = 20;
float gpa = 3.75;
Declaration Syntax: data_type variable_name;
Different Forms of Declaration:
int x;
x = 10;
int y = 20;
float a, b, c;
float radius = 1.5, height = 2.5;
Variables are case-sensitive: myNumber, MyNumber, and mynumber are different.
Declaration Scenarios
Without Initial Values:
int p, q;
float x, y, z;
char a, b, c;
With Initial Values:
int p = 129, q = 87;
float x = 8.2, y = 0.5;
char a = 'k', b = 'e';
Mixed Case Variables:
int a, B, c = 23;
float p = 23.75, q, R;
char x = 's', y, z;
4.2 Constant
A constant has a fixed value which does not change during program execution.
Types of Constants:
Integer Constants: 10, 450, -75, 0
Real Constants: 3.14, -0.005, 1.23e-5
Character Constants: 'A', 'z', '5', '\n'
String Literals: "Hello World", "123"
Defining Constants:
Using const keyword:
const float PI = 3.14159;
const int MAX_ATTEMPTS = 3;
const char NEWLINE_CHAR = '\n';
Using #define directive:
#define PI_DEFINE 3.14159265
#define MAX_BUFFER_SIZE 1024
#define MESSAGE "Hello, C Program!"
5. Data Types
A data type defines the type of data a variable can hold and how much memory it occupies.
int - Integer (whole numbers)
Size: 4 bytes
Example: int age = 20;
Literals: 20, -100, 0
Specifier: %d
float - Single-precision decimal
Size: 4 bytes
Example: float gpa = 3.5f;
Literals: 3.5, -1.23
Specifier: %f
double - Double-precision decimal
Size: 8 bytes
Example: double pi = 3.14159;
Literals: 3.14159, 70.5
Specifier: %lf (scanf), %f (printf)
char - Single character
Size: 1 byte
Example: char initial = 'A';
Literals: 'A', 'z', '5'
Specifier: %c
Modifiers: short, long, signed, unsigned - alter memory size or value range.
6. Operators and Expressions
Operators are symbols that perform operations. Expressions combine variables, constants, and
operators.
6.1 Arithmetic Operators
Used for mathematical operations: +, -, *, /, %
Operator Precedence:
High: *, /, % (Left to Right)
Low: +, - (Left to Right)
Example: y = 9 - 12 / (3 + 3) * 2 - 1; yields y = 4
6.2 Relational Operators
Compare two values. Result is 1 (true) or 0 (false).
<, <=, >, >=, ==, !=
6.3 Logical Operators
Used in control structures like if, while.
&& - Logical AND
|| - Logical OR
! - Logical NOT
6.4 Assignment Operators
Assign values to variables.
=, +=, -=, *=, /=, %=
6.5 Increment and Decrement Operators
Unary operators to increase (++) or decrease (--) value by 1.
Prefix (++x) vs. Postfix (x++) determines when increment happens.
7. Additional Clarifications & Tips
7.1 Comments in C
Comments help explain code and improve readability. They are ignored by the compiler.
// Single-line comment
/* Multi-line
comment */
Tip: Use comments to explain why something is done, not what is done.
7.2 Type Conversion (Typecasting)
Used to convert values between different data types.
Implicit Conversion: Done automatically by the compiler.
Explicit Conversion (Typecasting):
float result = (float) 5 / 2; // result is 2.5
7.3 Format Specifier Mismatch
Always use correct format specifiers with scanf/printf to avoid undefined behavior.
int a; scanf("%d", &a);
Incorrect format specifiers can cause unexpected output or crashes.
7.4 Common Beginner Mistakes
Using = instead of == in conditions.
Forgetting & in scanf (e.g., scanf("%d", &x);).
Not initializing variables (contain garbage values).
Buffer overflow in scanf("%s", str); (no size limit).
7.5 Importance of Practice
C programming builds core logic and teaches low-level understanding.
Write small programs like calculators or guessing games.
Trace output manually to understand flow.
Experiment with data types and operators.