Apply for Zend Framework Certification Training

c



< Pointers in c Pointer Arithmetic in C >



Introduction to C Programming
C is a general-purpose, procedural programming language widely used for system software,
embedded systems, operating systems, compilers, and application development. It is also an
excellent language for learning fundamental programming concepts such as variables, memory,
pointers, functions, and data structures.
1. History of C
The C programming language was developed by Dennis Ritchie at Bell Laboratories in the early
1970s.
Evolution
A simplified history is:
BCPL → B → C → C++ / Java / C# and many other languages
C was initially developed for the development of the UNIX operating system.
Important points
Developed by Dennis Ritchie
Developed at Bell Laboratories
Created around 1972
Originally used to develop UNIX
Standardized by ANSI in 1989 as ANSI C
Later standardized by ISO
Why is C still important?
Many modern programming languages have concepts that originated from or are strongly influenced by C.
For example:
int age = 20;
The concepts of variables, data types, operators, functions, arrays, and pointers are fundamental to
many programming languages.
2. Features of C
C has several important features.
1. Simple
C has a relatively small set of keywords and a simple syntax.
int a = 10;
int b = 20;
int sum = a + b;
2. Procedural
C follows a procedure-oriented programming approach.
A program can be divided into functions.
void display(){
    printf("Hello");
}
3. Portable
C programs can generally be moved between different platforms with relatively few changes.
For example, the same C source code can potentially be compiled on:
Windows
Linux
macOS
Embedded systems
4. Fast and Efficient
C provides low-level memory access and generally produces efficient machine code.
5. Structured
Large programs can be divided into smaller functions.
int add(int a, int b){
    return a + b;
}
6. Rich Set of Operators
C provides operators such as:
+   -   *   /   %
>   <   ==  !=
&&  ||  !
&   |   ^   <<  >>
7. Supports Pointers
Pointers allow a program to work directly with memory addresses.
int x = 10;
int *ptr = &x;
8. Extensible
C programs can be extended by creating user-defined functions and using libraries.
3. Structure of a C Program
A basic C program generally contains:
Preprocessor directives
Functions
main() function
Statements
Comments
Example:
#include <stdio.h>
int main(){
    printf("Hello World");
    return 0;
}
Explanation
#include
Includes the standard input/output library.
int main()
The main() function is the starting point of program execution.
printf("Hello World");
Displays output on the screen.
return 0;
Indicates successful program termination.
4. Compilation and Execution Process
A C program is not directly executed by the computer. It goes through several stages.
Process
C Source Code
     ↓
Preprocessor
     ↓
Compiler
     ↓
Assembler
     ↓
Linker
     ↓
Executable File
     ↓
Execution
Suppose we have:
#include <stdio.h>
int main(){
    printf("Hello");
    return 0;
}
Step 1: Source Code
The programmer writes the program in a .c file.
hello.c
Step 2: Preprocessing
Preprocessor directives such as:
#include
are processed.
Step 3: Compilation
The compiler checks the C code and converts it into assembly-level code.
Step 4: Assembly
The assembler converts assembly code into machine/object code.
Step 5: Linking
The linker combines the object code with required library code.
For example, the implementation of printf() comes from the standard library.
Step 6: Execution
The final executable program is loaded and executed.
5. Basic C Program
Example:
#include <stdio.h>
int main(){
    int a = 10;
    int b = 20;
    int sum;
    sum = a + b;
    printf("Sum = %d", sum);
    return 0;
}
Output
Sum = 30
Program breakdown
Part Purpose
#include Includes standard I/O functions
main() Starting point
int a = 10 Declares integer variable
sum = a + b Performs addition
printf() Displays result
return 0 Terminates program
6. Tokens in C
A token is the smallest meaningful unit of a C program.
C tokens are broadly classified into:
Keywords
Identifiers
Constants
String literals
Operators
Special symbols
Example:
int sum = a + 10;
Tokens include:
int
sum
=
a
+
10
;
7. Keywords
Keywords are reserved words that have a predefined meaning in C.
Examples:
int
float
char
double
if
else
for
while
return
void
struct
switch
case
break
continue
Example:
int age = 20;
Here:
int → keyword
age → identifier
20 → constant
= → operator
; → special symbol
You cannot use a keyword as a variable name.
Incorrect:
int int = 10;
8. Identifiers
Identifiers are names given to programming elements such as:
Variables
Functions
Arrays
Structures
Example:
int studentAge = 20;
Here:
studentAge
is an identifier.
Rules for identifiers
Can contain letters, digits and underscore.
Cannot start with a digit.
Cannot contain spaces.
Cannot be a keyword.
C is case-sensitive.
Valid:
age
studentAge
student_1
totalMarks
Invalid:
1student
student age
float
total-marks
9. Variables
A variable is a named memory location used to store data.
Example:
int age = 20;
Here:
int  → data type
age  → variable name
20   → initial value
Another example:
float salary = 25000.50;
char grade = 'A';
Declaration
int age;
Initialization
age = 20;
Or both together:
int age = 20;
10. Constants
A constant is a value that does not change during program execution.
Examples:
10
3.14
'A'
"Hello"
Using const
const float PI = 3.14159;
Now attempting to change PI is not allowed.
PI = 4.5;   // Error
Using #define
#define PI 3.14159
Example:
#include
#define PI 3.14159
int main(){
    printf("PI = %f", PI);
    return 0;
}
11. Data Types in C
A data type tells the compiler what kind of data a variable can store.
Common C data types include:
char
int
float
double
void
C also supports derived types such as:
arrays
pointers
functions
and user-defined types such as:
struct
union
enum
typedef
Basic data types
Data Type Typical Purpose
char Character
int Integer
float Single-precision decimal
double Double-precision decimal
void No value
 
Example:
char grade = 'A';
int age = 20;
float price = 99.50f;
double distance = 12345.6789;
The exact size of C data types is implementation-dependent, so use sizeof() when you need
to know the size on a particular system.
Example:
printf("%zu", sizeof(int));
12. Pointers as a Derived Data Type
A pointer is a variable that stores the memory address of another object.
Example:
int number = 10;
int *ptr = &number;
Here:
number → stores 10
ptr    → stores the address of number
Example
#include <stdio.h>
int main(){
    int number = 10;
    int *ptr = &number;
    printf("Value = %d\n", number);
    printf("Address = %p\n", (void *)ptr);
    printf("Value using pointer = %d\n", *ptr);
    return 0;
}
 
Output may look like:
Value = 10
Address = 0x7ffe1234
Value using pointer = 10
&number means address of number.
*ptr means value stored at the address contained in ptr.
13. Basic Input and Output
C commonly uses functions from for input and output.
Output using printf()
#include <stdio.h>
int main(){
    printf("Hello World");
    return 0;
}
Formatting values
int age = 20;
printf("Age = %d", age);
Common format specifiers:
 
Specifier Used for
%d Integer
%f Floating-point
%c Character
%s String
%lf double in scanf()
%p Pointer address
14. Input using scanf()
Example:
#include <stdio.h>
int main(){
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Your age is %d", age);
    return 0;
}
Suppose the user enters:
20
Output:
Your age is 20
Notice:
scanf("%d", &age);
The & gives the address of age, allowing scanf() to store the entered value there.
15. Character Input
#include <stdio.h>
int main(){
    char grade;
    printf("Enter grade: ");
    scanf(" %c", &grade);
    printf("Grade = %c", grade);
    return 0;
}
The space before %c:
" %c"
helps scanf() skip whitespace left in the input stream.
 

< Pointers in c Pointer Arithmetic in C >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top