Programming in C
School of Computer Science, UPES
Data types specify the type of data that a variable can store and the operations that can be performed on it.
| Type | Size | Range |
|---|---|---|
| char | 1 byte | -128 to 127 |
| int | 4 bytes | -2³¹ to 2³¹-1 |
| float | 4 bytes | 3.4E-38 to 3.4E+38 |
| double | 8 bytes | 1.7E-308 to 1.7E+308 |
// Examples
char letter = 'A';
int count = 42;
float pi = 3.14f;
double precision = 3.14159265359;
// Examples
unsigned int positive_only = 4294967295U; // Max: 2^32 - 1
signed int both_signs = -2147483648; // Min: -2^31
short int small = 32767;
long int big = 2147483647L;
long long int huge = 9223372036854775807LL;
Automatic conversion by compiler
Manual conversion by programmer
// Implicit conversion
int i = 10;
float f = i; // int to float
// Explicit conversion
float pi = 3.14159;
int rounded = (int)pi; // float to int (3)
Promotion Direction: Lower types automatically convert to higher types
char c = 'A';
int result = c + 1; // 'A' promoted to int, result = 66
float f = 3.5f;
double d = f * 2; // f promoted to double
// Declaration only
extern int global_var;
extern float pi;
// Definition
int global_var = 100;
float pi = 3.14159f;
char name[50];
Key Point: Every definition is a declaration, but not every declaration is a definition.
Available throughout program
int global_count = 0; // Global variable
int main() {
int local_var = 10; // Local variable
global_count++; // Accessing global
return 0;
}
void counter() {
int auto_var = 0; // Reset every call
static int static_var = 0; // Retains value
auto_var++;
static_var++;
printf("Auto: %d, Static: %d\n", auto_var, static_var);
}
// First call: Auto: 1, Static: 1
// Second call: Auto: 1, Static: 2
Reserved words that have special meaning in C and cannot be used as identifiers.
Total: 32 Keywords in C
All keywords are lowercase and have predefined meanings.
signed, unsigned, short, long, const, volatile, sizeof, typedef, struct, union, enum, default
// Examples of keyword usage
int count; // Data type keyword
static float pi; // Storage class keyword
if (count > 0) { // Control flow keyword
return count; // Control flow keyword
}
Header files contain declarations of functions, macros, and data types that can be shared between source files.
#include
| Header | Purpose |
|---|---|
| stdio.h | Input/output functions (printf, scanf) |
| stdlib.h | Memory allocation, conversion functions |
| string.h | String manipulation functions |
| math.h | Mathematical functions |
| ctype.h | Character classification functions |
| time.h | Date and time functions |
#include// Standard library headers #include #include #include "myheader.h" // User-defined headers int main() { printf("Hello, World!\n"); // From stdio.h return 0; }
#ifndef MATHUTILS_H
#define MATHUTILS_H
// Function declarations
int add(int a, int b);
int multiply(int a, int b);
float divide(float a, float b);
// Constants
#define PI 3.14159
#define MAX_SIZE 100
#endif
#include "mathutils.h"
int add(int a, int b) {
return a + b;
}
int multiply(int a, int b) {
return a * b;
}
float divide(float a, float b) {
if (b != 0)
return a / b;
return 0;
}
#ifndef, #define, #endif prevent multiple inclusions of the same header file.
#include <stdio.h> // Searches system directories
#include <stdlib.h>
<header.h>
↓
System Directories:
/usr/include
C:\MinGW\include
#include "myheader.h" // Searches current directory first
#include "utils/helper.h"
"header.h"
↓
Current Directory
Then System Directories
Ready to dive deeper into C Programming?