0% found this document useful (0 votes)
2 views3 pages

C Programming Topic 1

The document outlines the structure of a C program, including preprocessor directives, the main function, and basic I/O operations using `printf` and `scanf`. It also explains comments, syntax rules, and provides example programs to illustrate these concepts. Overall, it serves as a foundational guide to understanding C programming basics.

Uploaded by

mosesmainam017
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

C Programming Topic 1

The document outlines the structure of a C program, including preprocessor directives, the main function, and basic I/O operations using `printf` and `scanf`. It also explains comments, syntax rules, and provides example programs to illustrate these concepts. Overall, it serves as a foundational guide to understanding C programming basics.

Uploaded by

mosesmainam017
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

C Program Structure

A C program typically consists of the following parts:

- **Preprocessor Directives**: Lines that begin with `#`, such as `#include` or `#define`. These are
processed by the preprocessor before compilation.

- **Main Function**: Every C program must have a `main` function. It’s the entry point of the program.

Here’s a simple example:

```c

#include <stdio.h> // Preprocessor directive

int main() { // Main function

printf("Hello, World!\n"); // Print to the console

return 0; // Return statement

```

2. Basic I/O Operations

- `printf`**: Used to print output to the console. It can format text using various format specifiers.

- `scanf`: Used to read input from the user.


Example:

```c

#include <stdio.h>

int main() {

int number;

printf("Enter an integer: ");

scanf("%d", &number); // Read an integer from the user

printf("You entered: %d\n", number); // Print the integer

return 0;

```

### **3. Comments and Documentation**

- **Single-line Comments**: Use `//` to comment a single line.

```c

// This is a single-line comment

```

- **Multi-line Comments**: Use `/* ... */` to comment multiple lines.

```c

This is a multi-line comment

It spans multiple lines

/
```

4. Basic Syntax Rules

- **Semicolons**: Every statement must end with a semicolon.

- **Braces `{ }`**: Used to define blocks of code (e.g., the body of functions and control structures).

- **Indentation**: Not required but recommended for readability.

5. Example Program

Here’s a simple program demonstrating these concepts:

```c

#include <stdio.h> // Include the standard I/O library

int main() { // Main function starts here

int age; // Declare an integer variable named age

printf("Enter your age: "); // Prompt the user

scanf("%d", &age); // Read an integer from the user and store it in age

printf("You are %d years old.\n", age); // Print the user's age

return 0; // Return 0 to indicate successful completion

```

This covers the basics of C program structure and syntax. Let me know if you need more details or if you
want to move on to another topic!

You might also like