Unit 1 (C)
Unit 1 (C)
UNIT I
Introduction
► C programming is considered as the base for other programming languages.
► It is known as mother language.
► The C Language is developed by Dennis Ritchie for creating system
applications that directly interact with the hardware devices such as drivers,
kernels, etc.
► Mother language
► System programming language
► Procedure-oriented programming language
► Structured programming language
► Mid-level programming language[ it supports the feature of both low-level and
high-level languages.]
Features of C Language
► C is the widely used language. It provides many features that are given below.
► Simple and Efficient
► Machine Independent or Portable
► Mid-level programming language
► structured programming language
► Rich Library
► Memory Management
► Fast Speed
► Pointers
► Recursion
► Extensible
Example of a simple C Program
► #include <stdio.h>
► int main(){
► printf("Hello C Language");
► return 0;
► }
C Program Life Cycle
Execution:
► when the program become don’t has errors, the computer execute it to
produce the ……output.
Implement the algorithm
• Errors are of three types:
. syntax errors
. run-time errors
. logic errors
• Logic errors.
- caused by faulty algorithm
- sign of error: incorrect program output
cure: thorough testing and comparison with expected results
► Identifiers
► Keywords
► Operators
► Strings
► Special Characters
► Constant
Identifiers in C
► Ternary Operator
► Using this operator would require a total of three operands.ie conditional Operator ?:
in place of the if-else conditions.
Strings in C
► The strings in C always get represented in the form of an array of
characters. We have a ‘\0′ null character at the end of any string-
thus, this null character represents the end of that string.
► There are different ways in which we can describe a string:
► char x[9] = “chocolate’; // Here, the compiler allocates a total of 9 bytes to the ‘x’
array.
► char x[] = ‘chocolate’; // Here, the compiler performs allocation of memory during
the run time.
► char x[9] = {‘c’,’h’,’o’,’c’,’o’,’l’,’a’,’t’,’e’,’\0′}; // Here, we are representing the
string in the form of the individual characters that it has.
example
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
printf("%s", greetings);
return 0;
}
Output:
Hello World!
Special Characters in C
► The special characters in the C language, holds a special meaning that we cannot use for any
other purpose.
► () Simple brackets – Used this during function calling as well as during function declaration.
For instance, the function printf() is pre-defined.
► [ ] Square brackets – The closing and opening brackets represent the multidimensional and
single subscripts.
► (,) Comma – Used the comma for separating more than one statement, separating the function
parameters used in a function call, and for separating various variables when we print the value
of multiple variables using only one printf statement.
► { } Curly braces – Used it during the closing as well as opening of any code. We also use the
curly braces during the closing and opening of the loops.
► (*) Asterisk – Used for representing the pointers and we also use this symbol as a type of
operator for multiplication.
► (#) Hash/preprocessor – Used for the preprocessor directive. This processor basically denotes
that the user is utilizing the header file.
► (.) Period – Used for accessing a member of a union or a structure.
► (~) Tilde –Used in the form of a destructor for free memory.
Constant in C
► Constant is basically a value of a variable that does not change throughout a program.
► This will declare the variable as "constant“(const), which
means unchangeable and read-only:
► Example
► const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum‘
Comments in C
► Comments can be used to explain code, and to make it more readable. It can also be used
to prevent execution when testing alternative code.
► Comments can be singled-lined or multi-lined.
► Single-line comments start with two forward slashes (//).
► Any text between // and the end of the line is ignored by the compiler (will not be
executed). Normally used // for short comments
► Example
#include <stdio.h>
int main() {
// This is a comment
printf("Hello World!");
return 0;
}
▪ Multi-line Comments
► Multi-line comments start with /* and ends with */.
► Any text between /* and */ will be ignored by the compiler:
► Example:
#include <stdio.h>
int main() {
/* The code below will print the words Hello World!
to the screen, and it is amazing */
printf("Hello World!");
return 0;
}
Output function(printf statements).
► Syntax:
Form 1 : printf(“string”);
#include <stdio.h>
int main() {
// Create an integer variable that will store the number we get from the user
int myNum;
// Ask the user to type a number
printf("Type a number and press enter: \n");
// Get and save the number the user types
scanf("%d", &myNum);
// Print the number the user typed
printf("Your number is: %d", myNum);
return 0;
}
Types Of Data Types In C
Data type Format Specifier
Octal integer %o
Hexadecimal integer %x
#include <stdio.h>
int main()
{
int sum=0;
printf("The memory address of sum is %p \n",&sum);
return 0;
}
#include <stdio.h>
int main() {
int x = 5;
printf("%d", --x);
return 0;
}
► Write a Program to perform all arithmetic operations:
#include <stdio.h>
int main()
{
int x, y;
int sum, sub, mult, mod;
float div;
printf("Enter any two numbers: \n ");
scanf("%d%d", &x, &y);
sum = x+ y;
sub = x - y;
mult = x * y;
div = (float)x/ y;
mod = x % y;
printf("SUM = %d\n", sum);
printf("DIFFERENCE = %d\n", sub);
printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);
return 0;
}
► Write a program to subtract two long integers.
#include<stdio.h>
#include<conio.h>
int main()
{
long int num1, num2, diff=0;
printf("\n Enter the numbers:" );
scanf("%ld %ld", &num1, &num2);
diff= num1-num2;
printf("\n Difference = %ld", diff);
return 0;
}
Relational Operators in C
► The relational operators are used to perform the relational operation between two
operands or variables. e.g. comparison, equality check, etc.
► < If an expression is x < y, it will return TRUE if and only if x is
less than y, otherwise it will return FALSE.
► <= If an expression is x <= y, it will return TRUE if and only if x is
less than or equal to y, otherwise it will return FALSE.
► > If an expression is x > y, it will return TRUE if and only if x is
greater than y, otherwise it will return FALSE.
► >= If an expression is x >= y, it will return TRUE if and only if x is
greater than or equal to y, otherwise it will return FALSE.
► == If an expression is x == y, it will return TRUE if and only if x is
equal to y, otherwise it will return FALSE.
► != If an expression is x != y, it will return TRUE if and only if x is
not equal to y, otherwise it will return FALSE.
Bitwise Operators
► Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &,
|, and ^ are as follows: