0% found this document useful (0 votes)
36 views

Introduction To C-Programming

Uploaded by

danieladerinto78
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Introduction To C-Programming

Uploaded by

danieladerinto78
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to C Programming

C is a general-purpose, procedural programming language


developed in the early 1970s by Dennis Ritchie at Bell Labs.
Known for its efficiency and control, C has influenced many
other popular programming languages, such as C++, Java, and
Python. It is widely used in systems programming, embedded
systems, and developing operating systems.
Key Features of C
•Low-level Access: Provides direct manipulation of hardware through pointers.
•Portability: Programs written in C can run on different machines with little or
no modification.
•Rich Library: Comes with a robust standard library that includes many useful
functions.
•Structured Language: Supports structured programming with functions and
control flow constructs.
•Efficient: Produces highly optimized machine code that runs efficiently on
hardware
Variables and Data Types
Variables are storage locations that have a name and can hold a
value.
C supports several basic data types:
int: Integer type.
float: Floating-point type.
char: Character type.
double: Double-precision floating-point type.
Operators
C supports a wide range of operators for performing arithmetic, logical,

relational, and bitwise operations:

 Arithmetic Operators: +, -, *, /, %

 Relational Operators: ==, !=, <, >, <=, >=

 Logical Operators: &&, ||, !

 Bitwise Operators: &, |, ^, ~, <<, >>


Control Structures

Control structures manage the flow of the program.


Conditional statements such as:
if, else, switch Loops: for, while, do-while
Basic Program Structure
1. A simple C program typically includes the following components:
Preprocessor Directives Begin with # and instruct the compiler to include libraries or define
constants.c

#include <stdio.h>

2. Main Function: The entry point of every C program.

int main() {
// code
return 0;
}
3. Statements and Expressions

This execute the program logic.

printf("Hello, World!\n");
INPUT STATEMENT IN C- PROGRAMMING
In C programming, input statements are used to take input from the user. The most used function for this
purpose is scanf, which is part of the standard input/output library (stdio.h).

The scanf function reads formatted input from the standard input (keyboard). It requires a format
specifier to determine the type of data to be read and a pointer to the variable where the input will be
stored. Format Specifiers. Here are some common format specifiers used with scanf:
%d for integers
%f for floating-point numbers
%c for characters
%s for strings (character arrays)
%lf for double-precision floating-point numbers e.t.c .
OUTPUT STATEMENT IN C- PROGRAMMING
Basic Output using printfThe printf function prints formatted output to the standard output (usually
the screen).
It requires a format specifier to determine the type of data to be printed and the actual data to be
printed.
Format Specifiers.
 %d for integers
 %f for floating-point numbers
 %c for characters
 %s for strings (character arrays)
 %lf for double-precision floating-point numbers
 %x for hexadecimal numbers
 %o for octal numbers
Sample code:
#include <stdio.h>

int main() {
int integer = 42;
float floating_point = 3.14159;
char character = 'A';
char string[] = "Hello, World!";
double double_precision = 2.718281828459;
int hex = 255;
int octal = 255;

// Print an integer
printf("Integer: %d\n", integer);

// Print a floating-point number


printf("Floating-point number: %f\n", floating_point);

// Print a character
printf("Character: %c\n", character);

// Print a string
Example of C-Programming

#include <stdio.h> // Preprocessor directive to include the standard


input/output library

int main() { // Main function


printf("Hello, World!\n"); // Print statement
return 0; // Return statement
}
C-program to find sum of largest of three numbers .
#include <stdio.h>

int main() {
int num1, num2, num3;

// Input three numbers from the user


printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Determine the largest number


if (num1 >= num2 && num1 >= num3) {
printf("The largest number is %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The largest number is %d\n", num2);
} else {
printf("The largest number is %d\n", num3);
}

return 0;
}
C- program to find sum of N-integers.
#include <stdio.h>

int main() {
int N, i, number, sum = 0;

// Input the number of integers to be summed


printf("Enter the number of integers: ");
scanf("%d", &N);

// Loop to read each integer and add it to the sum


for (i = 0; i < N; i++) {
printf("Enter integer %d: ", i + 1);
scanf("%d", &number);
sum += number; // Add the input number to the sum
}

// Output the total sum


printf("The sum of the %d integers is: %d\n", N, sum);

return 0;
}
C program to calculate factorial of a number using iterative.
#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1; // Factorial can be a large number, so we use unsigned
long long

// Input the number


printf("Enter a number to find its factorial: ");
scanf("%d", &n);

// Check if the number is negative


if (n < 0) {
printf("Factorial of a negative number doesn't exist.\n");
} else {
// Calculate the factorial using a loop
for (i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", n, factorial);
}

return 0;
}
C program to calculate factorial of a number using iterative.
#include <stdio.h>

// Function prototype
unsigned long long factorial(int n);

int main() {
int n;

// Input the number


printf("Enter a number to find its factorial: ");
scanf("%d", &n);

// Check if the number is negative


if (n < 0) {
printf("Factorial of a negative number doesn't exist.\n");
} else {
printf("Factorial of %d = %llu\n", n, factorial(n));
}

return 0;
}
// Function to calculate factorial using recursion
unsigned long long factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: factorial of 0 or 1 is 1
} else {
return n * factorial(n - 1); // Recursive case
}
}

You might also like