Download C Programming Language Fundamentals: A Comprehensive Syllabus and more Study notes C programming in PDF only on Docsity!
PROGRAMMING IN
“C” language
By
KAMMARA KESAVA
SYLLABUS
UNIT-l
C Language Fundamentals, Character set, Identifiers, keyword, data types,
Constants and variables, statements, expression, operators, precedence of
operators, Input-output, Assignments, control structures decision making
and branching.
UNIT-ll
Arrays, Functions and Strings:
Declaration, manipulation and String – handling functions, monolithic vs.
Modular programs, user defined vs. standard functions, formal vs. actual
arguments, function – category, function prototypes, parameter passing,
recursion, and storage classes: auto, extern, global, static.
UNIT-lll
Pointers, Structures, Unions, File handling:
Pointer variable and its importance, pointer arithmetic, passing parameters,
Declaration of structures, pointer to pointer, pointer to structure, pointer to
function, union, dynamic memory allocation, file managements.
Unit-ll Lecture 13: String library functions Lecture 14: functions, categories Lecture 15: functions categories cont.. Lecture 16: Actual arguments and Formal arguments, call by value call by reference Lecture 17:local, global, static variable Lecture 18: monolithic vs modular programming, Storage classes Lecture 19:storage class cont.., pointer Lecture 20: pointer comparison, increment decrement Lecture 21: precedence level of pointer, pointer comparison Lecture 22: pointer to pointer, pointer to structure Lecture 23: pointer initialization, accessing elements
Unit-lll Lecture 24: size of Structure in, array vs structure, array within structure Lecture 25: passing structure to function, Nested Structure Lecture 26: Union Lecture 27: nesting of unions, dynamic memory allocation Lecture 28: dynamic memory allocation conti… Lecture 29: dynamic array, file Lecture 30: file operation Lecture 31: file operation on string
problem-solving methods. Programs are sequences of instructions
executing defined algorithms, leveraging a computer's capabilities.
So as programming languages concern these are of two types.
1) Low level language
2) High level language
Low level language:
Low-level languages like machine code and assembly language
operate at the hardware level. Machine code uses binary digits (0s and 1s),
making it challenging to write and maintain. Assembly language, a step up, uses
English-like instructions (e.g., ADD, MOV), making it easier for humans but still
requires translation to machine code by an assembler. Assembly language
programs are tied to specific computer architectures due to register usage,
making them non-portable.
High level language:
High-level languages, like Python and Java, offer human-readable syntax,
making them easier to understand and maintain. They are platform-independent
and translated into machine code by compilers or interpreters. High-level
languages abstract away low-level hardware details, enabling focus on
problem-solving. They are widely used across software development, data
science, and artificial intelligence fields.
Now let us discuss about translators, There are three types:
1.Compiler
2.Interpreter
3.Assembler
Compiler and interpreter convert high-level language to machine code. Source
program becomes object program. Compiler checks errors at once, preferred for
large programs. Interpreter checks line-by-line, slower but suitable for smaller
programs
Integrated Development Environments (IDE):
IDEs (Integrated Development Environments) simplify program management,
offering editing, compiling, running, and debugging tools. Notable examples
include CodeWarrior and Xcode for Mac OS X, Microsoft Visual Studio for
Windows, and Kylix for Linux. These platforms support various programming
languages like C#, C++, in addition to C.
Lecture-
Structure of C Language program
A C program typically consists of several elements:
1.Preprocessor Directives: These are instructions to the preprocessor, such as
including header files (#include) or defining macros (#define).
2.Global Declarations: This section contains global variables and function
declarations that can be accessed throughout the program.
3.Main Function: Every C program must have a main() function, which serves
as the entry point. Execution of the program begins here.
4.Function Definitions: These sections define the implementation of functions
used in the program. Functions can be defined before or after main().
5.Statements and Expressions: These are the actual instructions that perform
tasks within the program, such as variable assignments, control flow statements
(if-else, loops), and function calls.
6.Comments: Comments are used for documentation within the code. They are
ignored by the compiler and serve to explain the logic or purpose of code
segments.
Here's a simple example illustrating the structure of a C program:
Steps for Compiling and executing the Programs
A C program is compiled and executed through the following steps:
1.Writing the Program: Using a text editor, write the C program in a file
with a .c extension.
2.Compilation: Use a compiler (e.g., gcc or cc on Unix) to compile the
source file (prog1.c).
3.Error Checking: Compiler checks for syntax and semantic errors. If
errors exist, they must be corrected in the source file.
4.Translation: Compiler translates the program into assembly language,
then into object code.
5.Linking: Assembler links object code with system libraries and other
modules to create an executable file (default: a.out on Unix).
6.Execution: Execute the program (a.out by default) by loading it into
memory and running it. Debugging may be necessary to fix any issues
encountered.
Example:
1.Sum of two numbers
Output:
Lecture- Character set, identifiers, keywords
1.Character set:
In C programming, a character encompasses any alphabet, digit, or special
symbol used for conveying information. These elements, when correctly
combined, constitute constants, variables, and keywords within the language.
Alphabets
Digits
Special symbols
A to Z (capital letters)
a to z (lower letters)
~, @,! ,$, # ,^ ,* ,% ,& ,( ), [ ] ,{ } ,<, > ,+ ,= ,_
,–, /,, ; ,:, ‘ ,“ ,. ,?
2. Identifiers:
For a valid identifier we should follow certain rules while naming variables,
the rules are given below.
1.Use only letters (a-z or A-Z) and digits (0-9) in identifiers.
2.Special characters are not allowed, except for the underscore _.
3.Spaces are not permitted within identifiers.
4.Identifiers must begin with a letter or underscore, not a digit.
5.Avoid naming identifiers after keywords reserved for specific tasks, like
printf, scanf, int, char, struct, etc., to prevent compiler errors.
6.Each identifier must be unique within its namespace.
7.Remember that C is case-sensitive, so name and NAME are treated as
distinct identifiers.
3.Keyword:
Reserved words, also known as keywords, are predefined terms in C
programming that serve specific functions and must be written in
lowercase. They cannot be used as variable names as they are assigned
fixed meanings.They are 32 keywords in c programming. Some of them are
- int, short, signed, unsigned, default, volatile, float, long, double, break,
Lecture- Topics: constants and variables 1.Constants:
In C, a constant is a value that remains unchanged during program
execution, such as a number, single character, or character string.
Constants are immutable, unlike variables, which can change. For
instance, the integer value 50 or the character string "Dhoni is a great
captain\n" are examples of constants. C constants can be classified into
two main categories:
1.Primary Constants - Integer, Real, Character Constants
2.Secondary Constants - Array, String, pointer, Structure, Enum, Union etc.
2.Variables:
A variable in C is a named entity used to store data values or represent
program computations and results. Unlike constants, the value of a variable
can change during program execution. Variable names must adhere to the
same rules as identifiers. Before use in a program, variables must be
declared, specifying their name, data type, and the range of values they
can store based on their data type.
Syntax for variable declaration:
Int a;
Char b;
Float c;
Variable initialization occurs when an initial value is assigned to a variable
during declaration. When a variable is declared but remains undefined, it
contains a garbage value. Initialization is achieved using the assignment
operator:
Data type variable name=constant;
Example: int a=20; Or int a; a=20;
Lecture-5 & 6 Topics: Expressions and operators
1.Expressions:
Expressions in C are combinations of variables, constants, operators, and
function calls, categorized into arithmetic, logical, and relational types.
Examples include:
● Arithmetic expression: int z = x + y
● Relational expression: a > b
● Logical expression: a == b
● Function call: func(a, b)
Expressions composed solely of constant values are termed constant
expressions. For instance, 121 + 17 - 110 qualifies as a constant
expression because each term is a constant value. However, if i were an
integer variable, the expression 180 + 2- j would not be a constant
expression.
2.Operators:
Operators in C are symbols that perform operations on operands. These
operations can include arithmetic, logical, relational, bitwise, assignment,
and other types of operations. Common operators in C include:
Operators Types
++,-- Unary operators
+, -, *, /, % Arithmetic operators
<, <=, >, >=, ==,!= Relational operators
&&, ||,! Logical operators
&, |, <<, >>, ~, ^ Bitwise operators
+=, -=, *=, /=, %= Assignment operators
?: Ternary operators
printf("%u & %u is %u\n", m, n, m & n); printf("%u | %u is %u\n", m, n, m | n); printf("%u ^ %u is %u\n", m, n, m ^ n); printf("~%u is %u\n", m, ~m); printf("%u << 1 is %u\n", m, m << 1); printf("%u >> 1 is %u\n", m, m >> 1); // Assignment operators int c = 10; c += 5; // equivalent to c = c + 5 printf("\nAssignment Operators:\n"); printf("c = %d\n", c); // Increment and decrement operators int d = 10; printf("\nIncrement and Decrement Operators:\n"); printf("d++ is %d\n", d++); printf("++d is %d\n", ++d); printf("d-- is %d\n", d--); printf("--d is %d\n", --d); // Ternary operator int e = 20, f = 30; int max = (e > f)? e : f; printf("\nTernary Operator:\n"); printf("Maximum value: %d\n", max); return 0;
Output:
int i = 0; // If...else statement if (num > 0) { printf("Number is positive.\n"); } else { printf("Number is non-positive.\n"); } // Switch statement switch(num) { case 10: printf("Number is 10.\n"); break; case 20: printf("Number is 20.\n"); break; default: printf("Number is neither 10 nor 20.\n"); } // While loop printf("Counting from 0 to 4 using while loop:\n"); while (i < 5) { printf("%d ", i); i++; } printf("\n"); // Do...while loop i = 0; printf("Counting from 0 to 4 using do...while loop:\n"); do { printf("%d ", i); i++; } while (i < 5);
printf("\n"); // For loop printf("Counting from 0 to 4 using for loop:\n"); for (i = 0; i < 5; i++) { printf("%d ", i); } printf("\n"); // Break statement printf("Exiting loop using break statement:\n"); for (i = 0; i < 10; i++) { if (i == 5) { break; } printf("%d ", i); } printf("\n"); // Continue statement printf("Skipping iteration using continue statement:\n"); for (i = 0; i < 5; i++) { if (i == 2) { continue; } printf("%d ", i); } printf("\n"); // Goto statement printf("Using goto statement to jump to a label:\n"); i = 0; start: printf("%d ", i); i++; if (i < 5) {