1 / 15

Unit II: C Programming Fundamentals

Data Types, Variables, Keywords & Header Files

Programming in C

School of Computer Science, UPES

Outline

What we'll cover today:

  • Data Types in C
  • Type Conversion
  • Variables (Declaration vs Definition, Scope)
  • Keywords in C
  • Header Files

What are Data Types?

Definition

Data types specify the type of data that a variable can store and the operations that can be performed on it.

  • Determines memory allocation
  • Defines range of values
  • Specifies operations allowed
  • Ensures type safety
Data Types
Basic
Derived
User-defined

Basic Data Types

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;
                        

Memory Visualization

char
int (4 bytes)
float (4 bytes)
double (8 bytes)

Type Modifiers

Signed vs Unsigned

  • signed: Can store negative and positive values
  • unsigned: Can store only positive values (0 and above)

Size Modifiers

  • short: Smaller storage
  • long: Larger storage
  • long long: Even larger storage
// 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;
                

Type Conversion Overview

Implicit Conversion

Automatic conversion by compiler

  • Widening conversions
  • Mixed arithmetic
  • Assignment conversions

Explicit Conversion (Casting)

Manual conversion by programmer

  • Type casting operator
  • Narrowing conversions
  • Precise control
// 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)
                

Type Promotion Hierarchy

char
short
int
long
float
double
long double

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
                

Variable Declaration vs Definition

Declaration

  • Tells compiler about variable
  • Specifies name and type
  • No memory allocated
  • Can declare multiple times
// Declaration only
extern int global_var;
extern float pi;
                        

Definition

  • Creates and allocates memory
  • May initialize with value
  • Can define only once
  • Also serves as declaration
// 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.

Local vs Global Variables

Global Variables

Available throughout program

main() function

Local Variables
Function Parameters

other() function

Local Variables
Static Variables
int global_count = 0;        // Global variable

int main() {
    int local_var = 10;      // Local variable
    global_count++;          // Accessing global
    return 0;
}
                

Variable Scope and Lifetime

Automatic Variables (Local)

  • Created when function is called
  • Destroyed when function exits
  • Stored in stack memory

Static Variables

  • Retain value between function calls
  • Initialized only once
  • Stored in data segment
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
                

C Keywords Overview

What are Keywords?

Reserved words that have special meaning in C and cannot be used as identifiers.

auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while

Total: 32 Keywords in C
All keywords are lowercase and have predefined meanings.

Categories of Keywords

Data Types

  • char
  • int
  • float
  • double
  • void

Storage Classes

  • auto
  • extern
  • static
  • register

Control Flow

  • if, else
  • for, while, do
  • break, continue
  • switch, case
  • goto, return

Type Modifiers & Others

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
}
                

What are Header Files?

Definition

Header files contain declarations of functions, macros, and data types that can be shared between source files.

  • Files with .h extension
  • Contain function prototypes
  • Define macros and constants
  • Declare external variables
  • Include using #include directive
Header File
(.h)
Source File 1
(.c)
Source File 2
(.c)

#include

Standard Header Files

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;
}
                

Creating Custom Header Files

mathutils.h

#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
                        

mathutils.c

#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;
}
                        

Header Guards

#ifndef, #define, #endif prevent multiple inclusions of the same header file.

Include Directive Syntax

System Headers

#include <stdio.h>     // Searches system directories
#include <stdlib.h>
                        

<header.h>

System Directories:
/usr/include
C:\MinGW\include

User Headers

#include "myheader.h"  // Searches current directory first
#include "utils/helper.h"
                        

"header.h"

Current Directory
Then System Directories

Summary

Key Concepts Covered

  • Data Types: Fundamental building blocks (int, float, char, double)
  • Type Conversion: Implicit vs explicit, promotion hierarchy
  • Variables: Declaration vs definition, local vs global scope
  • Keywords: 32 reserved words with special meanings
  • Header Files: Code organization and reusability

Best Practices

  • Choose appropriate data types
  • Use meaningful variable names
  • Minimize global variables
  • Include necessary header files
  • Use header guards in custom headers

Thank You!

Questions & Discussion
?

Ready to dive deeper into C Programming?