C Language review. its review about arrays, Strings, pointers etc.. The first high-level programming languages were designed in the 1950s. Now there are dozens of
different languages, including Ada , Algol, BASIC, COBOL, C, C++, JAVA, FORTRAN, LISP,
Pascal, and Prolog. Such languages are considered high-level because they are closer to human
languages and farther from machine languages. In contrast, assembly languages are considered lowlevel because they are very close to machine languages. High-level language is any programming language that enables development of a program in much
simpler programming context and is generally independent of the computer's hardware architecture.
High-level language has a higher level of abstraction from the computer, and focuses more on the
programming logic rather than the underlying hardware components such as memory addressing and
register utilization. Machine code or machine language is a set of instructions executed directly by a computer's central
processing unit (CPU). Each instruction performs a very specific task, such as a load, a jump, or an
ALU operation on a unit of data in a CPU register or memory. Every program directly executed by a CPU is made up of a series of such instructions.
Polymorphism means the ability to take more than one form. An operation may exhibit different
instance. The behaviour depends upon the type of data used in the operation.
A language feature that allows a function or operator to be given more than one definition. The types
of the arguments with which the function or operator is called determines which definition will be
used.
Overloading may be operator overloading or function overloading.
2. C Orientation
• Created in 1972 to write operating systems (Unix
in particular)
– By Dennis Ritchie
– Bell Labs
• Evolved from B
• Can be portable to other hardware
• Built for performance and memory management
– operating systems, embedded systems, real-
time systems, communication systems
3. What are the advantages of C?
• Portable: allows code to run on different computers and operating
systems without making any change.
• Efficient: It is a general-purpose programming language.
Therefore it works efficiently.
• Case-sensitive: You need to be very careful while writing the code
as it treats lowercase and uppercase letter differently.
• Memory Manipulation and allocation: allows allocating the
memory dynamically.
• Middle-level language: It merges the features of both low level
and high-level languages in itself.
4. What are the disadvantages of C?
• C language is devoid with the terminology and the concept of
OOPS which is a very popular and an important concept these
days among all high-level programming language.
• No Strict type checking possible.
• No checks for runtime
• It doesn’t give us the provision of having a namespace.
• It also doesn’t have the concept of the constructor as well as a
destructor.
5. A First Program
#include <stdio.h>
int main(void)
{
printf("This is my first C program.n");
return(0);
}
statements
header
open and close braces mark
the beginning and end
makes input
and output available
to us
6. A First Program – What Does It Do?
printf("This is my first C program.n");
return(0);
Prints the message
This is my first C program.
Ends the program Ends the line
7. C Program Phases
• Editor - code by programmer
• Compiling using gcc:
– Preprocess – expand the programmer’s code
– Compiler – create machine code for each file
– Linker – links with libraries and all compiled
objects to make executable
• Running the executable:
– Loader – puts the program in memory to run it
– CPU – runs the program instructions
10. Using variables
#include <stdio.h>
int main(void)
{
int sum, value1, value2, value3;
float average;
value1 = 2;
value2 = 4;
value3 = 6;
sum = 2 + 4 + 6;
average = sum / 3;
printf("The average of %d , %d, %d is %fn",
value1, value2, value3, average);
return(0);
}
Print a float value from the
rest of the parameter list
11. A program that uses a character variable
#include <stdio.h>
/* A very polite program that greets you by name */
int main(void)
{
char name[25];
/* Ask the user his/her name */
printf("What is your name ? ");
scanf("%s", name);
/* Greet the user */
printf("Glad to meet you, %sn.", name);
return(0);
}
12. Features so far
• Include
• Variable types: int, float, char
• Read using scanf
– requires & for address of variable being read
• Print using printf
• Format strings: %f (float), %d (int), %u
(unsigned int), %c (char), %s (character array)
• Comments /*.. */ or //
13. Array - Review
• Array – a set of elements all of the same type
stored contiguously in memory – e.g.,
– int A[25]; // 25 integers
– struct Str B[15]; /* 15 objects of
type struct Str */
– double C[]; /* indeterminate #
of doubles */
• Pointer – a variable whose value is the
location of some other object
– float *p; // pointer to a float
14. Array - Review
int A[25]; // 25 integers
• Type of A[i] is int (for all i).
• Type of A is int *
• I.e., pointer to int
15. 15
Strings in C
• Definition:– A string is a character array ending in
'0' — i.e.,
– char s[256];
– char t[] = "This is an initialized string!";
– char *u = "This is another string!";
• String constants are in double quotes
• May contain any characters
• Including " and '
• String constants may not span lines
• However, they may be concatenated — e.g.,
"Hello, " "World!n" is the same as "Hello, World!n"
16. 16
Strings in C (continued)
• Let
– char *u = "This is another string!";
• Then
– u[0] == 'T'
u[1] == 'h'
u[2] == 'i'
…
u[21] == 'g'
u[22] == '!'
u[23] == '0'
17. Strings, Arrays, and Pointers CS-2303, C-Term 2010 17
Support for Strings in C
• Most string manipulation is done through functions
in <string.h>
• String functions depend upon final '0'
• So you don’t have to count the characters!
• Examples:–
– int strlen(char *s) – returns length of string
• Excluding final '0'
– char* strcpy(char *s, char *ct) – Copies string ct to
string s, return s
• s must be big enough to hold contents of ct
• ct may be smaller than s
18. 18
Support for Strings in C (continued)
• Examples (continued):–
– int strcmp(char *s, char *t)
• lexically compares s and t, returns <0 if s < t, >0 if
s > t, zero if s and t are identical
– D&D §8.6, K&R p. 106
– char* strcat(char *s, char *ct)
• Concatenates string ct to onto end of string s, returns s
• s must be big enough to hold contents of both strings!
• Other string functions
– strchr(), strrchr(), strspn(), strcspn()
strpbrk(), strstr(), strtok(), …
20. Pointers - Review
A pointer is a reference to another variable (memory
location) in a program
– Used to change variables inside a function (reference
parameters)
– Used to remember a particular member of a group (such
as an array)
– Used in dynamic (on-the-fly) memory allocation (especially
of arrays)
– Used in building complex data structures (linked lists,
stacks, queues, trees, etc.)
21. Pointer - Review
Variables are allocated at addresses in computer memory
(address depends on computer/operating system)
Name of the variable is a reference to that memory address
A pointer variable contains a representation of an address of
another variable (P is a pointer variable in the following):
101
V P
Name
Abstract
Representation
Concrete
Representation
Address
4 bytes for
int value 101
4 bytes for
mem address v
v (some value) p (some value)
int V = 101;
int *P = &V;
22. Pointer Variable Definition
Basic syntax: Type *Name
Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints */
– more on how to read complex declarations later
23. Sample Program
#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
printf("Enter the value of x and yn");
scanf("%d%d", &x, &y);
printf("Before Swappingnx = %dny = %dn", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swappingnx = %dny = %dn", x, y);
return 0;
}