Example output
Let's Learn C
Welcome to the world of C programming! As a language that has stood the test of time, C serves as a foundational skill for many programmers. Its efficiency, close-to-hardware capabilities, and widespread use make it an essential language to master. Let's dive into the basics and get you started on your journey.
Getting Started With C
1. Hello, World!
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
In this simple program, we include the standard input/output library, stdio.h. The main function is the entry point of every C program. Inside it, we use printf to display the classic "Hello, World!" message. The return 0; line indicates successful program execution.
2. Variables and Data Types
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Age: %d\n", age);
printf("Height: %f\n", height);
printf("Grade: %c\n", grade);
return 0;
}
Here, we declare variables with different data types (int, float, and char). We then print them using printf. %d is a format specifier for integers, %f for floats, and %c for characters.
3. User Input
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!\n", name);
return 0;
}
This program uses scanf to get user input. We declare a character array name to store the input. Note that %s is used as the format specifier for strings.
4. Conditional Statements (if-else)
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Positive number\n");
} else if (num < 0) {
printf("Negative number\n");
} else {
printf("Zero\n");
}
return 0;}
This example demonstrates the use of conditional statements (
if, else if, and else) based on user input.
Resources
Books: "C Programming Absolute Beginner's Guide" by Perry and Miller
Online Courses: Coursera's "C Programming for Beginners" by University of California, Irvine
Interactive Platforms: HackerRank's C Language Certification
Documentation: C Programming Documentation
Practice Problems: LeetCode's C Programming Section