C Basics

C Basics

  • C is a general-purpose computer programming language.
  • It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential.

Key Points

  • Structured Language: C is a structured programming language, allowing developers to write clear, organized, and efficient code.
  • Low-Level and High-Level: It provides both low-level memory manipulation and high-level abstraction for creating a wide range of software.
  • Portable: Code written in C is highly portable, meaning it can run on different platforms with minimal modification.
  • Extensive Standard Library: C comes with a rich standard library that simplifies common programming tasks.

Structure of a Basic C Program

A basic C program consists of a main function, which is the entry point of the program.
Here's a simple structure.
Loading…

Writing and Executing the First C Program

Here's a basic "Hello, World!" program:
Loading…
  • To execute the program, save it to a file with a .c extension (e.g., hello.c), and then compile and run it using a C compiler like gcc:gcc hello.c -o hello./hello
  • This code prints "Hello, World!" to the console.

C Character Set

  • In C, a character set is the collection of characters that are recognized by the language.
  • The basic character set in C includes letters (both uppercase and lowercase), digits, and a few special characters like punctuation symbols. Here's a simple example that uses characters:
Loading…
In this example, we've used the characters 'A' and 'B' to initialize two variables and then printed them using the %c format specifier.

Identifiers and Keywords

  • Identifiers in C are names given to various program elements such as variables, functions, and arrays.
  • They must start with a letter or an underscore and can be followed by letters, digits, or underscores. C also has a set of keywords, which are reserved words with predefined meanings.
Here's an example that demonstrates identifiers and keywords:
Loading…

Data Types

  • Data types in C specify the type of data a variable can hold.
  • Common data types include int, char, float, and double. Here's an example using different data types:
Loading…

Constants

  • Constants are fixed values in C.
  • They can be of various types, including integer constants, floating-point constants, and character constants.
  • Here's an example with integer and character constants:
Loading…

Symbolic Constants

  • Symbolic constants are user-defined constants represented by identifiers.
  • They are typically created using the #define preprocessor directive.
Here's an example:
Loading…
In this example, we've defined a symbolic constant PI and used it to calculate the area of a circle.

Variable Declarations

  • Variable declarations specify the data type and name of a variable.
Here's an example:
Loading…
In this example, we declare and initialize an integer variable x.

#include Preprocessor Directive

  • The #include directive is used to include header files in a C program.
  • It allows you to use functions and features defined in those header files. For example:
Loading…
In this example, we include the stdio.h header to use functions like printf.

Expression Statements

Expression statements are statements that consist of expressions followed by a semicolon.
They perform some computation and may have side effects. Here's an example:
Loading…
In this example, the expression a + b calculates the sum of two numbers.

Compound Statements

  • Compound statements, also known as blocks, are enclosed within curly braces {} and can contain multiple statements.
  • They are often used for grouping statements together.
Loading…

Conclusion

C is a foundational, versatile programming language that combines low-level control with high-level capabilities, making it vital for systems programming and providing a strong foundation for developers.