Podcast
Questions and Answers
What will be printed by the program in Example 1 with the static variables?
What will be printed by the program in Example 1 with the static variables?
Which storage class allows variables to hold their value between multiple function calls?
Which storage class allows variables to hold their value between multiple function calls?
In C programming, which class of variable is allocated memory in the CPU registers?
In C programming, which class of variable is allocated memory in the CPU registers?
What happens if you try to dereference a register variable in C?
What happens if you try to dereference a register variable in C?
Signup and view all the answers
What is the default initial value of external integral type variables in C?
What is the default initial value of external integral type variables in C?
Signup and view all the answers
Which type of variable can only be initialized globally, not within any block or method?
Which type of variable can only be initialized globally, not within any block or method?
Signup and view all the answers
What is the visibility of static local variables in C?
What is the visibility of static local variables in C?
Signup and view all the answers
Which keyword is used to define a static variable in C?
Which keyword is used to define a static variable in C?
Signup and view all the answers
What is the purpose of the 'register' keyword in C programming?
What is the purpose of the 'register' keyword in C programming?
Signup and view all the answers
How many times can an external variable be initialized in a C program?
How many times can an external variable be initialized in a C program?
Signup and view all the answers
Study Notes
History and Development of C
- C language was developed in 1972 by Dennis M. Ritchie at Bell Laboratories, USA.
- The American National Standards Institute (ANSI) formed a committee in 1983 to create a standard definition of C, resulting in ANSI C.
- C is closely associated with UNIX, with most of UNIX's code written in C, making it one of the first portable operating systems.
- The language evolved from BCPL and earlier forms, including the B language by Ken Thompson.
- The foundational book on C, "The C Programming Language," was published in 1978 by Brian Kernighan and Dennis Ritchie.
Importance of C
- C is robust with a rich set of built-in functions, data types, and operators facilitating complex program development.
- Programs written in C are efficient due to diverse data types and operators.
- Combines low-level capabilities similar to assembly language with the features of high-level languages.
- Highly portable; code can be executed across different hardware architectures.
- Supports low-level programming like bit manipulation and memory access through pointers.
- User-friendly syntax that closely resembles English.
Basic Structure of C Program
- C programs follow a defined structure:
- Documentation section
- Preprocessor section
- Definition section
- Global declaration
- Main function
- User-defined functions
Key Sections of a C Program
- Documentation Section: Provides an overview of the program (name, description, etc.) using comments.
-
Preprocessor Section: Includes header files, e.g.,
#include <stdio.h>
. -
Define Section: Contains constants defined with
#define
. - Global Declaration: Declares global variables available throughout the program.
- Main Function: Entry point of the program, mandatory for execution. Can be structured with local declarations, statements, and expressions.
- User Defined Functions: Functions created as per user requirements; optional but commonly used.
Program Execution Process
- C program files are saved with a
.c
extension and compiled into an object file (.obj
). - The linker combines object files with library functions to create an executable file (
.exe
). - The loader loads the executable into RAM for execution, managing CPU instructions through the Instruction Register and Program Counter.
Basic C Programs Examples
-
Printing a Message:
#include <stdio.h> int main() { printf("welcome to c programming"); }
-
Addition of Two Numbers:
#include <stdio.h> int main() { int a, b, sum; printf("\nEnter two no: "); scanf("%d %d", &a, &b); sum = a + b; printf("Sum : %d", sum); return 0; }
-
Interest Calculation:
#include <stdio.h> int main() { int p, r, t, int_amt; printf("Input principle, Rate of interest & time:\n"); scanf("%d%d%d", &p, &r, &t); int_amt = (p * r * t) / 100; printf("Simple interest = %d", int_amt); return 0; }
C Math Functions
- C provides mathematical functions in the
<math.h>
library, such as:-
ceil()
: Rounds up to the nearest integer. -
floor()
: Rounds down to the nearest integer. -
sqrt()
: Calculates the square root. -
pow()
: Raises a number to a power. -
abs()
: Returns the absolute value.
-
C Tokens and Keywords
- Tokens: Individual elements in a C program, including keywords, identifiers, constants, and operators.
-
Keywords: Reserved words with predefined meanings in C (e.g.,
int
,return
,if
,while
).
Identifiers and Variable Declaration
- Identifiers are variable names composed of letters, digits, and underscores, beginning with a letter or underscore.
- Variables must be declared with their data types before use, in the format:
data-type id1, id2...;
.
Data Types in C
- C supports four classes of data types:
-
Basic Data Types: Fundamental types for variables (e.g.,
int
,char
,float
,double
). - Derived Data Types: Types that store sets of values (e.g., arrays, structures).
-
User Defined Data Types: Custom types using
typedef
. - Pointer Data Types: Types for storing memory addresses.
-
Basic Data Types: Fundamental types for variables (e.g.,
Constants in C
- Constants do not change during program execution, classified into numeric (e.g., integer, floating-point) and character constants.
- Escape sequences (e.g.,
\n
,\t
) represent special characters in strings.
Enumeration Constants
- Enumerations provide named constants with sequential integer values, improving code readability.
This condensed guide summarises the primary aspects of C programming, its history, structure, and key features.### Variables in C
- A variable is a symbolic name for a memory location used to store data.
- Variables can be reused, and their values can be changed, making them versatile.
- Syntax for declaring a variable:
type variable_list;
- Examples of variable declarations:
-
int a;
-
float b;
-
char c;
-
- Initialization of variables can be done at declaration:
-
int a=10, b=20;
-
float f=20.8;
-
char c='A';
-
Rules for Defining Variables
- Variable names can include alphabets, digits, and underscores.
- Variable names must start with an alphabet or underscore and cannot begin with a digit.
- Whitespace is prohibited within variable names.
- Reserved keywords (e.g., int, float) cannot be used as variable names.
Valid vs Invalid Variable Names
- Valid names:
-
int a;
-
int _ab;
-
int a30;
-
- Invalid names:
-
int 2;
-
int a b;
-
int long;
-
Types of Variables in C
- Local Variable: Declared within a function or block, must be initialized before use.
- Example:
void function1() { int x=10; }
- Example:
- Global Variable: Declared outside any function, accessible from any function.
- Example:
int value=20;
- Example:
- Static Variable: Retains its value across multiple function calls and is declared using the
static
keyword.- Example behavior:
x
remains constant, whiley
increments with each function call.
- Example behavior:
- Automatic Variable: Default for all variables in C, declared inside a block.
- Example:
auto int y=20;
- Example:
- External Variable: Used for sharing a variable across multiple C source files, declared with the
extern
keyword.- Example:
extern int x=10;
- Example:
Storage Classes in C
- Storage classes determine a variable's lifetime, visibility, memory allocation, and initial value.
- Four main types: Automatic, External, Static, Register.
Storage Class Details
-
Automatic:
- Memory allocated at runtime.
- Limited visibility and scope confined to the defining block.
- Default initialization to garbage values.
- Memory is freed upon exiting the block.
- Keyword:
auto
.
-
External:
- Resides in RAM and initialized to zero.
- Global scope and lasts until the end of the main program.
- Can be declared anywhere in the program.
-
Static:
- Resides in RAM and initialized to zero.
- Local scope and retains value across function calls.
- Lifetime lasts until the end of the main program.
-
Register:
- Stored in CPU registers rather than RAM.
- Initialization to garbage values.
- Local scope and lifetime confined to the defining block.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn about the history and development of the C programming language, starting from its origins at Bell Laboratories in 1972 to its standardization by the American National Standards Institute (ANSI) in 1983. Explore how C became closely associated with UNIX and its efficient approach to computer programming.