simple-c-code-using-stdio

command
Version: v0.0.0-...-a2a1f02 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 2 Imported by: 0

README

simple-c-code-using-stdio example

Same code from previous example, but using stdio.h for printf.

GitHub Webpage

THE C CODE

Here is a very simple c code example,

int Add(int a, int b, char * name){
    int s;
    s = a+b;
    printf("Hi %s. ", name);
    printf("Print the sum from C: %d\n", s);
    return s;
}

IMPLEMENT WITH GO

This example will use stdio.h for printf. To implement using go use import "C",

package main

/*
#include <stdio.h>

int Add(int a, int b, char * name){
    int s;
    s = a+b;
    printf("Hi %s. ", name);
    printf("Print the sum from C: %d\n", s);
    return s;
}
*/
import "C"
import "fmt"

func main() {
    a := C.int(10)
    b := C.int(20)
    name := C.CString("jeff")
    c := C.Add(a, b, name)
    fmt.Printf("Print returned sum from go: %v\n", c) // 30
}

RUN

go run simple-c-code-using-stdio.go

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL