Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

C Programming - Control Flow

The document provides an overview of C programming, detailing its basic structure, syntax, data types, control flow statements, functions, and memory management. It outlines essential components such as the main function, conditional statements, loops, and the use of pointers and arrays. Additionally, it covers input/output functions and file handling, offering examples for clarity.

Uploaded by

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

C Programming - Control Flow

The document provides an overview of C programming, detailing its basic structure, syntax, data types, control flow statements, functions, and memory management. It outlines essential components such as the main function, conditional statements, loops, and the use of pointers and arrays. Additionally, it covers input/output functions and file handling, offering examples for clarity.

Uploaded by

06303648
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

C programming follows a structured syntax with specific rules that must be followed to create functional

programs. Here’s an overview of the basic syntax and structure of C programming:

1. Basic Structure of a C Program

A typical C program consists of the following sections:

CopyEdit

#include <stdio.h> // Preprocessor directive

// Function prototype (optional)

void greet();

int main() // Main function: program execution starts here

printf("Hello, World!\n"); // Print statement

greet(); // Function call

return 0; // Return statement

// Function definition

void greet() {

printf("Welcome to C programming!\n");

2. Basic Components of C Syntax

a) Header Files and Preprocessor Directives

 These include necessary libraries.

 Syntax: #include <library_name>

 Example: #include <math.h> for mathematical operations.

b) The main() Function


 Every C program must have a main() function.

 It serves as the entry point for execution.

c) Statements and Blocks

 Each statement ends with a semicolon (;).

 Blocks are enclosed within curly braces {}.

d) Comments

 Single-line comment: // This is a comment

 Multi-line comment: /* This is a multi-line comment */

3. C Data Types

C provides different data types for handling variables.

Data Type Size Example

int 4 bytes int age = 25;

float 4 bytes float pi = 3.14;

double 8 bytes double precise = 3.141592;

char 1 byte char grade = 'A';

void 0 bytes Used for functions with no return value

4. Control Flow Statements

a) Conditional Statements

 if, else if, else for decision-making.

CopyEdit

if (age >= 18) {

printf("Adult\n");

} else {

printf("Minor\n");

}
b) Loops

 Used to repeat a block of code.

CopyEdit

// for loop

for (int i = 0; i < 5; i++) {

printf("%d ", i);

// while loop

int count = 0;

while (count < 5) {

printf("%d ", count);

count++;

5. Functions in C

C programs can be modularized using functions.

CopyEdit

int add(int a, int b) {

return a + b;

int main() {

int sum = add(5, 3);

printf("Sum = %d", sum);

return 0;

}
6. Input and Output Functions

The stdio.h library provides I/O functions.

CopyEdit

// Input

int age;

printf("Enter your age: ");

scanf("%d", &age);

// Output

printf("You entered: %d", age);

7. Operators in C

Common operators include:

 Arithmetic: +, -, *, /, %

 Relational: ==, !=, >, <, >=, <=

 Logical: &&, ||, !

 Bitwise: &, |, ^, <<, >>

 Assignment: =, +=, -=, *=, /=

 Increment/Decrement: ++, --

8. Arrays and Strings

Handling multiple values using arrays.

CopyEdit

int numbers[5] = {1, 2, 3, 4, 5};

char name[] = "John";


9. Pointers in C

Pointers store memory addresses.

CopyEdit

int x = 10;

int *ptr = &x;

printf("Value of x: %d", *ptr);

10. Memory Management

 malloc(), calloc(), free() are used for dynamic memory allocation.

11. File Handling

C allows file operations like reading and writing.

CopyEdit

FILE *file = fopen("example.txt", "w");

fprintf(file, "Hello, file!");

fclose(file);

Summary of C Programming Structure

1. Preprocessor directives (#include)

2. Global variables and function declarations

3. main() function

4. User-defined functions

5. Return statement in main()

Would you like examples on specific topics?

4o

You said:
Control Flow in C programming

ChatGPT said:

ChatGPT

Control flow in C programming refers to the order in which individual statements, instructions, or
function calls are executed or evaluated. C provides several control flow constructs to manage the
execution of code based on conditions, loops, and branching.

1. Conditional Statements (Decision Making)

Conditional statements allow the program to make decisions based on specific conditions.

a) if Statement

Executes a block of code only if the given condition is true.

CopyEdit

if (condition) {

// Code to execute if condition is true

Example:

CopyEdit

int age = 20;

if (age >= 18) {

printf("You are an adult.\n");

b) if-else Statement

Executes one block of code if the condition is true, and another block if it's false.

CopyEdit

if (condition) {
// Code to execute if condition is true

} else {

// Code to execute if condition is false

Example:

CopyEdit

int age = 16;

if (age >= 18) {

printf("You are an adult.\n");

} else {

printf("You are a minor.\n");

c) else if Ladder

Checks multiple conditions sequentially.

CopyEdit

if (condition1) {

// Code to execute if condition1 is true

} else if (condition2) {

// Code to execute if condition2 is true

} else {

// Code to execute if none of the conditions are true

Example:

CopyEdit

int marks = 85;


if (marks >= 90) {

printf("Grade: A\n");

} else if (marks >= 75) {

printf("Grade: B\n");

} else if (marks >= 50) {

printf("Grade: C\n");

} else {

printf("Fail\n");

d) switch Statement

Used to select one of many possible values for a variable.

CopyEdit

switch (expression) {

case value1:

// Code to execute if expression == value1

break;

case value2:

// Code to execute if expression == value2

break;

default:

// Code to execute if no cases match

Example:

CopyEdit

int day = 3;

switch (day) {
case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

case 3:

printf("Wednesday\n");

break;

default:

printf("Invalid day\n");

2. Looping Statements (Iteration)

Loops allow repetitive execution of a block of code.

a) for Loop

Used when the number of iterations is known beforehand.

CopyEdit

for (initialization; condition; update) {

// Code to execute

Example:

CopyEdit

for (int i = 1; i <= 5; i++) {

printf("%d ", i);

Output: 1 2 3 4 5
b) while Loop

Executes a block of code as long as the condition is true.

CopyEdit

while (condition) {

// Code to execute

Example:

CopyEdit

int i = 1;

while (i <= 5) {

printf("%d ", i);

i++;

Output: 1 2 3 4 5

c) do-while Loop

Similar to the while loop, but guarantees at least one execution.

CopyEdit

do {

// Code to execute

} while (condition);

Example:

CopyEdit

int i = 1;
do {

printf("%d ", i);

i++;

} while (i <= 5);

Output: 1 2 3 4 5

3. Control Statements (Loop Control)

These statements control the flow inside loops.

a) break Statement

Exits from the loop or switch statement immediately.

Example:

CopyEdit

for (int i = 1; i <= 5; i++) {

if (i == 3) {

break;

printf("%d ", i);

Output: 1 2

b) continue Statement

Skips the current iteration and continues with the next iteration of the loop.

Example:

CopyEdit

for (int i = 1; i <= 5; i++) {

if (i == 3) {

continue;
}

printf("%d ", i);

Output: 1 2 4 5

c) goto Statement (Avoid Using)

Jumps to a labeled statement elsewhere in the program.

Example:

CopyEdit

int i = 1;

start:

printf("%d ", i);

i++;

if (i <= 5) {

goto start;

Output: 1 2 3 4 5

4. Nesting of Control Flow Statements

Control flow statements can be nested within each other.

Example (Nested if and for loop):

CopyEdit

for (int i = 1; i <= 3; i++) {

if (i % 2 == 0) {

printf("%d is even\n", i);

} else {

printf("%d is odd\n", i);


}

Output:

csharp

CopyEdit

1 is odd

2 is even

3 is odd

Summary of Control Flow in C

Control Flow Description Example

if Executes code based on a condition if (x > 0) { ... }

if-else Executes one block if true, another if false if (x > 0) else { ... }

else if Checks multiple conditions else if (x < 10)

switch Selects from multiple cases switch (x) { ... }

for Loop with known iterations for (i=0; i<5; i++)

while Loop with condition while (x > 0)

do-while Executes at least once do { ... } while (x);

break Exits a loop/switch break;

continue Skips current loop iteration continue;

goto Jumps to a labeled statement goto label;

You might also like