본 강의에서는 C언어의 do-while 반복문에 대해 알아보겠습니다. do-while 반복문은 주어진 문장 먼저 실행한 후 특정한 조건이 참일때 이를 반복적으로 실행하는 문장입니다.
- Youtube 강의동영상
https://youtu.be/5K_qIX7BaDo
- 코드는 여기에서 다운 받으세요
https://github.com/dongupak/Basic-C-Programming
본 강의에서는 C언어의 do-while 반복문에 대해 알아보겠습니다. do-while 반복문은 주어진 문장 먼저 실행한 후 특정한 조건이 참일때 이를 반복적으로 실행하는 문장입니다.
- Youtube 강의동영상
https://youtu.be/5K_qIX7BaDo
- 코드는 여기에서 다운 받으세요
https://github.com/dongupak/Basic-C-Programming
Type conversion in C provides two methods: implicit type conversion which occurs automatically during expressions, and explicit type conversion using cast expressions. Implicit conversion occurs when different types are used in expressions, such as when an int is used in a calculation with a float. The usual arithmetic conversions implicitly promote operands to the smallest type that can accommodate both values. Explicit casting uses cast operators to force a type conversion.
Structures allow users to define their own data types that group together variables of different types under a single name. A structure is defined using the struct keyword followed by member variables enclosed in braces. Structure variables can then be declared and accessed using the member operator (.). Structures can contain other structures (nested structures), and pointers to structures allow structures to be passed to functions and dynamically allocated. Linked lists are a common data structure that use structures - each list node contains a struct with a data element and pointer to the next node, with the list connected via these pointers.
Type conversion in C provides two methods: implicit type conversion which occurs automatically during expressions, and explicit type conversion using cast expressions. Implicit conversion occurs when different types are used in expressions, such as when an int is used in a calculation with a float. The usual arithmetic conversions implicitly promote operands to the smallest type that can accommodate both values. Explicit casting uses cast operators to force a type conversion.
Structures allow users to define their own data types that group together variables of different types under a single name. A structure is defined using the struct keyword followed by member variables enclosed in braces. Structure variables can then be declared and accessed using the member operator (.). Structures can contain other structures (nested structures), and pointers to structures allow structures to be passed to functions and dynamically allocated. Linked lists are a common data structure that use structures - each list node contains a struct with a data element and pointer to the next node, with the list connected via these pointers.
A pointer is a variable that stores the memory address of another variable. Pointers allow accessing and modifying the data stored at the referenced memory location. Pointers can be declared by specifying the data type followed by an asterisk, and are initialized by assigning the address of a variable to the pointer variable. Pointer variables can be used in expressions and arithmetic and can be passed to functions to modify the referenced data. Arrays can also be accessed and traversed using pointers by treating the array name as a pointer to its first element. Pointers to functions allow functions to be passed as arguments and enables polymorphism.
The document defines various data types in C including basic, user-defined, and derived types. It describes the common basic types like integer, character, and floating point, specifying their sizes and value ranges. User-defined types include type definitions, enumerated types, structures, and unions. Structures and unions allow grouping multiple variables of different types under a single name. The document also briefly mentions derived types such as arrays, functions, pointers, and references.
An algorithm is a procedure or set of steps to solve a problem in a specific order. It consists of sequence, selection, iteration, and case-type statements. Common iteration statements in algorithms include while, do, and for loops. A computer program can be viewed as an elaborate algorithm, and applying an algorithm to input produces an output through computation.
The break and continue statements can be used to control the flow of loops in C programming.
- The break statement causes immediate exit from a loop. Program execution continues after the loop. It is commonly used to escape early from a loop or skip the remainder of a switch structure.
- The continue statement skips the remaining statements in the body of a loop for that iteration and proceeds with the next iteration. In a while or do-while loop, the continuation test is evaluated immediately after continue. In a for loop, the increment expression is executed before the continuation test.
- An example shows using continue in a for loop to skip printing the value 5 and proceed to the next iteration of the loop.
Enumerated types allow a set of named integer constants to be defined. An enumeration type declaration specifies the enumeration tag name and defines the enumerator constants. A variable of an enum type can only hold one of the defined enumerator constant values. Enumerations provide an alternative to #define with advantages like automatically generated values and normal scoping rules. Typedef allows custom type names to be defined as synonyms for existing types like int, float, and double without reserving new storage. Typedef names exist in the same namespace as other identifiers except for variably modified types which have block scope.
Bit fields allow integer members of a structure to be stored in memory spaces smaller than normally allowed by the compiler. A bit field is declared by specifying the number of bits after the member name, separated by a colon. Bit fields are packed together efficiently in memory and accessed like regular structure members. They are interpreted as unsigned integers and only the declared number of lower bits can be assigned or accessed.
Dynamic memory allocation allows programs to allocate memory at runtime using functions like malloc(), calloc(), free(), and realloc(). Malloc allocates memory and returns a pointer, calloc allocates and initializes to 0, free releases previously allocated memory, and realloc changes the size of allocated memory. Memory allocation occurs on the heap which changes size during program execution. Out of memory errors can occur if allocation requests exceed available heap space.
Union is similar to a structure but allows storing different data types in the same memory location. A union defines a type that can hold one of its member types. All members share the same memory allocation, which is the size of the largest member. Therefore, only one member can be accessed at a time in a union, whereas all members can be accessed simultaneously in a structure, which allocates separate memory for each member.
The document discusses file management in C. It defines a file as a collection of related data treated as a single unit by a computer. It stores in secondary storage so contents remain when the computer shuts down. It discusses basic file operations like opening, reading, writing and closing files. It explains different modes for opening files like read, write and append. It also discusses functions for input/output like getc, putc, getw, putw, fscanf and fprintf. Finally, it covers random access to files using functions like fseek and ftell.
1. Addition of 2 nos
Recursion Prog
#include <stdio.h>
#include<conio.h>
main()
{
int add(int pk,int pm);
int k = 2;
int i;
int m = 3;
clrscr();
i = add(k,m);
printf("i = %dn",i);
getch();
}
int add(int addk,int addm){
if(addm==0)
return(addk);
else
return(1+add(addk,addm-1));
}
2. Power function
Recursion Prog
#include<conio.h>
#include <stdio.h>
int main()
{
double power(double x, int n);
double x = 0.0;
int n = 0;
clrscr();
printf("%lf",power(3,2));
getch();
}
double power(double x, int n) {
if(n == 0)
return 1.0;
else
return x * power( x , n - 1 );
}
3. Even sum
Recursion Prog
#include<conio.h>
#include <stdio.h>
void main()
{
int sum(int,int );
int total;
total=sum(2,4);
clrscr();
printf("%d",total);
getch();
}
int sum(int i,int n){
static int even=0;
if(i<=n){
even=even+i;
sum(i+2,n); //calling same function
}
return even;
}
5. Reverse Number
Recursion Prog
#include<conio.h>
#include <stdio.h>
int sum=0,r;
void main()
{
int reverse(int);
int num,rev;
clrscr();
printf("nEnter a
number :");
scanf("%d",&num);
rev=reverse(num);
printf("nAfter reverse
the no is :%d",rev);
getch();
}
Int reverse(int num)
{
if(num>0)
{
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else{
return sum;
}
return sum;
}
6. Fibnocci Series
Recursion Prog
void main()
{
long term(int);
long term(int n)
int i,n;
{
clrscr();
if(n==1)
printf(“Enter Limit”);
return(0);
scanf("%d",&n);
else if(n==2||n==3)
printf("nThe Series is :”);
for(i=1;i<=n;i++)
return 1;
{
else
printf(" %ld ",term(i));
return(term(n}
1)+term(n-2));
getch();
return 0;
}
}