1 Getting Started
What is C?
Getting Started with C
The C Character Set
Constants, Variables and Keywords
Types of C Constants
Rules for Constructing Integer Constants
Rules for Constructing Real Constants
Rules for Constructing Character Constants
Types of C Variables
Rules for Constructing Variable Names
C Keywords
What is C?
C is a programming language developed at AT & T’s Bell Laboratories of
USA in 1972. It was designed and written by a man named Dennis Ritchie.
The C Character Set
A character denotes any alphabet, digit or special symbol used to represent information.
Constants, Variables and Keywords
The alphabets, digits and special symbols when properly combined form constants, variables and keywords. Let us now understand the meaning of each of them. A constant is an thing that doesn’t change, whereas, a variable is an thing that may change. A keyword is a word that carries special meaning.
Types of C Constants
C constants can be divided into two major categories:
- Primary Constants
- Secondary Constants
Rules for Constructing Integer Constants
- An integer constant must have at least one digit.
- It must not have a decimal point.
- It can be either positive or negative.
- If no sign precedes an integer constant, it is assumed to be positive.
- No commas or blanks are allowed within an integer constant.
- The allowable range for integer constants is -2147483648 to +2147483647.
Rules for Constructing Real Constants
Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form.
Following rules must be observed while constructing real constants expressed in fractional form:
- A real constant must have at least one digit.
- It must have a decimal point.
- It could be either positive or negative.
- Default sign is positive.
- No commas or blanks are allowed within a real constant.
Rules for Constructing Character Constants
- A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
Types of C Variables
A particular type of variable can hold only the same type of constant. For example, an integer variable can hold only an integer constant, a real variable can hold only a real constant and a character variable can hold only a character constant.
Rules for Constructing Variable Names
- A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters
- The first character in the variable name must be an alphabet or underscore ( _ ).
- No commas or blanks are allowed within a variable name.
- No special symbol other than an underscore (as in gross_sal) can be used in a variable name.
C Keywords
Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). There are only 32 keywords available in C. The keywords cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed.
/* Calculation of simple interest */ /* Author: HTCE Date: 28/08/2024 */ # include <stdio.h> int main( ) { int p, n ; float r, si ; p = 1000 ; n = 3 ; r = 8.5 ; /* formula for simple interest */ si = p * n * r / 100 ; printf ( “%f\n” , si ) ; } | /* Calculation of Compound interest */ /* Author: HTCE Date: 28/08/2024 */ # include <stdio.h> # Include <math.h> int main( ) { int p, n ; float r, ci ; p = 1000 ; n = 3 ; r = 8.5 ; /* formula for compound interest */ ci = p *Pow((1+r/100), n )-p ; printf ( “%f\n” , ci ) ; } |
- Every C statement must end with a semicolon ( ; ). Thus ; acts as a statement terminator.
Comments in a C Program
Comments are used in a C program to clarify either the purpose of the program or the purpose of some statement in the program.
Comment about the program should be enclosed within /* */.
- Constant is an entity whose value remains fixed.
- Variable is an entity whose value can change during course of execution of the program.
- Keywords are special words whose meaning is known to the Compiler.
- There are certain rules that must be followed while building constants or variables.
- The three primary constants and variable types in C are integer, float and character.
- We should not use a keyword as a variable name.
- Comments should be used to indicate the purpose of the program or statements in a program.
- Comments can be single line or multi-line.
Types of Instructions
There are basically three types of instructions in C:
- Type Declaration Instruction – This instruction is used to declare the type of variables used in a C program.
- Arithmetic Instruction – This instruction is used to perform arithmetic operations on constants and variables.
- Control Instruction – This instruction is used to control the sequence of execution of various statements in a C program.
Hierarchy of Operations
The priority in which the operations in an arithmetic statement are performed is called the hierarchy of operations.
- Instructions in a program control the behavior/working of the program.
- A C program can contain three types of instructions—Type declaration instruction, Arithmetic instruction, Control instruction.
- An expression may contain any sequence of constants, variables and operators.
- An expression is evaluated based on the hierarchy or precedence of operators.
- Operators having equal precedence are evaluated using associativity of operators.
- Associativity of all operators is either left to right or right to left.
The if Statement
C uses the keyword if to implement the decision control instruction.
Data Types
- Integers, long and short
- Integers, signed and unsigned
- Chars, signed and unsigned
Floats and Doubles
Storage Classes in C
storage class tells us:
- Where the variable would be stored.
- What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the default initial value).
- What is the scope of the variable; i.e. in which functions the value of the variable would be available.
- What is the life of the variable; i.e. how long would the variable exist.
There are four storage classes in C:
- Automatic storage class
- Register storage class
- Static storage class
- External storage class
The features of a variable defined to have an automatic storage class are as under:
Storage: Memory.
Default value: a garbage value.
Scope: Local to the block in which the variable is defined.
Life: Till the control remains within the block in which the variable is defined.
# include <stdio.h>
int main( )
{
auto int i, j ; printf ( “%d %d\n”, i, j ) ;
return 0 ;
Register Storage Class
The features of a variable defined to be of register storage class are as under:
Storage: CPU registers.
Default value: Garbage value.
Scope: Local to the block in which the variable is defined.
Life: Till the control remains within the block in which the variable is defined.
Static Storage Class
The features of a variable defined to have a static storage class are as under:
Storage: Memory.
Default value: Zero.
Scope: Local to the block in which the variable is defined.
Life: Value of the variable persists between different function calls.
static int x = 0;
#include <stdio.h>
int myfunc()
{
static int sum =4 ;
sum++;
return sum;
}
void main()
{
printf(“sum = %d \n”, myfunc());
printf(“sum = %d \n”, myfunc());
}
External Storage Class
The features of a variable whose storage class has been defined as external are as follows:
Storage: Memory.
Default value: Zero.
Scope: Global.
Life:As long as the program’s execution doesn’t come to an end.
What is a Function?
A function is a self-contained block of statements that perform a coherent task of some kind. Every C program can be thought of as a collection of these functions.
- A C program is a collection of one or more functions.
- If a C program contains only one function, it must be main( ).
- If a C program contains more than one function, then one (and only one) of these functions must be main( ), because program execution always begins with main( ).
- There is no limit on the number of functions that might be present in a C program.
- Each function in a program is called in the sequence specified by the function calls in main( ).
- After each function has done its thing, control returns to main( ). When main( ) runs out of statements and function calls, the program ends.
- Why use Functions?
Writing functions avoids rewriting the same code over and over.
By using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently.
Passing Values between Functions
/* Sending and receiving values between functions */
# include <stdio.h>
int calsum ( int x, int y, int z ) ;
int main( )
{
int a, b, c, sum ;
printf ( “Enter any three numbers ” ) ;
scanf ( “%d %d %d”, &a, &b, &c ) ;
sum = calsum ( a, b, c ) ;
printf ( “Sum = %d\n”, sum ) ;
return 0 ;
}
int calsum ( int x, int y, int z )
{
int d ;
d = x + y + z ;
return ( d ) ;
}
An Introduction to Pointers
# include <stdio.h>
int main( )
{
int i = 3 ;
int *j ;
j = &i ;
printf ( “Address of i = %u\n”, &i ) ;
printf ( “Address of i = %u\n”, j ) ;
printf ( “Address of j = %u\n”, &j ) ;
printf ( “Value of j = %u\n”, j ) ;
printf ( “Value of i = %d\n”, i ) ;
printf ( “Value of i = %d\n”, *( &i ) ) ;
printf ( “Value of i = %d\n”, *j ) ;
return 0;
}
# include <stdio.h>
int main( )
{
int i = 3, *j, **k ;
j = &i ; k = &j ;
printf ( “Address of i = %u\n”, &i ) ;
printf ( “Address of i = %u\n “, j ) ;
printf ( “Address of i = %u\n “, *k ) ;
printf ( “Address of j = %u\n “, &j ) ;
printf ( “Address of j = %u\n “, k ) ;
printf ( “Address of k = %u\n “, &k) ;
printf ( “Value of j = %u\n “, j ) ;
printf ( “Value of k = %u\n “, k ) ;
printf ( “Value of i = %d\n “, i ) ;
printf ( “Value of i = %d\n “, * ( &i ) ) ;
printf ( “Value of i = %d\n “, *j ) ;
printf ( “Value of i = %d\n “, **k ) ;
return 0 ;
}
# include <stdio.h>
void swapv ( int x, int y ) ;
int main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
printf ( “a = %d b = %d\n”, a, b ) ;
return 0 ;
}
void swapv ( int x, int y )
{
int t ;
t = x ;
x = y ;
y = t ;
printf ( “x = %d y = %d\n”, x, y ) ;
}
String With Pointers
# include <stdio.h>
int main( )
{
char name[ ] = “Klinsman” ;
char *ptr ;
ptr = name ; /* store base address of string */
while ( *ptr != ‘\0’ )
{
printf ( “%c”, *ptr ) ;
ptr++ ;
}
printf ( “\n” ) ;
return 0 ;
}
Input A String
# include <stdio.h>
int main( )
{
char name[ 25 ] ;
}
printf ( “Enter your name ” ) ;
scanf ( “%s”, name ) ;
printf ( “Hello %s!\n”, name ) ;
return 0 ;
String with get and put
# include <stdio.h>
int main( )
{
char name[ 25 ] ;
printf ( “Enter your full name: ” ) ;
gets ( name ) ;
puts ( “Hello!” ) ;
puts ( name ) ;
return 0 ;
}
Standard Library String Functions
Length of a string with strlen( ) function
# include <stdio.h>
# include <string.h>
int main( )
{
char arr[ ] = “Bamboozled” ;
int len1, len2 ;
len1 = strlen ( arr ) ;
len2 = strlen ( “Humpty Dumpty” ) ;
printf ( “string = %s length = %d\n”, arr, len1 ) ;
printf ( “string = %s length = %d\n”, “Humpty Dumpty”, len2 ) ;
return 0 ;
/* A look-alike of the function strlen( ) */
# include <stdio.h>
int xstrlen ( char * ) ;
int main( )
{
char arr[ ] = “Bamboozled” ;
int len1, len2 ;
len1 = xstrlen ( arr ) ;
len2 = xstrlen ( “Humpty Dumpty” ) ;
printf ( “string = %s length = %d\n”, arr, len1 ) ;
printf ( “string = %s length = %d\n”, “Humpty Dumpty”, len2 ) ;
return 0 ;
}
int xstrlen ( char *s )
{
int length = 0 ;
while ( *s != ‘\0’ )
{
length++ ;
s++ ;
}
return ( length ) ;
}
strcpy( ) Function
# include <stdio.h>
# include <string.h>
int main( )
{
char source[ ] = “Sayonara” ;
char target[ 20 ] ;
strcpy ( target, source ) ;
printf ( “source string = %s\n”, source ) ;
printf ( “target string = %s\n”, target ) ; return 0 ;
}
String copy function by user
# include <stdio.h>
void xstrcpy ( char *, char * ) ;
int main( )
{
char source[ ] = “Sayonara” ;
char target[ 20 ] ;
xstrcpy ( target, source ) ;
printf ( “source string = %s\n”, source ) ;
printf ( “target string = %s\n”, target ) ; return 0 ;
}
void xstrcpy ( char *t, char *s )
{
while ( *s != ‘\0’ )
{
*t = *s ;
s++ ;
t++ ;
}
*t = ‘\0’ ;
}
# include <stdio.h>
int main( )
{
float r, a ;
float pi = 3.14 ;
printf ( “Enter radius of circle ” ) ;
scanf ( “%f”, &r ) ;
a = pi * r * r ;
printf ( “Area of circle = %f\n”, a ) ;
return 0 ;
}
strcat( ) function
for addition of 2 strings
# include <stdio.h> # include <string.h>
int main( )
{
char source[ ] = “Folks!” ;
char target[ 30 ] = “Hello” ;
strcat ( target, source ) ;
printf ( “source string = %s\n”, source ) ;
printf ( “target string = %s\n”, target ) ; return 0 ;
}
strcmp( )
# include <stdio.h>
# include <string.h>
int main( )
{
char string1[ ] = “Jerry” ;
char string2[ ] = “Ferry” ;
int i, j, k ;
i = strcmp ( string1, “Jerry” ) ;
j = strcmp ( string1, string2 ) ;
k = strcmp ( string1, “Jerry boy” ) ;
printf ( “%d %d %d\n”, i, j, k ) ;
return 0 ;
}
# include <stdio.h>
void areaperi ( int, float *, float * ) ;
int main( )
{
int radius ;
float area, perimeter ;
printf ( “Enter radius of a circle ” ) ;
scanf ( “%d”, &radius ) ;
areaperi ( radius, &area, &perimeter ) ;
printf ( “Area = %f\n”, area ) ;
printf ( “Perimeter = %f\n”, perimeter ) ; return 0 ;
}
void areaperi ( int r, float *a, float *p )
{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}
Recursion
# include <stdio.h>
int factorial ( int ) ;
int main( )
{
int a, fact ;
printf ( “Enter any number ” ) ;
scanf ( “%d”, &a ) ;
fact = factorial ( a ) ;
printf ( “Factorial value = %d\n”, fact ) ; return 0 ;
}
int factorial ( int x )
{
int f = 1, i ;
for ( i = x ; i >= 1 ; i– )
f = f * i ;
}
return ( f ) ;