- C is a general-purpose programming language designed for system and application programming.
- It is widely used for developing firmware and portable applications.
- Understanding C lays a strong foundation for learning other programming languages like C++, Java, and Python.
- Visit the official Visual Studio Code website.
- Download the installer for your operating system (Windows, macOS, or Linux).
- Run the installer and follow the setup wizard instructions.
- Open VS Code.
- Create a new folder for your project.
- Go to File > Open Folder and select your project folder.
- Use the Explorer tab to manage files and create new ones.
- C requires a compiler to translate the code into machine language.
- Install GCC (GNU Compiler Collection):
- Windows: Install MinGW or TDM-GCC.
- macOS: Use
xcode-select --installto install GCC. - Linux: Use
sudo apt install gcc.
- Install useful extensions:
- C/C++ by Microsoft: Provides IntelliSense, debugging, and code browsing.
- Code Runner: Enables running code snippets directly in the editor.
-
In the project folder, create a new file:
hello.c. -
Write your first C program:
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
- Configure the compiler and terminal:
- Go to File > Preferences > Settings.
- Set the path for the GCC compiler in the environment variable or tasks.json.
-
Open the terminal in VS Code.
-
Compile the program:
gcc hello.c -o hello -
Run the executable:
./hello
#include <stdio.h>: Includes the standard I/O library for functions likeprintf.int main(): Entry point of the program.printf("Hello, World!\n");: Prints text to the console.return 0;: Indicates successful execution.
-
Create a file:
sum.c. -
Write a program to add two numbers:
#include <stdio.h> int main() { int a = 5, b = 10; printf("Sum: %d\n", a + b); return 0; }
-
Single-line:
// This is a comment -
Multi-line:
/* This is a multi-line comment */
- Basic:
int,float,char,double. - Derived:
array,pointer,structure. - Enumeration:
enum.
-
Containers for storing data values.
-
Example:
int age = 25; float pi = 3.14;
-
Returns the memory size of a data type or variable.
-
Example:
printf("Size of int: %lu\n", sizeof(int));
- Names must begin with a letter or underscore.
- Case-sensitive.
- Cannot use reserved keywords.
-
Declares variables as read-only.
-
Example:
const float pi = 3.14159;
- Arithmetic:
+,-,*,/,%. - Relational:
>,<,==. - Logical:
&&,||,!.
-
Example using
scanf:int age; printf("Enter your age: "); scanf("%d", &age);
-
Converts one data type to another.
-
Example:
float x = 5.7; int y = (int)x;
-
if, else, else if:
if (age > 18) { printf("Adult"); } else { printf("Minor"); }
-
Example:
int day = 3; switch (day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; default: printf("Other day"); }
-
for loop:
for (int i = 0; i < 10; i++) { printf("%d\n", i); }
-
while loop:
int i = 0; while (i < 10) { printf("%d\n", i); i++; }
-
do-while loop:
int i = 0; do { printf("%d\n", i); i++; } while (i < 10);
-
Breaks the program into reusable parts.
-
Example:
int add(int a, int b) { return a + b; }
-
Stores multiple values of the same type.
-
Example:
int arr[5] = {1, 2, 3, 4, 5};
-
Variables that store the memory address of another variable.
-
Example:
int a = 10; int *ptr = &a;
-
Array of characters ending with a null terminator (
\0). -
Example:
char name[] = "John"; printf("Name: %s", name);
-
User-defined data type to group variables.
-
Example:
struct Person { char name[50]; int age; };